本文主要是介绍opengl地球行星太阳系动画(学习笔记-仅供参考),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#include <gl/glut.h>float fEarth = 2.0f; //地球绕太阳的旋转角度float fMoon = 24.0f; //月球绕地球的旋转角度void Init()
{glEnable(GL_DEPTH_TEST); //启用深度测试glClearColor(0.0f, 0.0f, 0.0f, 0.8f); //背景为黑色
}void Reshape(int w, int h)
{if (0 == h)h = 1;glViewport(0, 0, w, h); //设置视区尺寸glMatrixMode(GL_PROJECTION); //指定当前操作投影矩阵堆栈glLoadIdentity(); //重置投影矩阵//指定透视投影的观察空间gluPerspective(45.0f, (float)w / (float)h, 1.0f, 1000.0f);glMatrixMode(GL_MODELVIEW);glLoadIdentity();
}void Display(void)
{//清除颜色和深度缓冲区glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glMatrixMode(GL_MODELVIEW); //指定当前操作模型视图矩阵堆栈glLoadIdentity(); //重置模型视图矩阵glTranslatef(0.0f, 0.0f, -500.0f); //将图形沿z轴负向移动glColor3f(1.0f, 0.0f, 0.0f); //红色glutSolidSphere(50.0f, 20, 20);//画太阳glColor3f(0.0f, 0.0f, 1.0f);//蓝色glRotatef(23.27, 0.0, 0.0, 1.0); //地球与太阳的黄赤交角glRotatef(fEarth, 0.0f, 1.0f, 0.0f);glTranslatef(200.0f, 0.0f, 0.0f);glutSolidSphere(20.0f, 20, 20); //画地球glPopMatrix();glPopMatrix();glRotatef(6.0f, 1.0f, 1.0f, 1.0f);glRotatef(fMoon, 0.0f, 1.0f, 0.0f);glColor3f(1.0f, 1.0f, 0.0f);//黄色glTranslatef(30.0f, 0.0f, 0.0f);glutSolidSphere(5.0f, 20, 20); //画月球glLoadIdentity();glFlush();glutSwapBuffers();
}void myIdle(void) //在空闲时调用,达到动画效果
{fEarth += 0.03f; //增加旋转步长,产生动画效果if (fEarth > 360.0f)fEarth = 2.0f;fMoon += 0.24f;if (fMoon > 360.0f)fMoon = 24.0f;Display();
}int main(int argc, char* argv[])
{glutInit(&argc, argv);//窗口使用RGB颜色,双缓存和深度缓存glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowPosition(100, 100);glutInitWindowSize(600, 400);glutCreateWindow("太阳系动画");glutReshapeFunc(Reshape);glutDisplayFunc(Display);glutIdleFunc(&myIdle);//CPU空闲时调用myIdleInit();glutMainLoop();return 0;
}
这篇关于opengl地球行星太阳系动画(学习笔记-仅供参考)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!