# How can i calculate world coordinates?

This topic is 3393 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
As the name GL_MODELVIEW_MATRIX implies, the resulting matrix is the product of the VIEW matrix and the MODEL matrix. Hence it defines the transformation from local space (across the world space) to view space.

Look at this thread. Therein I've described a method that does something similar like you want.

##### Share on other sites
Quote:
 Original post by haegarrAs the name GL_MODELVIEW_MATRIX implies, the resulting matrix is the product of the VIEW matrix and the MODEL matrix. Hence it defines the transformation from local space (across the world space) to view space.Look at this thread. Therein I've described a method that does something similar like you want.

can't exactly find where i can calculate world coordinates in it.

I just wanna know. when i am drawing anything in a scene who's position is not knows to me. what i know is that i have a camera and i am doing some transformations and after that i am drawing that object. can i find his position in terms of a vector (x,y,z) from the transformation matrices given?

##### Share on other sites
As explained in the cited thread, you've to break OpenGL's computation of the MODELVIEW matrix, intermediately perform the computation of the world matrix, and then continue the MODELVIEW matrix by multiplying with the world matrix.

An outline looks like this:
glLoadIdentity();// do here all transformations of the camera as usual// at this point, the MODELVIEW matrix consists of nothing but the VIEW portionglPushMatrix(); // create a (temporary) matrix that can be overwritten nextglLoadIdentity(); // initialize temporary matrix as identity matrix// do here all transformation of the model as usual// at this point, MODELVIEW has an identity VIEW portion and the wanted MODEL portion, ...// ... so it shows a pure local-to-global transformationGLfloat model[16];glGetFloatv(GL_MODELVIEW_MATRIX, model); // read the model's world matrix// at this point the "model" variable contains the wanted world matrixglPopMatrix(); // remove the temporary matrixglMultMatrix(model); // complete the MODELVIEW matrix// at this point the MODELVIEW matrix is the product of the original VIEW portion ...// ... and the model's world matrix// do here the rendering of the model

The shown method is not meant to be optimal. E.g. it may be better to isolate the world matrix computation and perform it (for each model) before entering the rendering loop.

##### Share on other sites
Yea i get it now.. thanx a bunch..

i feel a little dumb though :)

[Edited by - waZim on June 5, 2009 12:16:35 PM]

1. 1
2. 2
Rutin
22
3. 3
4. 4
frob
18
5. 5

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

• Total Topics
632566
• Total Posts
3007107

×