glLoadIdentity()与glTranslatef()和glRotatef()--坐标变换

原文地址:http://www.cnblogs.com/yujunyong/archive/2011/04/17/2018776.html

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();

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();

/

程序的运行结果如下:

左边的三角形是第一步绘制的,可以看到该三角形绘制的坐标系,实际上是以(-1.5f,0.0f,-6.0f)为原点的。

第二个三角形绘制的时候,由于使用glLoadIdentity()使原点重新回到屏幕中心来,因此其原点位于屏幕的中心。

glRotatef(angle, x, y, z)
与glTranslatef(x, y, z)类似,glRotatef(angle, x, y, z)也是对坐标系进行操作。
旋转轴经过原点,方向为(x,y,z),旋转角度为angle,方向满足右手定则。

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();

在未旋转的情况下如图所示:


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);
 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();
 glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
 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();

在旋转之后加了一个复位的指令,图形就不会旋转了。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值