Sign in to follow this  
Mr Furious

Unexpected Texture Sampling

Recommended Posts

In trying to mimic a 3 monitor setup I have tested rendering to texture and then applying that texture to 3 screen space quads. The scene consists of a skybox with a checkerboard texture on every face. For now, all 3 screen quads are rendering the exact same texture. It seems like my texture coordinates are not properly interpolated on the sides. Can someone explain this behavior? [Edited by - Mr Furious on October 8, 2008 6:23:44 PM]

Share this post


Link to post
Share on other sites
That's perspective-incorrect interpolation of texture coordinates. In GL you can fix this with glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)

Also perspective correct interpolation will only work if you are setting the z values of your vertices correctly.

Share this post


Link to post
Share on other sites
Ok, I've tried that and ended up with the same result. I'm rendering those 3 quads with an orthographic camera. Maybe it's not possible to be perspective correct when rendering in ortho.

I don't really understand why texturing behaves this way when rendering quads in ortho as opposed to rendering 3 world space quads with perspective projection. Does the projection matrix get used somehow in the process of sampling a texture?

Share this post


Link to post
Share on other sites
The W component (the reciprocal of depth) is used in the perspective correct interpolation to reconstruct the sample position in homogenous texture space, so yes, the projection matrix plays a part in the calculation of the sampling position.

Whereas affine (non-corrected) sampling is effectively a linear interpolation in screen space, not in texture space - which is the original problem) - the w-divided coordinate is calculated as follows:



//let frac = 0 to 1, interpolation parameter between tc0 and tc1
tc' =
((1-frac)*tc0.xy/tc0.z + frac*tc1.xy/tc1.z) //z divide
/
((1-frac)*1.0/tc0.z + frac*1.0/tc1.z) // reciprocal of z divide (1/z), AKA w divide
;


As you can see from the formula, you need to generate the per-vertex w value in order to achieve correct texture mapping. The projection matrix is usually set up to calculate it for you.

[Edited by - Nik02 on October 9, 2008 2:49:55 PM]

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