glMatrixMode(GL_PROJECTION);之间的区别和glMatrixMode(GL_MODELVIEW); [英] Difference between glMatrixMode(GL_PROJECTION); and glMatrixMode(GL_MODELVIEW);

查看:981
本文介绍了glMatrixMode(GL_PROJECTION);之间的区别和glMatrixMode(GL_MODELVIEW);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

glMatrixMode(GL_PROJECTION);和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(键盘);
glutMainLoop();
返回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键盘(无符号字符键,int mousePositionX,int mousePositionY){
switch(key){$ b $ case KEY_ESCAPE:
exit(0);
休息;
默认值:
break;






例子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(键盘);
glutMainLoop();
返回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键盘(无符号字符键,int mousePositionX,int mousePositionY){
switch(key){$ b $ case KEY_ESCAPE:
exit(0);
休息;
默认值:
break;
}
}

对于这两种代码,我都得到相同的结果。
任何人都可以显示这些glMatrixMode(GL_MODELVIEW)的区别;和glMatrixMode(GL_PROJECTION);改变这个代码?

解决方案

从GL的角度来看,当你看到一个场景时,你有一个摄像头和一个镜头。



ModelView是表示相机的矩阵(位置,指向和向上向量)。

ProjectionView是表示相机镜头的矩阵(光圈,远场,近场等)。 b
$ b

请参阅这里了解更多关于这些信息......



在像你这样的简单例子中,区别。但是,一般来说,您需要通过使用矩阵乘法和反转来要求OpenGL计算东西。例如,如果要将屏幕上的点转换为GL坐标系的点,则GL需要一个投影矩阵和一个模型矩阵,并将它们乘以特定的顺序。在这种情况下,您必须相应地使用它们,因为矩阵乘法是不可交换的。


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?

解决方案

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.

这篇关于glMatrixMode(GL_PROJECTION);之间的区别和glMatrixMode(GL_MODELVIEW);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