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.

After reading up on rendering reflections, it seems that the way to do it is the render the scene from underneath the plane of reflection, and then use projective texturing to map the reflected texture to the reflective surface. However, why can't you just use the image from rendering the scene underneath the water, and draw it upside down behind the terrain?

Here's what I mean: http://i.stack.imgur.com/g6VbX.png

The lines at the end of the views represent the drawn image, the colored dots are supposed to represent the corresponding points on the textures, and the brown is supposed to represent the terrain.

From the diagram, it would appear that you can just take the image rendered from the camera below the reflective surface, flip it upside down, and then draw it behind the terrain on the actual rendered image.

Is this another way to do it, or am I completely missing something here?

Thanks!

share|improve this question
    
If you draw the reflected image behind the terrain, then the terrain that's supposed to be underwater will occlude the reflection, instead of the reflection appearing in front of the underwater terrain. Unless I've completely misunderstood you - which is quite possible. –  Nathan Reed Jun 22 '13 at 5:15
    
Assuming that I clip the underwater terrain, though? –  Jonathon Xu Jun 22 '13 at 20:48

1 Answer 1

up vote 1 down vote accepted

When rendering planar reflections, you don't need to render the reflection to a texture at all. To render a reflective surface, you can use the following steps:

Basic reflection

  1. Render the surface (e.g. a table or pond) to a stencil buffer
  2. Setup a custom clipping plane that clips objects behind the surface
  3. Multiply the current camera matrix with a mirror matrix that reflects from the surface plane
  4. Invert polygon winding rule
  5. Enable stencil test to pass only pixels that were written in step 1.
  6. Render the scene
  7. Restore normal rendering settings
    • Original camera matrix
    • Polygon winding rule
    • Remove the custom clipping plane
    • Disable stencil test
  8. Render the surface again with alpha blending

This algorithm can be used at any time during the rendering of the scene. Few notes:

  • Step 1. and 5. are not needed if your reflective surface covers the whole area, i.e. the sea level
  • Step 2 is only needed if you can have polygons behind the surface (often true)
  • Step 8 is not needed if the surface if fully reflective

Post processing

If you need to post-process the reflection (e.g. blur it), then you need to render the reflection to a texture. After post-processing the reflection texture, you need to render it to the screen. Here you have two options. One is the projective texturing, where you render the surface with texture mapping. Alternatively you can just blit the texture to the screen behind other geometry. However if the surface is not infinite, you have to restrict the reflection to be visible only on the surface. This can be achieved with the same stencil buffer approach as described above in the normal reflection. Alternatively the projective texturing solves this. With a shader the projective texturing can be implemented easily by using screen coordinates directly as the texture coordinates.

Summary

To answer your actual question. Yes you can just draw the reflection behind your terrain, but if the reflection should appear only on specific polygon(s), you need either projective texturing or stencil buffer.

share|improve this answer
    
Thanks! Very thorough and exactly what I wanted. –  Jonathon Xu Jun 22 '13 at 20:49

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.