a simple stencil buffering example

Started by
0 comments, last by Raison 19 years, 11 months ago
From an example from "opengl game programming" below, the function DrawFloor()is suppose to draw a checkered floor. A torus is draw above the floor and there will be a reflection of the torus on the floor.According to the book, the floor is not drawn(when DrawFloor() is called the first time) when stencil buffering is enabled, but i dont understand why. Is it because color modification is turn off through glColorMask()?? If not, can someone tell me the reason?


	/// disable depth testing
    glDisable(GL_DEPTH_TEST);

	// disable modification of all color components
    glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

    // enable stencil testing
    glEnable(GL_STENCIL_TEST);

	// setup the stencil buffer for a function reference value
    glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
    glStencilFunc(GL_ALWAYS, 1, 1);

    // draw the floor; this will set the floor pixels in the stencil buffer
	// to 1, since we defined 1 as the mask value with the glStencilFunc() command
    DrawFloor();

    // enable modification of all color components
    glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);

	// enable depth testing
    glEnable(GL_DEPTH_TEST);

   // make it so we can only render where the stencil buffer is equal to 1
    glStencilFunc(GL_EQUAL, 1, 1);
    glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

	// draw "reflection"
	glPushMatrix();

        // reflect (invert) the torus
        glScalef(1.0, -1.0, 1.0);

		// eliminate front of polygons from drawing
        glCullFace(GL_FRONT);

		// draw the reflected torus
        DrawTorus();

        // re-enable backface culling
        glCullFace(GL_BACK);
    glPopMatrix();

	// disable stencil testing
	glDisable(GL_STENCIL_TEST);

// draw floor with 40% blending so we can see the "reflection"
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glColor4f(1.0f, 1.0f, 1.0f, 0.4f);
    DrawFloor();
    glDisable(GL_BLEND);

	// draw the "real" torus
	DrawTorus();

	glFlush();
	SwapBuffers(g_HDC);			// bring backbuffer to foreground

[\code]   
Advertisement
<div id="headline" style="position : absolute; height : 85px; z-index :
100; background : transparent; text-align : center; text-transform:
smallcaps; width : 100%; top : 0px; left : 0px; width : 100%; color :
#000000;">

<div style="position : absolute; top : 125px; margin-left : 10px; margin-right : 10px;">

GLCOLORMASK



Section: 3GMisc. Reference Manual Pages (3G)


NAME


<div>

glColorMask

- enable and disable writing of frame buffer color components


</div>

C SPECIFICATION


<div>

void glColorMask(
GLboolean red,
                    GLboolean green,                    GLboolean blue,                    GLboolean alpha )



</div>

PARAMETERS


<div>




red, green, blue, alpha
Specify whether red, green, blue, and alpha can or cannot be written
into the frame buffer.
The initial values are all GL_TRUE,
indicating that the color components can be written.

</div>

DESCRIPTION


<div>

glColorMask specifies whether the individual color components in the frame buffer
can or cannot be written.
If red is GL_FALSE,
for example,
no change is made to the red component of any pixel in any of the
color buffers,
regardless of the drawing operation attempted.




Changes to individual bits of components cannot be controlled.
Rather,
changes are either enabled or disabled for entire color components.
</div>

ERRORS


<div>

GL_INVALID_OPERATION is generated if glColorMask
is executed between the execution of glBegin
and the corresponding execution of glEnd.
</div>

ASSOCIATED GETS


<div>

glGet with argument GL_COLOR_WRITEMASK



glGet with argument GL_RGBA_MODE
</div>

SEE ALSO


<div>

glClear(3G),
glColor(3G),
glColorPointer(3G),
glDepthMask(3G),
glIndex(3G),
glIndexPointer(3G),

our new version has many new and good features. sadly, the good ones are not new and the new ones are not good

This topic is closed to new replies.

Advertisement