What's going on?

Started by
16 comments, last by YodaTheCoder 22 years, 6 months ago
Okay, I am relatively new to oGL, but could someone please explain to me whats going on when I say to popMatrix, pushMatrix, glLoadIdentity, or something? The definitions in the glut docs is just too techical.. Please ; Thanks! (Really, just how does this whole buffer thing work in relation to keyboard input)... ~Jesse
Advertisement
The identity matrix is a matrix that when multiplied by another will result in the other. So, in simpler terms, think of it as a ''1''. Multiplying it by 5 by it will result in 5. Calling glLoadIdentity matrix replaces the current matrix with the ''1'' matrix.

PushMatrix backs up the current matrix. When you use PopMatrix, it makes the current matrix the backup. You must call PopMatrix for each call to PushMatrix, otherwise the backups just waste space (and video cards can only hold a certain amount).

That''s a really simple description, hope it helps .

[Resist Windows XP''s Invasive Production Activation Technology!]
Ahh, as a matter of fact, we just went over identity matrices and other matrix-related stuff today in Algebra 2 (thats a coincidence!) =)

Well, what are these functions good for? And how can they help me in my game programming universe?

If you want to know what I''m gonna be making: its a simple game like Raptor. There''s a ship at the bottom that you control (move left, move right, move up, move down, shoot) and stuff...

~Jesse
For an example - look at my answer to your other thread ;o)
The Identity matrix is essentially your starting point for building up the final transformation matrix. It does absolutely nothing to anything until you start multiplying it with other matricies.

MENTAL
Ahh, thank you for your help so far, but my feeble little C++ mind requires some more oGL assistance. =)

When would I want to use these? I know I should be reading a graphics tutorial, but I cannot seem to find one WITHOUT win32 cra-, err, stuff... SO ya, I''m asking you smarter people.=)

By what I''ve seen so far, (this is only a pathetic guess), I would want to do this:

clear my buffer(s) and set the drawing color
glPushMatrix();
Draw my stuff
glPopMatrix();
glFlush();
glutSwapBuffers();

Yes, i am usin GLUT, for now anyways... Is that a right order? Because with my little triangle thing I cant seem to do anything... Im to the point where I am Guessing where to declare stuff... HELP!!

Here''s my renderScene code (as used as the DisplayFunc and IdleFunc):

void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//clearin my stuff

glColor3f(1.0,1.0,0.0);
//yellow is a fun color =)

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//still confused as to what this does, but I do know
//that glLoadIdentity loads the identity matrix of
//whatever you pushed, or popped, or... crap, I forgot...

glPushMatrix();
//saves(???) ...uhm, saves something..right?

glTranslated(fXPos,0.0,0.0);
//And what on Earth (and parts of Mars) does this do?
//I see that it moves my little triangle along the
//x-axis, but I still don''t have keyboard control.. =(
//And this only works when I take out alot of stuff
//and increase the value of fXPos at the end of this func.

glBegin(GL_TRIANGLES);
glVertex3f(0.0f, 0.0f,0.0f);
glVertex3f(0.5f, 1.0f,0.0f);
glVertex3f(1.0f, 0.0f,0.0f);
glEnd();

glPopMatrix();
//in stupid terms, this does...?

glFlush();
glutSwapBuffers();
}

Bah... I''m no idiot, but this is all confusing to me... Well, what the functions do isn''t really that confusing, it''s just placement that I think I have all wrong... Where do I put what to do what I want to do?

~Jesse

PS

Thank you to anyone who is responding to this.. Even if its just to laugh at me =)
Background.....

Push and Pop are commands that are used in what''s commonly called a Stack in programming terms. A stack is an Abstract Data Type. Which means it''s used to store d
At the heart both a Stack and a Queue are Arrays.
  Stack [0]  Array [0]      [1]        [1]      [2]        [2]              [3]        [3]             [4]        [4]         


As you can see they all look the same. So what makes them different?

Well in an array you can access any element in the array by doing arrayname[ element number ]. This allows you to move through the array and grab the data you need without loosing any of it.

Now the stack is sort of 1 way streets when compared to an array.

First we need to define what PUSH and POP are. You can think of these as if you were making a necklace. When you PUSH something onto a stack or Queue your adding it into the array. When You POP something off of a Stack your removing it. (NOTE you are literaly removing this data from the array. It does not save it).

The Stack is called a LIFO (Last In First Out) structure. You can think about this structure as if you had a bunch of Rubix Cubes that you were placing ontop of a spring. When you PUSH a Rubix Cube onto the spring it compresses the spring. Hold your hand on the top of the cube and keep the spring compressed, then continue to add more cubes one on top of the other. When your ready to take off a rubix cube the only way to take them off is to take the last cube you put onto the stack and remove it. You can continue this until your left with nothing but the spring. Each cube in this example is your data. and the Spring is how the "Stack" ADT works.

How this relates to your question......

Well in OpenGL they have a MATRIX STACK. Which you access when you use GLPushMatrix() or GLPopMatrix(). The first matrix you PUSH ends up at the bottom of the array as you keep pushing more matricies on top of it. Once you call POP your retrieving the last matrix you PUSHED onto the stack.

