src/gl/axesprogram.cpp

Tue, 14 Jun 2022 23:04:49 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Tue, 14 Jun 2022 23:04:49 +0300
changeset 222
72b456f2f3c2
parent 216
c7241f504117
child 234
87ee9824210b
permissions
-rw-r--r--

Edit tools: get rid of the preview polygon and render the result-to-be

69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2020 Teemu Piippo
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
19 #include "axesprogram.h"
69
a36913fc552a begin work on axes program
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 static constexpr char vertexShaderSource[] = R"(
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 #version 330 core
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 layout (location = 0) in vec3 in_position;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 layout (location = 1) in vec3 in_color;
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
26 uniform mat4 mvp;
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 smooth out vec3 ex_color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29 void main()
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 {
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
31 gl_Position = mvp * vec4(in_position, 1.0);
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
32 ex_color = in_color;
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 )";
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35
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
36 static constexpr char fragmentShaderSource[] = R"(
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 #version 330 core
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 out vec4 color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 smooth in vec3 ex_color;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42 void main(void)
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 {
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 69
diff changeset
44 color = vec4(ex_color, 1);
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 )";
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47
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
48 void AxesLayer::initializeGL()
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 {
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
50 constexpr struct VertexType
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 {
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 glm::vec3 position;
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53 glm::vec3 color;
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
54 } data[] = {
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 {{10000, 0, 0}, {1, 0, 0}},
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 {{0, 0, 0}, {1, 0, 0}},
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 {{-10000, 0, 0}, {0.5, 0, 0}},
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 {{0, 0, 0}, {0.5, 0, 0}},
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 {{0, 10000, 0}, {0, 1, 0}},
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 {{0, 0, 0}, {0, 1, 0}},
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 {{0, -10000, 0}, {0, 0.5, 0}},
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 {{0, 0, 0}, {0, 0.5, 0}},
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
63 {{0, 0, 10000}, {0, 0, 1}},
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
64 {{0, 0, 0}, {0, 0, 1}},
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 {{0, 0, -10000}, {0, 0, 0.5}},
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
66 {{0, 0, 0}, {0, 0, 0.5}},
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67 };
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
68 constexpr int stride = sizeof(VertexType);
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->shader.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
70 ::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
71 ::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
72 QOpenGLBuffer::StaticDraw,
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
73 {
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 GLAttributeSpec{
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 .type = GL_FLOAT,
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 .offset = offsetof(VertexType, position),
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 .tuplesize = 3,
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 .stride = 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
79 },
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 .type = GL_FLOAT,
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 .offset = offsetof(VertexType, color),
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 .tuplesize = 3,
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
84 .stride = 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
85 },
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 });
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
87 this->shader.bufferData(&data[0], countof(data), sizeof data[0]);
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
88 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
89
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
90 void AxesLayer::paintGL()
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
91 {
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
92 glLineWidth(5);
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
93 glEnable(GL_LINE_SMOOTH);
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
94 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
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
95 this->shader.draw(GL_LINES);
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 215
diff changeset
96 glDisable(GL_LINE_SMOOTH);
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
97 }
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
98
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
99 void AxesLayer::mvpMatrixChanged(const glm::mat4& mvpMatrix)
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
100 {
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
101 this->shader.setMvpMatrix(mvpMatrix);
69
a36913fc552a begin work on axes program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
102 }

mercurial