View Full Version : Local to World Coords
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?
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..c 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.
# Perspective divide...
Now, after this o[HPOS] will have the position of the vertex in window space... I guess putting only MODELVIEW in c.. 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.
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*/
WorldX = LocalX * modelview + LocalY * modelview + LocalZ * modelview + modelview;
WorldY = LocalX * modelview + LocalY * modelview + LocalZ * modelview + modelview;
WorldZ = LocalX * modelview + LocalY * modelview + LocalZ * modelview + modelview;
float w = LocalX*modelview + LocalY*modelview + LocalZ*modelview + modelview;
WorldX /= w;
WorldY /= w;
WorldZ /= w;
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.
Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.