700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > OpenGL模拟太阳地球月亮系统

OpenGL模拟太阳地球月亮系统

时间:2019-04-08 00:39:21

相关推荐

OpenGL模拟太阳地球月亮系统

我想说这是我们的结课作业,做完感觉不错哦,发上来与大家分享分享。

我编写时使用的是visual studio ,用的vc++。

这次编写使用了多文件管理。

控制功能:

1、a和d控制地球月亮一起转动

2、s和w是缩放,按太多图形会跑到视景体外,就看不到咯

3、z和x控制月亮单独绕地球转

4、空格按一下自动旋转,再按一下停止

5、鼠标可拖动控制

一、创建一个解决方案 solarSystem

二、画球的静态库

现在opengl教学基本都使用glut库,里面就有画球的函数,现成的,不过还是自己手写了一个,不是特别好。

1、在solarSystem中创建静态库项目 mysphere

2、在静态库中创建mySphere.h的头文件

#pragma oncevoid mySphere(double radius,int slices,int stacks);

3、在静态库中创建mySphere.cpp文件

#include "stdafx.h"#include<cmath>#include<glut.h>#include<cstring>#define pi 3.141592653void mySphere(double radius,int slices,int stacks){glPolygonMode(GL_FRONT_AND_BACK,GL_LINE);double a[1000],b[1000],c[1000],e[1000],f[1000],g[1000];memset(a,0,sizeof(a));memset(b,0,sizeof(b));memset(c,radius,sizeof(c));for(int x=0;2*x<=stacks;x++){double xx=x;double rr=sin(pi/2.0-xx*pi/stacks)*radius;for(int i=0;i<=slices;i++)g[i]=cos(pi/2.0-xx*pi/stacks)*radius;for(int y=0;y<=slices;y++){e[y]=cos((double)y*2*pi/(double)slices)*rr;f[y]=sin((double)y*2*pi/(double)slices)*rr;}glBegin(GL_QUAD_STRIP);for(int y=0;y<slices;y++){glVertex3d(e[y],f[y],g[y]);glVertex3d(a[y],b[y],c[y]);}glVertex3d(e[0],f[0],g[0]);glVertex3d(a[0],b[0],c[0]);glEnd();glBegin(GL_QUAD_STRIP);for(int y=0;y<slices;y++){glVertex3d(e[y],f[y],-g[y]);glVertex3d(a[y],b[y],-c[y]);}glVertex3d(e[0],f[0],-g[0]);glVertex3d(a[0],b[0],-c[0]);glEnd();for(int i=0;i<=slices;i++){a[i]=e[i];b[i]=f[i];c[i]=g[i];}}}

三、solarSystem的主体项目

创建了项目solarSystem后,在里面分别创建一下文件

1、创建主函数文件solarSystem.cpp

#include "stdafx.h"#include "mySphere.h"#include<glut.h>#include<math.h>#define pi 3.141592653int rotate=0;float translate=4.5;float eangle=0;float mangle=0;bool autorotate=0;int xmouse;void init(void);void sun(void);void earth(void);void moon(void);void display(void);void reshape(int w,int h);void keyboard(unsigned char key,int x,int y);void timer(int value);void mouse(int button,int state,int x,int y);void mousemove(int x,int y);int main(int argc,char** argv){glutInit(&argc,argv);glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);glutInitWindowSize(600,600);glutInitWindowPosition(100,100);glutCreateWindow("solarSystem");init();glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutMotionFunc(mousemove);glutMouseFunc(mouse);glutMainLoop();return 0;}

2、init.cpp

#include "stdafx.h"#include<glut.h>void init(void){glClearColor(0.0,0.0,0.0,0.0);glShadeModel(GL_FLAT);}

3、reshape.cpp

#include "stdafx.h"#include<glut.h>void reshape(int w,int h){glViewport(0,0,(GLsizei)w,(GLsizei)h);glMatrixMode(GL_PROJECTION);glLoadIdentity();glFrustum(-2.0,2.0,-2.0,2.0,1.0,10.0);//视景体glMatrixMode(GL_MODELVIEW);glLoadIdentity();}

4、display.cpp

#include "stdafx.h"#include<glut.h>void sun(void);void earth(void);void moon(void);void display(void){glClear(GL_COLOR_BUFFER_BIT);sun();earth();moon();glFlush();}

5、sun.cpp

