-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneralOpenGLWidget.cpp
More file actions
343 lines (297 loc) · 9.99 KB
/
GeneralOpenGLWidget.cpp
File metadata and controls
343 lines (297 loc) · 9.99 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "GeneralOpenGLWidget.h"
#include <QMessageBox>
GeneralOpenGLWidget::GeneralOpenGLWidget(QWidget *parent, Qt::WindowFlags f)
: QOpenGLWidget(parent, f)
{
// Make our OpenGL context current before calling any OpenGL functions.
makeCurrent();
// Done with our context.
doneCurrent();
}
GeneralOpenGLWidget::~GeneralOpenGLWidget()
{
// Make our OpenGL context current before calling any OpenGL functions.
makeCurrent();
/// @todo Replace with whatever the program should do.
teardownMyStuff();
// Delete the GLSL program and shaders.
teardownGL();
// Done with our context.
doneCurrent();
}
void GeneralOpenGLWidget::initializeGL()
{
// Our OpenGL context is automatically current in this method.
// Initialize OpenGL Backend
initializeOpenGLFunctions();
/// @todo Replace with whatever the program should do.
setupMyStuff();
}
void GeneralOpenGLWidget::resizeGL(int width, int height)
{
// Our OpenGL context is automatically current in this method.
// Currently we are not handling width/height changes.
// The glViewport will already have been handled for us.
/// @todo Note that this will distort the image as the aspect ratio
/// of the window changes.
(void)width;
(void)height;
}
void GeneralOpenGLWidget::paintGL()
{
// Our OpenGL context is automatically current in this method.
// Clear the background color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/// @todo Replace with whatever the program should do.
drawMyStuff();
}
void GeneralOpenGLWidget::teardownGL()
{
// Common teardown code.
if (m_vertexShaderId) {
glDeleteShader(m_vertexShaderId);
}
if (m_fragmentShaderId) {
glDeleteShader(m_fragmentShaderId);
}
glUseProgram(0);
if (m_programId) {
glDeleteProgram(m_programId);
}
}
static const GLchar* defaultVertexShaderBody =
"layout(location = 0) in vec3 position;\n"
"layout(location = 1) in vec4 vertexColor;\n"
"out vec4 fragmentColor;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position,1);\n"
" fragmentColor = vertexColor;\n"
"}\n";
static const GLchar* defaultFragmentShaderBody =
"in vec4 fragmentColor;\n"
"layout(location = 0) out vec4 color;\n"
"void main()\n"
"{\n"
" color = fragmentColor;\n"
"}\n";
QString GeneralOpenGLWidget::makeVertexShader(QString body)
{
QString ret;
// Add the header
if (QOpenGLContext::currentContext()->isOpenGLES()) {
/// @todo Do we need this casting here and elsewhere?
ret.append("#version 300 es\n");
} else {
ret.append("#version 330\n");
}
// Add the body
if (body.size() > 0) {
ret.append(body);
} else {
ret.append(defaultVertexShaderBody);
}
return ret;
}
QString GeneralOpenGLWidget::makeFragmentShader(QString body)
{
QString ret;
// Add the header
if (QOpenGLContext::currentContext()->isOpenGLES()) {
ret.append("#version 300 es\n");
// Add default precision specifiers for GLES fragment shaders.
// These can be overridden in the shader itself, but there must
// be a default for each type.
ret.append("precision mediump float;\n");
ret.append("precision mediump int;\n");
} else {
ret.append("#version 330\n");
}
// Add the body
if (body.size() > 0) {
ret.append(body);
} else {
ret.append(defaultFragmentShaderBody);
}
return ret;
}
QString GeneralOpenGLWidget::checkShaderError(GLuint shaderId)
{
QString ret;
GLint result = GL_FALSE;
int infoLength = 0;
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &result);
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &infoLength);
if (result == GL_FALSE) {
std::vector<GLchar> errorMessage(static_cast<unsigned>(infoLength) + 1);
glGetShaderInfoLog(shaderId, infoLength, nullptr, &errorMessage[0]);
ret = &errorMessage[0];
}
return ret;
}
QString GeneralOpenGLWidget::checkProgramError(GLuint programId) {
QString ret;
GLint result = GL_FALSE;
int infoLength = 0;
glGetProgramiv(programId, GL_LINK_STATUS, &result);
glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLength);
if (result == GL_FALSE) {
std::vector<GLchar> errorMessage(static_cast<unsigned>(infoLength) + 1);
glGetProgramInfoLog(programId, infoLength, nullptr, &errorMessage[0]);
ret = &errorMessage[0];
}
return ret;
}
QString GeneralOpenGLWidget::buildProgram(QString vertexShader, QString fragmentShader)
{
QString ret;
// If we already have the program and shaders defined, tear them down.
if (m_vertexShaderId) {
glDeleteShader(m_vertexShaderId);
m_vertexShaderId = 0;
}
if (m_fragmentShaderId) {
glDeleteShader(m_fragmentShaderId);
m_fragmentShaderId = 0;
}
if (m_programId) {
glDeleteProgram(m_programId);
m_programId = 0;
}
// Compile the vertex shader and fragment shader, then link them together
// into the program.
m_vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
std::string shaderString = vertexShader.toStdString();
const char *vertexPrograms[] = { shaderString.c_str() };
glShaderSource(m_vertexShaderId, 1, vertexPrograms, nullptr);
glCompileShader(m_vertexShaderId);
ret = checkShaderError(m_vertexShaderId);
if (ret.size() > 0) {
glDeleteShader(m_vertexShaderId);
m_vertexShaderId = 0;
return QString("Error compiling vertex shader: ") + ret;
}
m_fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
shaderString = fragmentShader.toStdString();
const char *fragmentPrograms[] = { shaderString.c_str() };
glShaderSource(m_fragmentShaderId, 1, fragmentPrograms, nullptr);
glCompileShader(m_fragmentShaderId);
ret = checkShaderError(m_fragmentShaderId);
if (ret.size() > 0) {
glDeleteShader(m_vertexShaderId);
m_vertexShaderId = 0;
glDeleteShader(m_fragmentShaderId);
m_fragmentShaderId = 0;
return QString("Error compiling fragment shader: ") + ret;
}
m_programId = glCreateProgram();
if (m_programId == 0) {
glDeleteShader(m_vertexShaderId);
m_vertexShaderId = 0;
glDeleteShader(m_fragmentShaderId);
m_fragmentShaderId = 0;
return QString("Error creating program.");
}
glAttachShader(m_programId, m_vertexShaderId);
glAttachShader(m_programId, m_fragmentShaderId);
glLinkProgram(m_programId);
ret = checkProgramError(m_programId);
if (ret.size() > 0) {
glDeleteShader(m_vertexShaderId);
m_vertexShaderId = 0;
glDeleteShader(m_fragmentShaderId);
m_fragmentShaderId = 0;
glDeleteProgram(m_programId);
m_programId = 0;
return QString("Error Linking GLSL program: ") + ret;
}
return ret;
}
/// @todo Replace all of the following with whatever is needed to make the
/// class behave as desired.
class myStuff_vertex {
public:
GLfloat pos[3];
GLfloat col[4];
};
void GeneralOpenGLWidget::setupMyStuff()
{
// Set global information
glClearColor(0.7f, 0.9f, 1.0f, 1.0f);
// Make the default vertex and fragment shaders.
QString ret = buildProgram(makeVertexShader(""), makeFragmentShader(""));
if (ret.size() > 0) {
QMessageBox::critical(this, "Error", "GeneralOpenGLWidget::setupMyStuff(): Failed to build program: " + ret);
return;
}
// Generate the vertex buffer we're going to use to send data to the GPU
glGenBuffers(1, &m_vertexBuffer);
// Set up the vertex array buffer that describes the bindings and mappings.
glGenVertexArrays(1, &m_vertexArray);
glBindVertexArray(m_vertexArray);
// Configure the vertex-buffer objects within our vertex-array object.
// On MacOSX, we had to create a vertex array object and use it before we
// could enable vertex attribute arrays and set the attribute pointers.
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
{
size_t const stride = sizeof(myStuff_vertex);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride,
reinterpret_cast<GLvoid*>(offsetof(myStuff_vertex, pos)));
// color
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, stride,
reinterpret_cast<GLvoid*>(offsetof(myStuff_vertex, col)));
}
glBindVertexArray(0);
}
void GeneralOpenGLWidget::teardownMyStuff()
{
glBindVertexArray(0);
if (m_vertexBuffer != 0) {
glDeleteBuffers(1, &m_vertexBuffer);
m_vertexBuffer = 0;
}
if (m_vertexArray != 0) {
glDeleteVertexArrays(1, &m_vertexArray);
m_vertexArray = 0;
}
}
void GeneralOpenGLWidget::drawMyStuff()
{
if (m_programId == 0) { return; }
// Make a triangle. Rebuilding this every frame is not efficient but it is simpler
// for this example than keeping track of state in the initialization and slot.
QVector<myStuff_vertex> vertexBufferData;
myStuff_vertex v;
v.col[0] = 1; v.col[1] = 1; v.col[2] = 1; v.col[3] = 1;
if (m_red) {
v.col[0] = 1;
v.col[1] = 0;
v.col[2] = 0;
}
v.pos[0] = -1; v.pos[1] = -1; v.pos[2] = 0;
vertexBufferData.push_back(v);
v.pos[0] = 1; v.pos[1] = -1; v.pos[2] = 0;
vertexBufferData.push_back(v);
v.pos[0] = 0; v.pos[1] = 1; v.pos[2] = 0;
vertexBufferData.push_back(v);
// Use the GLSL program and vertex array
glUseProgram(m_programId);
glBindVertexArray(m_vertexArray);
// Push new vertex data to the buffer (buffer was bound with glBindVertexArray).
glBufferData(GL_ARRAY_BUFFER,
sizeof(vertexBufferData[0]) * static_cast<unsigned>(vertexBufferData.size()),
&vertexBufferData[0], GL_STATIC_DRAW);
// Draw the triangle.
GLsizei numElements = static_cast<GLsizei>(vertexBufferData.size());
glDrawArrays(GL_TRIANGLES, 0, numElements);
}
void GeneralOpenGLWidget::Red(bool red)
{
/// @todo Replace this with whatever is appropriate, including removing the
/// red button from the GUI if needed.
m_red = red;
// Force the widget to repaint so that we see the change.
update();
}