Help needed with ModelView Matrix, Pushing and Popping

Hiya,

I don’t completly understand the modelview matrix, but I’m having some problems :slight_smile:

I have the following code sequence:

glLoadIdentity()
glPushMatrix()

glTranslatef()
drawCube()

glPopMatrix()

Inside drawCube is where I’m getting lost, I currently have:

glPushMatrix() //Save any transformations

glLoadIdentity() //Reset Matrix
glPushMatrix()

glTranslate()
DrawSide()

glPopMatrix()

glPopMatrix() //Restore transformations

I’m doing soemthing wrong as this is not working, any ideas?

Thanks

Paul

Hello there.
Can you tell us what are you trying to do here?
In the first part, you are loading the current matrix to identity and next you are pushing it to stack? So the matrix stack has identity matrix on it so if you are trying to save the current matrix to stack remove the loadidentity call before the pushmatrix call. see if this sorts your problem.Thanx
MMMovania

Hey,

Thanks for the reply, what I’m trying to do is:

  1. Initialise Model/View Matrix before any drawing (glLoadIdentity)

  2. Save the Matrix

  3. Draw a ground Rectangle involving translations

  4. Restore the Matrix (Wipe translations)

  5. Draw a building composed of many rectangles.

I want to be able to transform the location of the building as a whole. But when I build the building I want to build each part from the origin and translate into position.

So essentially I’m building objects at the orgin and moving them into place.

Paul

DragonSpawn, I have been experimenting with OpenGL for only a few days, so keep that in mind when you read my advice.

It seems a good way to call code is:

 glLoadIdentity();

/* the next four parts can be repeated as necessary */

glPushMatrix();

<Transformations>

<Define Geometry>

glPopMatrix();
 

The code from glPushMatrix() and glPopMatrix, inclusive, can be repeated to add independent models where you want.

By the way, I tried to only define one face of a box at a time (where you see
<Define Geometry>
), calling the above code 6 times. The results onscreen were weird!

Later, instead of defining a face and translating it to the part of the box where it should be, I just built the whole box and translated it. That turned out okay.

Yeah I think dezzroy is starting to understand it.

Now for a more complete answer:
(bearing in mind that you haven’t stated what happens and what you were expecting)

You’re doing this:

glLoadIdentity()
glPushMatrix()

glTranslatef()
(call drawCube())

glPushMatrix() //Save any transformations

glLoadIdentity() //Reset Matrix
glPushMatrix()

glTranslate()
DrawSide()

glPopMatrix()

glPopMatrix() //Restore transformations

(return)
glPopMatrix()

What I think you were expecting, but aren’t getting, is a cube at a different location than (0,0,0). That is because the glLoadIdentity in your drawcube function, resets the transformation matrix.
If you remove that glLoadIdentity, that issue should be solved. As for the sides of the cube: you have not posted the drawside function, but my guess is that you always send the same vertices in that function, and in that case you’ll need to do something like:

(drawcube function)
glPushMatrix()
// no glrotate here since this is the "front" face
glTranslate()
DrawSide()
glPopMatrix()
glPushMatrix()
glRotate(90,0,1,0) // one of the sides
glTranslate()
DrawSide()
glPopMatrix()
glPushMatrix()
glRotate(180,0,1,0) // another side
glTranslate()
DrawSide()
glPopMatrix()
// etc.

All things considered, it’s probably easier to just use a bunch of glVertex calls - there are only 8 vertices in a cube, and it’s easier to specify them directly than it is to use glTranslates and glRotates.

So something like:

(drawcube function)
glBegin(GL_QUADS);
 // first side
 glVertex3f( 1, 1, 1);
 glVertex3f( 1,-1, 1);
 glVertex3f(-1,-1, -1);
 glVertex3f(-1, 1, 1);
 // second side
 glVertex3f( ... // etc.
glEnd();
// note: no push/pop necessary. Caller decides
// on scale, location and rotation.