View Full Version : Local to World Coords

04-22-2003, 12:28 PM
Hope that someone can help...
I need to know the location of a point in world coordinates, when I only know the local coordinates. There are many, many translations in-between. Could I use the Modelview matrix with the (local) point in some manner?

04-24-2003, 10:59 AM
I used VPs some time ago and I did transformation looks as follows (not sure right now however):

- v[OPOS] is you vertex object space position (this should be the coordinates you have)
- o[HPOS] is vertex homogeneous position (position of vertex after transforming)
- c[0]..c[3] is modelview projection matrix

DP4 result,vec1,vec2 means
result.xyzw = vec1.x * vec2.x + vec1.y * vec2.y + vec1.z * vec2.z + vec1.w * vec2.w

# Standard gl transform thuru vp.
DP4 o[HPOS].x,v[OPOS],c[0];
DP4 o[HPOS].y,v[OPOS],c[1];
DP4 o[HPOS].z,v[OPOS],c[2];
DP4 o[HPOS].w,v[OPOS],c[3];
# Perspective divide...

Now, after this o[HPOS] will have the position of the vertex in window space... I guess putting only MODELVIEW in c[0]..[3] should yeld world space coords... not sure however.

Here, I used a VP-like syntax but of course, you have to do it in CPU. Looks kinda easy however.

I also suppose you could skip calculations on the w factor while you can surely skip things about the perspective division (since there's no perspective correction to do).

I may also need this quite soon. I will be really pleased if you can tell me if it works... just an idea.

04-25-2003, 08:21 AM
Thanks very much for your reply, but I had already come up with a solution. Though I'm a bit unsure of your syntax, but I believe that what you and I have done are one in the same.

In the draw routine I record the point in local coordinates and the current modelview matrix (when I draw the point). When I need the point in question in world coords, I execute the following code.


/*LocalX, LocalY, LocalZ and modelview are the values stored when I drew the point*/

float WorldX;
float WorldY;
float WorldZ;

WorldX = LocalX * modelview[0] + LocalY * modelview[4] + LocalZ * modelview[8] + modelview[12];

WorldY = LocalX * modelview[1] + LocalY * modelview[5] + LocalZ * modelview[9] + modelview[13];

WorldZ = LocalX * modelview[2] + LocalY * modelview[6] + LocalZ * modelview[10] + modelview[14];

float w = LocalX*modelview[3] + LocalY*modelview[7] + LocalZ*modelview[11] + modelview[15];

WorldX /= w;
WorldY /= w;
WorldZ /= w;

---end code---

I now have in the World* variables the desired point transformed into the world coords, and can highlight the point at any time in the code by simply translating there after a glLoadIdentity.

Thanks again for the reply.