Sat, 05 Mar 2022 13:19:59 +0200
only render svg to png if the png does not exist
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 | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
128 | gl::Compiler::Compiler(Model *model, const ldraw::ColorTable& colorTable, QObject* parent) : |
26 | 129 | QObject{parent}, |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
130 | model{model}, |
26 | 131 | colorTable{colorTable} |
21 | 132 | { |
133 | } | |
134 | ||
19 | 135 | gl::Compiler::~Compiler() |
136 | { | |
26 | 137 | } |
19 | 138 | |
53 | 139 | void gl::buildShaders( |
140 | QOpenGLShaderProgram* shaderProgram, | |
141 | const char* vertexShaderSource, | |
142 | const char* fragmentShaderSource) | |
143 | { | |
144 | shaderProgram->create(); | |
145 | const bool vertexShaderCompiled = shaderProgram->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource); | |
146 | QString log; | |
147 | if (not vertexShaderCompiled) | |
148 | { | |
149 | log += "\n" + QObject::tr("Vertex shader:") + "\n" + shaderProgram->log(); | |
150 | } | |
115 | 151 | const bool fragmentShaderCompiled = shaderProgram->addShaderFromSourceCode( |
152 | QOpenGLShader::Fragment, | |
153 | fragmentShaderSource); | |
53 | 154 | if (not fragmentShaderCompiled) |
155 | { | |
156 | log += "\n" + QObject::tr("Fragment shader:") + "\n" + shaderProgram->log(); | |
157 | } | |
158 | if (not vertexShaderCompiled or not fragmentShaderCompiled) | |
159 | { | |
160 | QMessageBox::critical( | |
161 | nullptr, | |
162 | QObject::tr("Shader compile error"), | |
163 | QObject::tr("Could not compile shaders.") + "\n" + log); | |
164 | std::exit(-1); | |
165 | } | |
166 | const bool linkSuccessful = shaderProgram->link(); | |
167 | if (not linkSuccessful) | |
168 | { | |
169 | QMessageBox::critical( | |
170 | nullptr, | |
171 | QObject::tr("Shader link error"), | |
172 | QObject::tr("Could not link shaders: %1").arg(shaderProgram->log()) | |
173 | ); | |
174 | } | |
175 | } | |
176 | ||
26 | 177 | void gl::Compiler::initialize() |
178 | { | |
179 | if (not this->initialized) | |
180 | { | |
181 | this->initializeOpenGLFunctions(); | |
80 | 182 | for (auto& object : this->glObjects) |
26 | 183 | { |
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
|
184 | object.program = new QOpenGLShaderProgram; |
53 | 185 | gl::buildShaders(object.program, ::vertexShaderSource, ::fragmentShaderSource); |
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
|
186 | object.program->bind(); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
187 | object.buffer.create(); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
188 | object.buffer.bind(); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
189 | object.buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
190 | object.vertexArray.create(); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
191 | object.vertexArray.bind(); |
51 | 192 | for (int k : {0, 1, 2, 3, 4}) |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
193 | { |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
194 | object.program->enableAttributeArray(k); |
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
195 | } |
80 | 196 | constexpr int stride = sizeof(Vertex); |
197 | object.program->setAttributeBuffer(0, GL_FLOAT, offsetof(Vertex, position), 3, stride); | |
198 | object.program->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, color), 4, stride); | |
199 | object.program->setAttributeBuffer(2, GL_FLOAT, offsetof(Vertex, normal), 3, stride); | |
200 | glVertexAttribIPointer(3, 1, GL_INT, stride, reinterpret_cast<void*>(offsetof(Vertex, id))); | |
201 | glVertexAttribIPointer(4, 1, GL_INT, stride, reinterpret_cast<void*>(offsetof(Vertex, 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
|
202 | object.vertexArray.release(); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
203 | object.buffer.release(); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
204 | object.program->release(); |
26 | 205 | } |
206 | this->initialized = true; | |
207 | } | |
19 | 208 | } |
21 | 209 | |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
210 | void gl::Compiler::build(DocumentManager* context, const gl::RenderPreferences& preferences) |
21 | 211 | { |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
212 | this->boundingBox = {}; |
80 | 213 | std::vector<Vertex> vboData[gl::NUM_POLYGON_TYPES]; |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
214 | std::optional<ModelId> modelId = context->findIdForModel(this->model); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
215 | if (modelId.has_value()) |
21 | 216 | { |
150
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
217 | PolygonCache* polygonBuilder = context->getPolygonCacheForModel(modelId.value()); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
218 | if (polygonBuilder != nullptr) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
219 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
220 | const std::vector<gl::Polygon> polygons = polygonBuilder->getPolygons(context); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
221 | for (const gl::Polygon& polygon : polygons) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
222 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
223 | this->buildPolygon(polygon, vboData, preferences); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
224 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
225 | for (int arrayId = 0; arrayId < gl::NUM_POLYGON_TYPES; arrayId += 1) |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
226 | { |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
227 | auto& buffer = this->glObjects[arrayId].buffer; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
228 | auto& vector = vboData[arrayId]; |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
229 | this->storedVertexCounts[arrayId] = vector.size(); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
230 | this->glObjects[arrayId].cachedData = vector; // todo: get rid of this copy |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
231 | buffer.bind(); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
232 | buffer.allocate(vector.data(), static_cast<int>(vector.size() * sizeof vector[0])); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
233 | buffer.release(); |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
234 | } |
b6cbba6e29a1
extract polygon cache out of Model
Teemu Piippo <teemu@hecknology.net>
parents:
139
diff
changeset
|
235 | } |
21 | 236 | } |
237 | } | |
238 | ||
26 | 239 | gl::ArrayClass classifyPolygon(const gl::Polygon& polygon) |
21 | 240 | { |
241 | switch (polygon.type) | |
242 | { | |
243 | case gl::Polygon::EdgeLine: | |
26 | 244 | return gl::ArrayClass::Lines; |
21 | 245 | case gl::Polygon::Triangle: |
26 | 246 | return gl::ArrayClass::Triangles; |
21 | 247 | case gl::Polygon::Quadrilateral: |
26 | 248 | return gl::ArrayClass::Quads; |
21 | 249 | case gl::Polygon::ConditionalEdge: |
26 | 250 | return gl::ArrayClass::ConditionalLines; |
21 | 251 | } |
26 | 252 | return gl::ArrayClass::Lines; |
21 | 253 | } |
254 | ||
78
97c3ce5aa498
fixed signed vs unsigned nonsense in gl::Compiler::idFromColor
Teemu Piippo <teemu@hecknology.net>
parents:
73
diff
changeset
|
255 | ldraw::id_t gl::Compiler::idFromColor(const std::array<GLubyte, 3>& data) |
47 | 256 | { |
257 | return {data[0] * std::int32_t{0x10000} + data[1] * std::int32_t{0x100} + data[2]}; | |
258 | } | |
259 | ||
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
260 | void gl::Compiler::buildPolygon( |
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
261 | gl::Polygon polygon, |
80 | 262 | std::vector<Vertex>* vboData, |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
263 | const gl::RenderPreferences& preferences) |
21 | 264 | { |
26 | 265 | const gl::ArrayClass vboClass = classifyPolygon(polygon); |
80 | 266 | std::vector<Vertex>& vertexBuffer = vboData[static_cast<int>(vboClass)]; |
21 | 267 | auto vertexRing = iter::ring(polygon.vertices, polygon.numPolygonVertices()); |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
268 | reserveMore(vertexBuffer, polygon.numPolygonVertices()); |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
269 | const QColor color = this->getColorForPolygon(polygon, preferences); |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
270 | for (unsigned int i = 0; i < polygon.numPolygonVertices(); i += 1) |
21 | 271 | { |
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
272 | const glm::vec3& v1 = vertexRing[i - 1]; |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
273 | const glm::vec3& v2 = vertexRing[i]; |
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
274 | const glm::vec3& v3 = vertexRing[i + 1]; |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
275 | this->boundingBox.consider(polygon.vertices[i]); |
80 | 276 | Vertex& vertex = vertexBuffer.emplace_back(); |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
277 | vertex.position = polygon.vertices[i]; |
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
278 | vertex.normal = glm::normalize(glm::cross(v1 - v2, v3 - v2)); |
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
279 | vertex.color = glm::vec4{color.redF(), color.greenF(), color.blueF(), color.alphaF()}; |
48
3c10f0e2fbe0
added selection highlighting
Teemu Piippo <teemu@hecknology.net>
parents:
47
diff
changeset
|
280 | vertex.id = polygon.id.value; |
21 | 281 | } |
282 | } | |
283 | ||
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
284 | QColor gl::Compiler::getColorForPolygon(const gl::Polygon& polygon, const gl::RenderPreferences& preferences) |
21 | 285 | { |
286 | QColor color; | |
26 | 287 | // For normal colors, use the polygon's color. |
139
72098474d362
Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
288 | if (polygon.color == ldraw::MAIN_COLOR) |
21 | 289 | { |
39
caac957e9834
Main color is now configurable
Teemu Piippo <teemu@hecknology.net>
parents:
37
diff
changeset
|
290 | color = preferences.mainColor; |
21 | 291 | } |
139
72098474d362
Document and refactor colors.cpp and colors.h
Teemu Piippo <teemu@hecknology.net>
parents:
119
diff
changeset
|
292 | else if (polygon.color == ldraw::EDGE_COLOR) |
21 | 293 | { |
26 | 294 | // 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
|
295 | color = luma(preferences.backgroundColor) > (40.0 / 256.0) ? Qt::black : Qt::white; |
21 | 296 | } |
297 | else | |
298 | { | |
26 | 299 | // Not main or edge color, use the polygon's color as is. |
300 | color = this->colorTable[polygon.color].faceColor; | |
21 | 301 | } |
302 | return color; | |
303 | } | |
304 | ||
33
4c41bfe2ec6e
replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents:
30
diff
changeset
|
305 | glm::vec3 gl::Compiler::modelCenter() const |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
306 | { |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
307 | return boxCenter(this->boundingBox); |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
308 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
309 | |
51 | 310 | double gl::Compiler::modelDistance() const |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
311 | { |
51 | 312 | return static_cast<double>(longestMeasure(this->boundingBox)); |
22
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
313 | } |
6da867fa5429
commit work on GL rendering
Teemu Piippo <teemu@hecknology.net>
parents:
21
diff
changeset
|
314 | |
26 | 315 | void gl::Compiler::bindVertexArray(gl::ArrayClass arrayClass) |
316 | { | |
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
|
317 | auto& object = this->glObjects[static_cast<int>(arrayClass)]; |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
318 | object.vertexArray.bind(); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
319 | object.program->bind(); |
26 | 320 | } |
321 | ||
322 | void gl::Compiler::releaseVertexArray(gl::ArrayClass arrayClass) | |
323 | { | |
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
|
324 | auto& object = this->glObjects[static_cast<int>(arrayClass)]; |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
325 | object.program->release(); |
c57fb7a5ffa3
commit work done on plugging vao to the gl renderer, renders nonsense for now
Teemu Piippo <teemu@hecknology.net>
parents:
26
diff
changeset
|
326 | object.vertexArray.release(); |
26 | 327 | } |
328 | ||
73
97df974b5ed5
ldraw::Id is now templated for extra type safety
Teemu Piippo <teemu@hecknology.net>
parents:
53
diff
changeset
|
329 | void gl::Compiler::setSelectedObjects(const QSet<ldraw::id_t> ids) |
51 | 330 | { |
80 | 331 | for (auto& object : this->glObjects) |
51 | 332 | { |
80 | 333 | std::vector<Vertex>& vector = object.cachedData; |
334 | for (Vertex& vertex : vector) | |
51 | 335 | { |
336 | vertex.selected = (ids.contains({vertex.id})) ? 1 : 0; | |
337 | } | |
338 | const GLsizeiptr size = static_cast<int>(vector.size() * sizeof vector[0]); | |
80 | 339 | object.buffer.bind(); |
51 | 340 | glBufferSubData(GL_ARRAY_BUFFER, 0, size, vector.data()); |
80 | 341 | object.buffer.release(); |
51 | 342 | } |
343 | } | |
344 | ||
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
345 | std::size_t gl::Compiler::vertexCount(const gl::ArrayClass arrayClass) const |
28 | 346 | { |
34
1de2b8d64e9f
added some sort of lighting
Teemu Piippo <teemu@hecknology.net>
parents:
33
diff
changeset
|
347 | return this->storedVertexCounts[static_cast<int>(arrayClass)]; |
28 | 348 | } |