Help end child hunger

Varying Variables

Prev: Statements and Functions Next: Shader Examples

As mentioned before we have two types of shaders: vertex and fragment shaders. In order to compute values per fragment it is often required to access vertex interpolated data. For instance, when performing lighting computation per fragment, we need to access the normal at the fragment. However in OpenGL, the normals are only specified per vertex. These normals are accessible to the vertex shader, but not to the fragment shader since they come from the OpenGL application as an attribute variable.

After the vertices, including all the vertex data, are processed they move on to the next stage of the pipeline (which still remains fixed functionality) where connectivity information is available. It is in this stage that the primitives are assembled and fragments computed. For each fragment there is a set of variables that are interpolated automatically and provided to the fragment shader. An example is the color of the fragment. The color that arrives at the fragment shader is the result of the interpolation of the colors of the vertices that make up the primitive.

This type of variables, where the fragment receives interpolated, data are “varying variables”. GLSL has some predefined varying variables, such as the above mentioned color. GLSL also allows user defined varying variables. These must be declared in both the vertex and fragment shaders, for instance:

varying float intensity;

A varying variable must be written on a vertex shader, where we compute the value of the variable for each vertex. In the fragment shader the variable, whose value results from an interpolation of the vertex values computed previously, can only be read.


Prev: Statements and Functions Next: Shader Examples

  2 Responses to “Varying Variables”

  1. It seems quite confusing that variable already means a data storage element which by definition changes in value. I dont fully understand the notion of a varying variable, anyone care to explain it to me a bit more?

    • remember that these shaders are working with in the context of a rasterized triangle. Varying just determines how that variable (color, texture coords) is interpolated. Another option is Flat, that is no interpolation of the variable, i think ‘varying’ has been changed to ‘smooth’ in later versions of GLSL which should be clearer.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: