# Perspective texture mapping

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

## Recommended Posts

I was looking around for information on perspective correct texture mapping and found lots of articles and tutorials, and read them all. They basically say use u/z, v/z, and 1/z, and you're done. But, obviously I'm not done and still have questions. I'm using a z-buffer, so I already know the z at each pixel, but it's not a perspective correct z. If I divide the u and v by z, then divide by 1/z, it's a wash. Where do each of these z's come from? Also, I'm not sure how to step through the edges and scan lines. How do I calculate the deltas? Thanks for the help.

##### Share on other sites
for each pixel of your triangle you calculate 1/z through interpolation. you can change your zbuffer to a 1/z buffer also. you can't just divide your u by z. you need to interpolate with u/z instead of u. do you see the difference? then when calculating your true perspective correct u value you divide by 1/z. you cannot just interpolate u, and z and then take the division of the two.

suppose I am doing one scanline of a triangle
I have 1/zl for the left most point and 1/zr further say I have
ul, vl, ur, vr, for the left and right most points these are the texture coord
also I have xl and xr the pixel position for the start(left) and end(right) of my current scanline.

dZ = (1/zr - 1/zl)/(xr - xl)
z = 1/zl

u = ul/zl
v = vl/zl
dU = (ul/zl - ur/zr)/(xr - xl)
dV = (vl/zl - vr/zr)/(xr - xl)

for(x = xl;x <= xr; x++) { //xl and xr are the left and right most pixel positions of this scanline
plot(x,y, 1/z, u/z, v/z);
u += dU;
v += dV;
z += dZ;
}

Quote:
 If I divide the u and v by z, then divide by 1/z

if you were doing this, you'd simply be dividing by z then multiplying, it would just give you back u,v.

Tim

##### Share on other sites
Have a look at how Direct3D and OpenGL use homogeneous coordinates and a 4x4 projection matrix. After homogeneous division and viewport scaling, this results in (x, y, z, w) per vertex. 'z' is to be used -only- for the z-buffer. It is scaled by the projection matrix between 0.0 and 1.0. 'w' is used only for perspective correction and corresponds to 1/z where this 'z' denotes the 'z' before the projection matrix.

Confused? Sorry, the full explanation takes a few pages. But at least you have some keywords to google up. This approach also has great advantages for frustum clipping. In time you'll notice that the approach taken by hardware APIs is optimal.

For the actual rasterization I can only recommend Chris Hecker's Perspective Texture Mapping articles. They are worth gold for creating a flawless rasterizer with perspective texture mapping so read them ten times if you have to.

Don't hesitate to ask for some clarification!

##### Share on other sites
Thanks for the replies, and I do need some clarification if you don't mind.

Tim, did you really mean:

dU = (ul/zl - ur/zr)/(xr - xl)
dV = (vl/zl - vr/zr)/(xr - xl)

or...

dU = (ur/zr - ul/zl)/(xr - xl)
dV = (vr/zr - vl/zl)/(xr - xl)

C0D1F1ED, I have looked at Chris Hecker's excellent articles but am confused when he says that x' is derived from x/z and interpolated from x0' to x1'. My scan lines are already projected into screen space when I get to the texturing, so the only interpolation for x is by exactly 1 each pixel. What am I missing?

Thanks.

##### Share on other sites
Sorry for 2 consecutive posts, but I got the perspective working.

Thanks to Tim and Cod.

• ### What is your GameDev Story?

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

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634090
• Total Posts
3015432
×