opengl实现太阳系天体运动(3)

2019-08-31 23:39

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


opengl实现太阳系天体运动(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:人工智能

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: