src/gl/gridprogram.cpp

Wed, 22 Jun 2022 16:13:15 +0300

author
Teemu Piippo <teemu.s.piippo@gmail.com>
date
Wed, 22 Jun 2022 16:13:15 +0300
changeset 249
37d3c819cafa
parent 243
959469a7e149
child 250
2837b549e616
permissions
-rw-r--r--

Done some work on object editor

53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 /*
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
2 * LDForge: LDraw parts authoring CAD
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 * Copyright (C) 2020 Teemu Piippo
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4 *
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 * This program is free software: you can redistribute it and/or modify
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 * it under the terms of the GNU General Public License as published by
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 * (at your option) any later version.
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 *
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 * This program is distributed in the hope that it will be useful,
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 * GNU General Public License for more details.
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14 *
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
17 */
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
18
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #include "gridprogram.h"
243
959469a7e149 Make the grid black on bright backgrounds
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
20 #include "partrenderer.h"
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
22 constexpr char vertexShaderSource[] = R"(
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 #version 330 core
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 layout (location = 0) in vec2 in_position;
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
26 uniform mat4 mvp;
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 smooth out vec2 ex_uv;
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
28 uniform mat4 grid;
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 void main()
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 {
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
32 gl_Position = mvp * grid * vec4(in_position, 0.0, 1.0);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 ex_uv = in_position;
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 )";
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
37 constexpr char fragmentShaderSource[] = R"(
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 #version 330 core
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 out vec4 color;
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 smooth in vec2 ex_uv;
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
42 uniform vec4 gridColor;
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 void main(void)
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 {
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
46 float dx = fract(ex_uv.y);
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
47 float dy = fract(ex_uv.x);
54
a4055f67b9c7 made the grid look nicer
Teemu Piippo <teemu@hecknology.net>
parents: 53
diff changeset
48 /* fade the grid towards extreme co-ordinates */
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
49 float d = (1.0f - 0.015 * max(abs(ex_uv.x), abs(ex_uv.y)));
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
50 color = vec4(gridColor.xyz, gridColor.w * d);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 )";
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
54 template<int extent>
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
55 constexpr auto calcGridData()
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
56 {
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
57 std::array<glm::vec2, 8 * extent + 4> result;
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
58 int ix = 0;
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
59 for (int i = -extent; i <= extent; i += 1) {
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
60 result[ix++] = {i, -extent};
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
61 result[ix++] = {i, extent};
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
62 }
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
63 for (int i = -extent; i <= extent; i += 1) {
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
64 result[ix++] = {-extent, i};
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
65 result[ix++] = {extent, i};
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
66 }
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
67 return result;
156
65b75beed7e0 render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents: 155
diff changeset
68 }
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
70 void GridLayer::setGridMatrix(const glm::mat4& newGridMatrix)
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
71 {
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 216
diff changeset
72 this->gridMatrix = newGridMatrix;
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 216
diff changeset
73 if (this->isInitialized) {
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 216
diff changeset
74 this->shader.setUniformMatrix("grid", newGridMatrix);
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 216
diff changeset
75 }
64
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
76 }
f99d52b1646b grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents: 55
diff changeset
77
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
78 void GridLayer::setGridColor(const QColor& newGridColor)
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
79 {
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
80 this->gridColor = gl::colorToVector4(newGridColor);
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
81 if (this->isInitialized) {
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
82 this->shader.setUniformVector("gridColor", this->gridColor);
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
83 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
84 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 54
diff changeset
85
243
959469a7e149 Make the grid black on bright backgrounds
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
86 void GridLayer::settingsChanged()
959469a7e149 Make the grid black on bright backgrounds
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
87 {
959469a7e149 Make the grid black on bright backgrounds
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
88 this->setGridColor(this->renderer->isDark() ? Qt::white : Qt::black);
959469a7e149 Make the grid black on bright backgrounds
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
89 }
959469a7e149 Make the grid black on bright backgrounds
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
90
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
91 void GridLayer::initializeGL()
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
92 {
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
93 this->shader.initialize(
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
94 ::vertexShaderSource,
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
95 ::fragmentShaderSource,
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
96 QOpenGLBuffer::StaticDraw,
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
97 {
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
98 GLAttributeSpec{
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
99 .type = GL_FLOAT,
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
100 .offset = 0,
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
101 .tuplesize = 2,
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
102 .stride = 0,
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
103 },
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
104 }
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
105 );
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
106 this->isInitialized = true;
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
107 constexpr auto data = calcGridData<50>();
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
108 this->shader.setUniformVector("gridColor", this->gridColor);
217
6d95c1a41e6e reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 216
diff changeset
109 this->setGridMatrix(this->gridMatrix);
243
959469a7e149 Make the grid black on bright backgrounds
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 217
diff changeset
110 this->settingsChanged();
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
111 this->shader.bufferData(data.data(), data.size(), sizeof data[0]);
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
112 }
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
113
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
114 void GridLayer::paintGL()
70
f21b800b02a4 Added axes rendering
Teemu Piippo <teemu@hecknology.net>
parents: 64
diff changeset
115 {
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
116 glLineWidth(1);
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
117 glEnable(GL_BLEND);
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
118 glLineStipple(1, 0x8888);
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
119 glEnable(GL_LINE_STIPPLE);
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
120 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
121 this->shader.draw(GL_LINES);
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
122 glDisable(GL_BLEND);
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
123 glDisable(GL_LINE_STIPPLE);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
124 }
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
125
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
126 void GridLayer::mvpMatrixChanged(const glm::mat4& mvpMatrix)
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
127 {
216
c7241f504117 Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents: 167
diff changeset
128 this->shader.setMvpMatrix(mvpMatrix);
53
3af627f7a40f added grid program
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
129 }

mercurial