Reputation: 541
I'm using Lazy Foo's tutorials. I am finding the glPushMatrix() and glPopMatrix() functions very confusing. I need Someone to explain how the matrices are stored, how to make one of them active(if that is what you would call it) and also this code from Lazy Foo's site:
void render()
{
//Clear color buffer
glClear( GL_COLOR_BUFFER_BIT );
//Pop default matrix onto current matrix
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
//Save default matrix again
glPushMatrix();
What purpose does 'opening' and then restoring a matrix serve?
Upvotes: 0
Views: 93
Reputation:
Matrix are stored in a stack form. glPushMatrix pushes (copies top and push a copy) to this stack. glPopMatrix pops the matrix at the top. On your example, they should be switched, you first push then pop a matrix.
You use GL_MODELVIEW matrix to alter what you want to draw. If a model is centered at 0,0,0 you may use matrix translate operation to move it to x,y,z while drawing.
Let's say you want to draw two objects. One at x, y, z other is at a, b, c
translate x, y, z
draw first object
translate a, b, c
draw second object
if you do this, you will see that second object is drawn at x+a, y+b, z+c because second translate is added to first. you need use push/pop matrix to save and restore state
push matrix //no translation at this point
translate x, y, z
draw first object
pop matrix
translate a, b, c
draw second object
when you push matrix, it saves the current state of matrix, which is assumed to be at origin. after that you translate this matrix by x, y, z and when you pop again this translation is 'reverted' and you popped back to state when you last pushed a matrix. when you translate to a, b, c this time it works because we popped last translate operation
Upvotes: 7