原地址:http://blog.chinaunix.net/uid-25799257-id-3498005.html
之前一直做地图的算法,没什么时间学习opengl,之前看nehe_OpenGL.chm和Qt+OpenGL教程_for_Qt_4.5.3.doc(感谢分享的网友),看了一些,大概入门了算是;因为公司的opengl代码是高手写的,我们呢??只是调用接口,呵呵,做算法,不过了,还是慢慢的学习了很多东西;
这里还是把之前从文档里面摘取除了的qt的opengl框架留着,想用的时候方便,省的每次敲,作为学习模版还是不错的。
Qt安装好后,基本上opengl可以使用了,不过需要在工程配置文件中加入配置选项;然后基本的.c、.h文件,差不多了!!!
工程配置文件:(.pro)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#------------------------------------------------- # # Project created by QtCreator 2012-12-29T12:00:04 # #------------------------------------------------- QT += core gui QT += opengl //OPENGL库支持
QT += qt3support //支持qt3支持
TARGET = SecondOpenggl TEMPLATE = app SOURCES += main.cpp\ widget.cpp
HEADERS += widget.h FORMS += widget.ui |
.h文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
#ifndef WIDGET_H #define WIDGET_H #include <qgl.h> namespace Ui {
class Widget;
} class Widget : public QGLWidget
{ Q_OBJECT
public :
explicit Widget(QGLWidget *parent = 0, const char * name = 0, bool fs = false );
~Widget();
protected :
void initializeGL(); //glpt初始化
void paintGL(); //绘制纹理
void resizeGL( int width, int height); //调整宽高
protected :
bool fullscreen; //是否全屏
private :
Ui::Widget *ui;
}; #endif // WIDGET_H |
.c文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include "widget.h" #include "ui_widget.h" Widget::Widget(QGLWidget *parent, const char * name, bool fs) :
QGLWidget(parent, name),
ui( new Ui::Widget)
{ ui->setupUi( this );
fullscreen = fs;
} Widget::~Widget() { delete ui;
} void Widget::initializeGL() // 初始化OpenGL
{ /********************用户自定义的初始化过程*******************/
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // 设置颜色缓冲
glClearDepth(1.0f); // 设置深度缓冲
glDepthFunc(GL_LEQUAL); // 指定深度缓冲比较值(如果输入的深度值小于或等于参考值,则通过)
glEnable(GL_DEPTH_TEST); // 启用深度缓冲
glShadeModel(GL_SMOOTH); // 指定两点间其他点颜色的过渡模式(GL_SMOOTH会出现过渡效果,GL_FLAT 则只是以指定的某一点的单一色绘制其他所有点)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 指定所控制行为的符号常量
/*********************************************************/
return ;
} void Widget::paintGL()
{ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除颜色缓冲和深度缓冲
glLoadIdentity(); // 重置模型观察矩阵
///////////////////在此处添加绘制代码////////////////
glTranslatef( -1.5, 0.0, -6.0); // 相对与当前所在的屏幕位置进行移动
///////////////////在此处添加绘制代码////////////////
} void Widget::resizeGL( int width, int height) // 窗口大小改变时重新调用
{ if (0 == height)
{
height = 1;
}
if (0 == width)
{
width = 0;
}
glViewport(0, 0, (GLint)width, (GLint)height); // 重置当前的视口(Viewport)
glMatrixMode(GL_PROJECTION); // 选择投影矩阵
glLoadIdentity(); // 重置投影矩阵
gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 0.1, 100.0); // 建立透视投影矩阵
glMatrixMode(GL_MODELVIEW); // 选择模型观察矩阵
glLoadIdentity(); // 重置模型观察矩阵
} |
main.c文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
#include <QtGui/QApplication> #include <qicon.h> #include "widget.h" int main( int argc, char *argv[])
{ QApplication a(argc, argv);
a.setWindowIcon(QIcon( "dog.bmp" ));
Widget w(0, 0, false );
w.setWindowTitle( "YOU IS YOU" );
w.show();
return a.exec();
}
|