src/gl/basicshaderprogram.cpp

Sun, 12 Jun 2022 23:59:37 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Sun, 12 Jun 2022 23:59:37 +0300
changeset 215
34c6e7bc4ee1
parent 118
8e1c9f18ae15
child 216
c7241f504117
permissions
-rw-r--r--

Reimplement the axes program as a layer that can be added to PartRenderer

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
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
21 BasicShader::BasicShader() :
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 buffer{QOpenGLBuffer::VertexBuffer},
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 vertexShader{QOpenGLShader::Vertex},
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 fragmentShader{QOpenGLShader::Fragment}
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 {
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
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
28 BasicShader::~BasicShader()
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 {
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
30 if (this->isInitialized) {
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
31 this->teardown();
70
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 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
35 void BasicShader::initialize(
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
36 const char* vertexShaderSource,
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
37 const char* fragmentShaderSource,
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
38 QOpenGLBuffer::UsagePattern usagePattern,
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
39 const std::vector<GLAttributeSpec>& attributeSpecs)
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 {
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
41 if (not this->isInitialized)
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
42 {
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
43 this->initializeOpenGLFunctions();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
44 this->isInitialized = true;
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
45 this->program = std::make_unique<gl::ShaderProgram>();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
46 gl::buildShaders(&*this->program, vertexShaderSource, fragmentShaderSource);
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
47 this->program->bind();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
48 this->buffer.create();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
49 this->buffer.bind();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
50 this->buffer.setUsagePattern(usagePattern);
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
51 this->vertexArrayObject.create();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
52 this->vertexArrayObject.bind();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
53 for (std::size_t i = 0; i < attributeSpecs.size(); ++i) {
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
54 const auto& spec = attributeSpecs[i];
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
55 this->program->enableAttributeArray(i);
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
56 this->program->setAttributeBuffer(i, spec.type, spec.offset, spec.tuplesize, spec.stride);
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
57 }
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
58 this->vertexArrayObject.release();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
59 this->buffer.release();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
60 this->program->release();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
61 gl::checkForGLErrors(nullptr);
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
62 }
70
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
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
65 void BasicShader::setMvpMatrix(const glm::mat4& newMvpMatrix)
70
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 Q_ASSERT(this->isInitialized);
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 this->program->bind();
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
69 this->program->setUniformMatrix("mvp", newMvpMatrix);
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 this->program->release();
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
71 gl::checkForGLErrors(nullptr);
70
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
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
74 void BasicShader::bufferData(const void* data, std::size_t count, std::size_t size)
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
75 {
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
76 this->buffer.bind();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
77 this->buffer.allocate(data, count * size);
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
78 this->buffer.release();
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
79 this->vertexCount = count;
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
80 }
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
81
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
82 void BasicShader::draw(GLenum drawMode)
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
83 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
84 this->program->bind();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
85 this->vertexArrayObject.bind();
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
86 glDrawArrays(drawMode, 0, this->vertexCount);
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 this->vertexArrayObject.release();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 this->program->release();
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
89 gl::checkForGLErrors(nullptr);
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
90 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91
215
34c6e7bc4ee1 Reimplement the axes program as a layer that can be added to PartRenderer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 118
diff changeset
92 void BasicShader::teardown()
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
93 {
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 this->vertexArrayObject.destroy();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
95 this->buffer.destroy();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
96 this->program.reset();
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 }

mercurial