# Fast perspective-mapped textures

[latexpage]

Scanline rendering is a common method of drawing polygons. A quad is a polygon, therefore we’ll scanline render it. If you’re unfamiliar with scanline rendering, it rasterises graphics one (axis-aligned) line at a time. How exactly you set each pixel is up to you: if drawing a single polygon, you can use standard line-drawing algorithms like Bresenham’s; if you want to render adjacent polygons, you probably only want to set pixels that lie within or on the polygon’s edges. And when antialiasing, you’d probably take another approach entirely. If you are interested in the details, check out chapters 38-42 of the Graphics Programming Black Book.

If you’re paying attention, you’re probably now thinking “can’t we map an x, y coordinate in screen space to find alpha and beta, and thus the texture coordinates at a given pixel?” The intuitive approach is to do what we did in part 1, except instead of drawing a line between two points on opposite sides given a coefficient, we draw a horizontal line at y and find the intersections. Let’s start by rearrange our equations from part 1 to solve for $\alpha$ given $\vec{AB}$:

\begin{eqnarray*}
\vec{AB_{\alpha}} &=& \frac{ (1 – \alpha ) \frac{ \vec{A} }{ z_A } + \alpha \frac{ \vec{B} }{ z_B } }{ (1 – \alpha ) \frac{ 1 }{ z_A } + \alpha \frac{ 1 }{ z_B } } \\
\vec{AB_{\alpha}}( (1 – \alpha) \frac{ 1 }{ z_A } + \alpha \frac{ 1 }{ z_B } ) &=& (1 – \alpha ) \frac{ \vec{A} }{ z_A } + \alpha \frac{ \vec{B} }{ z_B } \\
\alpha ( \frac{ \vec{B} }{ z_B } – \frac{ \vec{A} }{ z_A } + \frac{ \vec{AB_{\alpha}} }{ z_A } – \frac{ \vec{AB_{\alpha}} }{ z_B }) &=& \frac{ \vec{AB_{\alpha}} }{ z_A } – \frac{ \vec{A} }{ z_A } \\
\alpha &=& \frac{z_B(\vec{AB_{\alpha}} – \vec{A})}{z_A(\vec{B} – AB_{\alpha}) + z_B(\vec{AB_{\alpha}} – \vec{A})}
\end{eqnarray*}

Perform appropriate subsitutions for the other lines of the quad. If the coefficient is not in the range [0, 1], it does not intersect the horizontal line at y; however it’s fastest simply to compare the y coordinates of each point to determine what will intersect.

Obviously, the u, v coordinates will vary non-linearly along the horizontal line due to the perspective transform, so we must compute the depth of the intersection points too. You already have that formula from part 1, however 🙂

## One thought on “Fast perspective-mapped textures”

1. I see you don’t monetize your site, don’t waste your
traffic, you can earn extra bucks every month because you’ve got high quality content.
If you want to know how to make extra \$, search for: