# How can i calculate world coordinates of anything i draw in scene.

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

## Recommended Posts

hi all, i want to draw an object with the following transformations
glTranslatef(1,2,3);
DrawObject();


Now if i do this instead
float Matrix[16];
float *vect1 = new float[3];

glTranslatef(1,2,3);
glGetFloatv(GL_MODELVIEW_MATRIX,Matrix);
vect[0] = Matrix[12];
vect[1] = Matrix[13];
vect[2] = Matrix[14];
DrawObject();


Would this "vect" be not the World Coordinates of Object i am drawing? In this case the vect should be (1,2,3); if Not.. then i how would i calculate its world coordinates. Thanks in advance.

##### Share on other sites
Have you considered setting the world coordinates first, and then drawing the scene object based on those world coordinates that you already know?

For example keeping a "vect1" for each object, in which you set the object coordinates before drawing anything. Then you can draw the object on those coordinates, and will know the exact coordinates without having to fetch them from inside OpenGL.

##### Share on other sites
no, because other matrices may be stacked to begin with.
But like Dim_Yimma_H, since you call the translate with 1,2,3, you already know this. I can think of no case of retrieving coordintaes for something in such a manner.

##### Share on other sites
If you apply a view transformation before your model transformation, any value you read back will be in eye space, and you will need to apply the inverse of the view matrix (I think) to get it back into world space. But you should actually avoid reading back any info from the GPU unless you can't help it, because GPUs are generally optimized for one-way communication. Just store this data in your scene to begin with.

##### Share on other sites
Thank you all for the replies.

I actually don't have to read those coordinates but it was just a test and a way to understand. And my confusion was cleared on this thread...

http://www.gamedev.net/community/forums/topic.asp?topic_id=537116

actually i was wondering if i can extract the World Matrix from Modelview matix. which i suppose is not "possible"? the only way that was mentioned on the cited thread was to keep track of your Worl matrix seperately..

##### Share on other sites
Quote:
 Original post by Kincaidno, because other matrices may be stacked to begin with.But like Dim_Yimma_H, since you call the translate with 1,2,3, you already know this. I can think of no case of retrieving coordintaes for something in such a manner.whats your application ?

Well ofcourse i know that.. if i am trying to find 1,2,3 back it means its just a test. :)

The use of this is just like you said.. i have many matrices stacked before i draw one object and i want to find this world coordinates after all that.

##### Share on other sites
Quote:
 Original post by waZimactually i was wondering if i can extract the World Matrix from Modelview matix. which i suppose is not "possible"? the only way that was mentioned on the cited thread was to keep track of your Worl matrix seperately..

If you keep track of your view matrix, I am pretty sure you can get back world from modelview. With just the modelview it is not possible as you don't know which part is the model and which part is the view - because it makes no difference to OpenGL, it's all just translations (well, and rotations and scales, but you get the idea).

##### Share on other sites
Quote:
Original post by lightbringer
Quote:
 Original post by waZimactually i was wondering if i can extract the World Matrix from Modelview matix. which i suppose is not "possible"? the only way that was mentioned on the cited thread was to keep track of your Worl matrix seperately..

If you keep track of your view matrix, I am pretty sure you can get back world from modelview. With just the modelview it is not possible as you don't know which part is the model and which part is the view - because it makes no difference to OpenGL, it's all just translations (well, and rotations and scales, but you get the idea).

Yes true,, and i was doing just like that..

when i do the camera setup i also save my modelview matrix. which at that time (i suppose) is just the view matrix. and now lets say i do alot of matrix pushes/pops and multiplications etc.. when now i get the modelview matrix it has some cooridnates and lets say i am just concerned with Translation factor. so

// after camera setupglGetFloatv(GL_MODELVIEW_MATRIX,ViewMatrix);// and Invert the ViewMatrix.............// Later afterDrawObject();glGetFloatv(GL_MODELVIEW_MATRIX,WorldMatrix);Object.x = WorldMatrix[12] - ViewMatrix[12];Object.y = WorldMatrix[13] - ViewMatrix[13];Object.z = WorldMatrix[14] - ViewMatrix[14];

Is this how it should be? i am not getting proper coordinates with this. .they are still in Eye space. or some other "space" :) but not in world.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• ### Forum Statistics

• Total Topics
634088
• Total Posts
3015421
×