Game Development Reference
In-Depth Information
If we need other lights, then these too must be enabled. If the lights need
to behave as spotlights then we would need to consider the cut-off angle
for the spotlight. If you have looked at any of the Toon3D demonstration
projects then you may be interested to know that I use the following for
spotlights:
glLightf(GL_LIGHTx, GL_SPOT_CUTOFF, coneAngle));
glLightf(GL_LIGHTx, GL_SPOT_EXPONENT, 2.0);
If a light that has been used as a spotlight is later used as a directional or
position light, then you must reset these values to the default, using this
code snippet:
glLightf(GL_LIGHTx, GL_SPOT_CUTOFF, 180.0f);
glLightf(GL_LIGHTx, GL_SPOT_EXPONENT, 1.0);
The code to draw the display is placed in the OnDraw event. MFC uses
this as standard when creating an application template. The window
where OpenGL drawing takes place is derived from an MFC CView class.
The first thing we do is clear the colour and depth buffers and reset the
modelview matrix. Next we set the ambient and diffuse levels for the light.
In this sample we have a single member variable of the document class
for each of these. This is used for all three components of the ambient and
diffuse RGB, with the alpha being set to 1.0. If this MFC style is unfamiliar
then take a quick look at Appendix B, where you can read a brief
introduction to MFC's Document/View architecture.
Having set up the properties for the light, we move on to set up the
material properties. In this example we set the emission, specularity,
shininess and diffuse colour. Only the diffuse colour acts as a true RGB
value. Finally, before we set the position for the light, we translate and
move the scene, ensuring that the light moves with the object. If the light
position is set after the matrix is cleared and before the translation and
rotation, then the modelview matrix does not affect the values for the light,
since the identity matrix leaves data unchanged. The last manipulation is
to set a position for the light. Here we use a vector with four values, ( x , y ,
z , w ). If w is 0.0 then lighting calculations regard the light as directional. If
w is any other value then lighting calculations assume the light to be
positional. If we were using a spotlight then now would be the time to set
the spotlight's direction using the following code snippet:
Glfloat spot_rot[] = { -1.0, -1.0, 1.0};
glLightfv(GL_LIGHTx, GL_SPOT_DIRECTION, spot_rot);
Search Nedrilad ::




Custom Search