Sign in to follow this  
waZim

How can i calculate world coordinates?

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 this post


Link to post
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 this post


Link to post
Share on other sites
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?

Share this post


Link to post
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 portion
glPushMatrix(); // create a (temporary) matrix that can be overwritten next
glLoadIdentity(); // 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 transformation
GLfloat model[16];
glGetFloatv(GL_MODELVIEW_MATRIX, model); // read the model's world matrix
// at this point the "model" variable contains the wanted world matrix
glPopMatrix(); // remove the temporary matrix
glMultMatrix(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 this post


Link to post
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this