Title: Difference between glMatrixMode(GL_PROJECTION) and glMatrixMode(GL_MODELVIEW)
Tags: c++;math;opengl;graphics
Question: What's the difference between ```glMatrixMode(GL_PROJECTION)``` and ```glMatrixMode(GL_MODELVIEW)```?
```#include <stdio.h&gt;
#include <GL/gl.h&gt;
#include <GL/glut.h&gt;

#define KEY_ESCAPE 27

void display();
void keyboard(unsigned char,int,int);

int main(int argc, char **argv) {
    glutInit(&amp;argc, argv);                                    
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH );
    glutInitWindowSize(600,400);                  
    glutCreateWindow(&quot;Opengl Test&quot;);                              
    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&gt;
#include <GL/gl.h&gt;
#include <GL/glut.h&gt;

#define KEY_ESCAPE 27

void display();
void keyboard(unsigned char,int,int);

int main(int argc, char **argv) {
    glutInit(&amp;argc, argv);                                    
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH );
    glutInitWindowSize(600,400);                  
    glutCreateWindow(&quot;Opengl Test&quot;);                              
    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)```?


Here is the accepted answer: 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.

Comment for this answer: BOTH the MODELVIEW matrix AND the PROJECTION matrix affect the final scene. If you look at examples, you will see people manipulating BOTH of those, e.g. [GL_MODELVIEW and GL_PROJECTION](http://stackoverflow.com/questions/15482641/gl-modelview-and-gl-projection). You are probably better off STARTING from a useful example, but if you want to alter your code to make it DO something with one of the matrices, try adding lines after your glLoadIdentity() that manipulate the current matrix, as seen in the linked post. `glFrustum` for PROJECTION, and/or `glTranslated` or `glRotated` for MODELVIEW.

Here is another answer: 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.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2012:00:05/02/12', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<c++><math><opengl><graphics>', 'Title': 'Difference between glMatrixMode(GL_PROJECTION) and glMatrixMode(GL_MODELVIEW)', 'ViewCount': 23925, 'id': 10408113, 'user_AboutMe': '', 'user_AccountId': 466633.0, 'user_DisplayName': 'shibly', 'user_Reputation': 11848.0, 'user_Views': 928.0, 'user_id': 884995.0}}