Perspective corrected texture mapping

Most approaches to this problem aim at finding some easily interpolatable expression (sometimes approximation) for U & V coordinates. One of the currently hottest techniques is outlined below.

Your "normal" texturemapper takes XYUV coordinates for each vertex. In your persp tmapper, take their Zs as well (the vertices' Zs used when 3d->2d projecting them). Calculate U/Z, V/Z, and 1/Z at each vertex, and interpolate these values linearly over the polygon (instead of interpolating U and V). Then, at each pixel, you perform these calcs to get UV: Z = 1 / 1/Z; U = U/Z * Z; V = V/Z * Z; that is, 1 div and 2 muls. 3-vertex polygons!

That div costs lots of CPU time though. If you optimize for P5, you can utilize a technique called "scanline subdivision" to get the div "for free". Perform all the U/Z, V/Z, 1/Z interpolation using the FPU. Then, only calculate UV at every 8th or 16th pixel. Run linearly in between (it's only noticeable that you cheat in extreme cases: check it out in Quake when you look almost parallel to a wall (ie when Z changes as much as possible per step in X direction)) -- and do the FDIV for next segment's 1/x calculation right before drawing current segment. Then the FDIV will be executed in parallel to the CPU's drawing code, and it will be done almost for free! Since you draw 8- or 16-pixel long segments (except for the last seg on each line but that's just a special case), Uinc = (u2 - u1) >> (3 or 4) and Vinc = (v2 - v1) >> (3 or 4) for each segment. (Good implementations of scanline subdivision are not more than 20% slower than corresponding linear/affine tmappers.)

Oh, and remember to check out the FPU's "accuracy" switch. If it is set to normal accuracy then the FDIV takes 39c and you probably will want to do 16-pixel segments, but if it is set to lowest possible accuracy the FDIV will only take 24c (and then 8-pixel segs might be possible at full speed).

Discuss this article in the forums

Date this article was posted to 7/16/1999
(Note that this date does not necessarily correspond to the date the article was written)

See Also:
Texture Mapping

© 1999-2011 All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!