   Reflection using texture rendering in OpenGL (gluProject problem)
  Hi all,I'm trying to render an animated water surface with reflection. Basically, the water surface is a rectangle. In order to get reflection effects, I do the following:1. Add a clipping plane which clips everything on the far side of the water.2. Reflect the camera (both position and viewing direction) on the plane defined by the rectangle.3. Render the scene into a texture (actually into the frame buffer and then use glCopyTexImage2D).4. Calculate the texture coordinates, which correspond to the four edges of the rectangle.5. Remove the clipping plane and restore the original camera position.6. Render the rectangle with the texture applied. Texture coordinates are determined by step 4.The problem lies in step 4: I simply use gluProject to calculate the texture coordinates. This seems to work fine, unless i move the camera close to the water surface (about 2-3 length units). Then the reflection begins to move to the wrong spot, and repeats itself, and even goes upside down. The cause of the repetition is the repetition of texture coordinates, of course.I made one observation: The effect of the distortion seems to get stronger, if the winZ-Values returned by gluProject() get smaller (not necessarily negative!), and vice versa.Has anyone a clue, why this happens? What could possibly the reason?I already checked the following things (maybe i overlooked something?):1. The matrices used are correct.2. The rendered texture seems to be correct.3. some more things i don't remember.Greetings,Jost(I just noticed this is wrong here; please move it to the OpenGL forum.)Some screens (now small):How it should look like (from far away. Only seems to look right, the distortion is already there, but not quite noticeable. You can notice it in the third screen.):The distortion when looking from near the water:The distortion noticable from the distance. The left part of the teapot should not be cut off.[Edited by - Jost on May 21, 2010 12:54:47 PM]
  Hi again,i've looked further into this, and found part of the reason for the effects: The rectangle for the water surface isn't merely a rectangle, but a nurbs surface controlled by 50 times 50 points. In object space the edges coordinates are:(0,0, 0),(width, 0, 0),(width, height, 0),(0,height, 0).As i had problems with the nurbs texture coordinate generation mechanism, i calculated the texture coordinates for each vertex (x,y,0) simply as (x/width, y/height).Then i used these texture coordinates to get the "real" texture coordinates for the reflection texture by linearly interpolating the texture coordinates of the four edges (which i gained by gluProject()). This does not work, as perspective projections are not length preserving. A way to fix this is described here:Perspective Correct Texture Mapping.However, this does not solve the problem, it only removes some of the distortions. The things mentioned in the first post still happen. I'm wondering, if the reason might be some numerical instabilities? What speaks against is that these distortions happen in all distances from the water surface.   ;  I have now solved this. The whole problem was indeed that the texture mapping was not correct due to projection. The fix mentioned in the post before was simply wrong.   