Graphics Reference
In-Depth Information
seting discrete uniform variables
in our examples, because we can
then put them on sliders so that
you can experiment with them. In
applications, though, you should
probably take the more global
OpenGL API approach. This will
be described in Chapter 14.
Recall our assumption that in our example
shader code, we use general attribute and
uniform variables with our first-letter naming
convention instead of the built-in OpenGL
variable names. These names are close enough
to the built-in variable names that you can
easily convert them if you are working in
compatibility mode.
Positional Lights
The most common kind of lighting in OpenGL scenes is with positional lights.
Each light has position, color, and a number of other values.
For positional lights, the primary consideration is the direction from a
vertex to the light source, and you can get that by a simple vector subtraction
so you can make it an out vector in the vertex shader and pass it to the frag-
ment shader. Alternately, you can make the vertex position in eye space an
out variable so the fragment shader can use the ADS lighting function. Your
choice will probably depend on the effect you are trying to achieve. As we will
see in examples below, you can get traditional smooth shading by computing
the light direction at each vertex and defining the color as an out variable in a
vertex (or tessellation) shader, while you can get Phong shading by defining
the normal as an out variable and interpolating either the vertex position or
the light direction for each pixel.
Lighting
Method
Vertex Shader
Does
Rasterizer
Interpolates
Fragment
Shader Does
Per-vertex
Lighting model
Color
Applies color
Per-fragment Setup
Normal and EC
position
Lighting model
Directional Lights
If you use directional lights or spot lights, the necessary data for using
these kinds of lights can be found in the components of the built-in uniform
uLightSource[i] struct. Directional lights, also called parallel light sources , are
 
Search WWH ::




Custom Search