问题描述
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(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;
}
}
示例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;
}
}
对于这两个代码,我得到相同的结果.谁能显示这些 glMatrixMode(GL_MODELVIEW)
和 glMatrixMode(GL_PROJECTION)
的区别?
For both codes, I got the same result.Can anyone show the difference of these glMatrixMode(GL_MODELVIEW)
and glMatrixMode(GL_PROJECTION)
?
推荐答案
在查看场景时,从GL的角度来看,您有相机和镜头.
When you are looking at a scene, from GL point of view, you have a camera and a lens.
ModelView是代表您的相机(位置,指向和上矢量)的矩阵.
ModelView is the matrix that represents your camera (position, pointing, and up vector).
ProjectionView是代表相机镜头(光圈,远场,近场等)的矩阵.
ProjectionView is the matrix that represents your camera's lens (aperture, far-field, near-field, etc).
有关这些内容的更多信息,请参见此处.
See here for more info on those...
在像您这样的简单示例中,这并没有多大区别.但是,通常,您需要让OpenGL使用矩阵乘法和求逆来计算东西.例如,如果要将屏幕上的点转换为GL坐标系的点,则GL将需要一个投影矩阵和一个模型矩阵,并将它们乘以特定的顺序.在这种情况下,您必须相应地使用它们,因为矩阵乘法是不可交换的.
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)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!