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