# Question about perspective-correct UV interpolation

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

## 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 on other sites
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 on other sites
Quote:
 Original post by PixelotTo 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 / WUs = U * ZsVs = V * ZsThen for each pixel you just store Zs to the depth buffer, and recover U and V:Up = Us / ZsVp = Vs / ZsHope 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 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 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 ;)

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634134
• Total Posts
3015749
×