222 CHAPTER 7: Well-Rendered Miscellany^
0.5, 0.0, -.5f,
-0.5, 0.0, 0.5,
0.5, 0.0, 0.5
};
static const GLubyte colors[] =
{
255, 0, 0, 128,
255, 0, 0, 255,
0, 0, 0, 0,
128, 0, 0, 128
};
glFrontFace(GL_CW);
glPushMatrix();
glTranslatef(0.0,-1.0,-3.0);
glScalef(2.5,1.5,2.0);
glVertexPointer(3, GL_FLOAT, 0, flatSquareVertices);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
glDepthMask(GL_FALSE); //1
glColorMask(GL_TRUE, GL_FALSE, GL_FALSE, GL_TRUE); //2
glDrawArrays( GL_TRIANGLE_STRIP,0, 4); //3
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); //4
glDepthMask(GL_TRUE); //5
glPopMatrix();
}
In line 1, writing to the depth buffer is disabled, and line 2 disables the
green and blue color channels, so only the red one will be used. That
is how the reflected area gets its little red highlight.
Now we can draw the image to the stencil buffer in line 3.
Lines 4 and 5 reset the masks.
At this point, the drawInRect() routine has to be modified, yet again. Yawn.... And if you
can keep your peepers open, check out Listing 7-10 for the cruel and unvarnished truth.
Sorry for repeating so much of the previous code, but it’s much easier than saying
‘‘...and after the line about squirrel trebuchets add such-and-such a line....’’
Listing 7-10. The reflection drawInRect() method
- (void)glkView:(GLKView *)view drawInRect:(CGRect)rect
{
static GLfloat z=-3.0;
static GLfloat spinX=0;
static GLfloat spinY=0;