QtOpenGL示例openglwindow
基于qt5.7.1示例程序openglwindow的解读1
- 程序流程
复制代码
1
2
3
4
5
6
7
8
show()-->exposeEvent()-->run()-->postEvent()
-->event()-->run()-->postEvent()
-->event()-->run()-->postEvent()
-->event()-->run()-->postEvent()
-->event()-->run()-->postEvent()
注释: 每次窗口大小改变是都会产生exposeEvent信号,
绘画结束后通过postEvent产生一个event信号。
- pro文件
复制代码
1
2
3
4
5
6
7
8
9
10
11
QT -= core
QT += gui
CONFIG += c++11
CONFIG += console
SOURCES += main.cpp
openglwindow.cpp
HEADERS +=
openglwindowain.cpp
- main.cpp文件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "openglwindow.h"
#include <QtGui/QGuiApplication>
int main(int argc, char *argv[])
{
QGuiApplication a(argc, argv);
OpenGLWindow window;
window.resize(640, 480);
window.show();
// window.run(); // 不需要run,show()自动进入
return a.exec()
}
- openglwindow.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
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef OPENGLWINDOW_H
#define OPENGLWINDOW_H
#include <QtGui/QWindow>
#include <QtGui/QOpenGLFunctions>
QT_BEGIN_NAMESPACE
class QPainter;
class QOpenGLContext;
class QOpenGLPaintDevice;
class QOpenGLShaderProgram;
QT_END_NAMESPACE
class OpenGLWindow : public QWindow, protected QOpenGLFunctions
{
Q_OBJECT
public:
explicit OpenGLWindow(QWindow *parent = 0);
~OpenGLWindow();
void TestFunction(); // Test绘图
void InitShader(); // 初始化着色器(画刷)
void DrawImage(); // 执行绘图动作
private:
QOpenGLContext *m_context; // 画板对象
QOpenGLShaderProgram *m_program; // 着色器(画刷)对象
GLuint m_posAttr; // 着色器参数:属性
GLuint m_colAttr; // 着色器参数:纹理
GLuint m_matrixUniform; // 着色器参数:uniform
int m_frame; // 循环变量,只要到一次
bool m_update_pending; // 绘图完成标志(窗口更新标志)
public slots:
void run(); // 动画效果实现
protected: // 消息发生器(重载而来)
bool event(QEvent *event) Q_DECL_OVERRIDE;
void exposeEvent(QExposeEvent *event) Q_DECL_OVERRIDE;
};
#endif // OPENGLWINDOW_H
- openglwindow.cpp文件
复制代码
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "openglwindow.h"
#include <QtCore/QCoreApplication>
#include <QtGui/QOpenGLShaderProgram>
#include <QtGui/QOpenGLContext>
#include <QtGui/QScreen>
static const char *vertexShaderSource = // 着色器语言
"attribute highp vec4 posAttr;n"
"attribute lowp vec4 colAttr;n"
"varying lowp vec4 col;n"
"uniform highp mat4 matrix;n"
"void main() {n"
" col = colAttr;n"
" gl_Position = matrix * posAttr;n"
"}n";
static const char *fragmentShaderSource = // 着色器语言
"varying lowp vec4 col;n"
"void main() {n"
" gl_FragColor = col;n"
"}n";
OpenGLWindow::OpenGLWindow(QWindow *parent)
: QWindow(parent)
, m_context(0)
, m_frame(0)
, m_update_pending(false)
{
setSurfaceType(QWindow::OpenGLSurface);
}
OpenGLWindow::~OpenGLWindow()
{
if(m_context)
delete m_context;
}
void OpenGLWindow::TestFunction()
{
if(m_context)
{
m_context->makeCurrent(this);
DrawImage();
m_context->swapBuffers(this);
return;
}
m_context = new QOpenGLContext(this);
m_context->create();
m_context->makeCurrent(this);
initializeOpenGLFunctions();
InitShader();
DrawImage();
m_context->swapBuffers(this);
}
void OpenGLWindow::InitShader()
{
m_program = new QOpenGLShaderProgram(this);
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
m_program->link();
m_posAttr = m_program->attributeLocation("posAttr");
m_colAttr = m_program->attributeLocation("colAttr");
m_matrixUniform = m_program->uniformLocation("matrix");
}
void OpenGLWindow::DrawImage()
{
const qreal retinaScale = devicePixelRatio();
glViewport(0, 0, width() * retinaScale, height() * retinaScale);
glClear(GL_COLOR_BUFFER_BIT);
m_program->bind();
QMatrix4x4 matrix;
matrix.perspective(60.0f, 4.0f/3.0f, 0.1f, 100.0f);
matrix.translate(0, 0, -2);
matrix.rotate(100.0f * m_frame++ / screen()->refreshRate(), 0, 1, 0);
m_program->setUniformValue(m_matrixUniform, matrix);
GLfloat vertices[] = {
0.0f, 0.707f,
-0.5f, -0.5f,
0.5f, -0.5f
};
GLfloat colors[] = {
1.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f
};
glVertexAttribPointer(m_posAttr, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, colors);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
m_program->release();
}
bool OpenGLWindow::event(QEvent *event)
{
if(event->type() != QEvent::UpdateRequest)
return QWindow::event(event);
m_update_pending = false;
if(isExposed()) run();
return true;
}
void OpenGLWindow::exposeEvent(QExposeEvent *event)
{
Q_UNUSED(event);
if(isExposed()) run();
}
void OpenGLWindow::run()
{
TestFunction();
if (m_update_pending) return;
m_update_pending = true;
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
}
- 参考网页:
- Qt OpenGL教程:http://qiliang.net/old/nehe_qt/index.html ↩
最后
以上就是英勇人生最近收集整理的关于解读Qt OpenGL示例程序 openglwindowQtOpenGL示例openglwindow的全部内容,更多相关解读Qt内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复