[MDX] Problem: Odd reflections with projective texture mapping

Started by
3 comments, last by Lukas Boersma 15 years, 11 months ago
Hi! I'm working on Water with Reflections. My idea so far is: -Render the reflections in a texture -Let the water shader project the texture on the water plane -Render the water The rest of the water and rendering into the texture with the reflections works fine, but the projection somehow doesn't work very well. Here's a result from my app: First Image Looks fine so far, but the reflection is somehow distorted (red arrow in the image) - and I haven't implemented any distortion yet! The Problem becomes clearer as I move closer: Second Image The distortions seems to become bigger as the edges of the water mesh move farther outside of the view. To do the projection, my vertex shader simply takes the transformed position data of my water mesh as texture coordinates. Here's a simplified code with the relevant pieces: Vertex Shader:
 
        //Calculate the position
        OUT.hPosition = mul( float4(IN.position.xyz , 1.0) , worldViewProj);

        [...]

        //Take the transformed position as texture coordinates
	OUT.ReflCoord = OUT.hPosition.xy / OUT.hPosition.w;
	
        //Transform it from the Unit-Cube coordinates (-1 to +1, zero at the middle) 
        //to texture coordinates (0 to 1, zero at the top left (or so))
	OUT.ReflCoord.x = OUT.ReflCoord.x*0.5 + 0.5;
	OUT.ReflCoord.y = 1-(OUT.ReflCoord.y*0.5 + 0.5);
        [...]



Pixel Shader:

        float4 refl = tex2D(ReflSampler, float2(IN.ReflCoord.x, IN.ReflCoord.y));
        return refl;



I know that the contents of my reflection-texture are correct. Can someone figure out where the problem is? Thanks a lot in advance, -uggi [Edited by - uggibuggi on May 14, 2008 9:48:02 AM]
Advertisement
Try moving the division by w and texture coordinate conversion to the pixel shader.
Hey, that works! Thanks!

But why?
What you get when you perform homogeneous division by w can't (generally) be linearly interpolated. This is why when you project a vertex position in your vertex shader, you leave it in the POSITION register without performing the divide. The pipeline then clips and rasterizes, and performs the division as the final step to determine the pixel's location on-screen.
Ah, that's good to know - thanks.

This topic is closed to new replies.

Advertisement