找回密码
 立即注册
收起左侧

Qt中配置glut库 ( Windows )

0
回复
9459
查看
[复制链接]
累计签到:43 天
连续签到:1 天
来源: 转载 2014-5-31 09:36:42 显示全部楼层 |阅读模式
本帖最后由 BruceLi 于 2014-5-31 10:10 编辑

最近开始学习qt OpenGL模块,按照网上教程Qt下的OpenGL 编程(8)文字、FPS、动画
发现Qt没有glut工具库,所以记录一下如何解决的!!

Qt本身不包括glut工具库,如果要使用glut库,该怎么做呢?
下面来简述一下Qt下怎么安装glut库:
1.首先需要去opengl的官网下载glut库:

2.解压后,将glut32.libglut.lib两个文件拷贝到qt目录下的./lib文件夹中;

3.glut.dllglut32.dll两个动态链接库拷贝到C:\windows\system32中;


4.glut.h文件拷贝到qt目录下的\include\QtOpenGL中,并建立glut文件
【内容写上#include "glut.h",保存为没有后缀名的文件;

5.切换到自己的程序中,在 **.pro 文件中添加:
   LIBS += -lgut32
   LIBS += -LC:\glut
6. main.cpp中加入“#include<glut>”或者“#include<glut.h>,这样就可以使用glut中的函数了。

参考链接:
http://www.cnblogs.com/BlueSky2012/articles/2507107.html

补充:
教程中有一行代码,貌似可以用其他替代!!
float currentTime =  glutGet(GLUT_ELAPSED_TIME)* 0.001f;//程序运行的时间
float currentTime = GetTickCount()* 0.001f;

后来发现:
        电脑卡了以后,FPS的数值只有30多,才明白帧的原理!!
附上源代码
  1. widget.h
  2. #ifndef WIDGET_H
  3. #define WIDGET_H

  4. //#include <QWidget>
  5. #include <QGLWidget>

  6. namespace Ui {
  7. class Widget;
  8. }

  9. class Widget : public QGLWidget
  10. {
  11.     Q_OBJECT
  12.    
  13. public:
  14.     explicit Widget(QWidget *parent = 0);
  15.     ~Widget();
  16.     void paintGL();
  17.     void initializeGL();
  18.     void resizeGL(int w, int h);
  19.     void timerEvent(QTimerEvent *event);
  20.     void calFrequency();
  21.     float colorSpan;</font>
  22.    
  23. private:
  24.     Ui::Widget *ui;
  25. };

  26. #endif // WIDGET_H
复制代码
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <GL/glu.h>
  4. //#include <GL/glext.h>
  5. //#include <glut.h>

  6. Widget::Widget(QWidget *parent) :
  7.     ui(new Ui::Widget)
  8. {
  9.     Q_UNUSED(parent);
  10.     ui->setupUi(this);
  11.     colorSpan = 0.0;
  12.     startTimer(20);
  13. }

  14. Widget::~Widget()
  15. {
  16.     delete ui;
  17. }

  18. void Widget::paintGL()
  19. {
  20.     GetTickCount();
  21.     QFont font("Times",20,QFont::Bold);
  22.     if(colorSpan<0.9&&colorSpan>=0.0)
  23.         colorSpan+=0.01;
  24.     else
  25.         colorSpan=0.0;
  26.     // 清除屏幕和深度缓存
  27.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  28.     glLoadIdentity();
  29.     //移到屏幕的左半部分,并且将视图推入屏幕背后足够的距离以便我们可以看见全部的场景
  30.     glTranslatef(0.0f,0.0f,-5);
  31.     glBegin(GL_TRIANGLES);                                                        // 绘制三角形
  32.         glColor3f(1.0f,0.0f+colorSpan,0.0f);
  33.         glVertex3f( 0.0f, 1.0f, 0.0f);                                        // 上顶点
  34.         glColor3f(0.0f,1.0f-colorSpan,0.0f);
  35.         glVertex3f(-1.0f,-1.0f, 0.0f);                                        // 左下
  36.         glColor3f(0.0f+colorSpan,0.0f,1.0f);
  37.         glVertex3f( 1.0f,-1.0f, 0.0f);                                        // 右下
  38.     glEnd();
  39.     renderText(100,100,"HelloWorld!  ",font);

  40.     //fps的字体颜色
  41.     glColor3f(0.0f,0.0f,1.0f);
  42.     glFinish();
  43.     //计算FPS
  44.     calFrequency();
  45. }

  46. void Widget::initializeGL()
  47. {
  48.     glShadeModel( GL_SMOOTH );
  49.     glClearColor( 0.0, 0.0, 0.0, 0.0 );
  50.     glClearDepth( 1.0 );
  51.     glEnable( GL_DEPTH_TEST );
  52.     glDepthFunc( GL_LEQUAL );
  53.     glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
  54. }

  55. void Widget::resizeGL(int width, int height)
  56. {
  57.     if ( height == 0 )
  58.     {
  59.       height = 1;
  60.     }
  61.     glViewport( 0, 0, (GLint)width, (GLint)height );
  62.     glMatrixMode( GL_PROJECTION );
  63.     glLoadIdentity();
  64.     gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
  65.     glMatrixMode( GL_MODELVIEW );
  66.     glLoadIdentity();
  67. }

  68. void Widget::calFrequency()
  69. {
  70.     QFont fpsFont("Times",20,QFont::Bold);
  71.     static  QString tmp="";
  72.     static float framesPerSecond=0.0f;      //fps的数值
  73.     static float frames    = 0.0f;          // 用于存储渲染的帧数
  74.     static float lastTime   = 0.0f;         // 前一秒的时刻
  75. //    float currentTime =  glutGet(GLUT_ELAPSED_TIME)* 0.001f;//程序运行的时间
  76.     float currentTime = GetTickCount()* 0.001f;
  77.     ++frames;
  78.     if( currentTime - lastTime > 1.0f )     //每秒刷新一次
  79.     {
  80.        framesPerSecond=frames;
  81.        tmp.setNum(framesPerSecond);
  82.        lastTime = currentTime;
  83.        frames= 0;
  84.     }
  85.     renderText(50,150,"FPS: "+tmp,fpsFont);//最终结果在窗口中渲染
  86. }

  87. void Widget::timerEvent(QTimerEvent *event)
  88. {
  89.     Q_UNUSED(event);
  90.     updateGL();
  91. }
复制代码



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了