src/gl/abstractshaderprogram.cpp

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

mercurial