Graphics Reference
In-Depth Information
Table 33.1: Predefined items in GLSL.
Name
Type Meaning
The homogeneous position of the current
vertex
gl_Vertex
vec4
The normal at the current vertex
gl_Normal
vec4
The RGBA color of the current fragment
gl_FragColor
vec4
The transformation from modeling coordi-
nates to normalized device coordinates
gl_ModelViewProjectionMatrix
mat44
The homogeneous normalized device coordi-
nates of the current fragment, before perspec-
tive divide, i.e., gl_Position.w may not be
1.0 . 2
gl_Position
vec4
Raises x to the y power; if x is a vector, do so
termwise
pow(x, y)
Returns the larger of x and y
max(x, y)
Dot product of vectors; x and y must be of the
same size (i.e., vec2 or vec3 or vec4 )
dot(x, y)
A vertex shader may also get other input data, in one of two forms. First, there
may be other per-vertex information, like the normal vector, or texture coordi-
nates. Second, there may be information that's specified per object . In our case,
the diffuse reflectivity is one such item (we don't use it in the vertex shader), and
the world-space position of the light is another. That world-space light position is
declared uniform vec3 wsLight; the keyword uniform tells GL that the value is
set once per object. The declaration of the variable before main indicates that it
needs to be linked to the rest of the program. In this case, it's linked to the host
program by a call in ConfigureShaderArgs .
Finally, a vertex shader may set values to be used by other shaders. These
values are computed once per vertex; during the rasterization and clipping phase,
they're interpolated to get values at each fragment. The default is perspective inter-
polation (i.e., barycentric interpolation in camera coordinates), but image-space
barycentric interpolation is also an option. The resultant values vary from point to
point on the triangle, and so they are declared varying .
Our shader computes one of these, gouraudFactor (line 2), which is the dot
product of the unit surface normal and the incoming light direction to the world-
space light source.
Having computed this dot product (line 10) and the location of each vertex,
we move on to the fragment shader (see Listing 33.4).
2. In OpenGL these are called “Clip coordinates,” while normalized device coordinates
are those after the perspective divide.
 
Search WWH ::




Custom Search