Skip to main content
gamedev.net
🔒 Locked

3d point of model from 2d texture coordinate

Started by smittix Aug 26, 2007 at 9:26 PM 1 replies 1k views
Original Post
smittix
smittix
Basically I'm trying to obtain the 3d position of a 2d texture coordinate on a 3d model, given the 3d position and 2d texture coordinate of the 3 points of the triangle which the 2d texture coordinate is on. I attempted to take a linear approach with the following fomula:

float v1, w1, x1, y1, z1;
float v2, w2, x2, y2, z2;
float v3, w3, x3, y3, z3;
float a, b, c, d, e, f;

//vertex 1
v1 = 0.1f;
w1 = 0.1f;
x1 = 1.0f;
y1 = 0.3f;
z1 = 1.7f;

//vertex 2
v2 = 0.8f;
w2 = 0.1f;
x2 = 18.2f;
y2 = 13.6f;
z2 = 7.6f;

//vertex 3
v3 = 0.1f;
w3 = 0.8f;
x3 = 0.3f;
y3 = 6.2f;
z3 = 10.3f;

//calc 2x3 matrix
a = (x1*w2 - x2*w1) / (w2*v1 - w1*v2);
b = (x1*v2 - x2*v1) / (w1*v2 - w2*v1);

c = (y1*w2 - y2*w1) / (w2*v1 - w1*v2);
d = (y1*v2 - y2*v1) / (w1*v2 - w2*v1);

e = (z1*w2 - z2*w1) / (w2*v1 - w1*v2);
f = (z1*v2 - z2*v1) / (w1*v2 - w2*v1);

//point in question
float v0, w0, x0, y0, z0;
v0 = 0.37f;
w0 = 0.53f;

//calc position of point in question
x0 = (a*v0)+(b*w0);
y0 = (c*v0)+(d*w0);
z0 = (e*v0)+(f*w0);

And here is a picture for the more visual thinkers: The 'x, y, z' represents the 3d position of the point, while the 'v, w' represents the texture coordinate of the point. 'x, y, z' can be any given point in 3d space, and the 'v, w' can be any value between 0 - 1. The formula I have there only takes 2 points into account which I think is the problem, but I'm not sure. Thanks in advance to anyone who attemps to solve this.
Nyarlath
Nyarlath
I don't know exactly how you compute the weights a-f, but I think you should get at the end something like:

//calc position of point in question
x0 = x1 + (a1*v0)+(b*w0);
y0 = y1 + (a2*v0)+(d*w0);
z0 = z1 + (a2*v0)+(f*w0);


I would do, with c1 = [x1, y1, z1] (and the same for y, z and 0, 2, 3) and u1 = [v1, w1] (and the same for 0, 2, 3):

Compute the wieghts t = [t1, t2]

u0 = u1 + (u2-u1) * t1 + (u3-u1) * t2

This is a 2 dimensional equation with 2 variables... (if the triangle is not a line you shall not have problems)
Once you have t, you can compute

c0 = c1 + (c2-c1) * t1 + (c3-c1) * t2

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.