src/gl/abstractshaderprogram.cpp

Wed, 17 Feb 2021 16:49:35 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 17 Feb 2021 16:49:35 +0200
changeset 102
9f435f66bd0c
permissions
-rw-r--r--

stuff

102
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2020 Teemu Piippo
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "abstractshaderprogram.h"
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21 AbstractShaderProgram::AbstractBasicShaderProgram(const QVector<ArraySpecification>& arraySpecifications, QObject* parent) :
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 QObject{parent},
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 buffer{QOpenGLBuffer::VertexBuffer},
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 vertexShader{QOpenGLShader::Vertex},
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 fragmentShader{QOpenGLShader::Fragment}
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 this->arrays.reserve(arraySpecifications.size());
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 for (const ArraySpecification& arraySpecification : arraySpecifications)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 this->arrays.emplace_back({arraySpecification});
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 void AbstractShaderProgram::initialize()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36 if (not this->isInitialized)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 this->initializeOpenGLFunctions();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 this->isInitialized = true;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 this->program.emplace(this);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 gl::buildShaders(&*this->program, this->vertexShaderSource(), this->fragmentShaderSource());
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 this->program->bind();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 for (auto& array : this->arrays)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 array.buffer.create();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 array.buffer.bind();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 array.buffer.setUsagePattern(array.specification.bufferUsagePattern);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 array.vertexArrayObject.create();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 array.vertexArrayObject.bind();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 this->setupVertexArrays();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 this->vertexArrayObject.release();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 this->releaseBuffers();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 this->buffer.release();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 this->program->release();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 this->checkForGLErrors();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 void AbstractShaderProgram::setViewMatrix(const glm::mat4& newViewMatrix)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 this->setMatrix("view", newViewMatrix);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 void AbstractShaderProgram::setProjectionMatrix(const glm::mat4& newProjectionMatrix)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 this->setMatrix("projection", newProjectionMatrix);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 void AbstractShaderProgram::setModelMatrix(const glm::mat4& newModelMatrix)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 this->setMatrix("model", newModelMatrix);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75 void AbstractShaderProgram::setMatrix(const char* name, const glm::mat4& matrix)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 Q_ASSERT(this->isInitialized);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 this->program->bind();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 this->program->setUniformMatrix(name, matrix);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 this->program->release();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 this->checkForGLErrors();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 void AbstractShaderProgram::draw()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 this->program->bind();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 for (Array& array : this->arrays)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 array.vertexArrayObject.bind();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 glDrawArrays(array.specification.drawMode, 0, array.count);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 this->vertexArrayObject.release();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 this->program->release();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 this->checkForGLErrors();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 void AbstractShaderProgram::teardown()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
99 this->vertexArrayObject.destroy();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 this->buffer.destroy();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
101 this->program.reset();
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
103
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
104 void AbstractShaderProgram::checkForGLErrors()
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
105 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
106 gl::checkForGLErrors(qobject_cast<QWidget*>(this->parent()));
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
107 }
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
108
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
109 void AbstractShaderProgram::upload(Array* array, const void* data, int count)
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
110 {
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
111 array->count = count;
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
112 array->buffer->buffer.allocate(data, count * array->specification.vertexSize);
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
113 }

mercurial