yRot = 355.0f;
// Refresh the Window
glutPostRedisplay();// this will refresh the window, so, it works the same to call RenderScene() directly }
void DrawAxis() {
//绘制x、y、z坐标轴 {
glColor3f( 0.0f, 0.0f, 1.0f );//指定线的颜色,蓝色
glBegin( GL_LINES ); {
// x-axis
glVertex3f( 0.0f, 0.0f, 0.0f); glVertex3f( 200.0f, 0.0f, 0.0f);
// x-axis arrow
glVertex3f( 200.0f, 0.0f, 0.0f); glVertex3f( 193.0f, 3.0f, 0.0f); glVertex3f( 200.0f, 0.0f, 0.0f);
glVertex3f( 193.0f,-3.0f, 0.0f); } glEnd();
glColor3f( 0.0f, 1.0f, 0.0f );//指定线的颜色,绿色
glBegin( GL_LINES ); {
// y-axis
glVertex3f( 0.0f, 0.0f, 0.0f); glVertex3f( 0.0f, 200.0f, 0.0f); glVertex3f( 0.0f, 200.0f, 0.0f); glVertex3f( 3.0f, 193.0f, 0.0f); glVertex3f( 0.0f, 200.0f, 0.0f); glVertex3f( -3.0f, 193.0f, 0.0f); } glEnd();
glColor3f( 1.0f, 0.0f, 0.0f );//指定线的颜色,红色
glBegin( GL_LINES ); {
// z-axis
glVertex3f( 0.0f, 0.0f, 0.0f ); glVertex3f( 0.0f, 0.0f, 200.0f ); glVertex3f( 0.0f, 0.0f, 200.0f );
glVertex3f( 0.0f, 3.0f, 193.0f ); glVertex3f( 0.0f, 0.0f, 200.0f ); glVertex3f( 0.0f, -3.0f, 193.0f); } glEnd(); } } 运行效果图:
如果,转换一下坐标,例如将上面的 RenderScene 函数修改为下面的样子, void RenderScene() { //清空颜色缓冲区,填充的颜色由 glClearColor( 0, 0.0, 0.0, 1 ); 指定为黑色
glClear( GL_COLOR_BUFFER_BIT );
// Save matrix state and do the rotation
glPushMatrix();
glRotatef(xRot, 1.0f, 0.0f, 0.0f); glRotatef(yRot, 0.0f, 1.0f, 0.0f);
DrawAxis();
//绘制太阳
glColor3f( 1.0f, 1.0f, 1.0f );
glRotatef(45, 0.0f, 0.0f, 1.0f); //沿着z轴旋转45‘
glTranslatef( 100, 0, 0 ); //向x轴平移100个单位
glutSolidSphere( 50.f, 15, 15 );
// Restore transformations
glPopMatrix();
glutSwapBuffers(); }
那么效果图如下:
3、添加绕太阳旋转的地球以及绕地球转的月亮
只需添加另一个球体,然后想办法让这个球体在每一帧动画中在不同的地方出现,给人的感觉就像这个地球在绕太阳旋转。 需要增加另一函数
glutIdleFunc (RenderScene);
该函数 glutIdleFunc 表示在CPU空闲的时间调用RenderScene来重新绘制窗体。
整体程序如下: // Purpose: To show how to draw the solar system // Author: zieckey // Date: 2009/10/7 #include \ #include