Evaluate matrix stack

Hi,

My question is that basic that I don’t even know how to explain it :slight_smile:

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

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

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:


glMatrixMode(GL_MODELVIEW)
gl.glLoadIdentity();
gl.glTranslatef(0f,0f,-5f);

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

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


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

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

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

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.

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

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.

@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 :wink:

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. :slight_smile:

CatDog

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

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

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.