CHAPTER 7: Well-Rendered Miscellany (^239)
// glLineWidth(3.0); //8
// glDrawElements( GL_LINES, 6 3, GL_UNSIGNED_BYTE, m_Tfan1);
// glDrawElements( GL_LINES, 6 3, GL_UNSIGNED_BYTE, m_Tfan2);
glDisable(GL_BLEND);
glPopMatrix();
}
First rotate everything to world space, just as we have done before in
line 1.
Line 2 multiplies the shadow matrix with the current Modelview matrix.
Lines 3 and 4ff perform the same transformations and rotations on the
shadow as on the actual cube.
Next we can add a little blending to the shadow in lines 5ff and 6. The
alpha value is set to .3. The higher the value, the darker the shadow,
with it being solid black, of course, when the alpha is at 1.0.
In lines 7ff we render the geometry just as with the actual cube, except
now it’s distorted to look like it’s stretched out along the surface.
Now it’s time to update the light’s position, as in Listing 7-17.
Listing 7-17. Updating the light’s position
- (void)updateLightPosition
{
iLightAngle += (GLfloat)1.0; //in degrees
iLightPosX = m_LightRadius cos( iLightAngle/57.29 );
iLightPosY = 4.0;
iLightPosZ = m_LightRadius sin( iLightAngle/57.29 );
iLightPos[0] = iLightPosX;
iLightPos[1] = iLightPosY;
iLightPos[2] = iLightPosZ;
}
This updates the light’s position one degree each refresh. The y-value is fixed, so the
light traces its little orbit in the x/z plane. In addition to all of the previous, make sure to
add the standard setClipping() routine we’ve used in the past and call it from
viewDidLoad().
And to top it off, Listing 7-18 is the header for this project.
Listing 7-18. The header for the shadow casting exercise
#import <UIKit/UIKit.h>
#import <GLKit/GLKit.h>
#import <OpenGLES/EAGL.h>