这两个都是glMatrixMode()函数的参数,那就先说说glMatrixMode吧~,这个函数其实就是对接下来要做什么进行一下声明,也就是在要做下一步之前告诉计算机我要对“什么”进行操作了,这个“什么”在glMatrixMode的“()”里的选项(参数)有,GL_PROJECTION,GL_MODELVIEW和GL_TEXTURE;
如果参数是GL_PROJECTION,这个是投影的意思,就是要对投影相关进行操作,也就是把物体投影到一个平面上,就像我们照相一样,把3维物体投到2维的平面上。这样,接下来的语句可以是跟透视相关的函数,比如glFrustum()或gluPerspective();
如果参数是GL_MODELVIEW,这个是对模型视景的操作,接下来的语句描绘一个以模型为基础的适应,这样来设置参数,接下来用到的就是像gluLookAt()这样的函数;
若是GL_TEXTURE,就是对纹理相关进行操作;
顺便说下,OpenGL里面的操作,很多是基于对矩阵的操作的,比如位移,旋转,缩放,所以,这里其实说的规范一点就是glMatrixMode是用来指定哪一个矩阵是当前矩阵,而它的参数代表要操作的目标,GL_PROJECTION是对投影矩阵操作,GL_MODELVIEW是对模型视景矩阵操作,GL_TEXTURE是对纹理矩阵进行随后的操作。
#!/usr/bin/perl -w use strict; use warnings; use OpenGL qw/ :all /; use OpenGL::Config; glutInit(); glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutInitWindowPosition(100,100); glutInitWindowSize(400,400); glutCreateWindow("my first OpenGL program"); glClearColor(0,0,0,255); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); gluOrtho2D(-10,10,-10,10); glutDisplayFunc(\&mydis); glutMainLoop(); return 0; sub mydis() { my $x=-10; my $y; my $z=-10; glPointSize(1); glBegin(GL_POINTS); for (0..100000) { $x+=0.01; $z+=0.01; $y=sin($x); glColor3f(255,0,0); glVertex2f($x,$y); $y=cos($z); glColor3f(0,255,0); glVertex2f($x,$y); } glEnd(); glBegin(GL_LINES); glVertex2f(-10,0); glVertex2f(10,0); glEnd(); glBegin(GL_LINES); glVertex2f(0,-10); glVertex2f(0,10); glEnd(); glFlush(); }
下面以3D效果
#!/usr/bin/perl -w use strict; use warnings; use OpenGL qw/ :all /; use OpenGL::Config; glutInit(); glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutInitWindowPosition(100,100); glutInitWindowSize(400,400); glutCreateWindow("my first OpenGL program"); glClearColor(0,0,0,255); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); gluOrtho2D(-10,10,-10,10); glutDisplayFunc(\&mydis); glutMainLoop(); return 0; sub mydis() { my $x=-10; my $y; my $z=-10; glPointSize(1); glBegin(GL_LINE_LOOP); for (0..100000) { $x+=0.01; $z+=0.01; $y=sin($x); glColor3f(255,0,0); glVertex3f($x,$y,($x+$y)/10); $y=cos($z); glColor3f(0,255,0); glVertex3f($x,$y,($x+$y)/10); } glEnd(); glBegin(GL_LINES); glVertex2f(-10,0); glVertex2f(10,0); glEnd(); glBegin(GL_LINES); glVertex2f(0,-10); glVertex2f(0,10); glEnd(); glFlush(); }
请发表评论