1.opengl中缩放使用的函数是glScalef
其原型为void glScalef(GLfloat x, GLfloat y, GLfloat z);
该函数表示模型在各轴上进行扩大和缩小。例如:glScalef (1.0, 2.0, 1.0);
表示y坐标值扩大两倍,这样原本的物体就变长了。
glTranslatef(-50.0f,0.0f,0.0f);
glScalef( 1.0f,2.0f,1.0f);//y轴方向放大两倍
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(50.0f,0.0f, 0.0f);
glVertex3f(0.0f,50.0f, 0.0f);
glEnd();
运行结果图为:
2.平移使用glTranslatef函数
其原型为void glTranslatef(GLfloat x, GLfloat y, GLfloat z);
例如:glTranslatef(-50.0,0.0,-20.0);表示物体沿x负方向移动50,沿z轴负方向移动20.
需要注意的是,当移动的时候,并不是相对屏幕中心移动,而是相对与当前所在的屏幕移动。
其作用就是将绘点坐标的原点在当前原点的基础上平移一个(x,y,z)向量。
glClear(GL_COLOR_BUFFER_BIT); //清除颜色
glBegin(GL_TRIANGLES); //划线
glVertex3f(0.0f,0.0f,0.0f);
glVertex3f(50.0f,0.0f,0.0f);
glVertex3f(0.0f,50.0f,0.0f);
glEnd();
glTranslatef(-50.0f,0.0f,0.0f);//向x的负方向平移50
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(50.0f,0.0f, 0.0f);
glVertex3f(0.0f,50.0f, 0.0f);
glEnd();
运行结果如下图:
3.旋转使用glRotatef函数
其原型为:void glRotatef(GLfloat angle, GLfloat x, GLfloat y, GLfloat z);
angle表示旋转的角度,(x,y,z)表示转轴。例如:glRotatef(45.0, 0.0, 0.0, 1.0);
表示模型沿着(0,0,1)这个轴旋转45°。
glTranslatef(-50.0f,0.0f,0.0f);
glRotatef(45,0.0f,.0f,1.0f);//按z轴方向旋转45°
glBegin(GL_TRIANGLES);
glVertex3f(0.0f,0.0f, 0.0f);
glVertex3f(50.0f,0.0f, 0.0f);
glVertex3f(0.0f,50.0f, 0.0f);
glEnd();
运行结果为: