src/gl/basicshaderprogram.cpp

Sat, 05 Mar 2022 13:18:40 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Sat, 05 Mar 2022 13:18:40 +0200
changeset 161
6fe485a84d06
parent 118
8e1c9f18ae15
child 215
34c6e7bc4ee1
permissions
-rw-r--r--

Icons update

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

mercurial