Graphics Reference
In-Depth Information
The steps in doing this are as follows:
Deine the atribute variable in the application and set the variable to its
appropriate value for each vertex as you define the vertex geometry.
Pick up the value of the atribute variable in the vertex shader and write it
to an out variable so it can be interpolated smoothly across each graphics
primitive.
Use the variable as an in variable to any shader that needs it and, if appro-
priate, use its value to determine the color to be used in filling pixels.
This could let us add pressure contour lines, or could let us color differ-
ent pressure regimes in distinct colors, or create other displays as needed. This
idea will be explored more fully in Chapter 15.
Defining Uniform Variables in Your Application
GLSL uniform variables contain information that can change at most with
each graphics primitive. You can think of these uniform variables as a sort of
“global variables” that are available to all the shaders currently being used.
If you want a shader to have data and that data isn't directly available from
OpenGL, you can define your own uniform variables to give that data to a
shader. Uniform variables are used within a shader, and their values are set by
the application. Uniform variables can hold any kind of data, including structs
and arrays, as we saw with the built-in uniform variables.
The mechanism for defining and using your own uniform variables is
indirect and somewhat unusual. When you define a uniform variable in your
shader program, you simply declare the variable in the usual way:
uniform type name;
This associates a name and a type with the variable, but does not associate an
address. An address is only assigned when the shader program is linked. Once
linking has been done, an address is available for each variable. You query the
address and then use it to set the variable from your application.
But how does the application get the address for a variable it does not
know about? The application must know the name of the uniform variable
in a linked shader program. It can then get the location (or address) with the
function
GLint glGetUniformLocation(GLuint program, const GLchar *name);
Here program is the value returned from the glCreateProgram( ) func-
tion, and name is the name (a text string) of the uniform variable. This function
Search WWH ::




Custom Search