5.7 - Example 3: One Color per Vertex

This example will help you understand varying variables in shader programs. We would like to have the color of the pixels inside a triangle change across the face of a triangle. This allows all kinds of special effects, such as gradient colors and lighting effects.

To demonstrate how varying variables work, we can keep the previous shader programs and just change our data. We will assign a different color to each vertex of a triangle.

The Model

A Triangle3 object will contain 3 vertices and 3 colors. A new version of our 3D model is shown in the following example.

Show: Code Canvas Run Info
./simple_pyramid_color_vertices/simple_pyramid_color_vertices.html

A simple, 3D model where each vertex has a different color.

Please use a browser that supports "canvas"
Animate
Shader errors, Shader warnings, Javascript info
Open this webgl program in a new tab or window

The Shader Programs

Our shader programs remain unchanged from the previous example, but they are displayed below so that we can discuss them again.

Show: Code Canvas Run Info
./simple_pyramid_color_vertices/simple_pyramid_color_vertices2.html

A simple, 3D model where each vertex has a different color.

Please use a browser that supports "canvas"
Animate
Shader errors, Shader warnings, Javascript info
Open this webgl program in a new tab or window

The previous explanations of vertex shaders said that a vertex shader‘s job was to position a vertex and set the gl_Position output variable for that vertex. This is true, but it is only half the story. A vertex shader also prepares and passes data about the vertex to the fragment shader. Remember, a fragment is a collection of data related to an individual pixel. Well, any varying variable declared in a vertex shader will be passed to the fragment shader for that individual vertex. If we declared and calculated six varying variables in a vertex shader, all six values will be passed on to the fragment shader. Varying variables can be thought of as parameters to the fragment shader for that individual vertex.

Why are they call them varying variables? It is because they automatically change their value as they are applied to individual pixels on a triangle. Technically the values are linearly interpolated. The term interpolated means that given a starting and ending value, the values in-between are gradually changed to morph from the starting value into the ending value. For example, starting with 10 and ending in 22, with 3 intermediate values, an interpolation would produce 13, 16, and 19. The term linearly interpolated means that the difference between any two sequential values is the same.

The linear interpolation of varying variables happens automatically. You have no control over the interpolation and you can’t stop the interpolation. If you have a value that you want to remain constant over all the pixels in a triangle, you must still declare it as a varying variable, but you can set the starting and ending values to be the same and the interpolation will calculate a value that doesn’t change. For example, interpolating from 10 to 10 will calculate 10 for every value in-between.

The Buffer Object(s)

The data in the buffer object for the vertex colors changes for this example. Study the code in the simple_model_render_03.js file in the following example.

Show: Code Canvas Run Info
./simple_pyramid_color_vertices/simple_pyramid_color_vertices3.html

A simple, 3D model where each vertex has a different color.

Please use a browser that supports "canvas"
Animate
Shader errors, Shader warnings, Javascript info
Open this webgl program in a new tab or window

Access to Shader Variables

The shader program did not change, so there is not changes to getting the shader variable locations.

Linking a Buffer Object to an Attribute Variable

Linking to the buffer objects remained unchanged.

Rendering

The rendering of the model remained unchanged. The rendering function in the example above in lines 172-193.

Summary

The colors of fragments that compose a point, line or triangle are assigned colors using interpolated values. The values calculated at the vertices are the starting and ending values used for the interpolation.

Next Section - 5.8 - Example 4: Textures