1

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

lhf
  • 216,483
lhog
  • 67

1 Answers1

0

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$.

amd
  • 53,693
  • I think I ended up doing something like this myself: triaNorm = cross(AC, AB); acNorm = cross(-triaNorm, AC); bProj = acNorm * dot(acNorm, B) / dot(acNorm, acNorm); u = dot(Ap, AC) / dot(AC, AC); v = dot(Ap, bProj) / dot(AC, AC);

    One thing left unclear was what vectors needed to be normalized. One suspicious vector was bProj, because whether normalization was on or off for this vector was giving the different results.

    – lhog Jun 05 '18 at 07:42
  • @akur Per the diagram in your question, you want $C=A+1.0U$, so $U=C-A$. If you normalize $U$, the $u$-coordinate of $C$ will be $|C-A|$, not $1.0$. – amd Jun 05 '18 at 17:42
  • U without any question doesn't need to be normalized as per your latest comment. I have my doubts about the vectors that eventually help define V, specifically bProj which is supposed to be the projection of point B onto normal to AC (coplanar to triangle plane). Also see: https://stackoverflow.com/questions/50688334/complete-tesselation-of-sphere-with-hexagons where I used the transformations above, but faced another issue. – lhog Jun 05 '18 at 18:10
  • @akur That all looks far more complicated than it needs to be. Compute the cross product that I’ve given and then adjust its length to get a $V$ such that $V\cdot(B-A)=1.0$. – amd Jun 05 '18 at 19:54