How can i calculate world coordinates?

Started by
3 comments, last by waZim 14 years, 10 months ago
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.
Advertisement
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.
Quote:Original post by haegarr
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.


i have read that thread many times. and i read it again today as well.

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?

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.
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]

This topic is closed to new replies.

Advertisement