Wed, 20 Jul 2022 12:59:07 +0300
Fix crashing
Add option to set tool button style
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 | ||
264
76a025db4948
Convert all includes to be relative to project root directory. Files that cannot be found in this manner use angle brackets.
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
259
diff
changeset
|
19 | #include "src/gl/basicshaderprogram.h" |
70 | 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 | 22 | buffer{QOpenGLBuffer::VertexBuffer}, |
23 | vertexShader{QOpenGLShader::Vertex}, | |
24 | fragmentShader{QOpenGLShader::Fragment} | |
25 | { | |
26 | } | |
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 | 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 | 32 | } |
33 | } | |
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 | 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->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
|
44 | this->program = std::make_unique<gl::ShaderProgram>(); |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
45 | gl::buildShaders(this->program.get(), vertexShaderSource, fragmentShaderSource); |
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
|
46 | 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
|
47 | 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
|
48 | 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
|
49 | 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
|
50 | 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
|
51 | 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
|
52 | 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
|
53 | const auto& spec = attributeSpecs[i]; |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
216
diff
changeset
|
54 | const int attr = narrow<int>(signed_cast(i)); |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
216
diff
changeset
|
55 | this->program->enableAttributeArray(attr); |
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
216
diff
changeset
|
56 | this->program->setAttributeBuffer(attr, spec.type, spec.offset, spec.tuplesize, spec.stride); |
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
|
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 | 63 | } |
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 | 66 | { |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
67 | this->setUniformMatrix("mvp", newMvpMatrix); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
68 | } |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
69 | |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
70 | void BasicShader::setUniformMatrix(const char* name, const glm::mat4& value) |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
71 | { |
70 | 72 | Q_ASSERT(this->isInitialized); |
73 | this->program->bind(); | |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
74 | this->program->setUniformMatrix(name, value); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
75 | this->program->release(); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
76 | gl::checkForGLErrors(nullptr); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
77 | } |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
78 | |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
79 | void BasicShader::setUniformVector(const char* name, const glm::vec4& value) |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
80 | { |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
81 | Q_ASSERT(this->isInitialized); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
82 | this->program->bind(); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
215
diff
changeset
|
83 | this->program->setUniformVector(name, value); |
70 | 84 | 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
|
85 | gl::checkForGLErrors(nullptr); |
70 | 86 | } |
87 | ||
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
|
88 | 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
|
89 | { |
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
|
90 | this->buffer.bind(); |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
216
diff
changeset
|
91 | this->buffer.allocate(data, narrow<int>(signed_cast(count * size))); |
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 | this->buffer.release(); |
250
2837b549e616
I felt that the compiler was too kind to me, so I enabled a big pile of warnings
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
216
diff
changeset
|
93 | this->vertexCount = narrow<int>(signed_cast(count)); |
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
|
94 | } |
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
|
95 | |
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
|
96 | void BasicShader::draw(GLenum drawMode) |
70 | 97 | { |
98 | this->program->bind(); | |
99 | 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
|
100 | glDrawArrays(drawMode, 0, this->vertexCount); |
70 | 101 | this->vertexArrayObject.release(); |
102 | 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
|
103 | gl::checkForGLErrors(nullptr); |
70 | 104 | } |
105 | ||
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
|
106 | void BasicShader::teardown() |
70 | 107 | { |
108 | this->vertexArrayObject.destroy(); | |
109 | this->buffer.destroy(); | |
110 | this->program.reset(); | |
111 | } |