Bounding rectangle for a triangle and normalized coordinates

654 Views Asked by At

I'm working on some computer graphic stuff and can't figure out solution to one problem:

I have three points in 3D space, each defined with (x,y,z) tupple. Three points together form a triangle. Let point names be A, B and C, so triangle is ABC.

Now I'd like to form a bounding rectangle such that AC is one of the sides. and point B lies on the opposide side of the rectangle. Now I want to map an arbitrary point (p) within the rectangle to normalized UV coordinates, so:

  • U represents how far projection of the point p to AC vector is from A.
  • V represents the orthogonal part of the rectangle. A bit hard to explain, but see the picture.

Anyway, I think I'm good with finding U:

vec3 AC = vec3(C - A);
vec3 Ap = vec3(p - A);
float u = dot(Ap, AC)/dot(AC, AC);

But I can't figure out how to find V. As I use GLSL, it's more than welcome if the answer will use matrix thingies like dot, cross or anything GLSL has.

Clarification picture

1

There are 1 best solutions below

4
On BEST ANSWER

In 3-d space, the cross product is your friend for problems like this. It generates a vector that is perpendicular to both of its input vectors: $U\times(B-A)$ is a vector normal to the triangle and, relative to your illustration, it points out of the page. Taking another cross product, $\left(U\times(B-A)\right)\times U$ lies in the plane of the triangle, is perpendicular to $U$ and is counterclockwise from it, i.e., it points in the direction of $V$. All that’s left is to adjust its length to suit.

However, I don’t think that you really want to normalize $U$ (as well as $V$, eventually) as you’ve done. That makes to $u$-coordinate of $C$ equal to its distance from $A$ instead of $1.0$ as the diagram indicates. I expact that you really need to leave $U$ equal to exactly $C-A$ and set the length of $V$ to the perpendicular distance of $B$ from $AC$, i.e., you want to adjust the length of $V$ so that $V\cdot(B-A)=1.0$.