1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013, 2014 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> |
|
22 #include "glCompiler.h" |
|
23 #include "ldObject.h" |
|
24 #include "colors.h" |
|
25 #include "ldDocument.h" |
|
26 #include "miscallenous.h" |
|
27 #include "glRenderer.h" |
|
28 #include "dialogs.h" |
|
29 |
|
30 struct GLErrorInfo |
|
31 { |
|
32 GLenum value; |
|
33 QString text; |
|
34 }; |
|
35 |
|
36 static const GLErrorInfo g_GLErrors[] = |
|
37 { |
|
38 { GL_NO_ERROR, "No error" }, |
|
39 { GL_INVALID_ENUM, "Unacceptable enumerator passed" }, |
|
40 { GL_INVALID_VALUE, "Numeric argument out of range" }, |
|
41 { GL_INVALID_OPERATION, "The operation is not allowed to be done in this state" }, |
|
42 { GL_INVALID_FRAMEBUFFER_OPERATION, "Framebuffer object is not complete"}, |
|
43 { GL_OUT_OF_MEMORY, "Out of memory" }, |
|
44 { GL_STACK_UNDERFLOW, "The operation would have caused an underflow" }, |
|
45 { GL_STACK_OVERFLOW, "The operation would have caused an overflow" }, |
|
46 }; |
|
47 |
|
48 CFGENTRY (String, SelectColorBlend, "#0080FF") |
|
49 EXTERN_CFGENTRY (Bool, BlackEdges) |
|
50 EXTERN_CFGENTRY (String, BackgroundColor) |
|
51 |
|
52 static QList<int> g_warnedColors; |
|
53 static const QColor g_BFCFrontColor (64, 192, 80); |
|
54 static const QColor g_BFCBackColor (208, 64, 64); |
|
55 |
|
56 // static QMap<LDObject*, String> g_objectOrigins; |
|
57 |
|
58 // ============================================================================= |
|
59 // |
|
60 void CheckGLErrorImpl (const char* file, int line) |
|
61 { |
|
62 QString errmsg; |
|
63 GLenum errnum = glGetError(); |
|
64 |
|
65 if (errnum == GL_NO_ERROR) |
|
66 return; |
|
67 |
|
68 for (const GLErrorInfo& err : g_GLErrors) |
|
69 { |
|
70 if (err.value == errnum) |
|
71 { |
|
72 errmsg = err.text; |
|
73 break; |
|
74 } |
|
75 } |
|
76 |
|
77 print ("OpenGL ERROR: at %1:%2: %3", Basename (QString (file)), line, errmsg); |
|
78 } |
|
79 |
|
80 // ============================================================================= |
|
81 // |
|
82 GLCompiler::GLCompiler (GLRenderer* renderer) : |
|
83 m_renderer (renderer) |
|
84 { |
|
85 needMerge(); |
|
86 memset (m_vboSizes, 0, sizeof m_vboSizes); |
|
87 } |
|
88 |
|
89 // ============================================================================= |
|
90 // |
|
91 void GLCompiler::initialize() |
|
92 { |
|
93 initializeOpenGLFunctions(); |
|
94 glGenBuffers (g_numVBOs, &m_vbo[0]); |
|
95 CHECK_GL_ERROR(); |
|
96 } |
|
97 |
|
98 // ============================================================================= |
|
99 // |
|
100 GLCompiler::~GLCompiler() |
|
101 { |
|
102 glDeleteBuffers (g_numVBOs, &m_vbo[0]); |
|
103 CHECK_GL_ERROR(); |
|
104 |
|
105 if (m_renderer != null) |
|
106 m_renderer->setCompiler (null); |
|
107 } |
|
108 |
|
109 // ============================================================================= |
|
110 // |
|
111 uint32 GLCompiler::colorToRGB (const QColor& color) |
|
112 { |
|
113 return |
|
114 (color.red() & 0xFF) << 0x00 | |
|
115 (color.green() & 0xFF) << 0x08 | |
|
116 (color.blue() & 0xFF) << 0x10 | |
|
117 (color.alpha() & 0xFF) << 0x18; |
|
118 } |
|
119 |
|
120 // ============================================================================= |
|
121 // |
|
122 QColor GLCompiler::indexColorForID (int id) const |
|
123 { |
|
124 // Calculate a color based from this index. This method caters for |
|
125 // 16777216 objects. I don't think that will be exceeded anytime soon. :) |
|
126 int r = (id / 0x10000) % 0x100, |
|
127 g = (id / 0x100) % 0x100, |
|
128 b = id % 0x100; |
|
129 |
|
130 return QColor (r, g, b); |
|
131 } |
|
132 |
|
133 // ============================================================================= |
|
134 // |
|
135 QColor GLCompiler::getColorForPolygon (LDPolygon& poly, LDObject* topobj, |
|
136 EVBOComplement complement) const |
|
137 { |
|
138 QColor qcol; |
|
139 |
|
140 switch (complement) |
|
141 { |
|
142 case VBOCM_Surfaces: |
|
143 case VBOCM_NumComplements: |
|
144 return QColor(); |
|
145 |
|
146 case VBOCM_BFCFrontColors: |
|
147 qcol = g_BFCFrontColor; |
|
148 break; |
|
149 |
|
150 case VBOCM_BFCBackColors: |
|
151 qcol = g_BFCBackColor; |
|
152 break; |
|
153 |
|
154 case VBOCM_PickColors: |
|
155 return indexColorForID (topobj->id()); |
|
156 |
|
157 case VBOCM_RandomColors: |
|
158 qcol = topobj->randomColor(); |
|
159 break; |
|
160 |
|
161 case VBOCM_NormalColors: |
|
162 if (poly.color == MainColor) |
|
163 { |
|
164 if (topobj->color() == MainColor) |
|
165 qcol = GLRenderer::getMainColor(); |
|
166 else |
|
167 qcol = topobj->color().faceColor(); |
|
168 } |
|
169 else if (poly.color == EdgeColor) |
|
170 { |
|
171 qcol = Luma (QColor (cfg::BackgroundColor)) > 40 ? Qt::black : Qt::white; |
|
172 } |
|
173 else |
|
174 { |
|
175 LDColor col = poly.color; |
|
176 |
|
177 if (col.isValid()) |
|
178 qcol = col.faceColor(); |
|
179 } |
|
180 break; |
|
181 } |
|
182 |
|
183 if (not qcol.isValid()) |
|
184 { |
|
185 // The color was unknown. Use main color to make the polygon at least |
|
186 // not appear pitch-black. |
|
187 if (poly.num != 2 and poly.num != 5) |
|
188 qcol = GLRenderer::getMainColor(); |
|
189 else |
|
190 qcol = Qt::black; |
|
191 |
|
192 // Warn about the unknown color, but only once. |
|
193 if (not g_warnedColors.contains (poly.color)) |
|
194 { |
|
195 print ("Unknown color %1!\n", poly.color); |
|
196 g_warnedColors << poly.color; |
|
197 } |
|
198 |
|
199 return qcol; |
|
200 } |
|
201 |
|
202 double blendAlpha = 0.0; |
|
203 |
|
204 if (topobj->isSelected()) |
|
205 blendAlpha = 1.0; |
|
206 else if (topobj == m_renderer->objectAtCursor()) |
|
207 blendAlpha = 0.5; |
|
208 |
|
209 if (blendAlpha != 0.0) |
|
210 { |
|
211 QColor selcolor (cfg::SelectColorBlend); |
|
212 double denom = blendAlpha + 1.0; |
|
213 qcol.setRed ((qcol.red() + (selcolor.red() * blendAlpha)) / denom); |
|
214 qcol.setGreen ((qcol.green() + (selcolor.green() * blendAlpha)) / denom); |
|
215 qcol.setBlue ((qcol.blue() + (selcolor.blue() * blendAlpha)) / denom); |
|
216 } |
|
217 |
|
218 return qcol; |
|
219 } |
|
220 |
|
221 // ============================================================================= |
|
222 // |
|
223 void GLCompiler::needMerge() |
|
224 { |
|
225 for (int i = 0; i < countof (m_vboChanged); ++i) |
|
226 m_vboChanged[i] = true; |
|
227 } |
|
228 |
|
229 // ============================================================================= |
|
230 // |
|
231 void GLCompiler::stageForCompilation (LDObject* obj) |
|
232 { |
|
233 /* |
|
234 g_objectOrigins[obj] = format ("%1:%2 (%3)", |
|
235 obj->document()->getDisplayName(), obj->lineNumber(), obj->typeName()); |
|
236 */ |
|
237 |
|
238 m_staged << obj; |
|
239 } |
|
240 |
|
241 // ============================================================================= |
|
242 // |
|
243 void GLCompiler::unstage (LDObject* obj) |
|
244 { |
|
245 m_staged.removeOne (obj); |
|
246 } |
|
247 |
|
248 // ============================================================================= |
|
249 // |
|
250 void GLCompiler::compileDocument (LDDocument* doc) |
|
251 { |
|
252 if (doc != null) |
|
253 { |
|
254 for (LDObject* obj : doc->objects()) |
|
255 compileObject (obj); |
|
256 } |
|
257 } |
|
258 |
|
259 // ============================================================================= |
|
260 // |
|
261 void GLCompiler::compileStaged() |
|
262 { |
|
263 RemoveDuplicates (m_staged); |
|
264 |
|
265 for (auto it = m_staged.begin(); it != m_staged.end(); ++it) |
|
266 { |
|
267 if (*it == null) |
|
268 continue; |
|
269 |
|
270 compileObject (*it); |
|
271 } |
|
272 |
|
273 m_staged.clear(); |
|
274 } |
|
275 |
|
276 // ============================================================================= |
|
277 // |
|
278 void GLCompiler::prepareVBO (int vbonum) |
|
279 { |
|
280 // Compile anything that still awaits it |
|
281 compileStaged(); |
|
282 |
|
283 if (not m_vboChanged[vbonum]) |
|
284 return; |
|
285 |
|
286 QVector<GLfloat> vbodata; |
|
287 |
|
288 for (auto it = m_objectInfo.begin(); it != m_objectInfo.end();) |
|
289 { |
|
290 if (it.key() == null) |
|
291 { |
|
292 it = m_objectInfo.erase (it); |
|
293 continue; |
|
294 } |
|
295 |
|
296 if (it.key()->document() == CurrentDocument() |
|
297 and not it.key()->isHidden()) |
|
298 { |
|
299 vbodata += it->data[vbonum]; |
|
300 } |
|
301 |
|
302 ++it; |
|
303 } |
|
304 |
|
305 glBindBuffer (GL_ARRAY_BUFFER, m_vbo[vbonum]); |
|
306 glBufferData (GL_ARRAY_BUFFER, vbodata.size() * sizeof(GLfloat), vbodata.constData(), GL_STATIC_DRAW); |
|
307 glBindBuffer (GL_ARRAY_BUFFER, 0); |
|
308 CHECK_GL_ERROR(); |
|
309 m_vboChanged[vbonum] = false; |
|
310 m_vboSizes[vbonum] = vbodata.size(); |
|
311 } |
|
312 |
|
313 // ============================================================================= |
|
314 // |
|
315 void GLCompiler::dropObject (LDObject* obj) |
|
316 { |
|
317 auto it = m_objectInfo.find (obj); |
|
318 |
|
319 if (it != m_objectInfo.end()) |
|
320 { |
|
321 m_objectInfo.erase (it); |
|
322 needMerge(); |
|
323 } |
|
324 |
|
325 unstage (obj); |
|
326 } |
|
327 |
|
328 // ============================================================================= |
|
329 // |
|
330 void GLCompiler::compileObject (LDObject* obj) |
|
331 { |
|
332 // print ("Compile %1\n", g_objectOrigins[obj]); |
|
333 |
|
334 if (obj == null or obj->document() == null or obj->document()->isImplicit()) |
|
335 return; |
|
336 |
|
337 ObjectVBOInfo info; |
|
338 info.isChanged = true; |
|
339 dropObject (obj); |
|
340 |
|
341 switch (obj->type()) |
|
342 { |
|
343 // Note: We cannot split quads into triangles here, it would mess up the |
|
344 // wireframe view. Quads must go into separate vbos. |
|
345 case OBJ_Triangle: |
|
346 case OBJ_Quad: |
|
347 case OBJ_Line: |
|
348 case OBJ_CondLine: |
|
349 { |
|
350 LDPolygon* poly = obj->getPolygon(); |
|
351 poly->id = obj->id(); |
|
352 compilePolygon (*poly, obj, &info); |
|
353 delete poly; |
|
354 break; |
|
355 } |
|
356 |
|
357 case OBJ_Subfile: |
|
358 { |
|
359 LDSubfile* ref = static_cast<LDSubfile*> (obj); |
|
360 auto data = ref->inlinePolygons(); |
|
361 |
|
362 for (LDPolygon& poly : data) |
|
363 { |
|
364 poly.id = obj->id(); |
|
365 compilePolygon (poly, obj, &info); |
|
366 } |
|
367 break; |
|
368 } |
|
369 |
|
370 default: |
|
371 break; |
|
372 } |
|
373 |
|
374 m_objectInfo[obj] = info; |
|
375 needMerge(); |
|
376 } |
|
377 |
|
378 // ============================================================================= |
|
379 // |
|
380 void GLCompiler::compilePolygon (LDPolygon& poly, LDObject* topobj, ObjectVBOInfo* objinfo) |
|
381 { |
|
382 EVBOSurface surface; |
|
383 int numverts; |
|
384 |
|
385 switch (poly.num) |
|
386 { |
|
387 case 2: surface = VBOSF_Lines; numverts = 2; break; |
|
388 case 3: surface = VBOSF_Triangles; numverts = 3; break; |
|
389 case 4: surface = VBOSF_Quads; numverts = 4; break; |
|
390 case 5: surface = VBOSF_CondLines; numverts = 2; break; |
|
391 default: return; |
|
392 } |
|
393 |
|
394 for (EVBOComplement complement = VBOCM_First; complement < VBOCM_NumComplements; ++complement) |
|
395 { |
|
396 const int vbonum = vboNumber (surface, complement); |
|
397 QVector<GLfloat>& vbodata = objinfo->data[vbonum]; |
|
398 const QColor color = getColorForPolygon (poly, topobj, complement); |
|
399 |
|
400 for (int vert = 0; vert < numverts; ++vert) |
|
401 { |
|
402 if (complement == VBOCM_Surfaces) |
|
403 { |
|
404 // Write coordinates. Apparently Z must be flipped too? |
|
405 vbodata << poly.vertices[vert].x() |
|
406 << -poly.vertices[vert].y() |
|
407 << -poly.vertices[vert].z(); |
|
408 } |
|
409 else |
|
410 { |
|
411 vbodata << ((GLfloat) color.red()) / 255.0f |
|
412 << ((GLfloat) color.green()) / 255.0f |
|
413 << ((GLfloat) color.blue()) / 255.0f |
|
414 << ((GLfloat) color.alpha()) / 255.0f; |
|
415 } |
|
416 } |
|
417 } |
|
418 } |
|
419 |
|
420 void GLCompiler::setRenderer (GLRenderer* renderer) |
|
421 { |
|
422 m_renderer = renderer; |
|
423 } |
|