Jump to content
  • Advertisement
Sign in to follow this  

Converting u,v to .MAP texture axes

This topic is 4555 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi folks, For our generic modeling package, I am trying to write a decent .MAP exporter (the 220 format, targeting the Torque map2dif tool to be precise). What I have is a polygon representation, including u, v coordinates, of a convex solid. What I need to output for each plane (polygon) is
( x1 y1 z1 ) ( x2 y2 z2 ) ( x3 y3 z3 ) TEXTURE_NAME [ tx1 ty1 tz1 toffs1 ] [ tx2 ty2 tz2 toffs2 ] rotation scaleX scaleY
I tackle the conversion from polygons to plane by simple writing out 3 vertex coordinates. However, I simply don't "grok" the calculation of texture axes. So far I have been postponing this by generating a simple planar mapping, but now is the time to face my lack of math skills and get this solved. :D The conversion from axes to coordinates is straightforward (as found in a different post on this forum):
// assuming rot_angle = 0, and scale is 1
u = (DotProduct(A, U_Axis) + U_shift) / texture_width;
v = (DotProduct(A, V_Axis) + V_shift) / texture_height;
Now how do I reverse this to solve for the U and V axes? Writing this out yields 2 sets of 3 equation with 4 unknowns, which implies an infinite set of solutions, but I am sure I am missing the point here. Should I assume U_Axis and V_Axis to be orthogonal (which makes sense for any texture mapping our tool can generate)? I don't really need the rot_angle, now do I? So, to close off, a small recap: How do I calculate the texture axes U and V (including shift, and possibly rotation and scale), given 3 vertices and 3 sets of (u,v) coordinates? Any help is most appreciated!

Share this post

Link to post
Share on other sites
Okidoki, I stumpled upon a lucky google-hit, did the math from there using trial and error, and it looks like I got things up and running now. For future reference, here is what I found:

Quoting Matt McLelland in a post on alt.math:


If I may generalize your problem a bit, lets assume that your texture has its
origin at some point O with U and V vectors along the unit u and v axes. So the
texel at (u,v) would be at P = O + u*U + v*V in 3-space. Now, to determine O,
U, and V from known locations of texels we will need three such points. So,
suppose we know that (x_i, y_i, z_i) corresponds to the texel (u_i, v_i) for
i=1,2,3. Then, the equations involving the x-coordinate would be:
x_i = O_x + u_i*U_x + v_i*V_x for i=1,2,3.
Three equations and three unknowns. For your convenience, here is the matrix
[x1] [1 u1 v1] [O.x]
[x2] = [1 u2 v2] * [U.x]
[x3] [1 u3 v3] [V.x]
Equations in the other coordinates are analogous.

Thanks Matt! Solving using cramers rule:
D := Det(
1, u1, v1,
1, u2, v2,
1, u3, v3 );

If D = 0, then no solution exists.
O.x = Det(
x1, u1, v1,
x2, u2, v2,
x3, u3, v3 ) / D;
U.x = Det(
1, x1, v1,
1, x2, v2,
1, x3, v3 ) / D;
V.x = Det(
1, u1, x1,
1, u2, x2,
1, u3, x3 ) / D;

Analogous for Y and Z. This gives us the U and V axis (we disregard the O for now).

However, the map file format uses axis that represent a fraction of the texture, not the distance covered in 3D space, so we need to scale:
U = U * TEXTURE_WIDTH / Length( U ) ^ 2;
V = V * TEXTURE_WIDTH / Length( U ) ^ 2;

And finally, we need to calculate our offsets along those axes
offsetU = u1 * TEXTURE_WIDTH - Dot( P1 , U )
offsetV = v1 * TEXTURE_HEIGHT - Dot ( P1, V )

Rotation and scale are redundant and can always be left to 0, 1, 1.

Hope it helps someone. Oh, and any comments are most welcome.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!