View Full Version : Evaluate matrix stack

Oliver Demetz

03-13-2008, 01:37 PM

Hi,

My question is that basic that I don't even know how to explain it :-)

Following situation:

I am somewhere on my transformation stack, eg

glPushMatrix()

glTranslatef(0,0,1)

glPushMatrix()

glRotatef(90,0,1,0)

glPushMatrix()

glTranslatef(0,0,1)

glPushMatrix()

glRotatef(100,1,0,0)

glTranslatef(0,0,1)

//where am I here???

glPopMatrix()

glPopMatrix()

glPopMatrix()

glPopMatrix()

If I want to determine, which world coordinates eg. A vertex (0,0,0) would have, how can I get this information???

Hope you could understand my problem,

thank you for your helpful answers in advance!

With best regards,

Oliver

-NiCo-

03-13-2008, 01:43 PM

You can readback the modelview matrix using

GLfloat mv[16];

glGetFloatv(GL_MODELVIEW_MATRIX,&mv[0]);

This returns the values of the modelview matrix in column major format. So multiplying this matrix with the (0,0,0,1) vector is equal to the last column which is

x = mv[12];

y = mv[13];

z = mv[14];

w = mv[15];

Oliver Demetz

03-13-2008, 01:51 PM

so this would be the point (0,0,0)?

OK. But what about things like gluLookAt?

In my special case, I am working in an Ortho-Setup and I just shifted my eyepoint 5 backwards in z-dir:

[CODE]

glMatrixMode(GL_MODELVIEW)

gl.glLoadIdentity();

gl.glTranslatef(0f,0f,-5f);

[CODE]

Would it be enough to add 5 to mv[14] in my special case then?

But I am also interested in the general case!

Regards,

Oliver

-NiCo-

03-13-2008, 01:57 PM

Basically it boils down to this:

glMatrixMode(GL_MODELVIEW);

//do some matrix ops here (gluLookat,glTranslate,glRotate,glScale,...)

//get modelview matrix

GLfloat mv[16];

glGetFloatv(GL_MODELVIEW_MATRIX,&mv[0]);

//x1,y1,z1,w1 in object space

float x1 = 0.0f;

float y1 = 0.0f;

float z1 = 0.0f;

float w1 = 1.0f;

//x2,y2,z2,w2 in world space

float x2 = x1*mv[0]+y1*mv[4]+z1*mv[ 8]+w1*mv[12];

float y2 = x1*mv[1]+y1*mv[5]+z1*mv[ 9]+w1*mv[13];

float z2 = x1*mv[2]+y1*mv[6]+z1*mv[10]+w1*mv[14];

float w2 = x1*mv[3]+y1*mv[7]+z1*mv[11]+w1*mv[15];

Oliver Demetz

03-13-2008, 02:08 PM

Hmm, but these x2,y2,z2 and w2 that you called to be in "world" space, haven't they been transformed from world space into a "perspective" world space already? I'm quite new to OpenGl, so I'm not sure about this question.

I mean that the position of the viewer is the first (or in reality the last) thing that is done to the MV matrix, isn't it? Take my example from the beginning. I translate my viewpoint in z-dir:

glTranslate(0,0,-5)

ok. So if in this case I put just one vertex (0,0,0) in my scene, then the MV matrix multiplied by (0,0,0,1) would give me (0,0,-5) which is not what I want! I would like to get (0,0,0) back!

Hope you can understand me.

In principle I would like to obtain hte MV matrix without the initial viewpoint and glFrustrum and gluLookat stuff.

Regards,

Oliver

CatDog

03-13-2008, 02:24 PM

You're right, Oliver. As the term "model-view-matrix" indicates, it is used to transform from model space (also called object space) to view space (also known as eye space).

There is no "model-world-matrix" in GL. You have to track it by yourself. This is due to the fact, that a "world" semantically depends on the kind of scene you want to render.

CatDog

Oliver Demetz

03-13-2008, 02:28 PM

Ok,thank you.

Next question: are there any other "free" matrix stacks in OpenGl, besides the Projection and the Modelview matrix? Or how would you track such transformations efficiently? Using your own matrix datastructure?

so long,

Oliver

-NiCo-

03-13-2008, 02:30 PM

Hmm, but these x2,y2,z2 and w2 that you called to be in "world" space, haven't they been transformed from world space into a "perspective" world space already? I'm quite new to OpenGl, so I'm not sure about this question.

No. The perspective transformation is done by the projection matrix, not the modelview matrix.

The modelview matrix transforms a point specified with glVertex4f(x1,y1,z1,w1) (object space) to the camera coordinate system (x2,y2,z2,w2) (eye-space). Finally, the projection matrix transforms the (x2,y2,z2,w2) coordinates to screen space.

I mean that the position of the viewer is the first (or in reality the last) thing that is done to the MV matrix, isn't it?

It all depends how you look at it. Suppose you start with

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

//some matrix ops here

and you want it to translate 5 units to the right of the viewer, it becomes:

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslatef(5.0,0.0,0.0);

//some matrix ops here

Take my example from the beginning. I translate my viewpoint in z-dir:

glTranslate(0,0,-5)

ok. So if in this case I put just one vertex (0,0,0) in my scene, then the MV matrix multiplied by (0,0,0,1) would give me (0,0,-5) which is not what I want! I would like to get (0,0,0) back!

Hope you can understand me.

In principle I would like to obtain hte MV matrix without the initial viewpoint and glFrustrum and gluLookat stuff.

So you would like to know the coordinates of a point in object space so that after transformation by the modelview matrix it return 0,0,0?

In that case you should calculate the inverse of the modelview matrix and multiply it with (0,0,0,1). Then it makes sense that if you multiply the modelview matrix with this result, you get (0,0,0) back.

-NiCo-

03-13-2008, 02:32 PM

@CatDog: You're right. Thanks for clearing that up for Oliver. Since there is no "world-space" in OpenGL I tend to consider world-space to be equal to eye-space. My bad ;)

CatDog

03-13-2008, 02:36 PM

Next question: are there any other "free" matrix stacks in OpenGl, besides the Projection and the Modelview matrix? Or how would you track such transformations efficiently? Using your own matrix datastructure?

The latter. Also remember that there is a driver/hardware dependant limit for matrix stack depth. The MV stack is very useful if you want a quick way to modify the current matrix.

If you're for example trying to render a scene tree, that contains local transforms at its nodes, this is not a good way to go. What if your tree depth is bigger than the maximum matrix stack depth?

So if you don't know how many matrices you're going to push, use your own stack and simply glLoadMatrix before rendering a node.

NiCo: The beauty of the "world" is in the eye of the viewer. :)

CatDog

Oliver Demetz

03-13-2008, 02:38 PM

To both of you again: thank you for your answers!

But what about the "free" matrix stacks? are there any? What ybout display lists?

-NiCo-

03-13-2008, 02:46 PM

The only matrix stacks in OpenGL are GL_TEXTURE, GL_MODELVIEW, GL_COLOR and GL_PROJECTION.

Like CatDog already pointed out, if you have a lot of transforms to track it's best to write your own matrix stack and use glLoadMatrix and glMultMatrix.

I don't think using display lists to track the changes helps you to avoid the matrix stack size limit.

Powered by vBulletin® Version 4.2.3 Copyright © 2018 vBulletin Solutions, Inc. All rights reserved.