Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
Would you like to have a sketch/drawing of "how I meant" this thing? –  daemonfire300 Sep 29 '10 at 20:17
    
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. –  Ricket Sep 29 '10 at 21:35

1 Answer 1

up vote 12 down vote accepted

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.

share|improve this answer
3  
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. –  dash-tom-bang Sep 30 '10 at 19:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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