Here is an example of how this works visualy
[/source]

//Before you call PUSH or POP anything onto the matrix stack.
MatrixStack [0]
[0]
[0]
[0]
[0]
[0]

GLPUSHMATRIX(matrix1);

//After you call PUSH
MatrixStack [matrix1]
[0]
[0]
[0]
[0]
[0]

GLPUSHMATRIX(matrix2);

//After you call PUSH
MatrixStack [matrix2]
[matrix1]
[0]
[0]
[0]
[0]

GLPUSHMATRIX(matrix3);

//After you call PUSH
MatrixStack [matrix3]
[matrix2]
[matrix1]
[0]
[0]
[0]


GLPOPMATRIX();

//After you call POP
MatrixStack [matrix2]
[matrix1]
[0]
[0]
[0]
[0]

GLPOPMATRIX();

//After you call POP
MatrixStack [matrix1]
[0]
[0]
[0]
[0]
[0]


GLPOPMATRIX();

//After you call POP
MatrixStack [0]
[0]
[0]
[0]
[0]
[0]

[/source]

Hopefuly this will clear things up as far as how the order of things should go.
Okay, I''m starting to get it now... I push to add stuff to the stack, and I pop to get the last thing I added (and it also erases it)... right?

Well, now howbout that glMatrixMode(GL_MODELVIEW); ... What does that mean?

~Jesse

PS If its easier, you can email me at icer@dog.com
oh boy.

Lets just imagine that we have 3 objects, one located at Pos1, one and Pos2, and one more at Pos3. The Camera is located at Cam1.

first of all, we set the position and andle of the viewport (camera). because this is the first thing we do, we need to clear all values currently in the matrix.

glLoadIdentity ();

this resets the matrix to a state of utter uselessness. right, now that we have cleared the matrix (just pretend it secretly calls something like glClear (GL_MATRIX_BUFFER_BIT). i would just like to point out that it doesn't, so dont try and call glClear (GL_MATRIX_BUFFER_BIT) or else strange compiler messages will appear (for example, "WTF IS GL_MATRIX_BUFFER_BIT?!?!). You get the idea).

Now, we set the camera's position. Positions are set using glTranslatef (matrix terminology. translate means move), and angle are set by glRotatef. To save confusion, we will only be moving stuff around.

glTranslatef (Cam1.X, Cam1.Y, Cam1.Z);


right. at the moment we are at the position of the camera. now, we have another 3 objects to move about the scene. If we keep calling glTranslatef (Pos1.X, ...) then the objects will end up all over the place. Instead, we need to save the current camera matrix.

glPushMatrix ();

right, it has saved the current matrix. Now we call glTranslatef for Pos1.

glTranslatef (Pos1.X, Pos1.Y, Pos1.Z);

lets draw a cube.

glutSolidCube (2);

oh no! how do we get back our original matrix?

glPopMatrix ();

oh look at that, it's back. we now have the original matrix. we now have to do the same for the second cube.

glPushMatrix ();
glTranslatef (Pos2.X, Pos2.Y, Pos2.Z);
glutSolidCube (2);
glPopMatrix ();

glPushMatrix ();
glTranslatef (Pos3.X, Pos3.Y, Pos3.Z);
glutSolidCube (2);
glPopMatrix ();

and finally we end up with our original matrix. we have finished drawing, and so can do the whole thing again.

    void Draw (){    glClear (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);    glLoadIdentity ();    glTranslatef (Cam1.X, Cam1.Y, Cam1.Z);    glPushMatrix ();        glTranslatef (Pos1.X, Pos1.Y, Pos1.Z);        glutSolidCube (2);    glPopMatrix ();    glPushMatrix ();        glTranslatef (Pos2.X, Pos2.Y, Pos2.Z);        glutSolidCube (2);    glPopMatrix ();    glPushMatrix ();        glTranslatef (Pos3.X, Pos3.Y, Pos3.Z);        glutSolidCube (2);    glPopMatrix ();}  if all else fails, then just copy this code into some header and use it instead:      inline void glSetPositionf (float X, float Y, float Z){    glTranslatef (X, Y, Z);}inline void glSetPositiond (double X, double Y, double Z){    glTranslated (X, Y, Z);}inline void glSetAnglef (float X, float Y, float Z){    glRotatef (X, 1.0f, 0.0f, 0.0f);    glRotatef (Y, 0.0f, 1.0f, 0.0f);    glRotatef (Z, 0.0f, 0.0f, 1.0f);}inline void glSetAngled (double X, double Y, double Z){    glRotated (X, 1.0, 0.0, 0.0);    glRotated (Y, 0.0, 1.0, 0.0);    glRotated (Z, 0.0, 0.0, 0.0);}inline void glResetMatrix (){    glLoadIdentity ();}inline void glSaveMatrix (){    glPushMatrix ();}inline void glLoadMatrix (){    glPopMatrix ();}    


i think you can work out what to do with it (i hope) .


MENTAL

Edited by - MENTAL on October 11, 2001 6:26:35 PM
glMatrixMode(GL_MODELVIEW);

this says that any function we call that touches the matrix stack will move about the models (that INCLUDES moving the camera about).

This topic is closed to new replies.

Advertisement