Sun, 24 Oct 2021 11:33:32 +0300
update
70 | 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 "basicshaderprogram.h" | |
20 | ||
21 | AbstractBasicShaderProgram::AbstractBasicShaderProgram(QObject* parent) : | |
22 | QObject{parent}, | |
23 | buffer{QOpenGLBuffer::VertexBuffer}, | |
24 | vertexShader{QOpenGLShader::Vertex}, | |
25 | fragmentShader{QOpenGLShader::Fragment} | |
26 | { | |
27 | } | |
28 | ||
29 | void AbstractBasicShaderProgram::initialize() | |
30 | { | |
31 | if (not isInitialized) | |
32 | { | |
33 | this->initializeOpenGLFunctions(); | |
34 | this->isInitialized = true; | |
35 | this->program.emplace(this); | |
36 | gl::buildShaders(&*this->program, this->vertexShaderSource(), this->fragmentShaderSource()); | |
37 | this->program->bind(); | |
38 | this->buffer.create(); | |
39 | this->buffer.bind(); | |
118 | 40 | const QOpenGLBuffer::UsagePattern pattern = this->usagePattern(); |
41 | this->buffer.setUsagePattern(pattern); | |
42 | if (pattern == QOpenGLBuffer::StaticDraw) | |
43 | { | |
44 | this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize()); | |
45 | } | |
70 | 46 | this->vertexArrayObject.create(); |
47 | this->vertexArrayObject.bind(); | |
48 | this->setupVertexArrays(); | |
49 | this->vertexArrayObject.release(); | |
50 | this->buffer.release(); | |
51 | this->program->release(); | |
52 | this->checkForGLErrors(); | |
53 | } | |
54 | } | |
55 | ||
56 | void AbstractBasicShaderProgram::setViewMatrix(const glm::mat4& newViewMatrix) | |
57 | { | |
58 | this->setMatrix("view", newViewMatrix); | |
59 | } | |
60 | ||
61 | void AbstractBasicShaderProgram::setProjectionMatrix(const glm::mat4& newProjectionMatrix) | |
62 | { | |
63 | this->setMatrix("projection", newProjectionMatrix); | |
64 | } | |
65 | ||
66 | void AbstractBasicShaderProgram::setModelMatrix(const glm::mat4& newModelMatrix) | |
67 | { | |
68 | this->setMatrix("model", newModelMatrix); | |
69 | } | |
70 | ||
71 | void AbstractBasicShaderProgram::setMatrix(const char* name, const glm::mat4& matrix) | |
72 | { | |
73 | Q_ASSERT(this->isInitialized); | |
74 | this->program->bind(); | |
75 | this->program->setUniformMatrix(name, matrix); | |
76 | this->program->release(); | |
77 | this->checkForGLErrors(); | |
78 | } | |
79 | ||
80 | void AbstractBasicShaderProgram::draw() | |
81 | { | |
82 | this->program->bind(); | |
83 | this->vertexArrayObject.bind(); | |
84 | glDrawArrays(this->drawMode(), 0, this->vertexCount()); | |
85 | this->vertexArrayObject.release(); | |
86 | this->program->release(); | |
87 | this->checkForGLErrors(); | |
88 | } | |
89 | ||
90 | void AbstractBasicShaderProgram::teardown() | |
91 | { | |
92 | this->vertexArrayObject.destroy(); | |
93 | this->buffer.destroy(); | |
94 | this->program.reset(); | |
95 | } | |
96 | ||
97 | void AbstractBasicShaderProgram::checkForGLErrors() | |
98 | { | |
99 | gl::checkForGLErrors(qobject_cast<QWidget*>(this->parent())); | |
100 | } |