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