18. OpenGL Popping and Pushing Matrices

You may notice that when you assign a colour to an object, that colour will also
attach itself to every object you draw after the colour statement. Or the same for
rotations and translations. This is because OpenGL is based on a state machine.

If you want to really understand what these following commands do, then you need
to know about stacks and state machines, but if you only want to use these commands,
then read on. The core ingredient behind OpenGL, and any 3D rendering engine infact, is a transformation matrix.

Now every time you perform an action such as glTranslate or glRotate, then you are
modifying the transformation matrix, and once something has been done, it cannot be
easily undone, so OpenGL makes use of states and says, OK, you are in this state, so
we will apply these transformations and all previous transformations. States still means

that once an operation has been performed, it will affect everything to come, but it means

we can go back to a previous state and forget about our current one.

To do this, we only need to make use of two commands.
They are:
glPushMatrix(); //set where to start the current object transformations
and:

glPopMatrix(); //end the current object transformations

The first call, glPushMatrix(), tells OpenGL to store the current state that we are in.
This then allows us to perform a bunch of different effects.

Then when we want to go back to our previous state, we call glPopMatrix().

This works fine for rotations and translations, but OpenGL allows for many other commands,
so it gives us the ability to store attributes with glPushAttrib() and glPopAttrib(). Some of the attributes that we can store include, GL_LIGHT and GL_COLOUR. Of course there are many
more, but they are out of the scope of this tutorial.

If you have any queries, feel free to email me at swiftless@gmail.com

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
// when you set the glPushMatrix(); it distinguishes where
to start the transformations for an object

// to set where to stop these transformations you would call
glPopMatrix();

// for instance, if you take the push and pop matrix code out of
this file

// cube1 would act normal, but cube2 would be rotating it’s
own way as well

// as including the rotation from cube1

#include <GL/gl.h>
#include <GL/glut.h>

GLfloat angle = 0.0; //angle for cube1
GLfloat tangle = 0.0; //angle for cube2

void cube (void) {
glPushMatrix(); //set where to start the current object transformations
glTranslatef(1, 0, 0); //move cube1 to the right
glRotatef(angle, 1.0, 0.0, 0.0);
glRotatef(angle, 0.0, 1.0, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
glColor3f(1.0, 0.0, 0.0); //change cube1 to red
glutWireCube(2);
glPopMatrix(); //end the current object transformations
}

void cube2 (void) {
glPushMatrix(); //set where to start the current object
transformations

glTranslatef(1, 0, 0); //move cube2 to the left
glRotatef(tangle, 1.0, 0.0, 0.0);
glRotatef(tangle, 0.0, 1.0, 0.0);
glRotatef(tangle, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0); //change cube2 to green
glutWireCube(2);
glPopMatrix(); //end the current object transformations
}

void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
cube();
cube2();
glutSwapBuffers();
angle+= 1.0;
tangle+= 2.0;
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}

int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (“A basic OpenGL Window);
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glutMainLoop ();
return 0;
}

  • March 25, 2010
  • 9