Qt下的OpenGL 编程(10)Solar System

一、提要
    今天的内容是OpenGL的编程实践—太阳系的模拟!
     红宝书上有相应的教程,但这里我们要实现得更全面一些。iPad上有一个很棒的应用,名字叫Solar System,我们尽量去达到它的效果。
    先来看一下最终效果:
    



 


 
思路:
     建立9个球体,分别赋予不同的材质,再通过动画不断变换它们的位置,就可以实现模拟了。

 
二、有关太阳系的知识
     太阳系有一颗恒星:太阳,8颗行星:水,金,地,火,木,土,天王,海王。9颗星球的位置如下:



 
 
    然后去搜集了解一下各个星球的自转和公转周期,脑袋里面有个概念。
    接着可以去找一些星球贴图的素材了,我在网上找到了一些资源,太阳的贴图是自己用ps绘制的,然后把它们通通添加到资源文件中,就像这样:
    

 
三、程序结构
    相比与之前的框架,这里添加了一个星球类,文件结构如下:
     程序还是比较清晰,直接贴代码了:
     
 
 
  1. /* 
  2. ----------------------------------------------------------------------------- 
  3. Filename:   star.h 
  4. ----------------------------------------------------------------------------- 
  5. //星球类 
  6. ----------------------------------------------------------------------------- 
  7.   */  
  8. #ifndef STAR_H  
  9. #define STAR_H  
  10. #include <QtOpenGL>  
  11. #include <GL/glut.h>  
  12. class Star  
  13. {  
  14. public:  
  15.     Star();  
  16.     Star(int tex,GLfloat r,GLfloat x,GLfloat y,GLfloat revS,GLfloat rotS,GLfloat a[], GLfloat d[], GLfloat p[],GLfloat s);  
  17.     ~Star();  
  18.     //公转  
  19.     void revolute();  
  20.     //自转  
  21.     void rotate();  
  22.     //星球半径  
  23.     float radious;  
  24.     //星球位置  
  25.     GLfloat disX;  
  26.     GLfloat disY;  
  27.     //纹理id  
  28.     int texId;  
  29.     //环境反射光  
  30.     GLfloat *ambient;  
  31.     //漫反射  
  32.     GLfloat *diffuse;  
  33.     //镜面反射  
  34.     GLfloat *specular;  
  35.     //镜面反射强度  
  36.     GLfloat shinniness;  
  37.     //公转速度  
  38.     GLfloat revSpeed;  
  39.     //自转速度  
  40.     GLfloat rotSpeed;  
  41.     //公转角度  
  42.     float revAngle;  
  43.     //自转角度  
  44.     float rotAngle;  
  45.     /*  //公转周期 
  46.     float revPeriod; 
  47.     //自转周期 
  48.     float rotPeriod; 
  49.      
  50.      
  51.      
  52.     //纹理属性 
  53.     GLfloat WRAP_S; 
  54.     GLfloat WRAP_T; 
  55.     GLfloat MAG_FILTER; 
  56.     GLfloat MIN_FILTER; 
  57.     GLfloat ENV_MODE; 
  58.     */  
  59. };  
  60.   
  61. #endif // STAR_H  

 
 
  1. /* 
  2. ----------------------------------------------------------------------------- 
  3. Filename:   star.cpp 
  4. ----------------------------------------------------------------------------- 
  5. //星球类 
  6. ----------------------------------------------------------------------------- 
  7.   */  
  8. #include "star.h"  
  9.   
  10. Star::Star()  
  11. {  
  12. }  
  13. Star::Star(int tex, GLfloat r, GLfloat x, GLfloat y, GLfloat revS, GLfloat rotS, GLfloat a[], GLfloat d[], GLfloat p[], GLfloat s)  
  14. {  
  15.     this->texId=tex;  
  16.     this->radious=r;  
  17.     this->disX=x;  
  18.     this->disY=y;  
  19.     this->ambient=a;  
  20.     this->diffuse=d;  
  21.     this->specular=p;  
  22.     this->shinniness=s;  
  23.     this->revSpeed=revS;  
  24.     this->rotSpeed=rotS;  
  25.     this->revAngle=0.0;  
  26.     this->rotAngle=0.0;  
  27.       
  28. }  
  29. Star::~Star()  
  30. {}  
  31. void Star::revolute()  
  32. {  
  33.     this->revAngle= this->revAngle + this->revSpeed< 360 ? this->revAngle + this->revSpeed: 0;  
  34. }  
  35. void Star::rotate()  
  36. {  
  37.     this->rotAngle= this->rotAngle + this->rotSpeed< 360 ? this->rotAngle + this->rotSpeed: 0;  
  38. }  
  1. /* 
  2. ----------------------------------------------------------------------------- 
  3. Filename:   nehewidget.h 
  4. ----------------------------------------------------------------------------- 
  5. //opengl渲染窗口类 
  6. ----------------------------------------------------------------------------- 
  7.   */  
  8. #ifndef NEHEWIDGET_H  
  9. #define NEHEWIDGET_H  
  10.   
  11. #include <QGLWidget>  
  12. #include <QtGui>  
  13. #include <QtOpenGL>  
  14. #include <QtCore>  
  15. #include <GL/glut.h>  
  16. #include<iostream>  
  17. #include "star.h"  
  18. #define PI 3.14159265  
  19. class NeHeWidget : public QGLWidget  
  20. {  
  21.     Q_OBJECT  
  22. public:  
  23.     explicit NeHeWidget(QWidget *parent = 0);  
  24.     ~NeHeWidget();  
  25.     void zoomOut();  
  26.     void zoomIn();  
  27.     void enableBlend();  
  28.     void disableBLend();  
  29.     void calFrequency();  
  30.     void speedUp();  
  31.     void speedDown();  
  32.     void eyeXup();  
  33.     void eyeXdown();  
  34.     void eyeZup();  
  35.     void eyeZdown();  
  36. protected:  
  37.     //设置渲染环境  
  38.     void initializeGL();  
  39.     //绘制窗口  
  40.     void paintGL();  
  41.     //响应窗口的大小变化  
  42.     void resizeGL( int width, int height );  
  43.     //加载纹理  
  44.     void loadGLTextures(QString filename,int id);  
  45.     //绘制星球  
  46.     void drawStar(Star *s);  
  47.     //材质设置  
  48.     void setMaterial(Star *s);  
  49.     //正方体在三个方向上的旋转  
  50.       
  51.     QFont fpsFont;  
  52.     GLfloat xRot, yRot, zRot;  
  53.     //纹理存储数组  
  54.     GLuint texture[13];  
  55.     //场景深入屏幕的距离  
  56.     GLfloat zoom;  
  57.     //立方体在X轴和Y轴上旋转的速度  
  58.     GLfloat xSpeed, ySpeed;  
  59.     //计时器,实现动画  
  60.     QTimer *timer;  
  61.     //帧刷新时间  
  62.     int fpsSpan;  
  63.     GLfloat colorSpan;  
  64.     GLUquadricObj *mySphere;  
  65.       
  66.     Star *sky;  
  67.     Star *sun;  
  68.     Star *mercury;  
  69.     Star *venus;  
  70.     Star *earth;  
  71.     Star *mars;  
  72.     Star *jupiter;  
  73.     Star *saturn;  
  74.       
  75.     GLfloat eyeX;  
  76.     GLfloat eyeY;  
  77.     GLfloat eyeZ;  
  78.       
  79. };  
  80.   
  81. #endif // NEHEWIDGET_H  

 
 
  1. /* 
  2. ----------------------------------------------------------------------------- 
  3. Filename:   nehewidget.cpp 
  4. ----------------------------------------------------------------------------- 
  5. //opengl渲染窗口类 
  6. ----------------------------------------------------------------------------- 
  7.   */  
  8. #include "nehewidget.h"  
  9.   
  10. NeHeWidget::NeHeWidget(QWidget *parent) :  
  11.     QGLWidget(parent)  
  12. {  
  13.     xRot = yRot = zRot = 0.0;  
  14.     zoom = -5.0;  
  15.     xSpeed = ySpeed = 0.0;  
  16.     fpsFont=QFont("Times", 20);  
  17.     colorSpan=0;  
  18.     fpsSpan=50;  
  19.     timer = new QTimer(this);  
  20.       
  21.     timer->start(fpsSpan);  
  22.     connect(timer,SIGNAL(timeout()),this,SLOT(updateGL()));  
  23.     mySphere=gluNewQuadric();  
  24.       
  25.     eyeX=0.0;  
  26.     eyeY=0.0;  
  27.     eyeZ=190.0;  
  28.     //夜空参数设置  
  29.     GLfloat  sky_ambient[]={0.0,0.0,0.0,1.0};  
  30.     GLfloat  sky_diffuse[]={0.0,0.0,0.0,1.0};  
  31.     GLfloat  sky_specular[]={0.0,0.0,0.0,1.0};  
  32.     GLfloat  sky_shininess=0.0;  
  33.     GLfloat  sky_radious=290.0;  
  34.     // GLfloat sky_rotSpeed= (GLfloat)360/58/100;  
  35.     sky=new Star(0,sky_radious,0,0,0,0,sky_ambient,sky_diffuse,sky_specular,sky_shininess);  
  36.       
  37.     //太阳参数设置  
  38.     GLfloat  sun_ambient[]={0.0,0.0,0.0,1.0};  
  39.     GLfloat  sun_diffuse[]={0.0,0.0,0.0,1.0};  
  40.     GLfloat  sun_specular[]={0.0,0.0,0.0,1.0};  
  41.     GLfloat  sun_shininess=20.0;  
  42.     GLfloat  sun_radious=10.0;  
  43.     GLfloat sun_rotSpeed= (GLfloat)360/58/100;  
  44.     sun=new Star(1,sun_radious,0,0,0,sun_rotSpeed,sun_ambient,sun_diffuse,sun_specular,sun_shininess);  
  45.       
  46.     //水星  
  47.     GLfloat  mercury_ambient[]={0.0,0.0,0.0,1.0};  
  48.     GLfloat  mercury_diffuse[]={0.5,0.5,0.5,1.0};  
  49.     GLfloat  mercury_specular[]={0.0,0.0,0.0,1.0};  
  50.     GLfloat  mercury_shininess=20.0;  
  51.     GLfloat  mercury_radious=0.7;  
  52.     GLfloat mecury_revSpeed=(GLfloat)360/88;  
  53.     GLfloat mecury_rotSpeed= (GLfloat)360/58/100;  
  54.     mercury=new Star(2,mercury_radious,15.2,0,mecury_revSpeed,mecury_rotSpeed,mercury_ambient,mercury_diffuse,mercury_specular,mercury_shininess);  
  55.       
  56.     //金星  
  57.     GLfloat  venus_ambient[]={0.0,0.0,0.0,1.0};  
  58.     GLfloat  venus_diffuse[]={0.8,0.8,0.8,1.0};  
  59.     GLfloat  venus_specular[]={0.0,0.0,0.0,1.0};  
  60.     GLfloat  venus_shininess=20.0;  
  61.     GLfloat  venus_radious=1.24;  
  62.     GLfloat venus_revSpeed=(GLfloat)360/224;  
  63.     GLfloat venus_rotSpeed= (GLfloat)360/243/100;  
  64.     venus=new Star(3,venus_radious,19.2,0,venus_revSpeed,venus_rotSpeed,venus_ambient,venus_diffuse,venus_specular,venus_shininess);  
  65.       
  66.     //地球  
  67.     GLfloat  earth_ambient[]={0.1,0.1,0.1,1.0};  
  68.     GLfloat  earth_diffuse[]={0.4,0.4,0.8,1.0};  
  69.     GLfloat  earth_specular[]={0.0,0.0,0.0,1.0};  
  70.     GLfloat  earth_shininess=20.0;  
  71.     GLfloat  earth_radious=1.24;  
  72.     GLfloat earth_revSpeed=(GLfloat)360/365;  
  73.     GLfloat earth_rotSpeed= (GLfloat)360/1/100;  
  74.     earth=new Star(4,earth_radious,26,0,earth_revSpeed,earth_rotSpeed,earth_ambient,earth_diffuse,earth_specular,earth_shininess);  
  75.       
  76.     //火星  
  77.     GLfloat  mars_ambient[]={0.1,0.1,0.1,1.0};  
  78.     GLfloat  mars_diffuse[]={0.6, 0.6, 0.6, 1.0};  
  79.     GLfloat  mars_specular[]={0.0,0.0,0.0,1.0};  
  80.     GLfloat  mars_shininess=20.0;  
  81.     GLfloat  mars_radious=1.0;  
  82.     GLfloat mars_revSpeed=(GLfloat)360/687;  
  83.     GLfloat mars_rotSpeed= (GLfloat)360/1/100;  
  84.     mars=new Star(5,mars_radious,31,0,mars_revSpeed,mars_rotSpeed,mars_ambient,mars_diffuse,mars_specular,mars_shininess);  
  85.       
  86.     //木星  
  87.     GLfloat  jupiter_ambient[]={0.0, 0.0, 0.0,1.0};  
  88.     GLfloat  jupiter_diffuse[]={0.6, 0.6, 0.6, 1.0};  
  89.     GLfloat  jupiter_specular[]={0.0,0.0,0.0,1.0};  
  90.     GLfloat  jupiter_shininess=20.0;  
  91.     GLfloat  jupiter_radious=4.0;  
  92.     GLfloat jupiter_revSpeed=(GLfloat)360/4329;  
  93.     GLfloat jupiter_rotSpeed= (GLfloat)360/0.3/100;  
  94.     jupiter=new Star(6,jupiter_radious,43,0,jupiter_revSpeed,jupiter_rotSpeed,jupiter_ambient,jupiter_diffuse,jupiter_specular,jupiter_shininess);  
  95.     //土星  
  96.     GLfloat  saturn_ambient[]={0.0, 0.0, 0.0,1.0};  
  97.     GLfloat  saturn_diffuse[]={0.6, 0.6, 0.6, 1.0};  
  98.     GLfloat  saturn_specular[]={0.0,0.0,0.0,1.0};  
  99.     GLfloat  saturn_shininess=20.0;  
  100.     GLfloat  saturn_radious=3.5;  
  101.     GLfloat saturn_revSpeed=(GLfloat)360/10768;  
  102.     GLfloat saturn_rotSpeed= (GLfloat)360/1.4/100;  
  103.     saturn=new Star(7,saturn_radious,56.5,0,saturn_revSpeed,saturn_rotSpeed,saturn_ambient,saturn_diffuse,saturn_specular,saturn_shininess);  
  104.       
  105.       
  106. }  
  107. NeHeWidget::~NeHeWidget()  
  108. {}  
  109. void NeHeWidget::loadGLTextures(QString filename, int id)  
  110. {  
  111.     QImage tex, buf;  
  112.     if ( !buf.load(filename ) )  
  113.     {  
  114.         //如果载入不成功,自动生成一个128*128的32位色的绿色图片。  
  115.         qWarning("Could not read image file!");  
  116.         QImage dummy( 128, 128,QImage::Format_RGB32 );  
  117.         dummy.fill( Qt::green );  
  118.         buf = dummy;  
  119.     }  
  120.     //转换成纹理类型  
  121.     tex = QGLWidget::convertToGLFormat( buf );  
  122.     //创建纹理  
  123.     glGenTextures( 1, &texture[id] );  
  124.     //使用来自位图数据生成的典型纹理,将纹理名字texture[0]绑定到纹理目标上  
  125.     glBindTexture( GL_TEXTURE_2D, texture[id] );  
  126.     glTexImage2D( GL_TEXTURE_2D, 0, 3, tex.width(), tex.height(), 0,  
  127.                   GL_RGBA, GL_UNSIGNED_BYTE, tex.bits() );  
  128.     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );  
  129.     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );  
  130. }  
  131. void NeHeWidget::drawStar(Star *s)  
  132. {  
  133.     glPushMatrix();  
  134.     //公转  
  135.     glRotatef(s->revAngle,0.0,0.0,1.0);  
  136.     glTranslatef(s->disX, s->disY, 0.0);  
  137.     //自转  
  138.     glRotatef(s->rotAngle,0.0,0.0,1.0);  
  139.     gluSphere(mySphere, s->radious, 32, 16);  
  140.     //设置材质属性  
  141.     glMaterialfv(GL_BACK, GL_AMBIENT, s->ambient);  
  142.     glMaterialfv(GL_BACK, GL_DIFFUSE, s->diffuse);  
  143.     glMaterialfv(GL_BACK, GL_SPECULAR, s->specular);  
  144.     glMaterialf(GL_BACK, GL_SHININESS, s->shinniness);  
  145.     //  
  146.     glPopMatrix();  
  147. }  
  148. void NeHeWidget::setMaterial(Star *s)  
  149. {  
  150.       
  151. }  
  152.   
  153. void NeHeWidget::initializeGL()  
  154. {  
  155.     //载入纹理  
  156.     loadGLTextures( ":/data/sun.jpg",sun->texId);  
  157.     loadGLTextures( ":/data/mercury.bmp",mercury->texId);  
  158.     loadGLTextures( ":/data/venus.jpg",venus->texId);  
  159.     loadGLTextures( ":/data/earth2.jpg",earth->texId);  
  160.     loadGLTextures( ":/data/mars.bmp",mars->texId);  
  161.     loadGLTextures( ":/data/saturn.jpg",saturn->texId);  
  162.     loadGLTextures( ":/data/jupiter.bmp",jupiter->texId);  
  163.     loadGLTextures( ":/data/sky.jpg",sky->texId);  
  164.     //loadGLTextures( ":/data/neptune.bmp",neptune->texId);  
  165.       
  166.     // 启用阴影平滑  
  167.     glShadeModel( GL_SMOOTH );  
  168.     // 黑色背景  
  169.     glClearColor( 0.0, 0.0, 0.0, 0.0 );  
  170.     // 设置深度缓存  
  171.     glClearDepth( 1.0 );  
  172.     // 启用深度测试  
  173.     glEnable( GL_DEPTH_TEST );  
  174.     //启用纹理  
  175.     glEnable( GL_TEXTURE_2D );  
  176.     // 所作深度测试的类型  
  177.     glDepthFunc( GL_LEQUAL );  
  178.     // 告诉系统对透视进行修正  
  179.     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );  
  180.     //    开启剔除操作效果  
  181.     //glEnable(GL_CULL_FACE);  
  182.     // 使用平滑法线  
  183.     gluQuadricNormals(mySphere, GL_SMOOTH);  
  184.     // 使用纹理  
  185.     gluQuadricTexture(mySphere, GL_TRUE);  
  186.     // 设置球纹理映射  
  187.       
  188. }  
  189. void NeHeWidget::paintGL()  
  190. {  
  191.       
  192.     // 清除屏幕和深度缓存  
  193.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );  
  194.     //glRotatef( yRot, 0.0,  0.0,  1.0 );  
  195.     glColor3f(1.0,1.0,1.0);  
  196.     glBindTexture(GL_TEXTURE_2D, texture[sky->texId]);  
  197.     drawStar(sky);  
  198.       
  199.     glBindTexture(GL_TEXTURE_2D, texture[sun->texId]);  
  200.     drawStar(sun);  
  201.       
  202.     glBindTexture(GL_TEXTURE_2D, texture[mercury->texId]);  
  203.     drawStar(mercury);  
  204.       
  205.     glBindTexture(GL_TEXTURE_2D, texture[venus->texId]);  
  206.     drawStar(venus);  
  207.       
  208.     glBindTexture(GL_TEXTURE_2D, texture[earth->texId]);  
  209.     drawStar(earth);  
  210.       
  211.     glBindTexture(GL_TEXTURE_2D, texture[mars->texId]);  
  212.     drawStar(mars);  
  213.       
  214.     glBindTexture(GL_TEXTURE_2D, texture[jupiter->texId]);  
  215.     drawStar(jupiter);  
  216.       
  217.     glBindTexture(GL_TEXTURE_2D, texture[saturn->texId]);  
  218.     drawStar(saturn);  
  219.       
  220.       
  221.     //旋转速度  
  222.     yRot += 0.4;  
  223.     sun->rotate();  
  224.     mercury->revolute();  
  225.     mercury->rotate();  
  226.     venus->revolute();  
  227.     venus->rotate();  
  228.     earth->revolute();  
  229.     earth->rotate();  
  230.     mars->revolute();  
  231.     mars->rotate();  
  232.     jupiter->revolute();  
  233.     jupiter->rotate();  
  234.     saturn->revolute();  
  235.     saturn->rotate();  
  236.     glLoadIdentity();  
  237.     gluLookAt (eyeX, eyeY, eyeZ, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0);  
  238.     // gluLookAt (80.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0);  
  239.     glFlush();  
  240.     //fps的字体颜色  
  241.     glColor3f(0.0f,0.0f,1.0f);  
  242.     //计算FPS  
  243.     calFrequency();  
  244.       
  245. }  
  246. // 重置OpenGL窗口大小  
  247. void NeHeWidget::resizeGL(int width, int height)  
  248. {  
  249.     // 防止窗口大小变为0  
  250.     if ( height == 0 )  
  251.     {  
  252.         height = 1;  
  253.     }  
  254.     // 重置当前的视口  
  255.     glViewport( 0, 0, (GLint)width, (GLint)height );  
  256.     // 选择投影矩阵  
  257.     glMatrixMode( GL_PROJECTION );  
  258.     // 重置投影矩阵  
  259.     glLoadIdentity();  
  260.     // 设置视口的大小  
  261.     gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 600.0 );  
  262.     // 选择模型观察矩阵  
  263.     glMatrixMode( GL_MODELVIEW );  
  264.     glLoadIdentity();  
  265. }  
  266. void NeHeWidget::speedUp()  
  267. {  
  268.     fpsSpan+=1;  
  269.     qDebug()<<fpsSpan;  
  270.     timer->setInterval(fpsSpan);  
  271.     //timer->start(fpsSpan);  
  272.     updateGL();  
  273. }  
  274. void NeHeWidget::speedDown()  
  275. {  
  276.     if(fpsSpan>1) fpsSpan-=1;  
  277.     else fpsSpan=1;  
  278.     qDebug()<<fpsSpan;  
  279.     timer->setInterval(fpsSpan);  
  280.     updateGL();  
  281. }  
  282. void NeHeWidget::eyeXup()  
  283. {  
  284.     eyeX+=1;  
  285. }  
  286. void NeHeWidget::eyeXdown()  
  287. {  
  288.     // if(eyeX>10) eyeX-=1;  
  289.     //else eyeX=10;  
  290.     eyeX-=1;  
  291. }  
  292. void NeHeWidget::eyeZup()  
  293. {  
  294.     eyeZ+=1;  
  295. }  
  296. void NeHeWidget::eyeZdown()  
  297. {  
  298.     // if(eyeX>10) eyeX-=1;  
  299.     //else eyeX=10;  
  300.     eyeZ-=1;  
  301. }  
  302. void NeHeWidget::zoomOut()  
  303. {  
  304.     zoom+= 0.2;  
  305.       
  306.     updateGL();  
  307. }  
  308. void NeHeWidget::zoomIn()  
  309. {  
  310.     zoom -= 0.2;  
  311.     updateGL();  
  312. }  
  313. void NeHeWidget::calFrequency()  
  314. {  
  315.     static  QString tmp="";  
  316.     static float framesPerSecond=0.0f;//fps的数值  
  317.     static float frames    = 0.0f;       // 用于存储渲染的帧数  
  318.     static float lastTime   = 0.0f;       // 前一秒的时刻  
  319.     float currentTime =  glutGet(GLUT_ELAPSED_TIME)* 0.001f;//程序运行的时间  
  320.     ++frames;  
  321.     if( currentTime - lastTime > 1.0f )//,每秒刷新一次  
  322.     {  
  323.         framesPerSecond=frames;  
  324.         tmp.setNum(framesPerSecond);  
  325.         lastTime = currentTime;  
  326.         frames= 0;  
  327.     }  
  328.     renderText(100,100,"FPS: "+tmp,fpsFont);//最终结果在窗口中渲染  
  329. }  

  1. /* 
  2. ----------------------------------------------------------------------------- 
  3. Filename:   mainwindow.h 
  4. ----------------------------------------------------------------------------- 
  5. //主窗口类 
  6. ----------------------------------------------------------------------------- 
  7.   */  
  8.   
  9. #ifndef MAINWINDOW_H  
  10. #define MAINWINDOW_H  
  11.   
  12. #include <QtGui/QMainWindow>  
  13. #include <QKeyEvent>  
  14. #include "nehewidget.h"  
  15. class MainWindow : public QMainWindow  
  16. {  
  17.     Q_OBJECT  
  18.       
  19. public:  
  20.     MainWindow(QWidget *parent = 0);  
  21.     ~MainWindow();  
  22. protected:  
  23.     bool fullscreen;  
  24.     //处理键盘事件  
  25.     void keyPressEvent( QKeyEvent *e );  
  26. private:  
  27.     NeHeWidget *neheWidget ;  
  28. };  
  29.   
  30. #endif // MAINWINDOW_H  


  1. /* 
  2. ----------------------------------------------------------------------------- 
  3. Filename:   mainwindow.cpp 
  4. ----------------------------------------------------------------------------- 
  5. //主窗口类 
  6. ----------------------------------------------------------------------------- 
  7.   */  
  8. #include "mainwindow.h"  
  9.   
  10. MainWindow::MainWindow(QWidget *parent)  
  11.     : QMainWindow(parent)  
  12. {  
  13.     neheWidget = new NeHeWidget();  
  14.     fullscreen = true;  
  15.     setGeometry(100,100,1000,768);  
  16.     setWindowTitle(tr("NeHe's OpenGL Framework"));  
  17.     setCentralWidget(neheWidget);  
  18. }  
  19.   
  20. MainWindow::~MainWindow()  
  21. {  
  22.       
  23. }  
  24. void MainWindow::keyPressEvent(QKeyEvent *e)  
  25. {  
  26.     switch ( e->key() )  
  27.     {  
  28.     case Qt::Key_F2:  
  29.         fullscreen = !fullscreen;  
  30.         if ( fullscreen )  
  31.         {  
  32.             showFullScreen();  
  33.         }  
  34.         else  
  35.         {  
  36.             showNormal();  
  37.         }  
  38.         neheWidget->updateGL();  
  39.         break;  
  40.     case Qt::Key_Escape:  
  41.         close();  
  42.         break;  
  43.     case Qt::Key_PageUp:  
  44.         neheWidget->zoomOut();  
  45.         break;  
  46.     case Qt::Key_PageDown:  
  47.         neheWidget->zoomIn();  
  48.         break;  
  49.     case Qt::Key_Down:  
  50.         neheWidget->speedUp();  
  51.         break;  
  52.     case Qt::Key_Up:  
  53.         neheWidget->speedDown();  
  54.         break;  
  55.     case Qt::Key_W:  
  56.         neheWidget->eyeXup();  
  57.         break;  
  58.     case Qt::Key_S:  
  59.         neheWidget->eyeXdown();  
  60.         break;  
  61.     case Qt::Key_E:  
  62.         neheWidget->eyeZup();  
  63.         break;  
  64.     case Qt::Key_D:  
  65.         neheWidget->eyeZdown();  
  66.         break;  
  67.     }  
  68. }  


  1. //main函数  
  2. #include <QtGui/QApplication>  
  3. #include "mainwindow.h"  
  4.   
  5. int main(int argc, char *argv[])  
  6. {  
  7.     QApplication a(argc, argv);  
  8.     MainWindow w;  
  9.     glutInit(&argc, argv);  
  10.     w.show();  
  11.       
  12.     return a.exec();  
  13. }  

四。程序中未完善的地方

这个程序应该只能算是一个大致的框架,还是有很多地方可以改进,比如:添加天王星,海王星,冥王星,添加每个星球的倾角,添加月球....

有兴趣的同学可以继续完善,我们可以继续讨论。

参考资料

1.      《 OpenGL Reference Manual 》, OpenGL 参考手册

2.      《 OpenGL 编程指南》(《 OpenGL Programming Guide 》), Dave Shreiner , Mason Woo , Jackie Neider , Tom Davis 著,徐波译,机械工业出版社

3.         《win32 OpenGL编程 》   一个大牛的博客     http://blog.csdn.net/vagrxie/article/category/628716/3
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值