PDA

View Full Version : planar reflections



mikemor
10-28-2002, 03:48 AM
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 ?

Relic
10-28-2002, 05:05 AM
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.)

Jan
10-28-2002, 05:39 AM
Goto nehe.gamedev.net
There is a tutorial about that (using the stencil buffer).

Jan.

mikemor
10-28-2002, 06:26 AM
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.

mikemor
10-28-2002, 06:44 AM
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

davepermen
10-28-2002, 07:20 AM
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)

mikemor
10-28-2002, 07:35 AM
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?

davepermen
10-28-2002, 07:55 AM
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..

Asgard
10-28-2002, 08:11 AM
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).]

mikemor
10-28-2002, 08:33 AM
Nope I have been inverting the culling when I do the reflected scene.

mikemor
10-28-2002, 09:21 AM
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?

Asgard
10-28-2002, 09:31 AM
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.

mikemor
10-28-2002, 09:55 AM
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
glStencilFunc(...,....,0x2);//shadows

? ( tried but didnt work)

Asgard
10-28-2002, 10:29 AM
glStencilMask is the function you're looking for.

mikemor
10-29-2002, 01:10 PM
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.

mikemor
10-29-2002, 01:27 PM
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)
glStencilMask(0x4)?

or should it be

glStencilFunc(...,...,0x2)
glStencilMask(0x2)?

Anybody knows where to find a good pointer to this basic questions?