I just wondered how to create a mirror within a 3D game? My suggestion is/was to trace rays, hitting the "mirror" object then drawing a picture of a second cam onto the "mirror" object which is located at the original camera's mirror "point" by using the hit side of the mirror object as mirroring plane.

We would have to use the one ray coming from the center of the original camera to determine the point where the center of the vision is located on the mirror plane.

  • 1
    \$\begingroup\$ Would you like to have a sketch/drawing of "how I meant" this thing? \$\endgroup\$ – daemonfire300 Sep 29 '10 at 20:17
  • \$\begingroup\$ You might try posting this at stackoverflow.com since it isn't directly game development related; perhaps there are more graphics experts over there that could help you. \$\endgroup\$ – Ricket Sep 29 '10 at 21:35

If you are using OpenGL, the OpenGL FAQ section 9: Transformations covers exactly how to do this. And no, it doesn't involve raytracing, as that's understandably a very inefficient (but high quality) way to accomplish this.

9.170 How do I render a mirror?

Here is essentially what the FAQ entry says, and the example code demonstrates:

  • Set up a reflected view matrix. Doing this for axis-aligned mirrors is explained in the second paragraph.
  • Draw the scene
  • Restore the view matrix and clear the depth buffer
  • Render the scene again; the actual mirror geometry in this scene should be translucent or entirely transparent, in order to show through to the previously rendered mirrored scene.

Obviously there is a lot of room for optimization. When rendering the reflected scene, you should probably as much culling as possible, since mirrors are typically small and the default off-screen culling won't kick in for the geometry which is on screen but not seen through the mirror. You can also render just a simplified version of the scene through the mirror. For effects, and to obscure the user from noticing the reduced quality, you could apply a shader (e.g. blur, or maybe bright/washed-out) when you do your mirrored render.

I imagine, if you're using DirectX, the procedure would be much the same.

  • 3
    \$\begingroup\$ An alternative, if you have more memory than render time, is to simply reflect your world geometry through the mirror in your 3D editor, then you can modify it to look however you want (enforce lower mip levels, etc.). We did this on the PS1, where stuff sitting on a shiny floor was duplicated/inverted underground, and the floor itself was rendered semitransparent. \$\endgroup\$ – dash-tom-bang Sep 30 '10 at 19:15
  • \$\begingroup\$ Note that sample code refers to deprecated OpenGL specification. Nevertheless concept is universal and probably can be applied similarly using newest OpenGL specification. \$\endgroup\$ – patryk.beza Dec 7 '15 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.