src/gl/basicshaderprogram.cpp

Tue, 11 Apr 2023 22:51:12 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Tue, 11 Apr 2023 22:51:12 +0300
changeset 377
e1c5e4310f8b
parent 264
76a025db4948
permissions
-rw-r--r--

BasicShader is no longer a class

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
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
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20
377
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
21 void gl::initialize_basic_shader(
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
22 gl::basic_shader* shader,
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
23 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
24 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
25 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
26 const std::vector<GLAttributeSpec>& attributeSpecs)
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 {
377
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
28 shader->program = std::make_unique<gl::ShaderProgram>();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
29 gl::buildShaders(shader->program.get(), vertexShaderSource, fragmentShaderSource);
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
30 shader->program->bind();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
31 shader->buffer.create();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
32 shader->buffer.bind();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
33 shader->buffer.setUsagePattern(usagePattern);
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
34 shader->vertexArrayObject.create();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
35 shader->vertexArrayObject.bind();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
36 for (std::size_t i = 0; i < attributeSpecs.size(); ++i) {
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
37 const auto& spec = attributeSpecs[i];
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
38 const int attr = narrow<int>(signed_cast(i));
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
39 shader->program->enableAttributeArray(attr);
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
40 shader->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
41 }
377
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
42 shader->vertexArrayObject.release();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
43 shader->buffer.release();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
44 shader->program->release();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
45 gl::checkForGLErrors(nullptr);
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47
377
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
48 void gl::set_shader_matrix_uniform(
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
49 gl::basic_shader* shader,
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
50 const char* name,
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
51 const glm::mat4& value)
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 {
377
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
53 Q_ASSERT(shader->program != nullptr);
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
54 shader->program->bind();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
55 shader->program->setUniformMatrix(name, value);
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
56 shader->program->release();
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
57 gl::checkForGLErrors(nullptr);
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
58 }
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
59
377
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
60 void gl::set_shader_vector_uniform(
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
61 gl::basic_shader* shader,
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
62 const char* name,
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
63 const glm::vec4& value)
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
64 {
377
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
65 Q_ASSERT(shader->program != nullptr);
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
66 shader->program->bind();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
67 shader->program->setUniformVector(name, value);
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
68 shader->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
69 gl::checkForGLErrors(nullptr);
70
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
377
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
72 void gl::buffer_shader_data(
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
73 gl::basic_shader* shader,
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
74 const void* data,
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
75 std::size_t count,
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
76 std::size_t 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
77 {
377
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
78 shader->buffer.bind();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
79 shader->buffer.allocate(data, narrow<int>(signed_cast(count * size)));
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
80 shader->buffer.release();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
81 shader->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
82 }
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
83
377
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
84 void gl::draw_shader(
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
85 gl::basic_shader* shader,
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
86 GLenum drawMode)
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
87 {
377
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
88 shader->program->bind();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
89 shader->vertexArrayObject.bind();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
90 glDrawArrays(drawMode, 0, shader->vertexCount);
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
91 shader->vertexArrayObject.release();
e1c5e4310f8b BasicShader is no longer a class
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 264
diff changeset
92 shader->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
93 gl::checkForGLErrors(nullptr);
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
94 }

mercurial