User Tools

Site Tools


More Shaders

For your lighting assignment, you will need to write a vertex and fragment shader that implements the Phong Lighting Model (details about the Phong Model can be found in the class slides). First, we need to learn a little more about shaders. In the previous set of notes, we talked about predefined incoming and outgoing variables that allow you to share information down the rendering pipeline from your program to vertex shader to fragment shader. However, what if we want to communicate other information between our vertex and fragment shaders? For this, we can use the redundantly named varying variables. Varying variables allow your vertex shader to share data with your fragment shader. In order for this to work, the varying variable must be declared identically in both shaders. For instance, if you wanted to share a vertex's normal with the fragment shader, you would need to include the following line of code at the top of both your vertex and fragment shaders.

varying vec3 vertex_normal;

Let's modify our shader examples from the previous set of notes to shade a model such that we can see how the normals change across its surface. Since our normals and colors both consist of three colors, we can substitute a normal for a color. This would result in the normal's X component producing red, the Y component producing green, and the Z component producing blue. Vertex Shader

varying vec3 vertex_normal; //shared with the fragment shader
void main()
        gl_Position = gl_ModelViewMatrix * gl_ProjectionMatrix * gl_Vertex;
        //sends the normal to the fragment shader

Fragment Shader

varying vec3 vertex_normal; //shared with the vertex shader
void main()
	//sets the fragment color to the normal
        gl_FragColor = vec4(, 1.0 );

The resulting image produced by these shaders is below.

tutorials/pfopengl_lighing.txt · Last modified: 2021/08/01 00:55 by jones