# OpenGL help with GL_MODELVIEW_MATRIX

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

## Recommended Posts

hello all, I am trying to figure out how to retrieve from the OPENGL where is an object located (in my camera coordinates systems). for example. I have created a few objects after translations\rotations, and I want in each certain time, to get the X Y Z of the current object location. how can I know that?? that is because I want to change my view according to a specific object created long time ago. thanks

##### Share on other sites
You can multiply the object's coordinates with the modelview matrix.

##### Share on other sites
but how?
should i (after rotating that object ) - between the PUSH and POP insert a line that says: glGetFloatv(GL_MODELVIEW_MATRIX,bla);
and when creating a new object what should i write??
glMultMatrix(bla);

becuase I tried that... it draws wierd thing, which changes when the camera is moving...

##### Share on other sites
glGetFloatv(GL_MODELVIEW_MATRIX,bla); returns the current modelview matrix on the stack

so if your code is
glPushMatrix();glMultMatrixf(...);(123)glPopMatrix();

You would need to put glGetFloatv(GL_MODELVIEW_MATRIX,bla); at line (123)
where bla is float bla[16];

The object's location, assuming the original position was {0,0,0} would
be {bla[12], bla[13], bla[14]}

##### Share on other sites
i am sorry i am asking, but I have a lttle lack of understanding:
when I draw the object, i am rotating\changing the current stack matrix.
if I want to "save" it, i use the GLGETFLOAT.... thats ok so far.

i have a camera currently located somewhere, and I want this gluLookAt to be exactly where the MODELMATRIX i saved in the object creation.

how can I achieve this???

to create a new object there I can, but not to change my view to there.
thanks

##### Share on other sites
If you use gluLookAt in your code, you will have something like this:
gluLookAt(  cameraX,cameraY,cameraZ,  targetX,targetY,targetZ,  upX,upY,upZ);glPushMatrix();//transform to object, ex:glTranslatef(objectX,objectY,objectZ);//draw the object (not listed here)float matrix[16];glGetFloatv(GL_MODELVIEW_MATRIX,matrix);//the current position of the object, in world space is now in matrix[12],matrix[13],matrix[14]glPopMatrix();

##### Share on other sites
I understand, this one I achieved in doing, but how to i implement these matrix[12],[13],[14] on the gluLookat??? it seems not to work well!

##### Share on other sites
First of all, it is ever a better way to deal self with transformations and spaces instead of "misusing" OpenGL's MODELVIEW matrix for such purposes.

OpenGL's MODELVIEW matrix is a composition of the VIEW portion and the MODEL portion. The VIEW portion defines a global-to-view (or also global-to-camera) space transformation, while the MODEL portion defines a model-to-global space transformation. Since the VIEW portion and MODEL portion are constructed in a sequence like
glMultMatrix(invCamTrafo); // view trafo is invers camera trafo
glMultMatrix(modelTrafo);
they are multiplied left to right like so
V * M

That means that if you're fetching the MODELVIEW matrix you ever get it conglomerated with the VIEW matrix, since the VIEW matrix is the leftmost matrix! Now, you may leave the VIEW matrix as the identity matrix and so nullifying its effect, constructing the MODEL portion, fetch the MODEL portion and continue with the usual sequence (as show above). You may also fetch MODELVIEW matrix just after constructing the VIEW portion, invert that matrix, and multiply it manually on the left of any subsequently fetched MODELVIEW matrix. As you can see here, all these ways are quirky.

If you still want to go this way, IMHO your best approach would be to compose the model transformation as usual but without any view transformation, fetch the MODELVIEW matrix from OpenGL and isolate the position vector (i.e. the 12th, 13th, and 14th value from the array), set-up the camera using these values, and then re-initialize the MODELVIEW stack, multiply the VIEW portion onto it, and multiply the MODEL portion onto it (perhaps the formerly fetched array).

##### Share on other sites
Quote:
 Original post by DaedalusOwnsYougluLookAt( cameraX,cameraY,cameraZ, targetX,targetY,targetZ, upX,upY,upZ);glPushMatrix();//transform to object, ex:glTranslatef(objectX,objectY,objectZ);//draw the object (not listed here)float matrix[16];glGetFloatv(GL_MODELVIEW_MATRIX,matrix);//the current position of the object, in world space is now in matrix[12],matrix[13],matrix[14]glPopMatrix();

Nope! That position is given in camera local space, not in world space.

Quote:
 Original post by HimberJackI understand, this one I achieved in doing, but how to i implement these matrix[12],[13],[14] on the gluLookat??? it seems not to work well!

To make it clear: As soon as you invoke gluLookAt (with the MODELVIEW stack being active) you've changed the MODELVIEW matrix with something that is intended to define the VIEW portion. Then it is too late for fetching a pure model matrix from the stack later on if you don't explicitely prepare the stack.

##### Share on other sites
I invoke the lookAt only outside that modelview on identity matrix, so I actually hold the currect MODELVIEW matrix

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 30
• 41
• 23
• 13
• 13
• ### Forum Statistics

• Total Topics
631741
• Total Posts
3001981
×