# [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.

## 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);
[...]


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

##### Share on other sites
Hey, that works! Thanks!

But why?

##### 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 on other sites
Ah, that's good to know - thanks.

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
11
5. 5

• 14
• 30
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631778
• Total Posts
3002306
×