|Hosting by PhotoTangler Collage Maker|
Mirrors And Reflections
Question submitted by Anonymous (05 July 2000)
|Return to The Archives|
|I've been trying to implement a mirror system in my OpenGL engine, but I'm having trouble. I don't want to just use the negative (scaled) of the scene and redraw it, because I want to have arbitrarily oriented mirrors. How can this be done?|
Actually, it is quite simple. You need a few things to get started. First,
you'll need some construction paper, a pencil, a piece of string, and a small
Next, determine the point at which your camera's vector intersects the mirror's plane (this point may not be within the mirror polygon) - a ray/plane intersection will do the trick (we'll call this point B).
Now you'll need to reflect the camera's location across the mirror's plane. To do this, you'll need your ray/plane intersection code again. Start off by inverting the plane's normal and use that as the vector for the ray. The origin for the ray is the camera's location. This will create a ray that points directly at the plane from the camera. Find the intersection of this ray with the mirror's plane. This is the closest point to the camera on the mirror's plane. From this, we create a vector from the camera's location to the resulting intersection point. This vector represents the same vector that was used in the last ray/plane intersection, but this time its length is the closest distance to the plane from the camera. Now double the length of the vector. If you add this vector to your camera's location, you'll have the camera's location reflected across the mirror's plane (this is point A.) Subtracting B from A, you get the vector V.
Your new camera will be centered on point A, using the "look-at vector" of V. To render the reflected scene through the mirror, you'll need to clip everything to the mirror's polygon and to the mirror's plane (i.e. not rendering anything behind the mirror.) When rendering, you'll also need to render the scene backwards (left-right) to account for the reflection of the mirror.
Finally, poke the pencil through the construction paper, throw the string away, and get that rodent out of the microwave!
Response provided by Paul Nettle
This article was originally an entry in flipCode's Ask Midnight, a Question and Answer column with Paul Nettle that's no longer active.