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

效果图如上:

步骤:略

实现代码如下:

  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 }

 

转载于:https://www.cnblogs.com/yuwl26/p/4218892.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STM32F4X7是一款高性能的微控制器,支持uC/OS、FreeRTOS、LWIP、SSL和MQTT等多种工具。它通过使用MDK5开发工具,能够提供稳定可靠的开发环境。 W5500是一款硬件模块,是一种以太网控制器,用于实现物联网设备的网络连接。它能够与STM32F4X7微控制器结合使用,实现MQTT协议的通信。在这个例程中,W5500与STM32F4X7通过SPI接口进行通信,使用MQTT协议实现设备之间的数据传输。 在这个例程和说明中,首先需要配置W5500模块的网络参数,包括IP地址、子网掩码、网关和DNS服务器等。然后,通过MQTT协议连接到MQTT服务器。在连接成功后,可以通过MQTT协议发布和订阅主题,进行设备之间的数据交换。 MQTT是一种轻量级的消息队列传输协议,它具有低带宽和低功耗的特点,适用于物联网设备的通信。通过使用MQTT协议,设备可以以发布订阅的方式进行消息传递,实现设备之间的数据交互。 这个例程和说明提供了详细的配置和使用指南,帮助开发者快速上手使用W5500和MQTT协议。同时,MDK5开发工具提供了丰富的调试和分析功能,帮助开发者进行代码的验证和调试,保证系统稳定可靠。 总而言之,STM32F4X7与uC/OS、FreeRTOS、LWIP、SSL和MQTT等工具的结合使用,以及W5500和MQTT协议的应用,能够提供稳定可靠的物联网解决方案。通过使用MDK5开发工具,开发者可以快速开发和验证自己的物联网应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值