Jump to content

  • Log In with Google      Sign In   
  • Create Account

14 years ago on June 15th Gamedev.net was first launched! We want to thank all of you for being part of our community and hope the best years are ahead of us. Happy birthday Gamedev.net!

Clipping about mirror plane?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 floatingwoods   Members   -  Reputation: 250

Like
0Likes
Like

Posted 02 September 2012 - 03:34 PM

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;

Sponsor:

#2 irreversible   Crossbones+   -  Reputation: 828

Like
0Likes
Like

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.

#3 floatingwoods   Members   -  Reputation: 250

Like
0Likes
Like

Posted 03 September 2012 - 03:33 AM

Thanks a lot irreversible!

Actually I vaguely read about the clip plane, but thought that was the near or far clip plane. Sorry I missed that.

Cheers!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS