What is The Stack

As I read about openGL there are references to the functions glPushMatrix() & glPopMatrix(). I find no explanation of what “the stack” is. A search of this forum for “what is the stack” produced no results. I understand the implementation of a stack in code and the operators push and pop. These two functions appear to operate on “the one and only stack.” I presume to use that phrase as I have yet to recognize any reference to a particular stack.

I have not recognized anything that explains: Why does that stack exist? What is pushed and popped on it? Yes, I can conclude matrices are pushed and popped, but what do those matrices represent?

I presume the matrices represent whatever is being modeled within openGL, but an explanation would inspire much more confidence than my presumptions. Why are they pushed and popped on a stack rather than, say, members of an array of matrices? Are the matrices handled differently according to their position in the stack? When I push a matrix on a stack then start drawing vertices, are those vertices written to the matrix just pushed.

The context I am more accustomed to is: Push something on the stack to save it while I work on something else. When that work is done, pop that item off the stack thereby making it available for use. Is the stack in openGL different from my presumption?

Edit: Maybe this will clarify / simplify my question. An example from another thread, in essence with my comments added:

glPushMatrix()           //push it from some standard location, really copy it to the top of the stack
glTranslatef( args )    // make some changes
DrawObject ( ONE )  // put the changes on the screen
glPopMatrix()            // pop the unchanged matrix off the stack to the standard location

Now that changes have been displayed on the screen, the pop operation replaced all the changes with the matrix that was pushed. All the work of the second line of code is gone.

Each matrix (as per glMatrixMode()) has an associated stack which can be used to save the current value then subsequently restore it.

Linear (or, more precisely, affine) transformations. Translation (offset), scale, and rotation (and potentially perspective projection, but that’s not relevant to 2D).

For many applications (including this one), you don’t need the stack. You can just set the matrix using e.g. glLoadIdentity() and glOrtho().


The matrix stacks are used if you have a hierarchy of transformations. See the section “Building a Solar System” in the red book for an example.

Hello GClements,

Each matrix (as per glMatrixMode()) has an associated stack which can be used to save the current value then subsequently restore it.

When I read “Each matrix” my expectation is to see something like: glPushMatrix( alpha ) and glPushMatrix( bravo ). If not, I presume only one matrix that can be pushed and popped.

Linear (or, more precisely, affine) transformations.

Puzzling. I am now thinking each matrix is instructions on how the model is to be modified. If so, where is the model? Has my experience been limited to code that just draws on the screen with the model being completely in the programmers head. If so, then will more complete apps will have a mathematical model somewhere and the drawing instructions fetched from the model?

I am looking at the solar system and presume the first 7 lines of code are for the sun. The second one is:

glutWireSphere( radius = 1, slices = 20, stacks = 16 )

I surmise that this perspective is to put the sun in space somewhere and our perspective is looking down from the north or south pole. The sun rotates about the X axis, slices are longitude, stacks latitude. The value 1, 20, 16 seem quite arbitrary compared to common perspective. Latitude and longitude are measured in degrees or radians. I don’t recognize either in 20 and 16. But, the perspective I just described is not congruent with Figure 3-24 from the book. The text does not mention a change in perspective.

glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);

What is the argument year? months, days, etc?

Here is what I found about glRotate(), and associating the arg types with the values from the example:

glRotate(  angle = year,
               x_coordinate_of_something_to_rotate = 0.0,
               y_coordinate_of_something_to_rotate = 1.0,
               z_coordinate_of_something_to_rotate = 0.0 )

I read that code as stating: There is something at x/y/z coordinate of 0, 1, 0 that will be rotated the amount: one year.
I see the wiresphere with some attributes defined, but I don’t see anything that specifies its location. What is at location 0,1,0?
The earth is not yet defined so is this to model the sun? It rotates about 15 times per year at the equator and 10 at the poles. What is a year from the perspective of the sun?
If this models the earth and we rotate one year, then it stops exactly where it began. I don’t understand what that rotation accomplished.

The way the matrix stack(s) works in OpenGL is confusing primarily because the matrix that sits at the top of the stack lives in OpenGL memory, not your memory.

