Is it possible to determine the current matrix mode used by OpenGL?
For example, I currently have the following (triggered by a window resize):
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-width, width, -height, height, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
However, it's preferable to return to the previously used matrix mode, rather than assume GL_MODElVIEW
. Is there a function that could be called beforehand to store the previous state?
Getting the current value with glGetIntegerv(GL_MATRIX_MODE, ...)
is the obvious answer.
However, there is a more elegant and most likely more efficient way. Legacy OpenGL has an attribute stack that allows you to save/restore attribute values without using any glGet*()
calls. In this example, you would use:
glPushAttrib(GL_TRANSFORM_BIT);
// Code that modifies transform mode.
glPopAttrib();
You can look up which bit passed to glPushAttrib()
saves what state in the table on the man page.
You should generally avoid glGet*()
calls where you can, since they can be harmful to performance. In the specific example where you execute the code only on window resize, this obviously isn't a concern. But in code that gets executed frequently, this becomes much more critical.
The attribute stack is deprecated, and not available in the OpenGL core profile. But since you're using the matrix stack (which is deprecated as well), I figure you're comfortable using legacy features in your code.