   OpenGL Matrix Stack
  Edit: This has been solved.  I'm not going to use a stack.[Edited by - X Abstract X on April 18, 2010 6:06:02 PM]
Quote:- This newly created matrix is added to the stack when push() is called- The current matrix is calculated by multiplying together every matrix on the stack (this sounds really costly)When you push on a matrix, automatically multiply it with the current top matrix, and push on the result. Then the top is always the 'current' (all multiplied together) matrix.   ;  No, thats not how it works in openGL.  You can do whatever you like, but thats not what openGL actually does.  Here's how to modify what you wrote to do what GL actually does.-use a std::stack (a std::stack and a std::deque are two different implentations of  a stack)-Any Transform operation creates a new matrix and then RIGHT multiplies it with the value on top of the stack .  The new matrix from the result is stored (in place, not pushed) on the top of the stack.  Code, it would be like this:translate(x,y,z) {NM=[1 0 0 x;0 1 0 y;0 0 1 z;0 0 0 1];//mat_stack.top()=mat_stack.top()*NM;mat_stack.top()*=NM}Then, a push implementation pushes the current matrix on top onto the top again (making a copy)push(){mat_stack.push(mat_stack.top());}The current matrix is then just read off the top of the stack.The reason they do it this way is so that you can do:translate(camera_position)push() // make a copytranslate(object_position)render_my_object()pop()push()translate(object2_position)render_my_object2()pop()   ;  Thanks guys.  This definitely makes a lot more sense than my idea.Quote:Original post by Steve132-use a std::stack (a std::stack and a std::deque are two different implentations of  a stack)Actually, std::stack is a container ADAPTER for an std::vector, std::deque or std::list.[Edited by - X Abstract X on April 18, 2010 1:17:24 AM];  My bad, you are right.  I didn't look into it that hard.   ;  Not directly an answer to the OP (which is already given by the other posters), but ... although I don't know your, i.e. (X Abstract X)'s, motivation of re-inventing OpenGL's matrix stack, I will hint at the fact that the matrix stack in OpenGL is an anachronism, and its deprecation in the newest specs is IMHO a Good Thing.There is no extra functionality in using a stack instead of using a couple of explicit matrices besides the push/pop. Well, the push/pop gives you a kind of local storage, but this local storage is (a) strictly temporary only (due to the FIFO nature of a stack) and (b) decouples the matrix from the belonging object. Its only advantage may IMHO be that using a stack requires somewhat less memory in total, but even that plays a role only under special circumstances.Especially (a) is somewhat cumbersome when looking at the whole picture. Often enough a matrix describing the spatial relation with the global space is needed, e.g. for collision detection, AI vicinity queries, and similar things. Computing the WORLD matrix (to use OpenGL's term) of an object 2 or more times per video frame is obviously a waste of performance.Moreover, copying also OpenGL's semantic of stacks (if you plan to do so) brings also the old problem of pre-multiplied VIEW and MODEL matrices.Composing matrices when using a stack is restricted, too, in that one cannot "insert" multiplications. So situations arise in which one composes matrices partly on the stack and partly elsewhere, IMHO driving the stack ad-absurdum.Hence such a matrix stack works fine as long as the application of matrices is very limited, e.g. to pure rendering. But even than I don't see real advantages. I would definitely consider to drop matrix stacks at all and to implement the functionality in a Matrix class, perhaps a Transform class specialized to translation, rotation, and scale which can then be implemented in a more efficient way.Just my 2 EUR-Cents :)   ;Quote:Original post by haegarr...Thanks, I always appreciate advice that will help to improve my design.  I'm not all that experienced so sometimes it's hard to plan for the future when you don't know exactly what you'll need or what problems can arise with a current design.  Anyway I'm going to implement this the way you described instead of using a stack since it simply makes more sense.;  The only real "stack"-mechanism in OpenGL is calls to glPushMatrix/glPopMatrix.All other matrix modifiers (glRotatef, glTranslatef, glScalef etc.) only modifies the current matrix (GL_MODELVIEW/GL_PROJECTION/GL_TEXTURE/GL_COLOR)   