Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Difference between glMatrixMode(GL_PROJECTION); and glMatrixMode(GL_MODELVIEW);

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2010
    Posts
    20

    Question Difference between glMatrixMode(GL_PROJECTION); and glMatrixMode(GL_MODELVIEW);

    What's the difference between glMatrixMode(GL_PROJECTION); and glMatrixMode(GL_MODELVIEW);

    Code :
    #include <stdio.h>
    #include <GL/gl.h>
    #include <GL/glut.h>
     
    #define KEY_ESCAPE 27
     
    void display();
    void keyboard(unsigned char,int,int);
     
    int main(int argc, char **argv) {
        glutInit(&argc, argv);                                    
        glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH );
        glutInitWindowSize(600,400);                  
        glutCreateWindow("Opengl Test");                              
        glutDisplayFunc(display);
        glutKeyboardFunc(keyboard);
        glutMainLoop();
        return 0;
    }
    void display() {
        float x,y,z;
        int i;
        x=0;
        y=-0.8;
        z=0;
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1,1,0);
        glBegin(GL_POINTS);
        for(i=0;i<98;i++) {
            glVertex3f(x,y,z);
            x=x+0.01;
        }
        glEnd();
        glutSwapBuffers();    
    }
    void keyboard(unsigned char key, int mousePositionX, int mousePositionY) { 
        switch ( key ) {
            case KEY_ESCAPE:
                exit ( 0 );   
                break;      
            default:      
                break;
        }
    }

    Code :
    #include <stdio.h>
    #include <GL/gl.h>
    #include <GL/glut.h>
     
    #define KEY_ESCAPE 27
     
    void display();
    void keyboard(unsigned char,int,int);
     
    int main(int argc, char **argv) {
        glutInit(&argc, argv);                                    
        glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH );
        glutInitWindowSize(600,400);                  
        glutCreateWindow("Opengl Test");                              
        glutDisplayFunc(display);
        glutKeyboardFunc(keyboard);
        glutMainLoop();
        return 0;
    }
    void display() {
        float x,y,z;
        int i;
        x=0;
        y=-0.8;
        z=0;
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glClear(GL_COLOR_BUFFER_BIT);
        glColor3f(1,1,0);
        glBegin(GL_POINTS);
        for(i=0;i<98;i++) {
            glVertex3f(x,y,z);
            x=x+0.01;
        }
        glEnd();
        glutSwapBuffers();    
    }
    void keyboard(unsigned char key, int mousePositionX, int mousePositionY) { 
        switch ( key ) {
            case KEY_ESCAPE:
                exit ( 0 );   
                break;      
            default:      
                break;
        }
    }

    For both codes, i get same result.
    Can anyone show the difference of these glMatrixMode(GL_MODELVIEW); and glMatrixMode(GL_PROJECTION); changing this code?

  2. #2
    Intern Contributor
    Join Date
    May 2011
    Posts
    65
    As far as I know, the only difference between the two matrix modes are the order in which they're multiplied (which does make a difference). Other than that, there should be no difference between the two.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2010
    Posts
    20
    Quote Originally Posted by WIld Sage View Post
    As far as I know, the only difference between the two matrix modes are the order in which they're multiplied (which does make a difference). Other than that, there should be no difference between the two.
    How can i multiply these two matrices using opengl and get the resulted matrix?

  4. #4
    Intern Contributor
    Join Date
    May 2011
    Posts
    65
    Not sure, sorry. I try to use core profile code as much as possible so I use GLM to handle my matrices, as well as a lot of really useful 3d math functions.

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Apr 2010
    Location
    Germany
    Posts
    1,128
    The fixed-function pipeline maintains multiple matrix stacks. Among them are the ModelView and the Projection stack. The difference between glMatrixMode(GL_MODELVIEW) and glMatrixMode(GL_PROJECTION) is simply that you're operating on either the ModelView or the Projection stack, depending on which of the above function calls was made. In general, it is NOT advisable to manipulate the projection matrix stack if you don't intend to really change the projection matrix. Most of the the time you'll want to modify the modelview stack.

    I can't tell why you get the same results in your example as I didn't look at the math involved. Still, it should be merely coincidence and you CANNOT rely that!

    Edit: BTW, if you say you're using core profile code as much as possible, you might have a certain misunderstanding what "core profile" means - what it definitely doesn't involve is using ANY fixed functionality like the matrix stacks and matrix manipulation functions.

  6. #6
    Member Regular Contributor
    Join Date
    Jan 2012
    Location
    Germany
    Posts
    325
    In the fixed function pipeline (note: outdated) all your vertices are projected like this:

    P_transformed = M_projection * M_modelview * P;

    M_projection is set by matrix manipulating calls in matrixmode GL_PROJECTION (e.g. LoadIdentity, transform, etc.), M_modelview in matrixmode GL_MODELVIEW. Default is the identity matrix. That's why you don't see a difference now. You would see errors in your lighting when you use the matices wrong.

    Are you sure, you really want to learn the fixed function pipeline?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •