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

◇◆丶佛笑我妖孽 提交于 2019-12-12 11:31:26

问题


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 get same result. Can anyone show the difference of these glMatrixMode(GL_MODELVIEW); and glMatrixMode(GL_PROJECTION); changing this code?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10408113/difference-between-glmatrixmodegl-projection-and-glmatrixmodegl-modelview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!