Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between glMatrixMode(GL_PROJECTION) and glMatrixMode(GL_MODELVIEW)

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

#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;
    }
}

Example 2:

#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 got the same result. Can anyone show the difference of these glMatrixMode(GL_MODELVIEW) and glMatrixMode(GL_PROJECTION)?

like image 885
shibly Avatar asked Dec 07 '22 14:12

shibly


2 Answers

When you are looking at a scene, from GL point of view, you have a camera and a lens.

ModelView is the matrix that represents your camera (position, pointing, and up vector).

ProjectionView is the matrix that represents your camera's lens (aperture, far-field, near-field, etc).

See here for more info on those...

In simple examples like yours, that doesn't make many difference. However, in general, you need to ask OpenGL to calculate stuff by using matrix multiplications and inversions. For instance, if you want to transform a point on screen to a point of the GL coordinate system, GL will need a projection matrix and a model matrix and multiply them by a specific order. In that case, you must use them accordingly, because matrix multiplications are non-commutable.

like image 110
Jorge Leitao Avatar answered Dec 22 '22 00:12

Jorge Leitao


Of course you didn't get a different result. You didn't actually do anything with the matrix. You set it to identity... which is the default value.

In general, you should only put the projection matrices into the GL_PROJECTION matrix. The transforms that go from the model's space to camera space should go into the GL_MODELVIEW matrix.

If you don't know what projection matrices are, or if you're unsure about what a matrix is, I would suggest looking up proper OpenGL learning materials. And avoiding the use of fixed-function GL.

like image 41
Nicol Bolas Avatar answered Dec 22 '22 00:12

Nicol Bolas