Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ OpenGL
this is an eg code
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 23-Oct-11 23:11pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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!) Smile | :)
  Permalink  
Comments
Sweety Khan at 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 at 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 at 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 at 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 at 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?
Dave Kerr at 24-Oct-11 10:04am
   
In this case if the code works do it without, mention however that in more advanced scenarios you should use the stencil buffer to clip the reflecting plane (or the geometry of the reflecting surface). IF this helps out can you flag the solution as a correct answer :)
Sweety Khan at 24-Oct-11 10:11am
   
yeh i will definitely do this. actually i was clearing all confusions, bc if i mark the solution in starting, u wont come on this question again :p
Sweety Khan at 24-Oct-11 10:29am
   
btw thanksSs a lot :)
Dave Kerr at 24-Oct-11 11:59am
   
no probs - I get email notifications so I keep track of comments if you need to update the question ;)

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

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 295
2 Afzaal Ahmad Zeeshan 224
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 160
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 24 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100