Jump to content
  • Advertisement
Sign in to follow this  
Lukas Boersma

[MDX] Problem: Odd reflections with projective texture mapping

This topic is 3721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
Advertisement
Try moving the division by w and texture coordinate conversion to the pixel shader.

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!