View Full Version : How to get model matrix in OpenGL

Tomasz G.

09-27-2011, 11:34 AM

Hello everyone,

How can i get Model matrix in OpenGL?

I can get ModelView Matrx:

glGetFloatv (GL_MODELVIEW_MATRIX, m);

but i need to get Model Matrix only for my calculations.

Aleksandar

09-27-2011, 12:02 PM

There is no distinct Model matrix in OpenGL.

The model transformation applied to the whole scene is, in fact, the view transformation. That's why OpenGL combines modeling and viewing transformation in a single matrix.

Everything said before refers to legacy (fixed functionality) OpenGL. The "modern" approach (i.e. using shaders) does not impose restrictions on the way the matrices are combined.

Dark Photon

09-27-2011, 06:46 PM

I can get ModelView Matrx... but i need to get Model Matrix only for my calculations.

What Aleksandar said.

If you need the MODELING matrix, couple ways you can go about that. As you multiply MODELING matrices onto the MODELVIEW matrix stack, you can multiply them on your own accumulated MODELING matrix stack on the side, and then you've got the accumulated MODELING matrix.

Alternatively, keep track of the inverse VIEWING matrix, and then whenever you want the accumulated MODELING matrix, just grab the MODELVIEW and multiply by the inverse VIEWING matrix to extract just the MODELING transform.

This all assumes that floating point precision is sufficient for you. If not, just do your own matrix math and use double precision. There are GL math helper libraries out there to help you with this in either case (e.g. glh, GLM, etc.)

Aleksandar

09-28-2011, 03:41 AM

I guess OP is using glTranslate*/glRotate*/glScale commands instead of direct matrices loading. In that case it is better to get current model-view matrix just before the first object drawn (MV) and before the object for which he needs the model matrix (MM).

Since MM = MV * M, where M is the model matrix for that object,

it can be relatively easily calculated using the following formula:

M = M^(-1) * MM

Tomasz, I hope you know that the model matrix is different for each object, so it is pretty unclear what you want to achieve. Maybe gluUnProject() is the function you actually need?

Tomasz G.

10-01-2011, 06:10 AM

Thanks a lot for help. gluUnProject is not what I need. Now I have all necessary variables for my calculations.

Powered by vBulletin® Version 4.2.2 Copyright © 2016 vBulletin Solutions, Inc. All rights reserved.