Click here to Skip to main content
15,860,859 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
this is an eg code
C#
void display (void) {

     glClearStencil(0); //clear the stencil buffer
     glClearDepth(1.0f);
     glClearColor (1.0,1.0,1.0,1);
     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |GL_STENCIL_BUFFER_BIT);
     glLoadIdentity();
     glTranslatef(0, 0, -10);
     //start
     glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); //disable the color mask
     glDepthMask(GL_FALSE); //disable the depth mask

     glEnable(GL_STENCIL_TEST); //enable the stencil testing


     glStencilFunc(GL_ALWAYS, 1, 0xFFFFFFFF);
     glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE); //setthe stencil buffer to replace our next lot of data

     floor(); //set the data plane to be replaced

     glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); //enablethe color mask
     glDepthMask(GL_TRUE); //enable the depth mask

     glStencilFunc(GL_EQUAL, 1, 0xFFFFFFFF);
     glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); //set the stencilbuffer to keep our next lot of data

     glDisable(GL_DEPTH_TEST); //disable depth testing of thereflection
     glPushMatrix();
     glScalef(1.0f, -1.0f, 1.0f); //flip the reflection vertically

     glTranslatef(0,2,0); //translate the reflection onto the drawing plane
     glRotatef(angle,0,1,0); //rotate the reflection
     square(); //draw the square as our reflection
     glPopMatrix();
     glEnable(GL_DEPTH_TEST); //enable the depth testing
     glDisable(GL_STENCIL_TEST); //disable the stencil testing

    //end

    glEnable(GL_BLEND); //enable alpha blending
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //set the blending function

     floor(); //draw our bench
     glDisable(GL_BLEND); //disable alpha blending

     glRotatef(angle,0,1,0); //rotate our square
     square(); //draw our square

    glutSwapBuffers();
    angle++;
 }

one thing more why we disable depth test before rendering reflection of square?
Posted

1 solution

Imagine a sitting cube on a mirrored surface. If depth test was enabled, when we draw the reflection, we'd see nothing - because the geometry of the reflection is BELOW the mirrored surface! We fake a reflection by re-drawing the geometry, but the reflection is an image on a surface - which is always above the geometry (imagine looking in a mirror - the geometry is 'behind' the mirror, with depth test on we'd get no pixels from the reflection because the mirrors in the way!) :)
 
Share this answer
 
Comments
Sweety Khan 24-Oct-11 8:37am    
o thanx i got it. but if i dont disable depth buffer the output is still the same. enabling/disabling depth buffer makes no difference. i observe tht the fact u said is controlled by colormask and depthmask. i remove all color n depth mask now enabling/disabling of depth buffer effects perfectly. do i understand it rightly?
Sweety Khan 24-Oct-11 8:55am    
one more thing wht stencil buffer doing here as we do all the things by ourself. we make a fake reflection by disabling depth buffer and inverting and scaling our square
Dave Kerr 24-Oct-11 9:25am    
Try removing the stencil buffering options and see if it still works - it may be that you've inherited this code from an example that used the stencil buffer but doesn't any more. Typically it would be used to draw a stencil of the reflective surface so that the reflection when drawn is only drawn on the reflective surface - avoiding reflections that don't get 'cut off' when the mirror they are in ends.
Sweety Khan 24-Oct-11 9:44am    
yeh its working without stencil buffer. see my code becomes so short :D
void display (void)
{
glClearDepth(1.0f);
glClearColor (1.0,1.0,1.0,1);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -10);
glDisable(GL_DEPTH_TEST); //disable depth testing of the reflection
glPushMatrix();
glScalef(1.0f, -1.0f, 1.0f); //flip the reflection vertically
glTranslatef(0,2,0); //translate the reflection onto the drawing plane
glRotatef(angle,0,1,0); //rotate the reflection
square(); //draw the square as our reflection
glPopMatrix();
glEnable(GL_DEPTH_TEST); //enable the depth testing
glEnable(GL_BLEND); //enable alpha blending
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //set the blending function
floor();
glDisable(GL_BLEND); //disable alpha blending
glRotatef(angle,0,1,0); //rotate our square
square(); //draw our square
glutSwapBuffers();
angle++;

}
Sweety Khan 24-Oct-11 9:46am    
i have to present tomorrow on the applications of depth buffer. do u think its fine if i do reflection without stencil buffer? will my teacher object on tht?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900