OpenGL 画太阳,地球,月亮
#include <windows.h> #include <gl/glut.h> #include <math.h> #define GL_PI 3.1415f static GLfloat xRot = 0.0f; static GLfloat yRot = 0.0f; GLfloat whiteLight[]={0.2f,0.2f,0.2f,1.0f}; GLfloat lightPos[]={0.0f,0.0f,0.0f,1.0f}; void RenderScene(void) { static float fMoonRot = 0.0f; static float fEarthRot= 0.0f; glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(0.0f,0.0f,-300.0f); //绘制太阳 glColor3ub(255,255,0); glDisable(GL_LIGHTING); //关闭光源 glutSolidSphere(20.0f,20.0f,20.0f); glEnable(GL_LIGHTING); //启动光源 glLightfv(GL_LIGHT0,GL_POSITION,lightPos); //绘制地球 glColor3ub(0,0,255); glRotatef(fEarthRot,0.0f,1.0f,0.0f); glTranslatef(105.0f,0.0f,0.0f); glutSolidSphere(15.0f,15.0f,15.0f); //颜色,移动,旋转速度都是要在画之前决定的 //根据基于地球的坐标进行旋转,并绘制月球 glColor3ub(200,200,200); glRotatef(fMoonRot,0.0f,1.0f,0.0f); glTranslatef(30.0f,0.0f,0.0f); fMoonRot += 15.0f; if(fMoonRot >= 365.0f) fMoonRot = 0.0f; glutSolidSphere(6.0f,15.0f,15.0f); //旋转角度确定好后才画的月球。 glPopMatrix(); fEarthRot += 5.0f; if(fEarthRot>=365.0f) fEarthRot=0.0f; glutSwapBuffers(); } void ChangeSize(GLsizei w,GLsizei h) { GLfloat fAspect; if(h==0) h=1; glViewport(0,0,w,h); fAspect = (GLfloat)w/(GLfloat)h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f,fAspect ,1.0,4000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void SetupRC(void) { glEnable(GL_DEPTH_TEST); glEnable(GL_COLOR_MATERIAL); glClearColor(0.0f,0.0f,0.0f,1.0f); glEnable(GL_LIGHTING); //启动光源 glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight); //使用whiteLght所指定的环境光 glLightfv(GL_LIGHT0,GL_POSITION,lightPos); //0号光源的位置 glEnable(GL_LIGHT0); } void SpecialKeys(int key ,int x, int y) { if(key==GLUT_KEY_UP) xRot -= 5.0f; if(key==GLUT_KEY_DOWN) xRot +=5.0f; if(key == GLUT_KEY_LEFT) yRot -=5.0f; if(key == GLUT_KEY_RIGHT) yRot +=5.0f; xRot = (GLfloat)((const int)xRot %360); yRot = (GLfloat)((const int)yRot %360); glutPostRedisplay(); } void TimerFunc(int value) { glutPostRedisplay(); glutTimerFunc(100,TimerFunc,1); } int main(int argc, char* argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(800,600); glutCreateWindow("Orthographic Projection"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glutSpecialFunc(SpecialKeys);//设置上下左右键对图形旋转的控制 SetupRC(); glutTimerFunc(250,TimerFunc,1); //自动动画,计时器 glutMainLoop(); return 0; }
太阳系八大行星
#include <windows.h> #include <gl/glut.h> #include <math.h> #define GL_PI 3.1415f float fMoonRot = 0.0f; float fAdamRot= 0.0f; float fEarthRot= 0.0f; float fMarsRot = 0.0f; float fHesperRot = 0.0f; float fJupiterRot = 0.0f; float fSaturnRot = 0.0f; float fUranusRot = 0.0f; float fNeptuneRot = 0.0f; GLfloat whiteLight[]={0.2f,0.2f,0.2f,1.0f}; GLfloat lightPos[]={0.0f,0.0f,0.0f,1.0f}; void sun() { //绘制太阳 glColor3ub(255,255,0); glDisable(GL_LIGHTING); //关闭光源 glutSolidSphere(25.0f,200.0f,200.0f); glEnable(GL_LIGHTING); //启动光源 glLightfv(GL_LIGHT0,GL_POSITION,lightPos); } void Adam() { //绘制水星 glPushMatrix(); glColor3ub(0,0,255); glRotatef(fAdamRot,0.0f,1.0f,0.0f); glTranslatef(50.0f,0.0f,0.0f); glutSolidSphere(2.0f,200.0f,200.0f); //水星半径是地球的40% 即2 fAdamRot += 4.152f; //水星公转周期为地球24.08% ,即4.152 if(fAdamRot>=365.0f) fAdamRot=0.0f; glPopMatrix(); } void Hesper() { //绘制金星 glPushMatrix(); glColor3ub(255,215,0); glRotatef(fHesperRot,0.0f,1.0f,0.0f); glTranslatef(65.0f,0.0f,0.0f); glutSolidSphere(4.75f,200.0f,200.0f); //金星半径是地球的95% 即4.75 fHesperRot += 1.62f; //金星公转周期为地球61.56% ,即1.62 if(fHesperRot>=365.0f) fHesperRot=0.0f; glPopMatrix(); } void Earth_Moon() { //绘制地球,所有运行参数以地球为标准 glPushMatrix(); glColor3ub(0,0,255); glRotatef(fEarthRot,0.0f,1.0f,0.0f); glTranslatef(80.0f,0.0f,0.0f); //设地球周期365 glutSolidSphere(5.0f,20.0f,20.0f); //设地球半径5 //根据基于地球的坐标进行旋转,并绘制月球 glColor3ub(200,200,200); glRotatef(fMoonRot,0.0f,1.0f,0.0f); glTranslatef(5.0f,0.0f,0.0f); fMoonRot += 13.3f; //月球的公转周期 27.3 天 365/27.3==13.3 if(fMoonRot >= 365.0f) // 月球半径是地球的3/11 即1.363 fMoonRot = 0.0f; glutSolidSphere(1.363f,200.0f,200.0f); glPopMatrix(); //出栈后值变化,要在下一次循环中才有效 fEarthRot += 1.0f; if(fEarthRot>=365.0f) fEarthRot=0.0f; } void Mars() { //绘制火星 glPushMatrix(); glColor3ub(255,0,0); glRotatef(fMarsRot,0.0f,1.0f,0.0f); glTranslatef(70.0f,0.0f,0.0f); glutSolidSphere(2.65f,200.0f,200.0f); //火星半径是地球的53% 即2.65 fMarsRot += 0.5f; //火星公转周期为地球2倍 if(fMarsRot>=365.0f) fMarsRot=0.0f; glPopMatrix(); } void Jupiter() { //绘制木星 glPushMatrix(); glColor3ub(255,215,0); glRotatef(fJupiterRot,0.0f,1.0f,0.0f); glTranslatef(110.0f,0.0f,0.0f); glutSolidSphere(30.0f,200.0f,200.0f); //木星半径是地球的11.21倍 即56.05 为了美观,定为30 fJupiterRot += 0.084f; //木星公转周期为地球11.87倍 ,即0.084 if(fJupiterRot>=365.0f) fJupiterRot=0.0f; glPopMatrix(); } void Saturn() { //绘制土星 glPushMatrix(); glColor3ub(53,50,42); glRotatef( fSaturnRot,0.0f,1.0f,0.0f); glTranslatef(170.0f,0.0f,0.0f); glutSolidSphere(25.0f,200.0f,200.0f); //土星半径是地球的9.45倍 即47.25 为了美观,定为25 fSaturnRot += 0.03f; //土星公转周期为地球29.47倍 ,即0.03 if( fSaturnRot>=365.0f) fSaturnRot=0.0f; glPopMatrix(); } void UranusRot() { //绘制天王星 glPushMatrix(); glColor3ub(0,0,215); glRotatef( fUranusRot,0.0f,1.0f,0.0f); glTranslatef(215.0f,0.0f,0.0f); glutSolidSphere(20.05f,200.0f,200.0f); //天王星半径是地球的倍 即4.01,即20.05 fUranusRot += 0.03f; //天王星公转周期为地球84.06倍 ,即0.0124 if( fUranusRot>=365.0f) fUranusRot=0.0f; glPopMatrix(); } void Neptune() { //绘制海王星 glPushMatrix(); glColor3ub(0,180,100); glRotatef( fNeptuneRot,0.0f,1.0f,0.0f); glTranslatef(245.0f,0.0f,0.0f); glutSolidSphere(19.45f,200.0f,200.0f); //海王星半径是地球的倍 即3.89,即19.45 fNeptuneRot += 2.17f; //天王星公转周期为地球46%倍 ,即2.17 if( fUranusRot>=365.0f) fNeptuneRot=0.0f; glPopMatrix(); } void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(0.0f,0.0f,-300.0f); sun(); Adam(); Hesper(); Earth_Moon(); Mars(); Jupiter(); Saturn(); UranusRot(); Neptune(); glPopMatrix(); glutSwapBuffers(); } void ChangeSize(GLsizei w,GLsizei h) { GLfloat fAspect; if(h==0) h=1; glViewport(0,0,w,h); fAspect = (GLfloat)w/(GLfloat)h; glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f,fAspect ,1.0,4000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void SetupRC(void) { glEnable(GL_DEPTH_TEST); glEnable(GL_COLOR_MATERIAL); glClearColor(0.0f,0.0f,0.0f,1.0f); glEnable(GL_LIGHTING); //启动光源 glLightModelfv(GL_LIGHT_MODEL_AMBIENT,whiteLight); //使用whiteLght所指定的环境光 glLightfv(GL_LIGHT0,GL_POSITION,lightPos); //0号光源的位置 glEnable(GL_LIGHT0); } void TimerFunc(int value) { glutPostRedisplay(); glutTimerFunc(100,TimerFunc,1); } int main(int argc, char* argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(800,600); glutCreateWindow("Orthographic Projection"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); SetupRC(); glutTimerFunc(250,TimerFunc,1); //自动动画,计时器 glutMainLoop(); return 0; }