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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With