Search code examples
copenglopengl-compat

Using glMatrixMode() in succession?


How would one go about using two different matrix modes in succession? I.e, say I want to do some operations using glOrtho() to the projection matrix. So I call glMatrixMode(GL_PROJECTION), and then perform the operations. From my understanding, using glPushMatrix() will apply these changes to the projection matrix. Now I want to apply some changes to the modelview matrix. I am confused on how to do this properly. Do I pop the matrix then call glMatrixMode or simply continue with glMatrixMode?


Solution

  • glPushMatrix and glPopMatrix are to store/restore currently selected matrix. Its used for example for sub meshes (like robotic arm) where you need to return to the state of the root submesh ...

    What you describe is what glMatrixMode is for. So your code should look like this:

    glMatrixMode(GL_PROJECTION);
    // here your stuff for setting projection
    glMatrixMode(GL_MODELVIEW);
    // here your stuff for setting modelview
    

    you should call glMatrixMode before each block of code that is manipulating matrix. Do not expect current matrix is set to stuff you set it to last. That leads to confusion later on ... for example many draw algos change modelview and or texture matrices on the run and in your code you can have something like this:

    glMatrixMode(GL_MODELVIEW);
    // here your stuff for setting modelview
    glMatrixMode(GL_PROJECTION);
    // here your stuff for setting projection
    
    object1.draw();
    
    // and here the current matrix could be changed from the object1.draw()