# How to generate a rotation matrix or a quaternion (properly rotated in regards to texture coordinates) for each triangle of a mesh?

I'm trying to build a uniform grid made of 3D points over the surface of an arbitrary mesh (we have texture-coordinates for each vertex...)

Or to reformulate: imagine a mesh textured with a texture featuring equally spaced dots: I want to obtain the 3D coordinates of these dots.

So far, I'm able to generate a normal for each triangle of the mesh but it is not enough.

If I had the right matrix, or quaternion or even orthonormal-basis for each triangle, I guess that I could transform any 2D coordinates (on the triangle's plane) to 3D coordinates (in object space...)

The problem is how to rotate properly in regards to texture coordinates?

To be more specific: for each triangle, I can define a normal. It could serve as the x,y,z part of a quaternion. But how could I generate the w part?

• The normal is not similar to the x, y, z part of the quaternion, that part is the rotation axis of the quaternion, that i.e. rotates a plane, so it's normal is similar to your normal. w defines the angle of rotation. The rest of your question is a bit ambiguous, I understand you need the rotation that rotates [?fill in here?] to your normal. I'm just not sure from where and what should be rotated. Can you try to simply list the input variables and the expected output variables? – Maik Semder Jun 5 '11 at 14:35
• Input: 3 vertices of a triangle in object-space and 3 texture coordinates. Desired output: 3d coordinates in object-space of a point on the triangle plane defined in 2d coordinates (similar to the texture coordinates) – Ariel Malka Jun 5 '11 at 15:29

Ok this shows you how to calculate the rotation-matrix that rotates the triangle from its local space into world-space.

Given normalLocal (the normal of the triangle in object-space) and normalWorld (the normal of the triangle in world-space)

1. Find the rotation-axis by taking the cross-product of the 2 normals

axis = normalLocal cross normalWorld

2. Find the rotation angle by taking arccosine of the dot-product of both normals

angle = acos(normalLocal dot normalWorld)

3. Now create a rotation-matrix from those values .

You can just use the texture-coordinates to define the triangle points. Just put them into x and y of the point and leave z at zero.

Example:

texUV {u, v} are the 3 texture-coordinates for 1 trangle

p are the 3 associated euclidean points of the triangle

texUV0 = {0.0, 0.0}
texUV1 = {0.5, 0.5}
texUV2 = {0.5, 0.0}

p0 = {texUV0.u, texUV0.v, 0.0}
p1 = {texUV1.u, texUV1.v, 0.0}
p2 = {texUV2.u, texUV2.v, 0.0}

p0 = {0.0, 0.0, 0.0}
p1 = {0.5, 0.5, 0.0}
p2 = {0.5, 0.0, 0.0}


The normal for this triangle is {0.0, 0.0, 1.0} or {0.0, 0.0, -1.0} depending on your definition of a front face, clockwise or counterclockwise.

• Maik, sorry but I can't accept your answer since I don't understand how to produce the 3D coordinates in object-space of a 2d location defined in the triangle's plane. – Ariel Malka Jun 5 '11 at 15:30
• Ok, no worries, we're getting there. Still trying to figure out what you need. I showed you in the post how to produce the 3D coordinates in object-space of a 2d location defined in the triangle's plane, it is p0, p1 and p2. Can you explain which step you didn't understand? Because those 3 points are exactly that. – Maik Semder Jun 5 '11 at 15:34
• I have a mesh made of many triangles. Each triangle has its own orientation (that is the object-space I'm talking about). Your example is totally ignoring the position and orientation information for each of the triangles... – Ariel Malka Jun 5 '11 at 15:39
• Ok I totally misunderstood your question, now it makes more sense. So you have a mesh (triangle points and normals) defined in world-space, you also have texture coords defined for each point. Now you need the transformation-matrix that transforms each triangle from object-space into world-space, right? – Maik Semder Jun 5 '11 at 15:44
• Thanks for still trying to help. I'm not sure anymore about the object-space / world-space concepts, so let's keep them aside. Imagine a mesh textured with an image of regularly spaced dots: I want to obtain the 3D coordinates of these dots. That is really the best way I can think of a defining my problem. – Ariel Malka Jun 5 '11 at 15:57

I found this answer by user S1CA in the gamedev.net forum:

A, B, C are the vertices of a particular triangle.
Auv, Buv, Cuv are the UV coordinates at the vertices.
Axyz, Bxyz, Cxyz are the positions of the vertices.
Puv are the UV coordinates of the point.
Pabc are the barycentric coordinates of the point [for that triangle]
Pxyz is the object space position of the point

aa = (Bu-Au) * (Cv-Av) - (Cu-Au) * (Bv-Av)
Pa = ((Bu-Pu) * (Cv-Pv) - (Cu-Pu) * (Bv-Pv)) / aa
Pb = ((Cu-Pu) * (Av-Pv) - (Au-Pu) * (Cv-Pv)) / aa
Pc = ((Au-Pu) * (Bv-Pv) - (Bu-Pu) * (Av-Pv)) / aa

The above are just a bunch of 2D cross products. The 2D cross product happens to give you the area of a triangle. If you look at the Wolfram reference posted above, it notes that the areas of the sub triangles ABP ACP and BCP are proportional to the barycentric coordinates.

The division of each sub triangle by the area of the main triangle is so we can get the useful property of areal coordinates that for any point which is inside the triangle, Pa+Pb+Pc=1

Once you have barycentric coordinates, you can use those to convert between texture units and object units by doing:

Pxyz = Axyz*Pa + Bxyz*Pb + Cxyz*Pc

• Just tested it and it works like a charm... – Ariel Malka Jun 6 '11 at 18:34