<p>(It actually <em>is</em> easier to think (and compute) about this with triangles, but for the sake of the answer, let's first stick to your quad example.)</p>

<p>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 <a href="https://en.wikipedia.org/wiki/Barycentric_coordinate_system" rel="noreferrer"><strong>barycentric coordinate system</strong></a>. More practically-speaking, you basically give the point $n$ weights, each describing <strong>the influence the $i$th vertex of the quad has on the point</strong>. Those are the point's barycentric coordinates with respect to that quad.</p>

<p>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$?)</p>

<p>These barycentric coordinates can then be used to <strong>interpolate whatever kind of per-vertex values</strong> you want, be that positions or texture coordinates. It all reduces to a simple <strong>weighted sum</strong>. And even more than that, they can also be quite easily computed by simple vector algebra from any <strong>existing per-vertex values if you have the point's values</strong> 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.</p>

<p>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.</p>

<ul>
<li>For anything but triangles the barycentric coordinates don't necessarily have to be uniquely defined for any arbitrary point. That's why <strong>thinking about triangles is much easier</strong>, especially for the computer.</li>
<li>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 <strong>checking <em>if</em> the point belongs to the triangle at all</strong> 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.</li>
<li>When it comes to interpolation in projected space, i.e. when <strong>projecting a 3D world onto the plane using perspective distortion</strong>, 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 <a href="https://en.wikipedia.org/wiki/Texture_mapping#Perspective_correctness" rel="noreferrer"><strong>perspective correct interpolation</strong></a>. But this still uses the point's barycentric coordinates.</li>
<li>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.</li>
</ul>

<p><sub>(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.)</sub></p>

CC BY-SA 3.0
2017-11-03T00:41:18.097Z
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
CC BY-SA 3.0
2017-11-03T13:32:09.157Z
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
CC BY-SA 3.0
2017-11-01T18:08:40.643Z
2017-11-01T18:29:48.963Z
2017-11-01T18:29:48.963Z
Christian Rau
<p>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.</p>

<p>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?</p>

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

<p>As an extra note lets define the texture coordinates on the quad so that they are as such
<a href="https://i.sstatic.net/2wkpm.png" rel="noreferrer"><img src="https://i.sstatic.net/2wkpm.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2017-11-02T16:28:33.570Z
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) https://blog.demofox.org/2015/04/30/bilinear-filtering-bilinear-interpolation/
Alan Wolfe
CC BY-SA 3.0
2017-11-14T18:33:51.950Z
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: http://www.gabrielgambetta.com/computer-graphics-from-scratch/shaded-triangles.html It doesn't use barycentric coordinates at all, so it complements the other answer nicely :)
ggambetta
CC BY-SA 4.0
2018-09-03T18:53:04.493Z
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
CC BY-SA 4.0
2018-09-03T18:58:54.753Z
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
<texture><gpu><interpolation>
The mathematics of two dimensional interpolation on a quad
J.Doe
# The mathematics of two dimensional interpolation on a quad

(Asked by: username_0 on 2017-11-01)

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
[![enter image description here](https://i.sstatic.net/2wkpm.png)](https://i.sstatic.net/2wkpm.png)

**Tags:** texture, gpu, interpolation

> **Comments:**
> **username_1**: 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) https://blog.demofox.org/2015/04/30/bilinear-filtering-bilinear-interpolation/  
> **username_2**: 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: http://www.gabrielgambetta.com/computer-graphics-from-scratch/shaded-triangles.html It doesn't use barycentric coordinates at all, so it complements the other answer nicely :)  
> **username_3**: 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.  
> **username_3**: 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.  

---

## 1 answers

### Answer by username_4 on 2017-11-01. Score: 8

(It actually *is* easier to think (and compute) about this with triangles, but for the sake of the answer, let's first stick to your quad example.)

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**](https://en.wikipedia.org/wiki/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**](https://en.wikipedia.org/wiki/Texture_mapping#Perspective_correctness). 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.)

> **Comments:**
> **username_5**: 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?  
> **username_4**: 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.  

---

