13
$\begingroup$

I want to find the texture coordinates for point P. I have the vertices of the triangle and their corresponding uv coordinates.

The numbers in the little squares in the texture represent color values.

What are the steps of computing the uv coordinates of P?

enter image description here

$\endgroup$
  • $\begingroup$ There is a affine transform that will map each corner to its texture coordinate, you can use that to map P to its uv. $\endgroup$ – ratchet freak Jan 5 '16 at 16:59
  • $\begingroup$ @ratchetfreak could you provide me a link plz ? $\endgroup$ – john john Jan 5 '16 at 17:06
  • $\begingroup$ There is a good write up on how to do the intersection point calculation as well as barycentric cord calculation in one go in this paper. This essentially amounts to transforming the triangle. $\endgroup$ – joojaa Jan 6 '16 at 7:26
14
$\begingroup$

This is achieved via Barycentric Interpolation.

First, we find the barycentric coordinates of $P$. Barycentric coordinates represent how much weight each vertex contributes to the point, and can be used to interpolate any value which is known at the vertices across the face of a triangle.

Consider the 3 inner triangles $ABP$, $PBC$ and $PCA$.

enter image description here

We can say that the barycentric coordinate, or weight of the vertex $A$ on the point $P$ is proportional to the ratio of the area of inner triangle $PBC$ to the area of the whole triangle $ABC$.

This is intuitively evident if we consider that as $P$ approaches $A$ the triangle $PBC$ grows larger and the other two become smaller.

Also intuitively evident should be that the sum of the barycentric coordinates of a point inside a triangle always equals $1$. So, it is enough to find only two of the coordinates to derive the 3rd.

The method for computing the barycentric coordinates is:

$$\begin{aligned} {Bary}_A &= \frac{(B_y-C_y)(P_x-C_x) + (C_x-B_x)(P_y-C_y)}{(B_y-C_y)(A_x-C_x) + (C_x-B_x)(A_y-C_y)}\\ {Bary}_B &= \frac{(C_y-A_y)(P_x-C_x) + (A_x-C_x)(P_y-C_y)}{(B_y-C_y)(A_x-C_x) + (C_x-B_x)(A_y-C_y)}\\ {Bary}_C &= 1 - {Bary}_A - {Bary}_B \end{aligned}$$

The derivation and reasoning is explained in the wikipedia article.

Once you have coordinates, you can determine the texture coordinates of $P$ by interpolating the values at the vertices using the barycentric coordinates as weights:

$$P_{uv} = {Bary}_A \cdot A_{uv} + {Bary}_B \cdot B_{uv} + {Bary}_C \cdot C_{uv}$$

The reasoning is also explained very nicely in this presentation.

Also see this question for efficient methods of computation.

$\endgroup$
  • $\begingroup$ i sthere a error in $Bary_B$? Should the first term be $(A_y-C_y)$ or am i wrong? $\endgroup$ – joojaa Jan 6 '16 at 7:35
  • $\begingroup$ @joojaa I don't think so. It's the same in the Wikipedia article, and it seems correct from a test calculation I did. $\endgroup$ – Rotem Jan 6 '16 at 7:52
  • $\begingroup$ ah, so it is $-(A_y-C_y)$, might be good to point out as you would pre calculate $AC_y = (A_y-C_y)$. $\endgroup$ – joojaa Jan 6 '16 at 8:11
  • 1
    $\begingroup$ @joojaa The entire denominator and some of the terms in the nominator can be precalculated for each triangle, only few of the terms depend on $P$. I've added a link to a question dealing with methods of calculation. In this formula I thought it would be better to keep the notation simple and uniform rather than efficient. $\endgroup$ – Rotem Jan 6 '16 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.