# planar reflections

I have a scene inside a room and a mirror in one of the walls (say the right one). I compute the reflected scene ok but then I am not sure how to get the image in the mirror. Do I need to project the reflected scene onto the mirror ?

Draw the mirror face into the stencil buffer and draw your mirrored scene only where the stencil buffer is set. (Some GLUT samples show that.)

Goto nehe.gamedev.net
There is a tutorial about that (using the stencil buffer).

Jan.

I have tried all approaches but there is no way I can see the image in the mirror (although I checked that the reflected image is produced correctely on the other side of the mirror). In the mirror I just see a dark image.

Must be doing some minor error but cant get it.

Something I dont understand happens: I set the stencil buffer to 1 where the mirror is and
then draw the reflected scene where the stencil is 1. This should draw where the mirror is but rather it draws the entire reflected scene on the back of the mirror. Maybe I am doing the mirror translation wrong? I have the mirror at x=12 and to draw the reflected image I do
glPush()
glScalef(-1,0,0);
glTranslate(-24,0,0);
draw_light
draw_scene
glPopMatrix

you have a clipplane to cut away whats not in the mirror? (stuff behind the mirror gets reflected as well and then sits in front of the mirror)

Yes I have a clipping plane to do that. Doing all this my scene consists of the original scene on the left, the mirror on the right wall and the reflected scene on the right (in other words my scene is a big box with the scene on the left half of the box, the mirror in the middle of the box and the reflected scene on the right). Do I need an additional transformation to make the reflected scene appear only on the mirror?

i suggest to just do the reflection, without stencil mirrors at all. just get the world correctly mirrored… and then set up stencils to only draw that mirrored world into the mirror. it’s much more easy to see the actual transformation you do then…

mikemor, sounds to me like you’re not flipping the culling mode when you’re rendering the mirrored scene.

Try calling glFrontFace(GL_CW) before rendering the mirrored scene. Set it back to the default with glFrontFace(GL_CCW) again after you’re done.

[This message has been edited by Asgard (edited 10-28-2002).]

Nope I have been inverting the culling when I do the reflected scene.

Ok I found the problem. It turns out that I am drawing the scene with shadows that also use the stencil buffer. So when I draw the reflected scene in the mirror the stencil buffer is set during shadowing in a different way and ends up messing the whole thing. This leads me to the question: How do I show the shadows in the reflected image?

For the mirror you only need one bit in the stencil buffer. You can use the other 7 bits (assuming an 8 bit stencil buffer here) for whatever you want, like shadows.

Thats a great idea. Unfortunately I dont know how to use say the first bit for mirror and the second bit for shadows.
Do I just do

glStencilFunc(…,…,0x1);//mirror

? ( tried but didnt work)

glStencilMask is the function you’re looking for.

I have a room with objects that cast shadows. There is a plane mirror in one of the walls and I would like to see the objects and their shadows reflected on it. Asgard had a good idea of using on bit of the stencil buffer to do the shadows and another to do the mirror.

Now I have found this quite difficult. to implement. The reason is each time I am producing the reflected image (with shadows) into one stencil bit (where mirror is) I am enabling and disabling other buffers (to produce the shadows) and hence I am messing states of the buffers.

May be the best way is to render the reflected image, save it to an array and then load it when drawing to the mirror. (rather than rendering it directly to the mirror)
As anybody tried to do this before (reflections where shadows appear). Would appreciate your pointers.

I also dont know very well how to set up the first and second bits of the stencil buffer independetely.

If I only want to work with the second bit would I use

glStencilFunc(…,…,0x4)