Jump to content
  • Advertisement
Sign in to follow this  

Question about perspective-correct UV interpolation

This topic is 3122 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

I read a few articles, and many of them say different things. So, I'd like to get a definite idea on how to do this. Is this right: When I get to the rasterizer stage (vertices in screen space), I divide U and V values by Z? (I've also read that you divide by W instead.) You interpolate these new U and V values, along with a 1/Z value. (I've also heard it's actually a 1/W value.) After the interpolation at the specific pixel, you multiply the interpolated values by Z. (Or is it multiply by W? by 1/Z? by 1/W?) And is the Z you multiply by, the original Z value? Or is it ALSO an interpolated value? And these are the texture coords you use to look up the color? Also, can you use the interpolated 1/Z for z-buffering? Or is it after you multiply it (by Z? or whatever) Thanks for reading this, and I'd appreciate any help :)

Share this post


Link to post
Share on other sites
Advertisement
To be able to linearly interpolate anything in screen space, you need to devide that interpolator by W. Also, you can just use Z/W to store in your depth buffer as long as you setup your projection matrix properly.

So the interpolators you'll need would be:
Zs = Z / W
Us = U * Zs
Vs = V * Zs

Then for each pixel you just store Zs to the depth buffer, and recover U and V:
Up = Us / Zs
Vp = Vs / Zs

Hope that's clear enough.

Share this post


Link to post
Share on other sites
Quote:
Original post by Pixelot
To be able to linearly interpolate anything in screen space, you need to devide that interpolator by W. Also, you can just use Z/W to store in your depth buffer as long as you setup your projection matrix properly.

So the interpolators you'll need would be:
Zs = Z / W
Us = U * Zs
Vs = V * Zs

Then for each pixel you just store Zs to the depth buffer, and recover U and V:
Up = Us / Zs
Vp = Vs / Zs

Hope that's clear enough.


I want to make sure I get this right:

So I start rasterizing a triangle by first dividing Z by W.
Then I multiply U and V by the new Z.

And then I do the interpolation (of U,V,Z) at every pixel,
and to get the texture look-up coordinates I use the interpolated U times the interpolated Z?

Share this post


Link to post
Share on other sites
yep, its quite simple, when I first did it I wasnt sure it was going to work, but what you said is correct.
Theres a few sites, just look up "perspective correct texture mapping" and youll get the same info again, if you want to double check.

Share this post


Link to post
Share on other sites
In few words, the reason is simple. None inverse function like f(x)= K/x is linear, but it is the reciprocal, so to interpolate it in the screen space (which is linear, because you add a fixed step at every pixel) you have to use the reciprocal. After this, it's simple math ;)

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!