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