PDA

View Full Version : Stencil in front of background



fortikur
03-29-2005, 06:07 AM
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
room.background_img.display_bgr_image();

//NeHe things
glDisable(GL_LIGHTING);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE);
glDisable(GL_DEPTH_TEST);
//draws the quad for the mirror
draw_quad(
100,0,0,
40,100,0,
200,100,0,
200,0,0
);
//NeHe stuff again
glEnable(GL_DEPTH_TEST);
glStencilFunc(GL_EQUAL, 1, 1);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);

//mirroring
glScalef(1.0f, 1.0f, -1.0f);
mesh.display_mesh();

glDisable(GL_STENCIL_TEST);
glEnable(GL_LIGHTING);

Can pls. someone tell me, how I can draw the mirror and the background?

dorbie
03-31-2005, 01:34 AM
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.

dorbie
03-31-2005, 01:42 AM
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.