码迷,mamicode.com
首页 > 其他好文 > 详细

openGL+VS2010的例程--太阳地球月球运动模型(三维)

时间:2015-01-12 18:50:09      阅读:273      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

效果图如上:

步骤:略

实现代码如下:

  1 #include "windows.h"
  2 #include <gl\glut.h>
  3 
  4 #define SunSize 0.4
  5 #define EarthSize 0.06
  6 #define MoonSize 0.016
  7 
  8 GLfloat SpeedMultiplicator = 1.0;
  9 GLfloat DaysPerYear = 50.0; //OK, ok... but it is soo slow with 360!
 10 GLfloat year = 0.0; //degrees
 11 GLfloat day = 0.0;
 12 GLfloat moonAroundEarth = 0.0;
 13 GLfloat moonItsSelf = 0.0;
 14 GLfloat EarthOrbitRadius = 1.0;
 15 GLfloat MoonOrbitRadius = 0.1;
 16 GLfloat daySpeed = 5.0 * SpeedMultiplicator;
 17 GLfloat yearSpeed = DaysPerYear / 360.0 * daySpeed * SpeedMultiplicator;
 18 GLfloat moonAroundEarthSpeed = 10 * SpeedMultiplicator;
 19 GLfloat moonItsSelfSpeed = 5 * SpeedMultiplicator;
 20 void RenderScene(void)
 21 {
 22     glPushMatrix();
 23         gluLookAt(    0.0,0.0,-4.0,
 24                     0.0,0.0,1.0,
 25                     0.0,-3.0,0.0);
 26         glColor3f(1.0,1.0,0.6);
 27         glutWireSphere(SunSize,50,50);
 28         glPushMatrix();
 29             glRotatef(year,0.0,1.0,0.0);
 30             glTranslatef(EarthOrbitRadius,0.0,0.0);
 31             glRotatef(-year,0.0,1.0,0.0);
 32             glPushMatrix();
 33                 glRotatef(day,0.25,1.0,0.0);
 34                 glColor3f(0.0,0.5,0.8);
 35                 glutWireSphere(EarthSize,10,10);  //Draw earth
 36                 //Draw earth rotation axis
 37                 glBegin(GL_LINES);
 38                     glVertex3f(-0.0625,-0.25,0.0);
 39                     glVertex3f(0.0625,0.25,0.0);
 40                 glEnd();
 41             glPopMatrix();
 42             glRotatef(moonAroundEarth,0.0,1.0,0.0);
 43             glTranslatef(MoonOrbitRadius,0.0,0.0);
 44             //The following 2 lines should be combined, but it is better to understand this way
 45             glRotatef(-moonAroundEarth,0.0,1.0,0.0);
 46             glRotatef(moonItsSelf,0.0,1.0,0.0);
 47             glColor3f(0.8,0.8,0.75);
 48             glutWireSphere(MoonSize,8,8);
 49         glPopMatrix();
 50                 
 51     glPopMatrix();
 52 }
 53 
 54 void Init(void)
 55 {
 56     glClearColor(0.0,0.0,0.0,0.0);
 57     glClearDepth(10.0);
 58     glMatrixMode(GL_MODELVIEW);
 59     glLoadIdentity();
 60 }
 61 
 62 void Display(void)
 63 {
 64     glClear(GL_COLOR_BUFFER_BIT);
 65     RenderScene();
 66     glFlush();
 67     glutSwapBuffers();
 68 }
 69 
 70 void Reshape(int x, int y)
 71 {
 72     if (y == 0) return;
 73     glMatrixMode(GL_PROJECTION);
 74     glLoadIdentity();
 75     gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0);
 76     glMatrixMode(GL_MODELVIEW);
 77     glViewport(0,0,x,y);
 78     Display();
 79 }
 80 static long long times = 0;
 81 void Idle(void)
 82 {
 83     times++;
 84     if(times>1000000)
 85         times =0;
 86     if(times% 1000000== 0)
 87     {
 88         day += daySpeed;
 89         year += yearSpeed;
 90         moonItsSelf += moonItsSelfSpeed;
 91         moonAroundEarth += moonAroundEarthSpeed;
 92         Display();
 93     }
 94 }
 95 
 96 
 97 int main(int argc, char* argv[])
 98 {
 99     glutInit(&argc, argv);
100     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
101     glutInitWindowSize(600,600);
102     glutCreateWindow(argv[0]);
103     Init();
104     glutReshapeFunc(Reshape);
105     glutDisplayFunc(Display);
106     glutIdleFunc(Idle);
107     glutMainLoop();
108     return 0;
109 }

 

openGL+VS2010的例程--太阳地球月球运动模型(三维)

标签:

原文地址:http://www.cnblogs.com/yuwl26/p/4218892.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!