#include<GL/glut.h> void display() { glClearColor(0.0,0.0,0.0,0.0); glOrtho(-10,10,-10,10,-10,10); glClear(GL_COLOR_BUFFER_BIT); glTranslatef(-4.5f,0.0f,-6.0f); glBegin(GL_TRIANGLES); glColor3f(1.0,0.0,0.0); glVertex3f(0.0f,0.0f, 0.0f); glVertex3f(1.0f,0.0f, 0.0f); glVertex3f(0.0f,1.0f, 0.0f); glEnd(); glTranslatef(1.0f,0.0f,-1.0f); glBegin(GL_TRIANGLES); glColor3f(1.0,0.0,0.0); glVertex3f(0.0f,0.0f, 0.0f); glVertex3f(1.0f,0.0f, 0.0f); glVertex3f(0.0f,1.0f, 0.0f); glEnd(); glFlush(); } int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow("Simple"); glClearColor(1.0,1.0,1.0,0.0); glutDisplayFunc(display); glutMainLoop(); //使程序一直处在监听状态中 return 0; }
沿着 X, Y 和 Z 轴移动。
注意在glTranslatef(x, y, z)中,当您移动的时候,您并不是相对屏幕中心移动,而是相对与当前所在的屏幕位置。其作用就是将你绘点坐标的原点在当前原点的基础上平移一个(x,y,z)向量
于是乎第二个三角形是相当于第一个三角形向左平移一个单位
如果第二个三角形的z方向小于 -4的话,将会看不到的
右边的三角形是第一步绘制的,可以看到该三角形绘制的坐标系,实际上是以(-4.5f,0.0f,-6.0f)为原点的。