I created a mirror according to the NeHe tutorial. If I try it in front of a black background (glClear()-ed), it shows the reflection. The stencil works OK.
My problem is, that i use a background image and if i display it, the “transparent” part of the stencil disappears, which means, no reflection is visible, just the bgr image. I think it is somehow related to the fact, that displaying the bgr makes the stencil buffer fill up with 1.
What I did:
//this draws the bgr
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
//draws the quad for the mirror
//NeHe stuff again
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glScalef(1.0f, 1.0f, -1.0f);
Can pls. someone tell me, how I can draw the mirror and the background?
It’s not exactly clear what the problem is. Remember that between frames state is not altered so whatever stencil operations you leave in the pipeline at the end of the frame you will inherit at the start of your next frame.
Also make sure you’re clearing the stencil buffer between frames.
Remember also that the mirror can be written to the zbuffer, so if you’re enabling depth testing when you draw the room and leaving that state around you could potentially occlude the reflection. One fix is to clear the depth buffer before drawing the reflection however a more robust method is to draw the mirror quad with a depth value that effectively clears the buffer (or writes a distant value). It is prudent to follow up rendering the mirror and reflection by drawing it to the zbuffer when you’re done drawing teh reflection.
I keep seeing tutorials on mirroring by people who repeat the myth that you need to use stencil for this. Often you don’t and typically when you do it may not be enough just to use stencil, you need a user clip plane when drawing the reflection for example AND careful treatment of the contents of the depthbuffer.
P.S. The simplest fix I think may be to clear the depth buffer after drawing the mirror quad. The stencil will still mask to the quad and 3D stuff in the scene won’t be incorrectly occluded. Add a user clip plane and it will be robust but you don’t need robustness for a simple ground reflection.
A more sophisticated solution is to draw the quad with distand z as I said in the earlier email, this will also help with multiple mirrors that may occlude eachother but that’s getting advanced because the surface must depth test with the scene so it’s slightly more tricky than simply drawing the quad, imagine drawing the mirror surface and then starting the reflection render with a stencil tested distant surface to ‘clear’ the mirror region’s zbuffer and no depth test. This woudl be a ‘depth clear’ that applies only to the mirror surface. Of course you fill in the hole when you’re done.