-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenglscenewidget.cpp
177 lines (142 loc) · 4.14 KB
/
openglscenewidget.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include <vector>
#include "openglscenewidget.h"
OpenGLSceneWidget::OpenGLSceneWidget(QWidget* parent) :
QOpenGLWidget(parent)
{
}
OpenGLSceneWidget::~OpenGLSceneWidget()
{
makeCurrent();
destroyGL();
doneCurrent();
}
bool OpenGLSceneWidget::initVertices(const std::vector<float>& vertices_data, QString& error_message)
{
if (!m_opengl_initialized) {
error_message = "OpenGL not initialized.";
return false;
}
if (!m_vertex_buffer.bind()) {
error_message = "Cannot bind OpenGL vertex buffer.";
return false;
}
m_vertex_buffer.allocate(vertices_data.data(), static_cast<int>(vertices_data.size() * sizeof(float)));
m_vertex_buffer.release();
return true;
}
GLuint OpenGLSceneWidget::getVertexBufferId() const
{
return m_vertex_buffer.bufferId();
}
void OpenGLSceneWidget::setZoom(float zoom)
{
m_zoom = zoom;
}
float OpenGLSceneWidget::getZoom() const
{
return m_zoom;
}
float OpenGLSceneWidget::xScale(int w, int h)
{
if (w > h) {
return static_cast<float>(h) / static_cast<float>(w) / m_zoom;
} else {
return 1.0f / m_zoom;
}
}
float OpenGLSceneWidget::yScale(int w, int h)
{
if (h > w) {
return static_cast<float>(w) / static_cast<float>(h) / m_zoom;
} else {
return 1.0f / m_zoom;
}
}
void OpenGLSceneWidget::initializeGL()
{
if (m_opengl_initialized) {
return;
}
initializeOpenGLFunctions();
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glDisable(GL_DEPTH_TEST);
glEnable(GL_PROGRAM_POINT_SIZE);
m_shader_program = new QOpenGLShaderProgram;
if (!m_shader_program->addCacheableShaderFromSourceFile(QOpenGLShader::Vertex, ":/openglscenevertex.vert")) {
emit errorOccurred("OpenGL shader compile error: \"" + m_shader_program->log() + "\".");
destroyGL();
return;
}
if (!m_shader_program->addCacheableShaderFromSourceFile(QOpenGLShader::Fragment, ":/openglscenevertex.frag")) {
emit errorOccurred("OpenGL shader compile error: \"" + m_shader_program->log() + "\".");
destroyGL();
return;
}
if (!m_shader_program->link()) {
emit errorOccurred("OpenGL shader compile error: \"" + m_shader_program->log() + "\".");
destroyGL();
return;
}
if (!m_shader_program->bind()) {
emit errorOccurred("Cannot bind OpenGL shader.");
destroyGL();
return;
}
m_shader_program->setUniformValue("point_color", POINT_COLOR);
m_shader_program->setUniformValue("point_size", POINT_SIZE);
m_shader_program->release();
if (!m_vertex_buffer.create()) {
emit errorOccurred("Cannot create OpenGL vertex buffer.");
destroyGL();
return;
}
m_opengl_initialized = true;
emit openGlInitialized();
}
void OpenGLSceneWidget::destroyGL()
{
if (m_shader_program != nullptr) {
delete m_shader_program;
m_shader_program = nullptr;
}
m_vertex_buffer.destroy();
m_opengl_initialized = false;
emit openGlDestroyed();
}
void OpenGLSceneWidget::resizeGL(int w, int h)
{
if (!m_opengl_initialized) {
return;
}
if (!m_shader_program->bind()) {
emit errorOccurred("Cannot bind OpenGL shader.");
destroyGL();
return;
}
m_shader_program->setUniformValue("x_scale", xScale(w, h));
m_shader_program->setUniformValue("y_scale", yScale(w, h));
m_shader_program->release();
}
void OpenGLSceneWidget::paintGL()
{
if (!m_opengl_initialized) {
return;
}
glClear(GL_COLOR_BUFFER_BIT);
if (!m_shader_program->bind()) {
emit errorOccurred("Cannot bind OpenGL shader.");
destroyGL();
return;
}
m_shader_program->enableAttributeArray("position");
if (!m_vertex_buffer.bind()) {
emit errorOccurred("Cannot bind OpenGL vertex buffer.");
destroyGL();
return;
}
m_shader_program->setAttributeBuffer("position", GL_FLOAT, 0, 2, 0);
glDrawArrays(GL_POINTS, 0, m_vertex_buffer.size() / sizeof(float) / 2);
m_vertex_buffer.release();
m_shader_program->disableAttributeArray("position");
m_shader_program->release();
}