#include "stdafx.h"#include "mySphere.h"#include<glut.h>#include<math.h>#define pi 3.141592653extern int rotate;extern float translate;extern float eangle;extern float mangle;void sun(void){glColor3f(1.0,0.0,0.0);//绘制颜色glPushMatrix();glTranslatef(0.0,0.0,-translate);glRotatef(0,0.0,0.0,0.0);mySphere(1.0,15,15);glPopMatrix();}

6、earth.cpp

#include "stdafx.h"#include "mySphere.h"#include<glut.h>#include<math.h>#define pi 3.141592653extern int rotate;extern float translate;extern float eangle;extern float mangle;void earth(void){glColor3f(0.0,1.0,0.0);//绘制颜色glPushMatrix();glTranslatef(3*cos(eangle*pi/360.0),0.0,3*sin(eangle*pi/360.0)-translate);glRotatef(2*rotate,0.0,1.0,0.0);mySphere(0.5,15,15);glPopMatrix();}

7、moon.cpp

#include "stdafx.h"#include "mySphere.h"#include<glut.h>#include<math.h>#define pi 3.141592653extern int rotate;extern float translate;extern float eangle;extern float mangle;void moon(void){glColor3f(1.0,1.0,0.0);//绘制颜色glPushMatrix();glTranslatef(3*cos(eangle*pi/360.0)+cos(mangle*pi/360.0),0.0,3*sin(eangle*pi/360.0)+sin(mangle*pi/360.0)-translate);glRotatef(rotate,0.0,1.0,0.0);mySphere(0.3,15,15);glPopMatrix();}

8、keyboard.cpp

#include "stdafx.h"#include "mySphere.h"#include<glut.h>#include<math.h>#define pi 3.141592653extern int rotate;extern float translate;extern float eangle;extern float mangle;extern bool autorotate;void timer(int value);void keyboard(unsigned char key,int x,int y){switch(key){case 'a':rotate=(rotate+10)%360;eangle+=3;mangle+=20;glutPostRedisplay();break;case 'd':rotate=(rotate-10)%360;eangle-=3;mangle-=20;glutPostRedisplay();break;case 's':translate-=0.25;glutPostRedisplay();break;case 'w':translate+=0.25;glutPostRedisplay();break;case 'z'://rotate=(rotate+10)%360;mangle+=20;glutPostRedisplay();break;case 'x'://rotate=(rotate-10)%360;mangle-=20;glutPostRedisplay();break;case ' ':if(autorotate==0)autorotate=1;else autorotate=0;if(autorotate)glutTimerFunc(20,timer,1);break;default:break;}}

9、mouse.cpp

#include "stdafx.h"#include "mySphere.h"#include<glut.h>#include<math.h>#define pi 3.141592653extern int rotate;extern float translate;extern float eangle;extern float mangle;extern int xmouse;void mouse(int button,int state,int x,int y){if(button==GLUT_LEFT_BUTTON&&state==GLUT_DOWN){xmouse=x;glutPostRedisplay();}}

10、mousemove.cpp

#include "stdafx.h"#include "mySphere.h"#include<glut.h>#include<math.h>#define pi 3.141592653extern int rotate;extern float translate;extern float eangle;extern float mangle;extern int xmouse;void mousemove(int x,int y){x-=xmouse;rotate-=(float)x;eangle-=3.0*(float)x/10;mangle-=(float)x*2.0;xmouse+=x;glutPostRedisplay();}

11、timer.cpp

#include "stdafx.h"#include "mySphere.h"#include<glut.h>#include<math.h>#define pi 3.141592653extern int rotate;extern float translate;extern float eangle;extern float mangle;extern bool autorotate;void timer(int value){rotate=(rotate-10)%360;eangle-=3;mangle-=20;glutPostRedisplay();if(autorotate)glutTimerFunc(20,timer,1);}

四、环境配置

按照自己的习惯写的,不对的地方见谅

1、依赖项目:在菜单栏,找到项目,找到项目依赖项,将solarSystem依赖于mySphere,solarSystem的属性中c/c++常规项附加包含目录填..\mySphere

2、glut库配置:将glut的三个文件放入glut文件夹中,文件夹再放入解决方案的文件夹中,然后在编译器里,将mySphere和solarSystem的属性中c/c++常规项附加包含目录填..\glut,solarSystem的属性中连接器常规的附加库目录填写..\glut,mySphere的属性管理员常规的附加库目录填写..\glut

3、dll文件配置:编译后将glut32.dll放入Debug文件夹中即可

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