nwn
nwn

Reputation: 584

Current OpenGL matrix mode

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?

Upvotes: 2

Views: 2129

Answers (3)

bitbang
bitbang

Reputation: 2182

so i am getting 5888, 5889 values

glMatrixMode(GL.GL_PROJECTION)
glLoadIdentity()  # Reset all graphic/shape's position
print("GL_MATRIX_MODE:", glGetIntegerv(GL_MATRIX_MODE))
# GL_MATRIX_MODE: 5889


glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
print("GL_MATRIX_MODE:", glGetIntegerv(GL_MATRIX_MODE))
# GL_MATRIX_MODE: 5888

Upvotes: 1

Reto Koradi
Reto Koradi

Reputation: 54602

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.

Upvotes: 6

lisyarus
lisyarus

Reputation: 15532

glGetIntegerv with argument GL_MATRIX_MODE should do it.

Upvotes: 3

Related Questions