本帖最后由 BruceLi 于 2014-5-31 10:10 编辑
最近开始学习qt OpenGL模块,按照网上教程Qt下的OpenGL 编程(8)文字、FPS、动画
发现Qt没有glut工具库,所以记录一下如何解决的!!
Qt本身不包括glut工具库,如果要使用glut库,该怎么做呢? 下面来简述一下Qt下怎么安装glut库: 1.首先需要去opengl的官网下载glut库:
2.解压后,将glut32.lib和glut.lib两个文件拷贝到qt目录下的./lib文件夹中;
3.将glut.dll和glut32.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多,才明白帧的原理!!
附上源代码- widget.h
- #ifndef WIDGET_H
- #define WIDGET_H
- //#include <QWidget>
- #include <QGLWidget>
- namespace Ui {
- class Widget;
- }
- class Widget : public QGLWidget
- {
- Q_OBJECT
-
- public:
- explicit Widget(QWidget *parent = 0);
- ~Widget();
- void paintGL();
- void initializeGL();
- void resizeGL(int w, int h);
- void timerEvent(QTimerEvent *event);
- void calFrequency();
- float colorSpan;</font>
-
- private:
- Ui::Widget *ui;
- };
- #endif // WIDGET_H
复制代码- #include "widget.h"
- #include "ui_widget.h"
- #include <GL/glu.h>
- //#include <GL/glext.h>
- //#include <glut.h>
- Widget::Widget(QWidget *parent) :
- ui(new Ui::Widget)
- {
- Q_UNUSED(parent);
- ui->setupUi(this);
- colorSpan = 0.0;
- startTimer(20);
- }
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::paintGL()
- {
- GetTickCount();
- QFont font("Times",20,QFont::Bold);
- if(colorSpan<0.9&&colorSpan>=0.0)
- colorSpan+=0.01;
- else
- colorSpan=0.0;
- // 清除屏幕和深度缓存
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
- glLoadIdentity();
- //移到屏幕的左半部分,并且将视图推入屏幕背后足够的距离以便我们可以看见全部的场景
- glTranslatef(0.0f,0.0f,-5);
- glBegin(GL_TRIANGLES); // 绘制三角形
- glColor3f(1.0f,0.0f+colorSpan,0.0f);
- glVertex3f( 0.0f, 1.0f, 0.0f); // 上顶点
- glColor3f(0.0f,1.0f-colorSpan,0.0f);
- glVertex3f(-1.0f,-1.0f, 0.0f); // 左下
- glColor3f(0.0f+colorSpan,0.0f,1.0f);
- glVertex3f( 1.0f,-1.0f, 0.0f); // 右下
- glEnd();
- renderText(100,100,"HelloWorld! ",font);
- //fps的字体颜色
- glColor3f(0.0f,0.0f,1.0f);
- glFinish();
- //计算FPS
- calFrequency();
- }
- void Widget::initializeGL()
- {
- glShadeModel( GL_SMOOTH );
- glClearColor( 0.0, 0.0, 0.0, 0.0 );
- glClearDepth( 1.0 );
- glEnable( GL_DEPTH_TEST );
- glDepthFunc( GL_LEQUAL );
- glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
- }
- void Widget::resizeGL(int width, int height)
- {
- if ( height == 0 )
- {
- height = 1;
- }
- glViewport( 0, 0, (GLint)width, (GLint)height );
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluPerspective( 45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- }
- void Widget::calFrequency()
- {
- QFont fpsFont("Times",20,QFont::Bold);
- static QString tmp="";
- static float framesPerSecond=0.0f; //fps的数值
- static float frames = 0.0f; // 用于存储渲染的帧数
- static float lastTime = 0.0f; // 前一秒的时刻
- // float currentTime = glutGet(GLUT_ELAPSED_TIME)* 0.001f;//程序运行的时间
- float currentTime = GetTickCount()* 0.001f;
- ++frames;
- if( currentTime - lastTime > 1.0f ) //每秒刷新一次
- {
- framesPerSecond=frames;
- tmp.setNum(framesPerSecond);
- lastTime = currentTime;
- frames= 0;
- }
- renderText(50,150,"FPS: "+tmp,fpsFont);//最终结果在窗口中渲染
- }
- void Widget::timerEvent(QTimerEvent *event)
- {
- Q_UNUSED(event);
- updateGL();
- }
复制代码
|