Sign in to follow this  
uggibuggi

[MDX] Problem: Odd reflections with projective texture mapping

Recommended Posts

uggibuggi    116
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]

Share this post


Link to post
Share on other sites
MJP    19753
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this