700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > openGL+VS的例程--太阳地球月球运动模型(三维)

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

时间:2019-09-06 05:27:04

相关推荐

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

效果图如上:

步骤:略

实现代码如下:

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 { 22glPushMatrix(); 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 51glPopMatrix(); 52 } 53 54 void Init(void) 55 { 56glClearColor(0.0,0.0,0.0,0.0); 57glClearDepth(10.0); 58glMatrixMode(GL_MODELVIEW); 59glLoadIdentity(); 60 } 61 62 void Display(void) 63 { 64glClear(GL_COLOR_BUFFER_BIT); 65RenderScene(); 66glFlush(); 67glutSwapBuffers(); 68 } 69 70 void Reshape(int x, int y) 71 { 72if (y == 0) return; 73glMatrixMode(GL_PROJECTION); 74glLoadIdentity(); 75gluPerspective(40.0,(GLdouble)x/(GLdouble)y,0.5,20.0); 76glMatrixMode(GL_MODELVIEW); 77glViewport(0,0,x,y); 78Display(); 79 } 80 static long long times = 0; 81 void Idle(void) 82 { 83times++; 84if(times>1000000) 85 times =0; 86if(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 { 99glutInit(&argc, argv);100glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);101glutInitWindowSize(600,600);102glutCreateWindow(argv[0]);103Init();104glutReshapeFunc(Reshape);105glutDisplayFunc(Display);106glutIdleFunc(Idle);107glutMainLoop();108return 0;109 }

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。