Graphics Reference
In-Depth Information
//specular light computation
vec3 specular = vec3( 0.0, 0.0, 0.0 );
if( dot(lightv, viewv) > 0.0)
{
specular = pow(max(0.0, dot(viewv,refl)),
myMaterialShininess)*myMaterialSpecular*
myLightSpecular;
}
return clamp( ambient + diffuse + specular, 0.0, 1.0);
}
This calculation does not take into account lighting atenuation. If you
want to include atenuation, you can enhance this computation by computing
the distance to the light and geting the light's constant, linear, and quadratic
atenuation terms as uniform variables, and then computing
1./(constant + linear*distance + quadratic*distance*distance)
as a multiplier of the diffuse and specular components, as described above.
(Atenuation does not act on the ambient light component.)
These computations use simple vector addition and subtraction, not
homogeneous addition and subtraction, because we want to keep this sim-
ple. If you want to make them fully general, you would need to replace these
with homogeneous vector addition and subtraction, as we discussed in Chap-
ter 1. This would be necessary, for instance, if you have a directional light
source (which acts as if it were placed at infinity).
Types of Lights
Since the fixed-function pipeline does all the color computations at the vertex
processing stage, whenever you use shaders to replace fixed-function opera-
tions, you must handle lighting yourself. Besides the full ADS lighting model,
there are other issues in lighting because OpenGL supports spot lights and
directional lights, as well as positional lights. To be able to replace fixed-func-
tion lighting computations, you must have ways to handle all the options that
you plan to use. If you are using lighting, you are probably using material
properties as well.
Overall, the OpenGL API gives you ways to define color, lights, and mate-
rial properties that are treated globally in the graphics system. So you may define
a light position, a color, etc. using the API calls to set their global properties, so
that any shader can pick them up. We have often used an alternate approach of
Search WWH ::




Custom Search