# The mathematics of two dimensional interpolation on a quad

I am trying to wrap my head around how exactly the GPU interpolates texture coords on a quad. I realize to the GPU a quad is two triangles but I find thinking in terms of the whole quad to be easiest.

If we define texture space as the texture coordinates from X:Y{0-1} and world space as the actual coordinates of the quad how can I given a point p1 that is on the quad figure out what its texture coordinate would be? Given a texture coordinate point p2 how would I figure out its real world coordinates?

Keep in mind that the quad can be any quadrilateral not necessarily always a parallelogram.

As an extra note lets define the texture coordinates on the quad so that they are as such

• From the question title I thought you were asking about bilinear interpolation. For other folks who came here looking for information on that, here's a link (but feel free to explicitly ask a question too) blog.demofox.org/2015/04/30/… – Alan Wolfe Nov 2 '17 at 16:28
• I'm on the "split the quad into two triangles" camp. I've written about how interpolation works in a triangle in some detail here: gabrielgambetta.com/computer-graphics-from-scratch/… It doesn't use barycentric coordinates at all, so it complements the other answer nicely :) – ggambett Nov 14 '17 at 18:33
• There is a great explanation on Nathan Reeds Blog. Normally I would not suggest posting many links here, but it is to much and the only exact and good answer to this problem. – thewhiteambit Sep 3 '18 at 18:53
• With split polys there are some situations resolving false. Whenever there are degenerated vertexes split mapping goes wrong, so there is a reason for real quad mapping. However on 99% usecases this works well enough. – thewhiteambit Sep 3 '18 at 18:58

For this you just have to define the point you're interested in in terms of the quad's (or whatever primitive's) vertices. This is called a barycentric coordinate system. More practically-speaking, you basically give the point $n$ weights, each describing the influence the $i$th vertex of the quad has on the point. Those are the point's barycentric coordinates with respect to that quad.

So for example the center point of the quad (or "center of mass") would have the weights $\frac{1}{4}$, $\frac{1}{4}$, $\frac{1}{4}$, and $\frac{1}{4}$. A point in the middle of the left edge would have a weight of $\frac{1}{2}$ for the left two vertices each and a weight of $0$ for the right two vertices. (Notice how they always sum to $1$?)

These barycentric coordinates can then be used to interpolate whatever kind of per-vertex values you want, be that positions or texture coordinates. It all reduces to a simple weighted sum. And even more than that, they can also be quite easily computed by simple vector algebra from any existing per-vertex values if you have the point's values given (comes down to just a few little 2x2 determinants for a triangle, or similarly easy computation). This means if you know a pixel's screen space position and those of the vertices, you can compute its barycentric coordinates. These you can then use to interpolate the pixel's texture coordinates (or normal, or whatever) from the per-vertex values, or vice versa.

So much to the theory, there's a few practical considerations to keep in mind, especially when using them for interpolation in a rasterizer, like GPUs do.

• For anything but triangles the barycentric coordinates don't necessarily have to be uniquely defined for any arbitrary point. That's why thinking about triangles is much easier, especially for the computer.
• Due to the fact that they are negative if and only if the point doesn't actually lie inside the triangle they can also be used for checking if the point belongs to the triangle at all and thus are more or less a by-product of the pixel-in-triangle test that the rasterizer already performs to, well, rasterize the triangle.
• When it comes to interpolation in projected space, i.e. when projecting a 3D world onto the plane using perspective distortion, it becomes a little more involved since you have to account for the fact that a line's length in screen space is not the same as its length in world space, or more precisely its screen space length doesn't increase linearly when going along a triangle edge in world space. The keyword here is perspective correct interpolation. But this still uses the point's barycentric coordinates.
• Of course in an actual rasterizer you also have to take account for shared triangle edges and only rasterize a shared edge once, which requires special rules as to which triangle a shared edge belongs and might make the computation of barycentric coordinates a little more complicated, together with using fixed-point coordinates or whatever optimized computations.

(This is a more general overview of the matter and how it generally works. For specific mathematical as well as technical details, I'd advise for further reading, maybe starting with the above Wikipedia articles.)

• Good answer. But it's mostly about using barycentric coordinates, which work best with triangles. So, do GPUs just break a quad into two triangles? If they do, aren't there some issues related to continuity across the junction? – bubba Nov 3 '17 at 0:41
• Yes, they do. And yes, there are (or can be), but those are relatively minor depending on your geometry. In fact modern APIs for hardware accelerated rasterization have dropped the concept of a quad altogether, since it has always been just a convenience feature without any special treatment compared to just two triangles. So nowadays even the breaking into two triangles isn't done by the GPU but has to be done by you. – Christian Rau Nov 3 '17 at 13:32