Sun, 09 Apr 2023 16:01:57 +0300
I forgot to add files again... this time it's modelsubwindow.h
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 | ||
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:
263
diff
changeset
|
19 | #include "src/gl/partrenderer.h" |
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:
263
diff
changeset
|
20 | #include "src/layers/gridlayer.h" |
53 | 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 | 23 | #version 330 core |
24 | ||
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 | 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 | 29 | |
30 | void main() | |
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 | 33 | ex_uv = in_position; |
34 | } | |
35 | )"; | |
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 | 38 | #version 330 core |
39 | ||
40 | out vec4 color; | |
41 | smooth in vec2 ex_uv; | |
55 | 42 | uniform vec4 gridColor; |
53 | 43 | |
44 | void main(void) | |
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 | 50 | color = vec4(gridColor.xyz, gridColor.w * d); |
53 | 51 | } |
52 | )"; | |
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; |
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:
243
diff
changeset
|
58 | std::size_t ix = 0; |
216
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 | 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 | 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 | 83 | } |
84 | } | |
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 | 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 | 112 | } |
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 | 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 | 124 | } |
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 | 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 | 129 | } |