glLoadIdentity()
将当前的用户坐标系的原点移到了屏幕中心:类似于一个复位操作
1.X坐标轴从左至右,Y坐标轴从下至上,Z坐标轴从里至外。
2.OpenGL屏幕中心的坐标值是X和Y轴上的0.0f点。
3.中心左面的坐标值是负值,右面是正值。
移向屏幕顶端是正值,移向屏幕底端是负值。
移入屏幕深处是负值,移出屏幕则是正值。
glTranslatef(x, y, z)
沿着 X, Y 和 Z 轴移动。
注意在glTranslatef(x, y, z)中,当您移动的时候,您并不是相对屏幕中心移动,而是相对与当前所在的屏幕位置。其作用就是将你绘点坐标的原点在当前原点的基础上平移一个(x,y,z)向量。
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
该三角形绘制的坐标系,实际上是以(-1.5f,0.0f,-6.0f)为原点的。
glRotatef(angle, x, y, z)
与glTranslatef(x, y, z)类似,glRotatef(angle, x, y, z)也是对坐标系进行操作。
旋转轴经过原点,方向为(x,y,z),旋转角度为angle,方向满足右手定则。
glLoadIdentity();
glRotatef(45,0.0f,0.0f,1.0f);
glTranslatef(0.0f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
绕Z轴正向旋转45度角,因为Z轴正方向由屏幕内指向屏幕外,由右手定则可知方向为逆时针转动。由于直角顶点即为原点,因此将围绕直角逆时针旋转。
旋转和平移函数都存在的情况下,程序会如何执行呢?
//////////////////////////////////
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f, 0.0f, -6.0f);
glBegin(GL_QUADS);
glVertex3f(-0.01f,-0.01f, 0.0f);
glVertex3f( 0.01f,-0.01f, 0.0f);
glVertex3f( 0.01f, 0.01f, 0.0f);
glVertex3f(-0.01f, 0.01f, 0.0f);
glEnd();
glLoadIdentity();
glRotatef(45.0f, 0.0f, 0.0f, 1.0f); //旋转(局部坐标系的X,Z轴也跟着旋转了)
glTranslatef(1.0f,0.0f,-6.0f); //平移
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
/////////////////////////////////
如果旋转和平移语句的顺序调换一下结果会怎么样呢?
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(0.0f, 0.0f, -6.0f);
glBegin(GL_QUADS);
glVertex3f(-0.01f,-0.01f, 0.0f);
glVertex3f( 0.01f,-0.01f, 0.0f);
glVertex3f( 0.01f, 0.01f, 0.0f);
glVertex3f(-0.01f, 0.01f, 0.0f);
glEnd();
glLoadIdentity();
glTranslatef(1.0f,0.0f,-6.0f);
glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
//////////////////////////
由这三个例子可知,所有对图形进行平移旋转等操作的语句的执行顺序都是从下到上执行的。
(感觉是从上到下执行的,不清楚怎么回事,知道的给解释下)
而且旋转语句的旋转中心知(0.0f, 0.0f, 0.0f)这个点。
////////////////////////////////////////////////////////////////
glLoadIdentity();
glRotatef(45,0.0f,0.0f,1.0f);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(1.0f,0.0f, 0.0f);
glVertex3f(0.0f,1.0f, 0.0f);
glEnd();
在旋转之后加了一个复位的指令,图形就不会旋转了。
参考链接:http://www.cnblogs.com/yujunyong/archive/2011/04/17/2018776.html