src/gl/basicshaderprogram.cpp

Thu, 05 Nov 2020 14:29:58 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 05 Nov 2020 14:29:58 +0200
changeset 95
06a1aef170aa
parent 70
f21b800b02a4
child 118
8e1c9f18ae15
permissions
-rw-r--r--

asioita

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();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 this->buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 this->buffer.allocate(this->vertexData(), this->vertexCount() * this->vertexSize());
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 this->vertexArrayObject.create();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 this->vertexArrayObject.bind();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 this->setupVertexArrays();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 this->vertexArrayObject.release();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 this->buffer.release();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 this->program->release();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 this->checkForGLErrors();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 void AbstractBasicShaderProgram::setViewMatrix(const glm::mat4& newViewMatrix)
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 this->setMatrix("view", newViewMatrix);
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
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 void AbstractBasicShaderProgram::setProjectionMatrix(const glm::mat4& newProjectionMatrix)
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 this->setMatrix("projection", newProjectionMatrix);
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
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 void AbstractBasicShaderProgram::setModelMatrix(const glm::mat4& newModelMatrix)
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 this->setMatrix("model", newModelMatrix);
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
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 void AbstractBasicShaderProgram::setMatrix(const char* name, const glm::mat4& matrix)
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 Q_ASSERT(this->isInitialized);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 this->program->bind();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 this->program->setUniformMatrix(name, matrix);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 this->program->release();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
73 this->checkForGLErrors();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
74 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
75
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
76 void AbstractBasicShaderProgram::draw()
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
77 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 this->program->bind();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
79 this->vertexArrayObject.bind();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
80 glDrawArrays(this->drawMode(), 0, this->vertexCount());
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
81 this->vertexArrayObject.release();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
82 this->program->release();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 this->checkForGLErrors();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
86 void AbstractBasicShaderProgram::teardown()
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 this->vertexArrayObject.destroy();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89 this->buffer.destroy();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 this->program.reset();
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
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 void AbstractBasicShaderProgram::checkForGLErrors()
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 gl::checkForGLErrors(qobject_cast<QWidget*>(this->parent()));
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 }

mercurial