First, OpenGL has multiple matrix stacks; there is the notion of a current matrix stack, set by the glMatrixMode function. Each individual stack has a different meaning and operation to OpenGL, which will be explained later. The most important part is this: all matrix operations act on whatever matrix stack is made current.

Second, there are a variety of functions that operate on the matrix stack. Or more specifically, they operate on the matrix at the top of the current stack. Calls like glRotate, glTranslate, and glMultMatrix perform the operation: “generate a matrix as described by the function and right-multiply it with the matrix at the top of the current matrix stack, with the resulting matrix becoming the new top”. In a normal stack, these would be pop/modify/push operations, but they’re backed into one operation to make things easier to use.

There are some matrix operations that work differently. The glLoadMatrix and glLoadIdentity matrix functions explicit replace whatever matrix is at the top of the current matrix stack. They do a pop/push of the given matrix.

glPushMatrix takes the current top matrix and pushes it onto the stack, such that when you do the corresponding glPopMatrix for this stack level, the matrix at the top will be the matrix you pushed. Basically, after the glPushMatrix call, the value of the top matrix and the value of the matrix just beneath the top is the same.

How matrices get used is a bit more complicated. Remember that I said that there were multiple matrix stacks? Well, each matrix stack is directly tied into some aspect of the rendering pipeline, as defined by the fixed-function pipeline. When you render something (regardless of how you invoke that rendering), the values at the top of these matrix stacks will be used to process that mesh data, in accord with the fixed-function pipeline.

Note that the values of the stacks used in rendering are the values at the time the rendering command is issued to OpenGL. Until you actually draw something (with one or two exceptions), the matrices on the stacks don’t do anything. And after you draw something, what you just drew will not be affected by later changes to the matrices on the stacks.

Positions are transformed by the GL_MODELVIEW matrix, which is then transformed by the GL_PROJECTION matrix. The result of those transformations are further transformed by other processes and result in deciding where that vertex is on the screen.

Vertex-based lighting computations involve using the position after transformation by GL_MODELVIEW, but before the transformation by GL_PROJECTION.

What is the argument year? months, days, etc?

What do you think it is? You’re basically asking us to take code we have not seen and know nothing about, and then tell you what it means. You probably know more than we do because you can see all of the code and we can’t.

I looked up glMatrixMode() and discovered there are two modes therefore two stacks. So the stack selection is made via this call and the selected mode sets the current stack and type of operations. In this case the two types of operations are MODELVIEW and PROJECTION.
Recognizing that I may be trying to think too deeply, there appear to be four groups or classifications of matrices.

1 An operation type matrix created by functions such as glRotate(). Each performs a certain operation on a working matrix. They are established using the arguments of the functions to control the specific of how they operate.

2 A working matrix upon which functions such as glRotate() operate to produce some desired result.

Question: Do the operators such as glVertex() write here?

3 The top of the stack. A matrix that is in the gl memory. I presume that means the data that is subject to the GPU processor operations.

4 Mactices “down” within the stack that have been pushed. They are in a “resting” state, no interactions for now. They can be recalled with a pop. When they get to the top the GPU can work on them. One more pop and the CPU can work on them.

Positions are transformed by the GL_MODELVIEW matrix, which is then transformed by the GL_PROJECTION matrix.

Question: Knowing that I am sticking with 2D work, should I stick with glMatrixModel( GL_MODELVIEW ) for most or all of my drawing and let open gl do its work though GL_PROJECTION?

You’re basically asking us to take code we have not seen and know nothing about, and then tell you what it means.

Valid point. In my defense GClements referenced that book and chapter with a link in his post. I do accept a hit in that since I had questions I should have posted at least a reasonable fragment of code to set the stage thus reducing the need for all readers to go look it up. I made preparations to post that code, thought about it, and decided: I will not be doing any of those transformations / operations so do not want to trouble you with analyzing it. I withdraw all the questions about that code and apologize for that trouble.

I am gradually learning more about this beast called open GL. (Read the word “beast” in a tone that respects its power and the knowledge needed to control it.) I can plot a polygon within my QGLWidget and now will attempt to plot points and make them scroll. I will keep monitoring here and ask additional questions when I get stuck.

Thank you for your time and thank you for your patience.