234 CHAPTER 7: Well-Rendered Miscellany^
O K , t a k e a d e e p b r e a t h -----we have more to cover. Listing 7-13 demonstrates the new
drawLight() routine, while Listing 7-14 demonstrates drawPlatform(). The meat of the
exercise is Listing 7-15, calculating the shadow’s matrix, and Listing 7-16, drawing the
shadow. Are you tingling yet? I know I am.
Listing 7-13. The modified drawLight() routine
-(void)drawLight:(int)lightNumber
{
static GLbyte lampVertices[]={0,0,0}; //1
glDisableClientState(GL_COLOR_ARRAY);
glEnable(GL_POINT_SMOOTH); //2
glPointSize(5.0); //3
glLightfv(lightNumber, GL_POSITION, iLightPos ); //4
glPushMatrix();
glRotatef(m_WorldRotationX, 1.0, 0.0, 0.0); //5
glRotatef(m_WorldRotationY, 0.0, 1.0, 0.0);
glTranslatef( iLightPosX, iLightPosY, iLightPosZ ); //6
glColor4f( 1.0, 1.0, 0, 1.0); //7
glVertexPointer( 3, GL_BYTE, 0, lampVertices ); //8
glDrawArrays(GL_POINTS, 0,1);
glPopMatrix();
glEnableClientState(GL_COLOR_ARRAY);
}
The preceding code will draw a round dot to the screen, showing where the light source
is located at any moment.
Since we want only a single point to draw, we can specify a single
vertex at the origin in line 1.
GL_POINT_SMOOTH is something new. It tells OpenGL that any points it
draws should be round. Without this, the light would be rendered as a
square.
Line 3 is another new call that tells the system that the point is to be 5
pixels across. The maximum size can vary on different devices but can
typically go up to 64 or 128 pixels in diameter.
Now we can set the absolute position of the actual light here in line 4.
Lines 5ff rotate the lamp in world space, while line 6 translates it.
The color is set to yellow via line 7, while lines 8ff supply the vertex
and then draw it via glDrawArrays().
But wait, there’s more!