Wed, 15 Jun 2022 12:33:16 +0300
remove old todo
53 | 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 | ||
19 | #include "gridprogram.h" | |
20 | ||
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
21 | constexpr char vertexShaderSource[] = R"( |
53 | 22 | #version 330 core |
23 | ||
24 | 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
|
25 | uniform mat4 mvp; |
53 | 26 | smooth out vec2 ex_uv; |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
27 | uniform mat4 grid; |
53 | 28 | |
29 | void main() | |
30 | { | |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
31 | gl_Position = mvp * grid * vec4(in_position, 0.0, 1.0); |
53 | 32 | ex_uv = in_position; |
33 | } | |
34 | )"; | |
35 | ||
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
36 | constexpr char fragmentShaderSource[] = R"( |
53 | 37 | #version 330 core |
38 | ||
39 | out vec4 color; | |
40 | smooth in vec2 ex_uv; | |
55 | 41 | uniform vec4 gridColor; |
53 | 42 | |
43 | void main(void) | |
44 | { | |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
45 | float dx = fract(ex_uv.y); |
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
46 | float dy = fract(ex_uv.x); |
54
a4055f67b9c7
made the grid look nicer
Teemu Piippo <teemu@hecknology.net>
parents:
53
diff
changeset
|
47 | /* fade the grid towards extreme co-ordinates */ |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
48 | float d = (1.0f - 0.015 * max(abs(ex_uv.x), abs(ex_uv.y))); |
55 | 49 | color = vec4(gridColor.xyz, gridColor.w * d); |
53 | 50 | } |
51 | )"; | |
52 | ||
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
53 | template<int extent> |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
54 | constexpr auto calcGridData() |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
55 | { |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
56 | 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
|
57 | int ix = 0; |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
58 | 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
|
59 | result[ix++] = {i, -extent}; |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
60 | result[ix++] = {i, extent}; |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
61 | } |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
62 | 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
|
63 | result[ix++] = {-extent, i}; |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
64 | result[ix++] = {extent, i}; |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
65 | } |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
66 | return result; |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
67 | } |
53 | 68 | |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
69 | 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
|
70 | { |
217
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
216
diff
changeset
|
71 | this->gridMatrix = newGridMatrix; |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
216
diff
changeset
|
72 | if (this->isInitialized) { |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
216
diff
changeset
|
73 | this->shader.setUniformMatrix("grid", newGridMatrix); |
6d95c1a41e6e
reimplement EditTools as a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
216
diff
changeset
|
74 | } |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
75 | } |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
76 | |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
77 | void GridLayer::setGridColor(const QColor& newGridColor) |
55 | 78 | { |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
79 | this->gridColor = gl::colorToVector4(newGridColor); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
80 | if (this->isInitialized) { |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
81 | this->shader.setUniformVector("gridColor", this->gridColor); |
55 | 82 | } |
83 | } | |
84 | ||
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
85 | void GridLayer::initializeGL() |
53 | 86 | { |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
87 | this->shader.initialize( |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
88 | ::vertexShaderSource, |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
89 | ::fragmentShaderSource, |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
90 | QOpenGLBuffer::StaticDraw, |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
91 | { |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
92 | GLAttributeSpec{ |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
93 | .type = GL_FLOAT, |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
94 | .offset = 0, |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
95 | .tuplesize = 2, |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
96 | .stride = 0, |
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 | } |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
99 | ); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
100 | this->isInitialized = true; |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
101 | constexpr auto data = calcGridData<50>(); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
102 | 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
|
103 | this->setGridMatrix(this->gridMatrix); |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
104 | this->shader.bufferData(data.data(), data.size(), sizeof data[0]); |
70 | 105 | } |
106 | ||
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
107 | void GridLayer::paintGL() |
70 | 108 | { |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
109 | glLineWidth(1); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
110 | glEnable(GL_BLEND); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
111 | glLineStipple(1, 0x8888); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
112 | glEnable(GL_LINE_STIPPLE); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
113 | 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
|
114 | this->shader.draw(GL_LINES); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
115 | glDisable(GL_BLEND); |
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
116 | glDisable(GL_LINE_STIPPLE); |
53 | 117 | } |
118 | ||
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
119 | void GridLayer::mvpMatrixChanged(const glm::mat4& mvpMatrix) |
53 | 120 | { |
216
c7241f504117
Reworked grid program into a render layer
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
167
diff
changeset
|
121 | this->shader.setMvpMatrix(mvpMatrix); |
53 | 122 | } |