Hello,
I just got my hands on mirrors, and everything looks good and reflective, EXCEPT for objects behind the mirror: they appear as ghost objects, and I'd like to clip them away. Of course the approximate way would be to discard all objects that are behind the mirror during mirror content rendering, but that somehow doesn't satisfy me completely, since ghost objects can still be seen when they are very large, behind the mirror, but their center is in front of the mirror.
So I was wondering if there was a more sofisticated solution to this problem?
Thanks for any advice. I currently use following code for the mirror part (allows for several mirrors):
// Prep stencil buffer:
glEnable(GL_STENCIL_TEST);
glClearStencil(0);
glClear (GL_STENCIL_BUFFER_BIT);
int drawOk=1;
glStencilFunc(GL_ALWAYS, drawOk, drawOk); // we can draw everywhere
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); // we draw drawOk where depth test passes
glDepthMask(GL_FALSE);
glColorMask(GL_FALSE,GL_FALSE,GL_FALSE,GL_FALSE);
glBegin (GL_QUADS);
glVertex3f (0, 1., 3.);
glVertex3f (0, -1., 3.);
glVertex3f (0, -1., -2.5);
glVertex3f (0, 1., -2.5);
glEnd ();
glDepthMask(GL_TRUE);
glColorMask(GL_TRUE,GL_TRUE,GL_TRUE,GL_TRUE);
// Draw the mirror view:
glStencilFunc(GL_EQUAL, drawOk, drawOk); // we draw only where stencil is drawOk
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glPushMatrix();
glScalef (-1., 1., 1.);
glTranslatef (0., 0., 0.);
glFrontFace (GL_CW);
drawObjects();
glPopMatrix();
glFrontFace (GL_CCW);
// Now draw the mirror overlay:
glStencilFunc(GL_LESS, 0, 65535); // we draw only where stencil is not zero
glClear (GL_DEPTH_BUFFER_BIT);
glStencilFunc(GL_EQUAL, drawOk, drawOk); // we draw only where stencil is drawOk
glPushAttrib (0xffffffff);
glDisable (GL_LIGHTING);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColor4f (0., 0., 0., 0.25);
glBegin (GL_QUADS);
glVertex3f (0, 1., 3.);
glVertex3f (0, -1., 3.);
glVertex3f (0, -1., -2.5);
glVertex3f (0, 1., -2.5);
glEnd ();
glPopAttrib();
drawOk*=2;
Clipping about mirror plane?
Started by floatingwoods, Sep 02 2012 03:34 PM
2 replies to this topic
Sponsor:
#2 Crossbones+ - Reputation: 828
Posted 03 September 2012 - 12:53 AM
You need to set up a clip plane matching the reflective plane when you draw the reflected geometry. This is actually quite easy: docs. NeHe tutorial.






