tags:

views:

104

answers:

4

Why would I use OpenGL's matrix stack (as opposed to wrapping my own matrix class and just using that)? I'm not really sure why it is so complicated for such a simple task. Is there's some special use case for it?

If it isn't necessary, what's a good replacement for it?

+2  A: 

You'd mostly use the built-in matrix stacks because OpenGL will apply them automatically. They've been removed in recent versions of OpenGL, so at least a few people apparently believe they're unnecessary. Personally, I think that was a poor decision, but such is life. The alternative is to implement them yourself -- the only consolation being that for most uses of 3D graphics, you're generally going to have to do some matrix multiplication (and such) yourself anyway, so it frequently doesn't make a lot of difference.

Jerry Coffin
Just a side question: How do I apply(?) my matricies to the scene in each frame without the matrix stack?
Clark Gaebel
@Clark: in that case, you'd typically set your current transformation matrices as `uniform` s, and "apply" them in a vertex shader, something like: `uniform mat4 projection; uniform mat4 model_view; in vec4 vertex; gl_Position = projection*model_view*vertex;` You might also want to look at GLM: http://glm.g-truc.net/.
Jerry Coffin
+1  A: 

It isn't necessary - you can do all your own matrix work yourself and call glLoadMatrix all the time - but also, it isn't necessary to replace it. You just make life difficult if you're using VBOs and display lists.

Will
+4  A: 

OpenGL's matrix stacks (there are multiples--one for each matrix mode) are intended for use with hierarchical models. That is, you can define the rigging of a child object (say, a wheel) with respect to its parent body (say, a car), without regard to the location/orientation of the parent when the child is drawn. That is, it allows you to compose transforms easily and intuitively.

The matix stacks (and GL matrix operations in general) are no substitute for a general purpose matrix math library. They simply exist to allow composed transforms to be efficiently applied to vertex data. As others have said, this functionality has been removed from OpenGL with the demise of the fixed-function pipeline, because the needs of shader programs are less uniform than the needs of the old pipeline.

Drew Hall
+1  A: 

The matrix stack is used to concatinate matrices. Typically used for heirarchical models where a transformation is expressed relative to another. One can load a matrix on the stack and transform vertices in that space and then go back to the parent space by popping the stack. You can manage all this yourself in code, and frequently you would prefer to - in particular the case where the matrices are used for other thing besides simply drawing or when you access them often. An animation system would be a classic example.

Montdidier