Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


What's going on?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
17 replies to this topic

#1 YodaTheCoder   Members   -  Reputation: 122

Like
Likes
Like

Posted 10 October 2001 - 05:20 PM

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

Sponsor:

#2 Null and Void   Moderators   -  Reputation: 1087

Like
Likes
Like

Posted 10 October 2001 - 05:37 PM

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

#3 YodaTheCoder   Members   -  Reputation: 122

Like
Likes
Like

Posted 10 October 2001 - 05:51 PM

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

#4 Gillies   Members   -  Reputation: 122

Like
Likes
Like

Posted 10 October 2001 - 11:52 PM

For an example - look at my answer to your other thread ;o)

#5 MENTAL   Members   -  Reputation: 382

Like
Likes
Like

Posted 11 October 2001 - 12:25 AM

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

#6 YodaTheCoder   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 October 2001 - 09:11 AM

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 =)

#7 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

Likes

Posted 11 October 2001 - 09:55 AM

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.


#8 YodaTheCoder   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 October 2001 - 10:59 AM

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

#9 MENTAL   Members   -  Reputation: 382

Like
Likes
Like

Posted 11 October 2001 - 11:21 AM

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

#10 MENTAL   Members   -  Reputation: 382

Like
Likes
Like

Posted 11 October 2001 - 11:24 AM

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).

#11 MENTAL   Members   -  Reputation: 382

Like
Likes
Like

Posted 11 October 2001 - 11:29 AM

one last thing, you dont have to push and pop when you clear matrix with glLoadIdentity. as the code above shows, its only after you''ve made some modifications to the matrix and you want to reuse them that you begin to push and pop.

curse this damn thead.

MENTAL

#12 YodaTheCoder   Members   -  Reputation: 122

Like
Likes
Like

Posted 11 October 2001 - 04:32 PM

I must thank you sir, all of yee, for your help. MENTAL, I thank you also, being as though you seem to go out of your way to help idiots like me =) THANK YOU EVERYONE!! And as always, I PROMISE TO SEND YOU 100 DOLLARS* FOR HELPING ME! Email me your name and address, and the 100 DOLLARS WILL BE SENT!!

~Jesse

*: $200 shipping and handling

#13 MENTAL   Members   -  Reputation: 382

Like
Likes
Like

Posted 12 October 2001 - 01:29 AM

i remember when i was a newbie, but i was an old school newbie so that made me cool. now i spread my 1337ness, enlightening all whom i see.

truthfully? i have the flu and have got nothing better to do

hey, that rhymes. i''m a poet and i dont know!

it!

MENTAL

#14 DoomX   Members   -  Reputation: 146

Like
Likes
Like

Posted 12 October 2001 - 08:50 AM

I like to think of pushes and pops as a way to combine transformations together into a single transformation matrix.

So... you use pushes and pops to do more than one thing to an object at the same time. Let''s say you want to translate, rotate, then translate again. You would use three sets of push/pops nested within each other.

push
translate
push
rotate
push
draw()
pop
pop
translate
pop

Or something like that.
Just keep in mind that extras pushes and pops won''t really hurt you (you''ll have some performance issues, but when you''re learning that''s ok).
Pure layman''s terms... Well almost.

#15 YodaTheCoder   Members   -  Reputation: 122

Like
Likes
Like

Posted 12 October 2001 - 08:15 PM

Wel thanks again! After I''m done with Quake 4, I''ll give you a call! =)

~Jesse

"Sleep is a waste of time"...

#16 Badone   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 October 2001 - 08:22 AM

I had a couple questions myself on this subject. Is there anyway to save more than one matrix? Would you have to do a glPushMatrix(); twice and then glPopMatrix(); twice to get back to it?

Can you use glPushMatrix(); and glPopMatrix(); kind of as levels in a game? Or has anyone ever done this? Load everything into the levels at the beginning of the game from the highest level to the lowest, and then when they beat the first level do a glPopMatrix();?

Is this ever used, or could it be used if you have like two different parts of a level? Have you ever seen games where you want to go into a castle, or a house or something, and it needs time to load? Could you save different rooms in the Matrixes?

Thanks for the help!


#17 YodaTheCoder   Members   -  Reputation: 122

Like
Likes
Like

Posted 13 October 2001 - 08:36 AM

Hmm, That''s a good question. Coming from a mathematical world, it sure would seem like you could, but I wouldn''t risk it. At least not until MENTAL has his coffee and responds to this =). My suggestion (only an idea) would be to have something like:

initLevel1();
initLevel2();
initLevel3();

And have each of those create their own world... That''s just an idea; I''m still thinkin about it...

I''ve learned that when you want to draw, you push the matrix, draw, then pop. I don''t know about levels, cause I only know about single object manipulation so far. DoomX made a good point though...

I would still think that you should have different functions. Like each of them would create your map, your terrain, your weapons, your PC''s, your items, blahblahblah... =)

~Jesse

"Sleep is a waste of time."

#18 MENTAL   Members   -  Reputation: 382

Like
Likes
Like

Posted 14 October 2001 - 04:42 AM

each matrix only stores a single location in the world. when you push, it saves that 1 location. also, once you pop all the matrices off the stack, they are gone, kaput, and have to be pushed again.

use display lists for objects in the levels, as that way you can draw each piece with a single command (glDrawList (listID) i think).

no, i''m not going into the ins and outs of display lists .


oh, and yes, you can save as many matrices as memory allows, but you can only access them in the reverse order they were pushed. example:

LoadIdentity
Current Matrix: 1

Push
Current Matrix: 2

Push
Current Matrix: 3

Push
Current Matrix: 4

Push
Current Matrix: 5

to get matrix 1 back, you would have to do this:

Current Matrix: 5

Pop
Current Matrix: 4

Pop
Current Matrix: 3

Pop
Current Matrix: 2

Pop
Current Matrix: 1

but then you loose matrices 2-5. look at my examples above (in funky C-o-vision) to see how you are meant to use push/pop and why.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS