Sat, 08 Apr 2023 15:11:39 +0300
Rename ColorButton -> ColorEdit
19 | 1 | /* |
2 | * LDForge: LDraw parts authoring CAD | |
3 | * Copyright (C) 2013 - 2018 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 | ||
26 | 19 | #include <QMessageBox> |
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:
260
diff
changeset
|
20 | #include "src/documentmanager.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:
260
diff
changeset
|
21 | #include "src/invert.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:
260
diff
changeset
|
22 | #include "src/gl/compiler.h" |
19 | 23 | |
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:
211
diff
changeset
|
24 | constexpr char VERTEX_SHADER_SOURCE[] = R"( |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
25 | #version 330 core |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
26 | |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
27 | layout(location=0) in vec3 position; |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
28 | layout(location=1) in vec4 color; |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
29 | layout(location=2) in vec3 normal; |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
30 | layout(location=3) in vec3 pickcolor; |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
31 | layout(location=4) in float selected; |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
32 | out vec4 vColor; |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
33 | out vec3 vFragPos; |
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
34 | out vec3 vNormal; |
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
35 | uniform mat4 modelMatrix; |
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
36 | uniform mat4 viewMatrix; |
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
37 | uniform mat4 projectionMatrix; |
37 | 38 | uniform int fragmentStyle; |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
39 | uniform vec4 selectedColor; |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
40 | uniform int highlighted; |
37 | 41 | |
42 | const int FRAGSTYLE_Normal = 0; | |
43 | const int FRAGSTYLE_BfcGreen = 1; | |
44 | const int FRAGSTYLE_BfcRed = 2; | |
45 | const int FRAGSTYLE_Random = 3; | |
46 | 46 | const int FRAGSTYLE_Id = 4; |
119 | 47 | const int FRAGSTYLE_Black = 5; |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
48 | |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
49 | void main() |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
50 | { |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
51 | mat3 normalMatrix = transpose(inverse(mat3(modelMatrix))); |
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
52 | vNormal = normalize(normalMatrix * normal); |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
53 | if (fragmentStyle == FRAGSTYLE_Id) |
37 | 54 | { |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
55 | vColor = vec4(pickcolor, 1.0); |
51 | 56 | } |
37 | 57 | else |
58 | { | |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
59 | if (fragmentStyle == FRAGSTYLE_BfcGreen) |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
60 | { |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
61 | vColor = vec4(0.2, 0.9, 0.2, 1.0); |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
62 | } |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
63 | else if (fragmentStyle == FRAGSTYLE_BfcRed) |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
64 | { |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
65 | vColor = vec4(0.9, 0.2, 0.2, 1.0); |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
66 | } |
119 | 67 | else if (fragmentStyle == FRAGSTYLE_Black) |
68 | { | |
69 | vColor = vec4(0.0, 0.0, 0.0, 1.0); | |
70 | } | |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
71 | else |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
72 | { |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
73 | vColor = color; |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
74 | } |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
75 | /* |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
76 | if (highlighted == id) |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
77 | { |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
78 | vColor = (vColor + vec4(selectedColor, 1.0) * 0.6) / 1.6; |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
79 | } |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
80 | */ |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
81 | vColor = (1 - selected) * vColor + selected * selectedColor; |
37 | 82 | } |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
83 | vFragPos = vec3(modelMatrix * vec4(position, 1.0)); |
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
84 | gl_Position = projectionMatrix * viewMatrix * vec4(vFragPos, 1.0); |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
85 | } |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
86 | )"; |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
87 | |
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:
211
diff
changeset
|
88 | constexpr char FRAGMENT_SHADER_SOURCE[] = R"( |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
89 | #version 330 core |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
90 | |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
91 | in vec4 vColor; |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
92 | in vec3 vFragPos; |
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
93 | in vec3 vNormal; |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
94 | out vec4 fColor; |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
95 | const vec3 lightPos = vec3(0.5, 0.5, 0.5); |
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
96 | const vec4 lightColor = vec4(1.0, 1.0, 1.0, 1.0); |
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
97 | const float ambientStrength = 0.7; |
46 | 98 | uniform bool useLighting; |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
99 | |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
100 | void main() |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
101 | { |
46 | 102 | if (useLighting) |
103 | { | |
104 | vec4 ambient = ambientStrength * lightColor; | |
105 | vec3 lightDirection = normalize(lightPos - vFragPos); | |
106 | vec4 diffuse = max(dot(vNormal, lightDirection), 0.0) * lightColor; | |
107 | fColor = (ambient + diffuse) * vColor; | |
108 | } | |
109 | else | |
110 | { | |
111 | fColor = vColor; | |
112 | } | |
27
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
113 | } |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
114 | )"; |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
115 | |
211 | 116 | template<typename Fn> |
117 | constexpr void pointsToRender(const PolygonElement& element, Fn func) | |
118 | { | |
119 | visitPolygon<void>( | |
120 | [&func](const LineSegment& edge) | |
121 | { | |
122 | func(edge.p1, glm::vec3{}); | |
123 | func(edge.p2, glm::vec3{}); | |
124 | }, | |
125 | [&func](const Triangle& tri) | |
126 | { | |
127 | func(tri.p1, normalVector({tri.p3, tri.p1, tri.p2})); | |
128 | func(tri.p2, normalVector({tri.p1, tri.p2, tri.p3})); | |
129 | func(tri.p3, normalVector({tri.p2, tri.p3, tri.p1})); | |
130 | }, | |
131 | [&func](const Quadrilateral& quad) | |
132 | { | |
133 | func(quad.p1, normalVector({quad.p4, quad.p1, quad.p2})); | |
134 | func(quad.p2, normalVector({quad.p1, quad.p2, quad.p3})); | |
135 | func(quad.p3, normalVector({quad.p2, quad.p3, quad.p4})); | |
136 | func(quad.p4, normalVector({quad.p3, quad.p4, quad.p1})); | |
137 | }, | |
138 | [&func](const ConditionalEdge& cedge) | |
139 | { | |
140 | func(cedge.p1, glm::vec3{}); | |
141 | func(cedge.p2, glm::vec3{}); | |
142 | }, | |
143 | element); | |
144 | } | |
145 | ||
53 | 146 | void gl::buildShaders( |
147 | QOpenGLShaderProgram* shaderProgram, | |
148 | const char* vertexShaderSource, | |
149 | const char* fragmentShaderSource) | |
150 | { | |
151 | shaderProgram->create(); | |
152 | const bool vertexShaderCompiled = shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource); | |
153 | QString log; | |
154 | if (not vertexShaderCompiled) | |
155 | { | |
156 | log += "\n" + QObject::tr("Vertex shader:") + "\n" + shaderProgram->log(); | |
157 | } | |
115 | 158 | const bool fragmentShaderCompiled = shaderProgram->addShaderFromSourceCode( |
159 | QOpenGLShader::Fragment, | |
160 | fragmentShaderSource); | |
53 | 161 | if (not fragmentShaderCompiled) |
162 | { | |
163 | log += "\n" + QObject::tr("Fragment shader:") + "\n" + shaderProgram->log(); | |
164 | } | |
165 | if (not vertexShaderCompiled or not fragmentShaderCompiled) | |
166 | { | |
167 | QMessageBox::critical( | |
168 | nullptr, | |
169 | QObject::tr("Shader compile error"), | |
170 | QObject::tr("Could not compile shaders.") + "\n" + log); | |
171 | std::exit(-1); | |
172 | } | |
173 | const bool linkSuccessful = shaderProgram->link(); | |
174 | if (not linkSuccessful) | |
175 | { | |
176 | QMessageBox::critical( | |
177 | nullptr, | |
178 | QObject::tr("Shader link error"), | |
179 | QObject::tr("Could not link shaders: %1").arg(shaderProgram->log()) | |
180 | ); | |
181 | } | |
182 | } | |
183 | ||
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
184 | void gl::initializeModelShaders(gl::ModelShaders *modelShaders) |
26 | 185 | { |
211 | 186 | if (not modelShaders->initialized) { |
187 | for (auto& shader : modelShaders->shaderObjects) { | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
188 | shader.program = std::make_unique<QOpenGLShaderProgram>(); |
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:
211
diff
changeset
|
189 | gl::buildShaders(shader.program.get(), VERTEX_SHADER_SOURCE, FRAGMENT_SHADER_SOURCE); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
190 | shader.program->bind(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
191 | shader.buffer.create(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
192 | shader.buffer.bind(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
193 | shader.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
194 | shader.vertexArray.create(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
195 | shader.vertexArray.bind(); |
211 | 196 | for (int k : {0, 1, 2, 3, 4}) { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
197 | shader.program->enableAttributeArray(k); |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
198 | } |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
199 | using Vertex = ModelShaders::Vertex; |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
200 | constexpr std::size_t stride = sizeof(Vertex); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
201 | shader.program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
202 | shader.program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 4, stride); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
203 | shader.program->setAttributeBuffer(2, GL_FLOAT, offsetof(Vertex, normal), 3, stride); |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
204 | shader.program->setAttributeBuffer(3, GL_FLOAT, offsetof(Vertex, pickcolor), 3, stride); |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
205 | shader.program->setAttributeBuffer(4, GL_FLOAT, offsetof(Vertex, selected), 1, stride); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
206 | shader.vertexArray.release(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
207 | shader.buffer.release(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
208 | shader.program->release(); |
26 | 209 | } |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
210 | modelShaders->initialized = true; |
26 | 211 | } |
19 | 212 | } |
21 | 213 | |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
214 | static constexpr gl::ArrayClass classifyPolygon(const PolygonElement& element) |
21 | 215 | { |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
216 | return visitPolygon<gl::ArrayClass>( |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
217 | [](const LineSegment&) { return gl::ArrayClass::Lines; }, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
218 | [](const Triangle&) { return gl::ArrayClass::Triangles; }, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
219 | [](const Quadrilateral&) { return gl::ArrayClass::Quads; }, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
220 | [](const ConditionalEdge&) { return gl::ArrayClass::ConditionalLines; }, |
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
221 | element); |
21 | 222 | } |
223 | ||
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
224 | template<typename Fn> |
338 | 225 | void iterateModelPolygons(QTextDocument* model, DocumentManager* context, Fn&& fn) |
21 | 226 | { |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
297
diff
changeset
|
227 | PolygonCache* const cache = findPolygonCacheForModel(model, context); |
211 | 228 | if (cache != nullptr) { |
305
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
297
diff
changeset
|
229 | recacheIfNeeded(cache, model, context); |
d891da20abca
Add support for BFC CERTIFY statements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
297
diff
changeset
|
230 | for (const WithId<PolygonElement>& polygon : cache->polygons) { |
211 | 231 | fn(polygon); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
232 | } |
21 | 233 | } |
234 | } | |
235 | ||
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
236 | static QColor getColorForPolygon( |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
237 | const PolygonElement& polygon, |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
238 | const gl::RenderPreferences& preferences, |
205 | 239 | const ColorTable& colorTable) |
21 | 240 | { |
241 | QColor color; | |
26 | 242 | // For normal colors, use the polygon's color. |
205 | 243 | if (polygon.color == MAIN_COLOR) |
21 | 244 | { |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
245 | color = preferences.mainColor; |
21 | 246 | } |
205 | 247 | else if (polygon.color == EDGE_COLOR) |
21 | 248 | { |
26 | 249 | // Edge color is black, unless we have a dark background, in which case lines need to be bright. |
43
08dc62e03a6d
made edges white in dark backgrounds
Teemu Piippo <teemu@hecknology.net>
parents:
39
diff
changeset
|
250 | color = luma(preferences.backgroundColor) > (40.0 / 256.0) ? Qt::black : Qt::white; |
21 | 251 | } |
252 | else | |
253 | { | |
26 | 254 | // Not main or edge color, use the polygon's color as is. |
205 | 255 | color = colorFace(polygon.color, colorTable).value_or(Qt::black); |
21 | 256 | } |
257 | return color; | |
258 | } | |
259 | ||
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
260 | /** |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
261 | * @brief Computes the minimum bounding box for a model |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
262 | */ |
338 | 263 | BoundingBox gl::boundingBoxForModel(QTextDocument* model, DocumentManager* context) |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
264 | { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
265 | BoundingBox result = emptyBoundingBox; |
210
232e7634cc8a
more refactoring, dosn't build yet
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
205
diff
changeset
|
266 | iterateModelPolygons(model, context, [&](const PolygonElement& polygon) |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
267 | { |
211 | 268 | visitPoints([&result](const glm::vec3& p) { |
269 | addPointToBox(result, p); | |
270 | }, polygon); | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
271 | }); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
272 | return result; |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
273 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
274 | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
275 | /** |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
276 | * @brief gl::build Creates GL vertices for objects in the model and buffers them to shaders. |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
277 | */ |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
278 | void gl::build( |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
279 | gl::ModelShaders* shaders, |
338 | 280 | QTextDocument* model, |
205 | 281 | const ColorTable& colorTable, |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
282 | DocumentManager* context, |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
283 | const gl::RenderPreferences& preferences) |
26 | 284 | { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
285 | for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects) { |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
286 | shader.cachedData.clear(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
287 | } |
211 | 288 | iterateModelPolygons(model, context, [&](const WithId<PolygonElement>& polygon) |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
289 | { |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
290 | const int index = static_cast<int>(classifyPolygon(polygon)); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
291 | std::vector<gl::ModelShaders::Vertex>& vertexBuffer = shaders->shaderObjects[index].cachedData; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
292 | const QColor color = getColorForPolygon(polygon, preferences, colorTable); |
211 | 293 | pointsToRender(polygon, [&](const glm::vec3& point, const glm::vec3& normal){ |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
294 | gl::ModelShaders::Vertex& vertex = vertexBuffer.emplace_back(); |
211 | 295 | vertex.position = point; |
296 | vertex.normal = normal; | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
297 | vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()}; |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
298 | vertex.id = polygon.linenumber; |
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
299 | vertex.pickcolor = idToColor(polygon.linenumber); |
211 | 300 | }); |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
301 | }); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
302 | for (gl::ModelShaders::ShaderObject& shader : shaders->shaderObjects) |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
303 | { |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
304 | shader.vertexCount = shader.cachedData.size(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
305 | shader.buffer.bind(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
306 | const int bytes = static_cast<int>(shader.cachedData.size() * sizeof shader.cachedData[0]); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
307 | shader.buffer.allocate(shader.cachedData.data(), bytes); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
308 | shader.buffer.release(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
309 | } |
26 | 310 | } |
311 | ||
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
312 | int gl::idFromUcharColor(const std::array<GLubyte, 3>& data) |
26 | 313 | { |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
314 | return |
290
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
315 | static_cast<std::int32_t>(data[0]) | |
0fd926ebb03b
Fix picking and rendering of selected colors
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
264
diff
changeset
|
316 | static_cast<std::int32_t>(data[1]) << 8 | |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
317 | static_cast<std::int32_t>(data[2]) << 16; |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
318 | } |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
319 | |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
320 | void gl::bindModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass) |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
321 | { |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
322 | ModelShaders::ShaderObject& shaderObject = shaders->shaderObjects[static_cast<int>(arrayClass)]; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
323 | shaderObject.vertexArray.bind(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
324 | shaderObject.program->bind(); |
26 | 325 | } |
326 | ||
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
327 | void gl::releaseModelShaderVertexArray(gl::ModelShaders* shaders, gl::ArrayClass arrayClass) |
51 | 328 | { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
329 | ModelShaders::ShaderObject& shaderObject = shaders->shaderObjects[static_cast<int>(arrayClass)]; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
330 | shaderObject.program->release(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
331 | shaderObject.vertexArray.release(); |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
332 | } |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
333 | |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
334 | /* |
309
d862721d19a3
Fixed ModelId being used to identify both models and elements, added ElementId to identify elements
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
305
diff
changeset
|
335 | void gl::setModelShaderSelectedObjects(gl::ModelShaders* shaders, const QSet<ElementId> &ids) |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
336 | { |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
337 | for (ModelShaders::ShaderObject& object : shaders->shaderObjects) |
51 | 338 | { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
339 | std::vector<ModelShaders::Vertex>& vector = object.cachedData; |
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
340 | for (ModelShaders::Vertex& vertex : vector) |
51 | 341 | { |
342 | vertex.selected = (ids.contains({vertex.id})) ? 1 : 0; | |
343 | } | |
344 | const GLsizeiptr size = static_cast<int>(vector.size() * sizeof vector[0]); | |
80 | 345 | object.buffer.bind(); |
291
42b4953dff85
Let's bring GLEW back after all
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
290
diff
changeset
|
346 | glBufferSubData(GL_ARRAY_BUFFER, 0, size, vector.data()); |
80 | 347 | object.buffer.release(); |
51 | 348 | } |
349 | } | |
333
07e65a4c6611
Experiment to delete the Model class and rely solely on text documents
Teemu Piippo <teemu.s.piippo@gmail.com>
parents:
309
diff
changeset
|
350 | */ |
51 | 351 | |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
352 | std::size_t gl::vertexCount(const gl::ModelShaders* shaders, const gl::ArrayClass arrayClass) |
28 | 353 | { |
189
815fbaae9cb2
cleanup, gl::Compiler changed to gl::ModelShaders
Teemu Piippo <teemu@hecknology.net>
parents:
150
diff
changeset
|
354 | return shaders->shaderObjects[static_cast<int>(arrayClass)].vertexCount; |
28 | 355 | } |