Sat, 05 Mar 2022 16:57:28 +0200
Draw tool now renders the winding of the new polygon
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 | ||
21 | const char vertexShaderSource[] = R"( | |
22 | #version 330 core | |
23 | ||
24 | layout (location = 0) in vec2 in_position; | |
25 | uniform mat4 view; | |
26 | uniform mat4 projection; | |
27 | uniform mat4 model; | |
28 | smooth out vec2 ex_uv; | |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
29 | uniform mat4 grid; |
53 | 30 | |
31 | void main() | |
32 | { | |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
33 | gl_Position = projection * view * model * grid * vec4(in_position, 0.0, 1.0); |
53 | 34 | ex_uv = in_position; |
35 | } | |
36 | )"; | |
37 | ||
38 | const char fragmentShaderSource[] = R"( | |
39 | #version 330 core | |
40 | ||
41 | out vec4 color; | |
42 | smooth in vec2 ex_uv; | |
55 | 43 | uniform vec4 gridColor; |
53 | 44 | |
45 | void main(void) | |
46 | { | |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
47 | float dx = fract(ex_uv.y); |
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
48 | float dy = fract(ex_uv.x); |
54
a4055f67b9c7
made the grid look nicer
Teemu Piippo <teemu@hecknology.net>
parents:
53
diff
changeset
|
49 | /* fade the grid towards extreme co-ordinates */ |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
50 | float d = (1.0f - 0.015 * max(abs(ex_uv.x), abs(ex_uv.y))); |
55 | 51 | color = vec4(gridColor.xyz, gridColor.w * d); |
53 | 52 | } |
53 | )"; | |
54 | ||
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
55 | GridProgram::GridProgram(QObject *parent) : |
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
56 | AbstractBasicShaderProgram{parent} |
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
57 | { |
167
c1ff4f107569
the grid now has less lines
Teemu Piippo <teemu@hecknology.net>
parents:
156
diff
changeset
|
58 | constexpr int extent = 50; |
c1ff4f107569
the grid now has less lines
Teemu Piippo <teemu@hecknology.net>
parents:
156
diff
changeset
|
59 | this->gridData.reserve(8 * extent + 4); |
c1ff4f107569
the grid now has less lines
Teemu Piippo <teemu@hecknology.net>
parents:
156
diff
changeset
|
60 | for (int i = -extent; i <= extent; i += 1) |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
61 | { |
167
c1ff4f107569
the grid now has less lines
Teemu Piippo <teemu@hecknology.net>
parents:
156
diff
changeset
|
62 | this->gridData.push_back({i, -extent}); |
c1ff4f107569
the grid now has less lines
Teemu Piippo <teemu@hecknology.net>
parents:
156
diff
changeset
|
63 | this->gridData.push_back({i, extent}); |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
64 | } |
167
c1ff4f107569
the grid now has less lines
Teemu Piippo <teemu@hecknology.net>
parents:
156
diff
changeset
|
65 | for (int i = -extent; i <= extent; i += 1) |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
66 | { |
167
c1ff4f107569
the grid now has less lines
Teemu Piippo <teemu@hecknology.net>
parents:
156
diff
changeset
|
67 | this->gridData.push_back({-extent, i}); |
c1ff4f107569
the grid now has less lines
Teemu Piippo <teemu@hecknology.net>
parents:
156
diff
changeset
|
68 | this->gridData.push_back({extent, i}); |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
69 | } |
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
70 | } |
53 | 71 | |
64
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
72 | void GridProgram::setGridMatrix(const glm::mat4& newGridMatrix) |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
73 | { |
f99d52b1646b
grid snapping now also works with transformed grids
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
74 | this->setMatrix("grid", newGridMatrix); |
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 | |
55 | 77 | void GridProgram::setGridColor(const QColor& newGridColor) |
78 | { | |
79 | const glm::vec4 vec = gl::colorToVector4(newGridColor); | |
80 | if (this->isInitialized) | |
81 | { | |
82 | this->program->bind(); | |
83 | this->program->setUniformVector("gridColor", vec); | |
84 | this->program->release(); | |
85 | this->checkForGLErrors(); | |
86 | } | |
87 | else | |
88 | { | |
89 | this->gridColor = vec; | |
90 | } | |
91 | } | |
92 | ||
70 | 93 | const char* GridProgram::vertexShaderSource() const |
94 | { | |
95 | return ::vertexShaderSource; | |
96 | } | |
97 | ||
98 | const char* GridProgram::fragmentShaderSource() const | |
53 | 99 | { |
70 | 100 | return ::fragmentShaderSource; |
101 | } | |
102 | ||
103 | const void* GridProgram::vertexData() const | |
104 | { | |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
105 | return this->gridData.data(); |
53 | 106 | } |
107 | ||
70 | 108 | int GridProgram::vertexSize() const |
53 | 109 | { |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
110 | return sizeof this->gridData[0]; |
70 | 111 | } |
112 | ||
113 | int GridProgram::vertexCount() const | |
114 | { | |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
115 | return this->gridData.size(); |
53 | 116 | } |
117 | ||
70 | 118 | void GridProgram::setupVertexArrays() |
53 | 119 | { |
70 | 120 | this->program->enableAttributeArray(0); |
121 | this->program->setAttributeBuffer(0, GL_FLOAT, 0, 2, 0); | |
122 | this->program->setUniformVector("gridColor", this->gridColor); | |
53 | 123 | } |
70 | 124 | |
125 | GLenum GridProgram::drawMode() const | |
126 | { | |
156
65b75beed7e0
render the grid with lines
Teemu Piippo <teemu@hecknology.net>
parents:
155
diff
changeset
|
127 | return GL_LINES; |
70 | 128 | } |
118 | 129 | |
130 | QOpenGLBuffer::UsagePattern GridProgram::usagePattern() const | |
131 | { | |
132 | return QOpenGLBuffer::StaticDraw; | |
133 | } |