src/gldraw.cc

changeset 557
04e140bdeb0b
child 560
39085791128f
equal deleted inserted replaced
556:5f4395ec5db0 557:04e140bdeb0b
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 Santeri 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 #include <QGLWidget>
20 #include <QWheelEvent>
21 #include <QMouseEvent>
22 #include <QContextMenuEvent>
23 #include <QInputDialog>
24 #include <QToolTip>
25 #include <QTimer>
26 #include <GL/glu.h>
27
28 #include "main.h"
29 #include "config.h"
30 #include "document.h"
31 #include "gldraw.h"
32 #include "colors.h"
33 #include "gui.h"
34 #include "misc.h"
35 #include "history.h"
36 #include "dialogs.h"
37 #include "addObjectDialog.h"
38 #include "messagelog.h"
39 #include "primitives.h"
40 #include "moc_gldraw.cpp"
41
42 static const LDFixedCameraInfo g_FixedCameras[6] =
43 { {{ 1, 0, 0 }, X, Z, false, false },
44 {{ 0, 0, 0 }, X, Y, false, true },
45 {{ 0, 1, 0 }, Z, Y, true, true },
46 {{ -1, 0, 0 }, X, Z, false, true },
47 {{ 0, 0, 0 }, X, Y, true, true },
48 {{ 0, -1, 0 }, Z, Y, false, true },
49 };
50
51 static const matrix g_circleDrawTransforms[3] =
52 { { 2, 0, 0, 0, 1, 0, 0, 0, 2 },
53 { 2, 0, 0, 0, 0, 2, 0, 1, 0 },
54 { 0, 1, 0, 2, 0, 0, 0, 0, 2 },
55 };
56
57 cfg (String, gl_bgcolor, "#CCCCD9");
58 cfg (String, gl_maincolor, "#707078");
59 cfg (Float, gl_maincolor_alpha, 1.0);
60 cfg (Int, gl_linethickness, 2);
61 cfg (Bool, gl_colorbfc, false);
62 cfg (Int, gl_camera, GLRenderer::EFreeCamera);
63 cfg (Bool, gl_blackedges, false);
64 cfg (Bool, gl_axes, false);
65 cfg (Bool, gl_wireframe, false);
66 cfg (Bool, gl_logostuds, false);
67 cfg (Bool, gl_aa, true);
68
69 // argh
70 const char* g_CameraNames[7] =
71 { QT_TRANSLATE_NOOP ("GLRenderer", "Top"),
72 QT_TRANSLATE_NOOP ("GLRenderer", "Front"),
73 QT_TRANSLATE_NOOP ("GLRenderer", "Left"),
74 QT_TRANSLATE_NOOP ("GLRenderer", "Bottom"),
75 QT_TRANSLATE_NOOP ("GLRenderer", "Back"),
76 QT_TRANSLATE_NOOP ("GLRenderer", "Right"),
77 QT_TRANSLATE_NOOP ("GLRenderer", "Free")
78 };
79
80 const GL::EFixedCamera g_Cameras[7] =
81 { GL::ETopCamera,
82 GL::EFrontCamera,
83 GL::ELeftCamera,
84 GL::EBottomCamera,
85 GL::EBackCamera,
86 GL::ERightCamera,
87 GL::EFreeCamera
88 };
89
90 const struct LDGLAxis
91 { const QColor col;
92 const vertex vert;
93 } g_GLAxes[3] =
94 { { QColor (255, 0, 0), vertex (10000, 0, 0) },
95 { QColor (80, 192, 0), vertex (0, 10000, 0) },
96 { QColor (0, 160, 192), vertex (0, 0, 10000) },
97 };
98
99 static bool g_glInvert = false;
100 static QList<int> g_warnedColors;
101
102 // =============================================================================
103 // -----------------------------------------------------------------------------
104 GLRenderer::GLRenderer (QWidget* parent) : QGLWidget (parent)
105 { m_Picking = m_rangepick = false;
106 m_camera = (GL::EFixedCamera) gl_camera.value;
107 m_drawToolTip = false;
108 m_EditMode = ESelectMode;
109 m_rectdraw = false;
110 m_panning = false;
111 setFile (null);
112 setDrawOnly (false);
113 setMessageLog (null);
114 m_width = m_height = -1;
115 m_hoverpos = g_origin;
116
117 m_toolTipTimer = new QTimer (this);
118 m_toolTipTimer->setSingleShot (true);
119 connect (m_toolTipTimer, SIGNAL (timeout()), this, SLOT (slot_toolTipTimer()));
120
121 m_thickBorderPen = QPen (QColor (0, 0, 0, 208), 2, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);
122 m_thinBorderPen = m_thickBorderPen;
123 m_thinBorderPen.setWidth (1);
124
125 // Init camera icons
126 for (const GL::EFixedCamera cam : g_Cameras)
127 { str iconname = fmt ("camera-%1", tr (g_CameraNames[cam]).toLower());
128
129 CameraIcon* info = &m_cameraIcons[cam];
130 info->img = new QPixmap (getIcon (iconname));
131 info->cam = cam;
132 }
133
134 for (int i = 0; i < 6; ++i)
135 { m_overlays[i].img = null;
136 m_depthValues[i] = 0.0f;
137 }
138
139 calcCameraIcons();
140 }
141
142 // =============================================================================
143 // -----------------------------------------------------------------------------
144 GLRenderer::~GLRenderer()
145 { for (int i = 0; i < 6; ++i)
146 delete m_overlays[i].img;
147
148 for (CameraIcon& info : m_cameraIcons)
149 delete info.img;
150 }
151
152 // =============================================================================
153 // Calculates the "hitboxes" of the camera icons so that we can tell when the
154 // cursor is pointing at the camera icon.
155 // -----------------------------------------------------------------------------
156 void GLRenderer::calcCameraIcons()
157 { int i = 0;
158
159 for (CameraIcon& info : m_cameraIcons)
160 { // MATH
161 const long x1 = (m_width - (info.cam != EFreeCamera ? 48 : 16)) + ((i % 3) * 16) - 1,
162 y1 = ((i / 3) * 16) + 1;
163
164 info.srcRect = QRect (0, 0, 16, 16);
165 info.destRect = QRect (x1, y1, 16, 16);
166 info.selRect = QRect (
167 info.destRect.x(),
168 info.destRect.y(),
169 info.destRect.width() + 1,
170 info.destRect.height() + 1
171 );
172
173 ++i;
174 }
175 }
176
177 // =============================================================================
178 // -----------------------------------------------------------------------------
179 void GLRenderer::initGLData()
180 { glEnable (GL_BLEND);
181 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
182 glEnable (GL_POLYGON_OFFSET_FILL);
183 glPolygonOffset (1.0f, 1.0f);
184
185 glEnable (GL_DEPTH_TEST);
186 glShadeModel (GL_SMOOTH);
187 glEnable (GL_MULTISAMPLE);
188
189 if (gl_aa)
190 { glEnable (GL_LINE_SMOOTH);
191 glEnable (GL_POLYGON_SMOOTH);
192 glHint (GL_LINE_SMOOTH_HINT, GL_NICEST);
193 glHint (GL_POLYGON_SMOOTH_HINT, GL_NICEST);
194 } else
195 { glDisable (GL_LINE_SMOOTH);
196 glDisable (GL_POLYGON_SMOOTH);
197 }
198 }
199
200 // =============================================================================
201 // -----------------------------------------------------------------------------
202 void GLRenderer::resetAngles()
203 { rot (X) = 30.0f;
204 rot (Y) = 325.f;
205 pan (X) = pan (Y) = rot (Z) = 0.0f;
206 zoomToFit();
207 }
208
209 // =============================================================================
210 // -----------------------------------------------------------------------------
211 void GLRenderer::resetAllAngles()
212 { EFixedCamera oldcam = camera();
213
214 for (int i = 0; i < 7; ++i)
215 { setCamera ((EFixedCamera) i);
216 resetAngles();
217 }
218
219 setCamera (oldcam);
220 }
221
222 // =============================================================================
223 // -----------------------------------------------------------------------------
224 void GLRenderer::initializeGL()
225 { setBackground();
226
227 glLineWidth (gl_linethickness);
228
229 setAutoFillBackground (false);
230 setMouseTracking (true);
231 setFocusPolicy (Qt::WheelFocus);
232 compileAllObjects();
233 }
234
235 // =============================================================================
236 // -----------------------------------------------------------------------------
237 QColor GLRenderer::getMainColor()
238 { QColor col (gl_maincolor);
239
240 if (!col.isValid())
241 return QColor (0, 0, 0);
242
243 col.setAlpha (gl_maincolor_alpha * 255.f);
244 return col;
245 }
246
247 // =============================================================================
248 // -----------------------------------------------------------------------------
249 void GLRenderer::setBackground()
250 { QColor col (gl_bgcolor);
251
252 if (!col.isValid())
253 return;
254
255 col.setAlpha (255);
256
257 m_darkbg = luma (col) < 80;
258 m_bgcolor = col;
259 qglClearColor (col);
260 }
261
262 // =============================================================================
263 // -----------------------------------------------------------------------------
264 void GLRenderer::setObjectColor (LDObject* obj, const ListType list)
265 { QColor qcol;
266
267 if (!obj->isColored())
268 return;
269
270 if (list == GL::PickList)
271 { // Make the color by the object's ID if we're picking, so we can make the
272 // ID again from the color we get from the picking results. Be sure to use
273 // the top level parent's index since we want a subfile's children point
274 // to the subfile itself.
275 long i = obj->topLevelParent()->getID();
276
277 // Calculate a color based from this index. This method caters for
278 // 16777216 objects. I don't think that'll be exceeded anytime soon. :)
279 // ATM biggest is 53588.dat with 12600 lines.
280 double r = (i / (256 * 256)) % 256,
281 g = (i / 256) % 256,
282 b = i % 256;
283
284 qglColor (QColor (r, g, b));
285 return;
286 }
287
288 if ((list == BFCFrontList || list == BFCBackList) &&
289 obj->getType() != LDObject::Line &&
290 obj->getType() != LDObject::CondLine)
291 { if (list == GL::BFCFrontList)
292 qcol = QColor (40, 192, 0);
293 else
294 qcol = QColor (224, 0, 0);
295 }
296 else
297 { if (obj->getColor() == maincolor)
298 qcol = getMainColor();
299 else
300 { LDColor* col = getColor (obj->getColor());
301
302 if (col)
303 qcol = col->faceColor;
304 }
305
306 if (obj->getColor() == edgecolor)
307 { qcol = luma (m_bgcolor) < 40 ? QColor (64, 64, 64) : Qt::black;
308 LDColor* col;
309
310 if (!gl_blackedges && obj->getParent() && (col = getColor (obj->getParent()->getColor())))
311 qcol = col->edgeColor;
312 }
313
314 if (qcol.isValid() == false)
315 { // The color was unknown. Use main color to make the object at least
316 // not appear pitch-black.
317 if (obj->getColor() != edgecolor)
318 qcol = getMainColor();
319
320 // Warn about the unknown colors, but only once.
321 for (int i : g_warnedColors)
322 if (obj->getColor() == i)
323 return;
324
325 log ("%1: Unknown color %2!\n", __func__, obj->getColor());
326 g_warnedColors << obj->getColor();
327 return;
328 }
329 }
330
331 long r = qcol.red(),
332 g = qcol.green(),
333 b = qcol.blue(),
334 a = qcol.alpha();
335
336 if (obj->topLevelParent()->isSelected())
337 { // Brighten it up for the select list.
338 const uchar add = 51;
339
340 r = min (r + add, 255l);
341 g = min (g + add, 255l);
342 b = min (b + add, 255l);
343 }
344
345 glColor4f (
346 ((double) r) / 255.0f,
347 ((double) g) / 255.0f,
348 ((double) b) / 255.0f,
349 ((double) a) / 255.0f);
350 }
351
352 // =============================================================================
353 // -----------------------------------------------------------------------------
354 void GLRenderer::refresh()
355 { update();
356 swapBuffers();
357 }
358
359 // =============================================================================
360 // -----------------------------------------------------------------------------
361 void GLRenderer::hardRefresh()
362 { compileAllObjects();
363 refresh();
364
365 glLineWidth (gl_linethickness);
366 }
367
368 // =============================================================================
369 // -----------------------------------------------------------------------------
370 void GLRenderer::resizeGL (int w, int h)
371 { m_width = w;
372 m_height = h;
373
374 calcCameraIcons();
375
376 glViewport (0, 0, w, h);
377 glMatrixMode (GL_PROJECTION);
378 glLoadIdentity();
379 gluPerspective (45.0f, (double) w / (double) h, 1.0f, 10000.0f);
380 glMatrixMode (GL_MODELVIEW);
381 }
382
383 // =============================================================================
384 // -----------------------------------------------------------------------------
385 void GLRenderer::drawGLScene()
386 { if (getFile() == null)
387 return;
388
389 if (gl_wireframe && !isPicking())
390 glPolygonMode (GL_FRONT_AND_BACK, GL_LINE);
391
392 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
393 glEnable (GL_DEPTH_TEST);
394
395 if (m_camera != EFreeCamera)
396 { glMatrixMode (GL_PROJECTION);
397 glPushMatrix();
398
399 glLoadIdentity();
400 glOrtho (-m_virtWidth, m_virtWidth, -m_virtHeight, m_virtHeight, -100.0f, 100.0f);
401 glTranslatef (pan (X), pan (Y), 0.0f);
402
403 if (m_camera != EFrontCamera && m_camera != EBackCamera)
404 { glRotatef (90.0f, g_FixedCameras[camera()].glrotate[0],
405 g_FixedCameras[camera()].glrotate[1],
406 g_FixedCameras[camera()].glrotate[2]);
407 }
408
409 // Back camera needs to be handled differently
410 if (m_camera == GLRenderer::EBackCamera)
411 { glRotatef (180.0f, 1.0f, 0.0f, 0.0f);
412 glRotatef (180.0f, 0.0f, 0.0f, 1.0f);
413 }
414 }
415 else
416 { glMatrixMode (GL_MODELVIEW);
417 glPushMatrix();
418 glLoadIdentity();
419
420 glTranslatef (0.0f, 0.0f, -2.0f);
421 glTranslatef (pan (X), pan (Y), -zoom());
422 glRotatef (rot (X), 1.0f, 0.0f, 0.0f);
423 glRotatef (rot (Y), 0.0f, 1.0f, 0.0f);
424 glRotatef (rot (Z), 0.0f, 0.0f, 1.0f);
425 }
426
427 const GL::ListType list = (!isDrawOnly() && isPicking()) ? PickList : NormalList;
428
429 if (gl_colorbfc && !isPicking() && !isDrawOnly())
430 { glEnable (GL_CULL_FACE);
431
432 for (LDObject* obj : getFile()->getObjects())
433 { if (obj->isHidden())
434 continue;
435
436 glCullFace (GL_BACK);
437 glCallList (obj->glLists[BFCFrontList]);
438
439 glCullFace (GL_FRONT);
440 glCallList (obj->glLists[BFCBackList]);
441 }
442
443 glDisable (GL_CULL_FACE);
444 }
445 else
446 { for (LDObject* obj : getFile()->getObjects())
447 { if (obj->isHidden())
448 continue;
449
450 glCallList (obj->glLists[list]);
451 }
452 }
453
454 if (gl_axes && !isPicking() && !isDrawOnly())
455 glCallList (m_axeslist);
456
457 glPopMatrix();
458 glMatrixMode (GL_MODELVIEW);
459 glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
460 }
461
462 // =============================================================================
463 // -----------------------------------------------------------------------------
464 // This converts a 2D point on the screen to a 3D point in the model. If 'snap'
465 // is true, the 3D point will snap to the current grid.
466 // -----------------------------------------------------------------------------
467 vertex GLRenderer::coordconv2_3 (const QPoint& pos2d, bool snap) const
468 { assert (camera() != EFreeCamera);
469
470 vertex pos3d;
471 const LDFixedCameraInfo* cam = &g_FixedCameras[m_camera];
472 const Axis axisX = cam->axisX;
473 const Axis axisY = cam->axisY;
474 const int negXFac = cam->negX ? -1 : 1,
475 negYFac = cam->negY ? -1 : 1;
476
477 // Calculate cx and cy - these are the LDraw unit coords the cursor is at.
478 double cx = (-m_virtWidth + ((2 * pos2d.x() * m_virtWidth) / m_width) - pan (X));
479 double cy = (m_virtHeight - ((2 * pos2d.y() * m_virtHeight) / m_height) - pan (Y));
480
481 if (snap)
482 { cx = Grid::snap (cx, (Grid::Config) axisX);
483 cy = Grid::snap (cy, (Grid::Config) axisY);
484 }
485
486 cx *= negXFac;
487 cy *= negYFac;
488
489 str tmp;
490 // Create the vertex from the coordinates
491 pos3d[axisX] = tmp.sprintf ("%.3f", cx).toDouble();
492 pos3d[axisY] = tmp.sprintf ("%.3f", cy).toDouble();
493 pos3d[3 - axisX - axisY] = getDepthValue();
494 return pos3d;
495 }
496
497 // =============================================================================
498 // -----------------------------------------------------------------------------
499 // Inverse operation for the above - convert a 3D position to a 2D screen
500 // position
501 // -----------------------------------------------------------------------------
502 QPoint GLRenderer::coordconv3_2 (const vertex& pos3d) const
503 { GLfloat m[16];
504 const LDFixedCameraInfo* cam = &g_FixedCameras[m_camera];
505 const Axis axisX = cam->axisX;
506 const Axis axisY = cam->axisY;
507 const int negXFac = cam->negX ? -1 : 1,
508 negYFac = cam->negY ? -1 : 1;
509
510 glGetFloatv (GL_MODELVIEW_MATRIX, m);
511
512 const double x = pos3d.x();
513 const double y = pos3d.y();
514 const double z = pos3d.z();
515
516 vertex transformed;
517 transformed[X] = (m[0] * x) + (m[1] * y) + (m[2] * z) + m[3];
518 transformed[Y] = (m[4] * x) + (m[5] * y) + (m[6] * z) + m[7];
519 transformed[Z] = (m[8] * x) + (m[9] * y) + (m[10] * z) + m[11];
520
521 double rx = (((transformed[axisX] * negXFac) + m_virtWidth + pan (X)) * m_width) / (2 * m_virtWidth);
522 double ry = (((transformed[axisY] * negYFac) - m_virtHeight + pan (Y)) * m_height) / (2 * m_virtHeight);
523
524 return QPoint (rx, -ry);
525 }
526
527 // =============================================================================
528 // -----------------------------------------------------------------------------
529 void GLRenderer::paintEvent (QPaintEvent* ev)
530 { Q_UNUSED (ev)
531
532 makeCurrent();
533 m_virtWidth = zoom();
534 m_virtHeight = (m_height * m_virtWidth) / m_width;
535
536 initGLData();
537 drawGLScene();
538
539 const QPen textpen = getTextPen();
540 const QBrush polybrush (QColor (64, 192, 0, 128));
541 QPainter paint (this);
542 QFontMetrics metrics = QFontMetrics (QFont());
543 paint.setRenderHint (QPainter::HighQualityAntialiasing);
544
545 // If we wish to only draw the brick, stop here
546 if (isDrawOnly())
547 return;
548
549 if (m_camera != EFreeCamera && !isPicking())
550 { // Paint the overlay image if we have one
551 const LDGLOverlay& overlay = m_overlays[m_camera];
552
553 if (overlay.img != null)
554 { QPoint v0 = coordconv3_2 (m_overlays[m_camera].v0),
555 v1 = coordconv3_2 (m_overlays[m_camera].v1);
556
557 QRect targRect (v0.x(), v0.y(), abs (v1.x() - v0.x()), abs (v1.y() - v0.y())),
558 srcRect (0, 0, overlay.img->width(), overlay.img->height());
559 paint.drawImage (targRect, *overlay.img, srcRect);
560 }
561
562 // Paint the coordinates onto the screen.
563 str text = fmt (tr ("X: %1, Y: %2, Z: %3"), m_hoverpos[X], m_hoverpos[Y], m_hoverpos[Z]);
564
565 QFontMetrics metrics = QFontMetrics (font());
566 QRect textSize = metrics.boundingRect (0, 0, m_width, m_height, Qt::AlignCenter, text);
567
568 paint.setPen (getTextPen());
569 paint.drawText (m_width - textSize.width(), m_height - 16, textSize.width(),
570 textSize.height(), Qt::AlignCenter, text);
571
572 QPen linepen = m_thinBorderPen;
573 linepen.setWidth (2);
574 linepen.setColor (luma (m_bgcolor) < 40 ? Qt::white : Qt::black);
575
576 // If we're drawing, draw the vertices onto the screen.
577 if (getEditMode() == EDrawMode)
578 { int numverts = 4;
579
580 if (!m_rectdraw)
581 numverts = m_drawedVerts.size() + 1;
582
583 if (numverts > 0)
584 { QPoint poly[4];
585 vertex polyverts[4];
586
587 if (!m_rectdraw)
588 { uchar i = 0;
589
590 for (vertex& vert : m_drawedVerts)
591 { poly[i] = coordconv3_2 (vert);
592 polyverts[i] = vert;
593 ++i;
594 }
595
596 // Draw the cursor vertex as the last one in the list.
597 if (numverts <= 4)
598 { poly[i] = coordconv3_2 (m_hoverpos);
599 polyverts[i] = m_hoverpos;
600 }
601 else
602 { numverts = 4;
603 }
604 }
605 else
606 { if (m_drawedVerts.size() > 0)
607 { // Get vertex information from m_rectverts
608 for (int i = 0; i < numverts; ++i)
609 { polyverts[i] = m_rectverts[i];
610 poly[i] = coordconv3_2 (polyverts[i]);
611 }
612 }
613 else
614 { poly[0] = coordconv3_2 (m_hoverpos);
615 polyverts[0] = m_hoverpos;
616 }
617 }
618
619 // Draw the polygon-to-be
620 paint.setPen (linepen);
621 paint.setBrush (polybrush);
622 paint.drawPolygon (poly, numverts);
623
624 // Draw vertex blips
625 for (int i = 0; i < numverts; ++i)
626 { QPoint& blip = poly[i];
627 drawBlip (paint, blip);
628
629 // Draw their coordinates
630 paint.drawText (blip.x(), blip.y() - 8, polyverts[i].stringRep (true));
631 }
632 }
633 }
634 elif (getEditMode() == ECircleMode)
635 { // If we have not specified the center point of the circle yet, preview it on the screen.
636 if (m_drawedVerts.isEmpty())
637 drawBlip (paint, coordconv3_2 (m_hoverpos));
638 else
639 { QVector<vertex> verts, verts2;
640 const double dist0 = getCircleDrawDist (0),
641 dist1 = (m_drawedVerts.size() >= 2) ? getCircleDrawDist (1) : -1;
642 const int segs = lores;
643 const double angleUnit = (2 * pi) / segs;
644 Axis relX, relY;
645 QVector<QPoint> ringpoints, circlepoints, circle2points;
646
647 getRelativeAxes (relX, relY);
648
649 // Calculate the preview positions of vertices
650 for (int i = 0; i < segs; ++i)
651 { vertex v = g_origin;
652 v[relX] = m_drawedVerts[0][relX] + (cos (i * angleUnit) * dist0);
653 v[relY] = m_drawedVerts[0][relY] + (sin (i * angleUnit) * dist0);
654 verts << v;
655
656 if (dist1 != -1)
657 { v[relX] = m_drawedVerts[0][relX] + (cos (i * angleUnit) * dist1);
658 v[relY] = m_drawedVerts[0][relY] + (sin (i * angleUnit) * dist1);
659 verts2 << v;
660 }
661 }
662
663 int i = 0;
664 for (const vertex& v : verts + verts2)
665 { // Calculate the 2D point of the vertex
666 QPoint point = coordconv3_2 (v);
667
668 // Draw a green blip at where it is
669 drawBlip (paint, point);
670
671 // Add it to the list of points for the green ring fill.
672 ringpoints << point;
673
674 // Also add the circle points to separate lists
675 if (i < verts.size())
676 circlepoints << point;
677 else
678 circle2points << point;
679
680 ++i;
681 }
682
683 // Insert the first point as the seventeenth one so that
684 // the ring polygon is closed properly.
685 if (ringpoints.size() >= 16)
686 ringpoints.insert (16, ringpoints[0]);
687
688 // Same for the outer ring. Note that the indices are offset by 1
689 // because of the insertion done above bumps the values.
690 if (ringpoints.size() >= 33)
691 ringpoints.insert (33, ringpoints[17]);
692
693 // Draw the ring
694 paint.setBrush ((m_drawedVerts.size() >= 2) ? polybrush : Qt::NoBrush);
695 paint.setPen (Qt::NoPen);
696 paint.drawPolygon (QPolygon (ringpoints));
697
698 // Draw the circles
699 paint.setBrush (Qt::NoBrush);
700 paint.setPen (linepen);
701 paint.drawPolygon (QPolygon (circlepoints));
702 paint.drawPolygon (QPolygon (circle2points));
703
704 { // Draw the current radius in the middle of the circle.
705 QPoint origin = coordconv3_2 (m_drawedVerts[0]);
706 str label = str::number (dist0);
707 paint.setPen (textpen);
708 paint.drawText (origin.x() - (metrics.width (label) / 2), origin.y(), label);
709
710 if (m_drawedVerts.size() >= 2)
711 { label = str::number (dist1);
712 paint.drawText (origin.x() - (metrics.width (label) / 2), origin.y() + metrics.height(), label);
713 }
714 }
715 }
716 }
717 }
718
719 // Camera icons
720 if (!isPicking())
721 { // Draw a background for the selected camera
722 paint.setPen (m_thinBorderPen);
723 paint.setBrush (QBrush (QColor (0, 128, 160, 128)));
724 paint.drawRect (m_cameraIcons[camera()].selRect);
725
726 // Draw the actual icons
727 for (CameraIcon& info : m_cameraIcons)
728 { // Don't draw the free camera icon when in draw mode
729 if (&info == &m_cameraIcons[GL::EFreeCamera] && getEditMode() != ESelectMode)
730 continue;
731
732 paint.drawPixmap (info.destRect, *info.img, info.srcRect);
733 }
734
735 str fmtstr = tr ("%1 Camera");
736
737 // Draw a label for the current camera in the bottom left corner
738 { const int margin = 4;
739
740 str label;
741 label = fmt (fmtstr, tr (g_CameraNames[camera()]));
742 paint.setPen (textpen);
743 paint.drawText (QPoint (margin, height() - (margin + metrics.descent())), label);
744 }
745
746 // Tool tips
747 if (m_drawToolTip)
748 { if (m_cameraIcons[m_toolTipCamera].destRect.contains (m_pos) == false)
749 m_drawToolTip = false;
750 else
751 { str label = fmt (fmtstr, tr (g_CameraNames[m_toolTipCamera]));
752 QToolTip::showText (m_globalpos, label);
753 }
754 }
755 }
756
757 // Message log
758 if (getMessageLog())
759 { int y = 0;
760 const int margin = 2;
761 QColor penColor = getTextPen();
762
763 for (const MessageManager::Line& line : getMessageLog()->getLines())
764 { penColor.setAlphaF (line.alpha);
765 paint.setPen (penColor);
766 paint.drawText (QPoint (margin, y + margin + metrics.ascent()), line.text);
767 y += metrics.height();
768 }
769 }
770
771 // If we're range-picking, draw a rectangle encompassing the selection area.
772 if (m_rangepick && !isPicking() && m_totalmove >= 10)
773 { int x0 = m_rangeStart.x(),
774 y0 = m_rangeStart.y(),
775 x1 = m_pos.x(),
776 y1 = m_pos.y();
777
778 QRect rect (x0, y0, x1 - x0, y1 - y0);
779 QColor fillColor = (m_addpick ? "#40FF00" : "#00CCFF");
780 fillColor.setAlphaF (0.2f);
781
782 paint.setPen (m_thickBorderPen);
783 paint.setBrush (QBrush (fillColor));
784 paint.drawRect (rect);
785 }
786 }
787
788 // =============================================================================
789 // -----------------------------------------------------------------------------
790 void GLRenderer::drawBlip (QPainter& paint, QPoint pos) const
791 { QPen pen = m_thinBorderPen;
792 const int blipsize = 8;
793 pen.setWidth (1);
794 paint.setPen (pen);
795 paint.setBrush (QColor (64, 192, 0));
796 paint.drawEllipse (pos.x() - blipsize / 2, pos.y() - blipsize / 2, blipsize, blipsize);
797 }
798
799 // =============================================================================
800 // -----------------------------------------------------------------------------
801 QColor GLRenderer::getTextPen () const
802 { return m_darkbg ? Qt::white : Qt::black;
803 }
804
805 // =============================================================================
806 // -----------------------------------------------------------------------------
807 void GLRenderer::compileAllObjects()
808 { if (!getFile())
809 return;
810
811 // Compiling all is a big job, use a busy cursor
812 setCursor (Qt::BusyCursor);
813
814 m_knownVerts.clear();
815
816 for (LDObject* obj : getFile()->getObjects())
817 compileObject (obj);
818
819 // Compile axes
820 glDeleteLists (m_axeslist, 1);
821 m_axeslist = glGenLists (1);
822 glNewList (m_axeslist, GL_COMPILE);
823 glBegin (GL_LINES);
824
825 for (const LDGLAxis& ax : g_GLAxes)
826 { qglColor (ax.col);
827 compileVertex (ax.vert);
828 compileVertex (-ax.vert);
829 }
830
831 glEnd();
832 glEndList();
833
834 setCursor (Qt::ArrowCursor);
835 }
836
837 // =============================================================================
838 // -----------------------------------------------------------------------------
839 void GLRenderer::compileSubObject (LDObject* obj, const GLenum gltype)
840 { glBegin (gltype);
841
842 const int numverts = (obj->getType() != LDObject::CondLine) ? obj->vertices() : 2;
843
844 if (g_glInvert == false)
845 for (int i = 0; i < numverts; ++i)
846 compileVertex (obj->getVertex (i));
847 else
848 for (int i = numverts - 1; i >= 0; --i)
849 compileVertex (obj->getVertex (i));
850
851 glEnd();
852 }
853
854 // =============================================================================
855 // -----------------------------------------------------------------------------
856 void GLRenderer::compileList (LDObject* obj, const GLRenderer::ListType list)
857 { setObjectColor (obj, list);
858
859 switch (obj->getType())
860 { case LDObject::Line:
861 compileSubObject (obj, GL_LINES);
862 break;
863
864 case LDObject::CondLine:
865
866 // Draw conditional lines with a dash pattern - however, use a full
867 // line when drawing a pick list to make selecting them easier.
868 if (list != GL::PickList)
869 { glLineStipple (1, 0x6666);
870 glEnable (GL_LINE_STIPPLE);
871 }
872
873 compileSubObject (obj, GL_LINES);
874
875 glDisable (GL_LINE_STIPPLE);
876 break;
877
878 case LDObject::Triangle:
879 compileSubObject (obj, GL_TRIANGLES);
880 break;
881
882 case LDObject::Quad:
883 compileSubObject (obj, GL_QUADS);
884 break;
885
886 case LDObject::Subfile:
887 { LDSubfile* ref = static_cast<LDSubfile*> (obj);
888 QList<LDObject*> objs;
889
890 objs = ref->inlineContents (
891 LDSubfile::DeepInline |
892 LDSubfile::CacheInline |
893 LDSubfile::RendererInline);
894 bool oldinvert = g_glInvert;
895
896 if (ref->getTransform().getDeterminant() < 0)
897 g_glInvert = !g_glInvert;
898
899 LDObject* prev = ref->prev();
900
901 if (prev && prev->getType() == LDObject::BFC && static_cast<LDBFC*> (prev)->type == LDBFC::InvertNext)
902 g_glInvert = !g_glInvert;
903
904 for (LDObject * obj : objs)
905 { compileList (obj, list);
906 delete obj;
907 }
908
909 g_glInvert = oldinvert;
910 }
911 break;
912
913 default:
914 break;
915 }
916 }
917
918 // =============================================================================
919 // -----------------------------------------------------------------------------
920 void GLRenderer::compileVertex (const vertex& vrt)
921 { glVertex3d (vrt[X], -vrt[Y], -vrt[Z]);
922 }
923
924 // =============================================================================
925 // -----------------------------------------------------------------------------
926 void GLRenderer::clampAngle (double& angle) const
927 { while (angle < 0)
928 angle += 360.0;
929
930 while (angle > 360.0)
931 angle -= 360.0;
932 }
933
934 // =============================================================================
935 // -----------------------------------------------------------------------------
936 void GLRenderer::addDrawnVertex (vertex pos)
937 { // If we picked an already-existing vertex, stop drawing
938 if (getEditMode() != ECircleMode)
939 { for (vertex& vert : m_drawedVerts)
940 { if (vert == pos)
941 { endDraw (true);
942 return;
943 }
944 }
945 }
946
947 m_drawedVerts << pos;
948 }
949
950 // =============================================================================
951 // -----------------------------------------------------------------------------
952 void GLRenderer::mouseReleaseEvent (QMouseEvent* ev)
953 { const bool wasLeft = (m_lastButtons & Qt::LeftButton) && ! (ev->buttons() & Qt::LeftButton),
954 wasRight = (m_lastButtons & Qt::RightButton) && ! (ev->buttons() & Qt::RightButton),
955 wasMid = (m_lastButtons & Qt::MidButton) && ! (ev->buttons() & Qt::MidButton);
956
957 if (m_panning)
958 m_panning = false;
959
960 if (wasLeft)
961 { // Check if we selected a camera icon
962 if (!m_rangepick)
963 { for (CameraIcon & info : m_cameraIcons)
964 { if (info.destRect.contains (ev->pos()))
965 { setCamera (info.cam);
966 goto end;
967 }
968 }
969 }
970
971 switch (getEditMode())
972 { case EDrawMode:
973 { if (m_rectdraw)
974 { if (m_drawedVerts.size() == 2)
975 { endDraw (true);
976 return;
977 }
978 } else
979 { // If we have 4 verts, stop drawing.
980 if (m_drawedVerts.size() >= 4)
981 { endDraw (true);
982 return;
983 }
984
985 if (m_drawedVerts.isEmpty() && ev->modifiers() & Qt::ShiftModifier)
986 { m_rectdraw = true;
987 updateRectVerts();
988 }
989 }
990
991 addDrawnVertex (m_hoverpos);
992 } break;
993
994 case ECircleMode:
995 { if (m_drawedVerts.size() == 3)
996 { endDraw (true);
997 return;
998 }
999
1000 addDrawnVertex (m_hoverpos);
1001 } break;
1002
1003 case ESelectMode:
1004 { if (!isDrawOnly())
1005 { if (m_totalmove < 10)
1006 m_rangepick = false;
1007
1008 if (!m_rangepick)
1009 m_addpick = (m_keymods & Qt::ControlModifier);
1010
1011 if (m_totalmove < 10 || m_rangepick)
1012 pick (ev->x(), ev->y());
1013 }
1014 } break;
1015 }
1016
1017 m_rangepick = false;
1018 }
1019
1020 if (wasMid && getEditMode() != ESelectMode && m_drawedVerts.size() < 4 && m_totalmove < 10)
1021 { // Find the closest vertex to our cursor
1022 double mindist = 1024.0f;
1023 vertex closest;
1024 bool valid = false;
1025
1026 QPoint curspos = coordconv3_2 (m_hoverpos);
1027
1028 for (const vertex& pos3d: m_knownVerts)
1029 { QPoint pos2d = coordconv3_2 (pos3d);
1030
1031 // Measure squared distance
1032 const double dx = abs (pos2d.x() - curspos.x()),
1033 dy = abs (pos2d.y() - curspos.y()),
1034 distsq = (dx * dx) + (dy * dy);
1035
1036 if (distsq >= 1024.0f) // 32.0f ** 2
1037 continue; // too far away
1038
1039 if (distsq < mindist)
1040 { mindist = distsq;
1041 closest = pos3d;
1042 valid = true;
1043
1044 // If it's only 4 pixels away, I think we found our vertex now.
1045 if (distsq <= 16.0f) // 4.0f ** 2
1046 break;
1047 }
1048 }
1049
1050 if (valid)
1051 addDrawnVertex (closest);
1052 }
1053
1054 if (wasRight && !m_drawedVerts.isEmpty())
1055 { // Remove the last vertex
1056 m_drawedVerts.removeLast();
1057
1058 if (m_drawedVerts.isEmpty())
1059 m_rectdraw = false;
1060 }
1061
1062 end:
1063 update();
1064 m_totalmove = 0;
1065 }
1066
1067 // =============================================================================
1068 // -----------------------------------------------------------------------------
1069 void GLRenderer::mousePressEvent (QMouseEvent* ev)
1070 { m_totalmove = 0;
1071
1072 if (ev->modifiers() & Qt::ControlModifier)
1073 { m_rangepick = true;
1074 m_rangeStart.setX (ev->x());
1075 m_rangeStart.setY (ev->y());
1076 m_addpick = (m_keymods & Qt::AltModifier);
1077 ev->accept();
1078 }
1079
1080 m_lastButtons = ev->buttons();
1081 }
1082
1083 // =============================================================================
1084 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1085 // =============================================================================
1086 void GLRenderer::mouseMoveEvent (QMouseEvent* ev)
1087 { int dx = ev->x() - m_pos.x();
1088 int dy = ev->y() - m_pos.y();
1089 m_totalmove += abs (dx) + abs (dy);
1090
1091 const bool left = ev->buttons() & Qt::LeftButton,
1092 mid = ev->buttons() & Qt::MidButton,
1093 shift = ev->modifiers() & Qt::ShiftModifier;
1094
1095 if (mid || (left && shift))
1096 { pan (X) += 0.03f * dx * (zoom() / 7.5f);
1097 pan (Y) -= 0.03f * dy * (zoom() / 7.5f);
1098 m_panning = true;
1099 } elif (left && !m_rangepick && camera() == EFreeCamera)
1100 { rot (X) = rot (X) + dy;
1101 rot (Y) = rot (Y) + dx;
1102
1103 clampAngle (rot (X));
1104 clampAngle (rot (Y));
1105 }
1106
1107 // Start the tool tip timer
1108 if (!m_drawToolTip)
1109 m_toolTipTimer->start (500);
1110
1111 // Update 2d position
1112 m_pos = ev->pos();
1113 m_globalpos = ev->globalPos();
1114
1115 // Calculate 3d position of the cursor
1116 m_hoverpos = (camera() != EFreeCamera) ? coordconv2_3 (m_pos, true) : g_origin;
1117
1118 // Update rect vertices since m_hoverpos may have changed
1119 updateRectVerts();
1120
1121 update();
1122 }
1123
1124 // =============================================================================
1125 // -----------------------------------------------------------------------------
1126 void GLRenderer::keyPressEvent (QKeyEvent* ev)
1127 { m_keymods = ev->modifiers();
1128 }
1129
1130 // =============================================================================
1131 // -----------------------------------------------------------------------------
1132 void GLRenderer::keyReleaseEvent (QKeyEvent* ev)
1133 { m_keymods = ev->modifiers();
1134 }
1135
1136 // =============================================================================
1137 // -----------------------------------------------------------------------------
1138 void GLRenderer::wheelEvent (QWheelEvent* ev)
1139 { makeCurrent();
1140
1141 zoomNotch (ev->delta() > 0);
1142 zoom() = clamp (zoom(), 0.01, 10000.0);
1143
1144 update();
1145 ev->accept();
1146 }
1147
1148 // =============================================================================
1149 // -----------------------------------------------------------------------------
1150 void GLRenderer::leaveEvent (QEvent* ev)
1151 { (void) ev;
1152 m_drawToolTip = false;
1153 m_toolTipTimer->stop();
1154 update();
1155 }
1156
1157 // =============================================================================
1158 // -----------------------------------------------------------------------------
1159 void GLRenderer::contextMenuEvent (QContextMenuEvent* ev)
1160 { g_win->spawnContextMenu (ev->globalPos());
1161 }
1162
1163 // =============================================================================
1164 // -----------------------------------------------------------------------------
1165 void GLRenderer::setCamera (const GLRenderer::EFixedCamera cam)
1166 { m_camera = cam;
1167 gl_camera = (int) cam;
1168 g_win->updateEditModeActions();
1169 }
1170
1171 // =============================================================================
1172 // -----------------------------------------------------------------------------
1173 void GLRenderer::pick (int mouseX, int mouseY)
1174 { GLint viewport[4];
1175 makeCurrent();
1176
1177 // Use particularly thick lines while picking ease up selecting lines.
1178 glLineWidth (max<double> (gl_linethickness, 6.5f));
1179
1180 // Clear the selection if we do not wish to add to it.
1181 if (!m_addpick)
1182 { QList<LDObject*> oldsel = selection();
1183 getCurrentDocument()->clearSelection();
1184
1185 for (LDObject* obj : oldsel)
1186 compileObject (obj);
1187 }
1188
1189 setPicking (true);
1190
1191 // Paint the picking scene
1192 glDisable (GL_DITHER);
1193 glClearColor (1.0f, 1.0f, 1.0f, 1.0f);
1194
1195 drawGLScene();
1196
1197 glGetIntegerv (GL_VIEWPORT, viewport);
1198
1199 int x0 = mouseX,
1200 y0 = mouseY;
1201 int x1, y1;
1202
1203 // Determine how big an area to read - with range picking, we pick by
1204 // the area given, with single pixel picking, we use an 1 x 1 area.
1205 if (m_rangepick)
1206 { x1 = m_rangeStart.x();
1207 y1 = m_rangeStart.y();
1208 }
1209 else
1210 { x1 = x0 + 1;
1211 y1 = y0 + 1;
1212 }
1213
1214 // x0 and y0 must be less than x1 and y1, respectively.
1215 if (x0 > x1)
1216 qSwap (x0, x1);
1217
1218 if (y0 > y1)
1219 qSwap (y0, y1);
1220
1221 // Clamp the values to ensure they're within bounds
1222 x0 = max (0, x0);
1223 y0 = max (0, y0);
1224 x1 = min (x1, m_width);
1225 y1 = min (y1, m_height);
1226
1227 const int areawidth = (x1 - x0);
1228 const int areaheight = (y1 - y0);
1229 const qint32 numpixels = areawidth * areaheight;
1230
1231 // Allocate space for the pixel data.
1232 uchar* const pixeldata = new uchar[4 * numpixels];
1233 uchar* pixelptr = &pixeldata[0];
1234
1235 assert (viewport[3] == m_height);
1236
1237 // Read pixels from the color buffer.
1238 glReadPixels (x0, viewport[3] - y1, areawidth, areaheight, GL_RGBA, GL_UNSIGNED_BYTE, pixeldata);
1239
1240 LDObject* removedObj = null;
1241
1242 // Go through each pixel read and add them to the selection.
1243 for (qint32 i = 0; i < numpixels; ++i)
1244 { qint32 idx =
1245 (*(pixelptr + 0) * 0x10000) +
1246 (*(pixelptr + 1) * 0x00100) +
1247 (*(pixelptr + 2) * 0x00001);
1248 pixelptr += 4;
1249
1250 if (idx == 0xFFFFFF)
1251 continue; // White is background; skip
1252
1253 LDObject* obj = LDObject::fromID (idx);
1254
1255 // If this is an additive single pick and the object is currently selected,
1256 // we remove it from selection instead.
1257 if (!m_rangepick && m_addpick)
1258 { if (obj->isSelected())
1259 { obj->unselect();
1260 removedObj = obj;
1261 break;
1262 }
1263 }
1264
1265 obj->select();
1266 }
1267
1268 delete[] pixeldata;
1269
1270 // Update everything now.
1271 g_win->updateSelection();
1272
1273 // Recompile the objects now to update their color
1274 for (LDObject* obj : selection())
1275 compileObject (obj);
1276
1277 if (removedObj)
1278 compileObject (removedObj);
1279
1280 // Restore line thickness
1281 glLineWidth (gl_linethickness);
1282
1283 setPicking (false);
1284 m_rangepick = false;
1285 glEnable (GL_DITHER);
1286
1287 setBackground();
1288 repaint();
1289 }
1290
1291 // =============================================================================
1292 // -----------------------------------------------------------------------------
1293 void GLRenderer::setEditMode (EditMode const& a)
1294 { m_EditMode = a;
1295
1296 switch (a)
1297 { case ESelectMode:
1298 { unsetCursor();
1299 setContextMenuPolicy (Qt::DefaultContextMenu);
1300 } break;
1301
1302 case EDrawMode:
1303 case ECircleMode:
1304 { // Cannot draw into the free camera - use top instead.
1305 if (m_camera == EFreeCamera)
1306 setCamera (ETopCamera);
1307
1308 // Disable the context menu - we need the right mouse button
1309 // for removing vertices.
1310 setContextMenuPolicy (Qt::NoContextMenu);
1311
1312 // Use the crosshair cursor when drawing.
1313 setCursor (Qt::CrossCursor);
1314
1315 // Clear the selection when beginning to draw.
1316 QList<LDObject*> priorsel = selection();
1317 getCurrentDocument()->clearSelection();
1318
1319 for (LDObject* obj : priorsel)
1320 compileObject (obj);
1321
1322 g_win->updateSelection();
1323 m_drawedVerts.clear();
1324 } break;
1325 }
1326
1327 g_win->updateEditModeActions();
1328 update();
1329 }
1330
1331 void GLRenderer::setFile (LDDocument* const& a)
1332 { m_File = a;
1333
1334 if (a != null)
1335 initOverlaysFromObjects();
1336 }
1337
1338 // =============================================================================
1339 // -----------------------------------------------------------------------------
1340 matrix GLRenderer::getCircleDrawMatrix (double scale)
1341 { matrix transform = g_circleDrawTransforms[camera() % 3];
1342
1343 for (int i = 0; i < 9; ++i)
1344 { if (transform[i] == 2)
1345 transform[i] = scale;
1346 elif (transform[i] == 1 && camera() >= 3)
1347 transform[i] = -1;
1348 }
1349
1350 return transform;
1351 }
1352
1353 // =============================================================================
1354 // -----------------------------------------------------------------------------
1355 void GLRenderer::endDraw (bool accept)
1356 { (void) accept;
1357
1358 // Clean the selection and create the object
1359 QList<vertex>& verts = m_drawedVerts;
1360 QList<LDObject*> objs;
1361
1362 switch (getEditMode())
1363 { case EDrawMode:
1364 { if (m_rectdraw)
1365 { LDQuad* quad = new LDQuad;
1366
1367 // Copy the vertices from m_rectverts
1368 updateRectVerts();
1369
1370 for (int i = 0; i < quad->vertices(); ++i)
1371 quad->setVertex (i, m_rectverts[i]);
1372
1373 quad->setColor (maincolor);
1374 objs << quad;
1375 }
1376 else
1377 { switch (verts.size())
1378 { case 1:
1379 { // 1 vertex - add a vertex object
1380 LDVertex* obj = new LDVertex;
1381 obj->pos = verts[0];
1382 obj->setColor (maincolor);
1383 objs << obj;
1384 } break;
1385
1386 case 2:
1387 { // 2 verts - make a line
1388 LDLine* obj = new LDLine (verts[0], verts[1]);
1389 obj->setColor (edgecolor);
1390 objs << obj;
1391 } break;
1392
1393 case 3:
1394 case 4:
1395 { LDObject* obj = (verts.size() == 3) ?
1396 static_cast<LDObject*> (new LDTriangle) :
1397 static_cast<LDObject*> (new LDQuad);
1398
1399 obj->setColor (maincolor);
1400
1401 for (int i = 0; i < obj->vertices(); ++i)
1402 obj->setVertex (i, verts[i]);
1403
1404 objs << obj;
1405 } break;
1406 }
1407 }
1408 } break;
1409
1410 case ECircleMode:
1411 { const int segs = lores, divs = lores; // TODO: make customizable
1412 double dist0 = getCircleDrawDist (0),
1413 dist1 = getCircleDrawDist (1);
1414 LDDocument* refFile = null;
1415 matrix transform;
1416 bool circleOrDisc = false;
1417
1418 if (dist1 < dist0)
1419 std::swap<double> (dist0, dist1);
1420
1421 if (dist0 == dist1)
1422 { // If the radii are the same, there's no ring space to fill. Use a circle.
1423 refFile = ::getDocument ("4-4edge.dat");
1424 transform = getCircleDrawMatrix (dist0);
1425 circleOrDisc = true;
1426 }
1427 elif (dist0 == 0 || dist1 == 0)
1428 { // If either radii is 0, use a disc.
1429 refFile = ::getDocument ("4-4disc.dat");
1430 transform = getCircleDrawMatrix ((dist0 != 0) ? dist0 : dist1);
1431 circleOrDisc = true;
1432 }
1433 elif (g_RingFinder (dist0, dist1))
1434 { // The ring finder found a solution, use that. Add the component rings to the file.
1435 for (const RingFinder::Component& cmp : g_RingFinder.bestSolution()->getComponents())
1436 { // Get a ref file for this primitive. If we cannot find it in the
1437 // LDraw library, generate it.
1438 if ((refFile = ::getDocument (radialFileName (::Ring, lores, lores, cmp.num))) == null)
1439 { refFile = generatePrimitive (::Ring, lores, lores, cmp.num);
1440 refFile->setImplicit (false);
1441 }
1442
1443 LDSubfile* ref = new LDSubfile;
1444 ref->setFileInfo (refFile);
1445 ref->setTransform (getCircleDrawMatrix (cmp.scale));
1446 ref->setPosition (m_drawedVerts[0]);
1447 ref->setColor (maincolor);
1448 objs << ref;
1449 }
1450 }
1451 else
1452 { // Ring finder failed, last resort: draw the ring with quads
1453 QList<QLineF> c0, c1;
1454 Axis relX, relY, relZ;
1455 getRelativeAxes (relX, relY);
1456 relZ = (Axis) (3 - relX - relY);
1457 double x0 = m_drawedVerts[0][relX],
1458 y0 = m_drawedVerts[0][relY];
1459
1460 vertex templ;
1461 templ[relX] = x0;
1462 templ[relY] = y0;
1463 templ[relZ] = getDepthValue();
1464
1465 // Calculate circle coords
1466 makeCircle (segs, divs, dist0, c0);
1467 makeCircle (segs, divs, dist1, c1);
1468
1469 for (int i = 0; i < segs; ++i)
1470 { vertex v0, v1, v2, v3;
1471 v0 = v1 = v2 = v3 = templ;
1472 v0[relX] += c0[i].x1();
1473 v0[relY] += c0[i].y1();
1474 v1[relX] += c0[i].x2();
1475 v1[relY] += c0[i].y2();
1476 v2[relX] += c1[i].x2();
1477 v2[relY] += c1[i].y2();
1478 v3[relX] += c1[i].x1();
1479 v3[relY] += c1[i].y1();
1480
1481 LDQuad* q = new LDQuad (v0, v1, v2, v3);
1482 q->setColor (maincolor);
1483
1484 // Ensure the quads always are BFC-front towards the camera
1485 if (camera() % 3 <= 0)
1486 q->invert();
1487
1488 objs << q;
1489 }
1490 }
1491
1492 if (circleOrDisc)
1493 { LDSubfile* ref = new LDSubfile;
1494 ref->setFileInfo (refFile);
1495 ref->setTransform (transform);
1496 ref->setPosition (m_drawedVerts[0]);
1497 ref->setColor (maincolor);
1498 objs << ref;
1499 }
1500 } break;
1501
1502 case ESelectMode:
1503 { // this shouldn't happen
1504 assert (false);
1505 return;
1506 } break;
1507 }
1508
1509 if (objs.size() > 0)
1510 { g_win->beginAction (null);
1511
1512 for (LDObject* obj : objs)
1513 { getFile()->addObject (obj);
1514 compileObject (obj);
1515 }
1516
1517 g_win->refresh();
1518 g_win->endAction();
1519 }
1520
1521 m_drawedVerts.clear();
1522 m_rectdraw = false;
1523 }
1524
1525 // =============================================================================
1526 // -----------------------------------------------------------------------------
1527 double GLRenderer::getCircleDrawDist (int pos) const
1528 { assert (m_drawedVerts.size() >= pos + 1);
1529 const vertex& v1 = (m_drawedVerts.size() >= pos + 2) ? m_drawedVerts[pos + 1] : m_hoverpos;
1530 Axis relX, relY;
1531 getRelativeAxes (relX, relY);
1532
1533 const double dx = m_drawedVerts[0][relX] - v1[relX];
1534 const double dy = m_drawedVerts[0][relY] - v1[relY];
1535 return sqrt ((dx * dx) + (dy * dy));
1536 }
1537
1538 // =============================================================================
1539 // -----------------------------------------------------------------------------
1540 void GLRenderer::getRelativeAxes (Axis& relX, Axis& relY) const
1541 { const LDFixedCameraInfo* cam = &g_FixedCameras[m_camera];
1542 relX = cam->axisX;
1543 relY = cam->axisY;
1544 }
1545
1546 // =============================================================================
1547 // -----------------------------------------------------------------------------
1548 static QList<vertex> getVertices (LDObject* obj)
1549 { QList<vertex> verts;
1550
1551 if (obj->vertices() >= 2)
1552 { for (int i = 0; i < obj->vertices(); ++i)
1553 verts << obj->getVertex (i);
1554 } elif (obj->getType() == LDObject::Subfile)
1555 { LDSubfile* ref = static_cast<LDSubfile*> (obj);
1556 QList<LDObject*> objs = ref->inlineContents (LDSubfile::DeepCacheInline);
1557
1558 for (LDObject* obj : objs)
1559 { verts << getVertices (obj);
1560 delete obj;
1561 }
1562 }
1563
1564 return verts;
1565 }
1566
1567 // =============================================================================
1568 // -----------------------------------------------------------------------------
1569 void GLRenderer::compileObject (LDObject* obj)
1570 { deleteLists (obj);
1571
1572 for (const GL::ListType listType : g_glListTypes)
1573 { if (isDrawOnly() && listType != GL::NormalList)
1574 continue;
1575
1576 GLuint list = glGenLists (1);
1577 glNewList (list, GL_COMPILE);
1578
1579 obj->glLists[listType] = list;
1580 compileList (obj, listType);
1581
1582 glEndList();
1583 }
1584
1585 // Mark in known vertices of this object
1586 QList<vertex> verts = getVertices (obj);
1587 m_knownVerts << verts;
1588 removeDuplicates (m_knownVerts);
1589
1590 obj->m_glinit = true;
1591 }
1592
1593 // =============================================================================
1594 // -----------------------------------------------------------------------------
1595 uchar* GLRenderer::getScreencap (int& w, int& h)
1596 { w = m_width;
1597 h = m_height;
1598 uchar* cap = new uchar[4 * w * h];
1599
1600 m_screencap = true;
1601 update();
1602 m_screencap = false;
1603
1604 // Capture the pixels
1605 glReadPixels (0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, cap);
1606
1607 return cap;
1608 }
1609
1610 // =============================================================================
1611 // -----------------------------------------------------------------------------
1612 void GLRenderer::slot_toolTipTimer()
1613 { // We come here if the cursor has stayed in one place for longer than a
1614 // a second. Check if we're holding it over a camera icon - if so, draw
1615 // a tooltip.
1616 for (CameraIcon & icon : m_cameraIcons)
1617 { if (icon.destRect.contains (m_pos))
1618 { m_toolTipCamera = icon.cam;
1619 m_drawToolTip = true;
1620 update();
1621 break;
1622 }
1623 }
1624 }
1625
1626 // =============================================================================
1627 // -----------------------------------------------------------------------------
1628 void GLRenderer::deleteLists (LDObject* obj)
1629 { // Delete the lists but only if they have been initialized
1630 if (!obj->m_glinit)
1631 return;
1632
1633 for (const GL::ListType listType : g_glListTypes)
1634 glDeleteLists (obj->glLists[listType], 1);
1635
1636 obj->m_glinit = false;
1637 }
1638
1639 // =============================================================================
1640 // -----------------------------------------------------------------------------
1641 Axis GLRenderer::getCameraAxis (bool y, GLRenderer::EFixedCamera camid)
1642 { if (camid == (GL::EFixedCamera) - 1)
1643 camid = m_camera;
1644
1645 const LDFixedCameraInfo* cam = &g_FixedCameras[camid];
1646 return (y) ? cam->axisY : cam->axisX;
1647 }
1648
1649 // =============================================================================
1650 // -----------------------------------------------------------------------------
1651 bool GLRenderer::setupOverlay (EFixedCamera cam, str file, int x, int y, int w, int h)
1652 { QImage* img = new QImage (file);
1653 LDGLOverlay& info = getOverlay (cam);
1654
1655 if (img->isNull())
1656 { critical (tr ("Failed to load overlay image!"));
1657 delete img;
1658 return false;
1659 }
1660
1661 delete info.img; // delete the old image
1662
1663 info.fname = file;
1664 info.lw = w;
1665 info.lh = h;
1666 info.ox = x;
1667 info.oy = y;
1668 info.img = img;
1669
1670 if (info.lw == 0)
1671 info.lw = (info.lh * img->width()) / img->height();
1672 elif (info.lh == 0)
1673 info.lh = (info.lw * img->height()) / img->width();
1674
1675 const Axis x2d = getCameraAxis (false, cam),
1676 y2d = getCameraAxis (true, cam);
1677 const double negXFac = g_FixedCameras[cam].negX ? -1 : 1,
1678 negYFac = g_FixedCameras[cam].negY ? -1 : 1;
1679
1680 info.v0 = info.v1 = g_origin;
1681 info.v0[x2d] = - (info.ox * info.lw * negXFac) / img->width();
1682 info.v0[y2d] = (info.oy * info.lh * negYFac) / img->height();
1683 info.v1[x2d] = info.v0[x2d] + info.lw;
1684 info.v1[y2d] = info.v0[y2d] + info.lh;
1685
1686 // Set alpha of all pixels to 0.5
1687 for (long i = 0; i < img->width(); ++i)
1688 for (long j = 0; j < img->height(); ++j)
1689 { uint32 pixel = img->pixel (i, j);
1690 img->setPixel (i, j, 0x80000000 | (pixel & 0x00FFFFFF));
1691 }
1692
1693 updateOverlayObjects();
1694 return true;
1695 }
1696
1697 // =============================================================================
1698 // -----------------------------------------------------------------------------
1699 void GLRenderer::clearOverlay()
1700 { if (camera() == EFreeCamera)
1701 return;
1702
1703 LDGLOverlay& info = m_overlays[camera()];
1704 delete info.img;
1705 info.img = null;
1706
1707 updateOverlayObjects();
1708 }
1709
1710 // =============================================================================
1711 // -----------------------------------------------------------------------------
1712 void GLRenderer::setDepthValue (double depth)
1713 { assert (camera() < EFreeCamera);
1714 m_depthValues[camera()] = depth;
1715 }
1716
1717 // =============================================================================
1718 // -----------------------------------------------------------------------------
1719 double GLRenderer::getDepthValue() const
1720 { assert (camera() < EFreeCamera);
1721 return m_depthValues[camera()];
1722 }
1723
1724 // =============================================================================
1725 // -----------------------------------------------------------------------------
1726 const char* GLRenderer::getCameraName() const
1727 { return g_CameraNames[camera()];
1728 }
1729
1730 // =============================================================================
1731 // -----------------------------------------------------------------------------
1732 LDGLOverlay& GLRenderer::getOverlay (int newcam)
1733 { return m_overlays[newcam];
1734 }
1735
1736 // =============================================================================
1737 // -----------------------------------------------------------------------------
1738 void GLRenderer::zoomNotch (bool inward)
1739 { if (zoom() > 15)
1740 zoom() *= inward ? 0.833f : 1.2f;
1741 else
1742 zoom() += inward ? -1.2f : 1.2f;
1743 }
1744
1745 // =============================================================================
1746 // -----------------------------------------------------------------------------
1747 void GLRenderer::zoomToFit()
1748 { if (getFile() == null || m_width == -1 || m_height == -1)
1749 { zoom() = 30.0f;
1750 return;
1751 }
1752
1753 bool lastfilled = false;
1754 bool firstrun = true;
1755 const uint32 white = 0xFFFFFFFF;
1756 bool inward = true;
1757 const int w = m_width, h = m_height;
1758
1759 glClearColor (1.0, 1.0, 1.0, 1.0);
1760 glDisable (GL_DITHER);
1761
1762 // Use the pick list while drawing the scene, this way we can tell whether borders
1763 // are background or not.
1764 setPicking (true);
1765
1766 for (;;)
1767 { if (zoom() > 10000.0 || zoom() < 0.0)
1768 { // Obviously, there's nothing to draw if we get here.
1769 // Default to 30.0f and break out.
1770 zoom() = 30.0;
1771 break;
1772 }
1773
1774 zoomNotch (inward);
1775
1776 uchar* cap = new uchar[4 * w * h];
1777 drawGLScene();
1778 glReadPixels (0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, cap);
1779 uint32* imgdata = reinterpret_cast<uint32*> (cap);
1780 bool filled = false;
1781
1782 // Check the top and bottom rows
1783 for (int i = 0; i < w && !filled; ++i)
1784 if (imgdata[i] != white || imgdata[((h - 1) * w) + i] != white)
1785 filled = true;
1786
1787 // Left and right edges
1788 for (int i = 0; i < h && !filled; ++i)
1789 if (imgdata[i * w] != white || imgdata[(i * w) + w - 1] != white)
1790 filled = true;
1791
1792 delete[] cap;
1793
1794 if (firstrun)
1795 { // If this is the first run, we don't know enough to determine
1796 // whether the zoom was to fit, so we mark in our knowledge so
1797 // far and start over.
1798 inward = !filled;
1799 firstrun = false;
1800 }
1801 else
1802 { // If this run filled the screen and the last one did not, the
1803 // last run had ideal zoom - zoom a bit back and we should reach it.
1804 if (filled && !lastfilled)
1805 { zoomNotch (false);
1806 break;
1807 }
1808
1809 // If this run did not fill the screen and the last one did, we've
1810 // now reached ideal zoom so we're done here.
1811 if (!filled && lastfilled)
1812 break;
1813
1814 inward = !filled;
1815 }
1816
1817 lastfilled = filled;
1818 }
1819
1820 setBackground();
1821 setPicking (false);
1822 }
1823
1824 // =============================================================================
1825 // -----------------------------------------------------------------------------
1826 void GLRenderer::zoomAllToFit()
1827 { EFixedCamera oldcam = camera();
1828
1829 for (int i = 0; i < 7; ++i)
1830 { setCamera ((EFixedCamera) i);
1831 zoomToFit();
1832 }
1833
1834 setCamera (oldcam);
1835 }
1836
1837 // =============================================================================
1838 // -----------------------------------------------------------------------------
1839 void GLRenderer::updateRectVerts()
1840 { if (!m_rectdraw)
1841 return;
1842
1843 if (m_drawedVerts.isEmpty())
1844 { for (int i = 0; i < 4; ++i)
1845 m_rectverts[i] = m_hoverpos;
1846
1847 return;
1848 }
1849
1850 vertex v0 = m_drawedVerts[0],
1851 v1 = (m_drawedVerts.size() >= 2) ? m_drawedVerts[1] : m_hoverpos;
1852
1853 const Axis ax = getCameraAxis (false),
1854 ay = getCameraAxis (true),
1855 az = (Axis) (3 - ax - ay);
1856
1857 for (int i = 0; i < 4; ++i)
1858 m_rectverts[i][az] = getDepthValue();
1859
1860 m_rectverts[0][ax] = v0[ax];
1861 m_rectverts[0][ay] = v0[ay];
1862 m_rectverts[1][ax] = v1[ax];
1863 m_rectverts[1][ay] = v0[ay];
1864 m_rectverts[2][ax] = v1[ax];
1865 m_rectverts[2][ay] = v1[ay];
1866 m_rectverts[3][ax] = v0[ax];
1867 m_rectverts[3][ay] = v1[ay];
1868 }
1869
1870 // =============================================================================
1871 // -----------------------------------------------------------------------------
1872 void GLRenderer::mouseDoubleClickEvent (QMouseEvent* ev)
1873 { if (!(ev->buttons() & Qt::LeftButton) || getEditMode() != ESelectMode)
1874 return;
1875
1876 pick (ev->x(), ev->y());
1877
1878 if (selection().isEmpty())
1879 return;
1880
1881 g_win->beginAction (null);
1882 LDObject* obj = selection().first();
1883 AddObjectDialog::staticDialog (obj->getType(), obj);
1884 g_win->endAction();
1885 ev->accept();
1886 }
1887
1888 // =============================================================================
1889 // -----------------------------------------------------------------------------
1890 LDOverlay* GLRenderer::findOverlayObject (EFixedCamera cam)
1891 { LDOverlay* ovlobj = null;
1892
1893 for (LDObject * obj : getFile()->getObjects())
1894 { if (obj->getType() == LDObject::Overlay && static_cast<LDOverlay*> (obj)->getCamera() == cam)
1895 { ovlobj = static_cast<LDOverlay*> (obj);
1896 break;
1897 }
1898 }
1899
1900 return ovlobj;
1901 }
1902
1903 // =============================================================================
1904 // -----------------------------------------------------------------------------
1905 // Read in overlays from the current file and update overlay info accordingly.
1906 // -----------------------------------------------------------------------------
1907 void GLRenderer::initOverlaysFromObjects()
1908 { for (EFixedCamera cam : g_Cameras)
1909 { if (cam == EFreeCamera)
1910 continue;
1911
1912 LDGLOverlay& meta = m_overlays[cam];
1913 LDOverlay* ovlobj = findOverlayObject (cam);
1914
1915 if (!ovlobj && meta.img)
1916 { delete meta.img;
1917 meta.img = null;
1918 } elif (ovlobj && (!meta.img || meta.fname != ovlobj->getFileName()))
1919 setupOverlay (cam, ovlobj->getFileName(), ovlobj->getX(),
1920 ovlobj->getY(), ovlobj->getWidth(), ovlobj->getHeight());
1921 }
1922 }
1923
1924 // =============================================================================
1925 // -----------------------------------------------------------------------------
1926 void GLRenderer::updateOverlayObjects()
1927 { for (EFixedCamera cam : g_Cameras)
1928 { if (cam == EFreeCamera)
1929 continue;
1930
1931 LDGLOverlay& meta = m_overlays[cam];
1932 LDOverlay* ovlobj = findOverlayObject (cam);
1933
1934 if (!meta.img && ovlobj)
1935 { // If this is the last overlay image, we need to remove the empty space after it as well.
1936 LDObject* nextobj = ovlobj->next();
1937
1938 if (nextobj && nextobj->getType() == LDObject::Empty)
1939 { getFile()->forgetObject (nextobj);
1940 delete nextobj;
1941 }
1942
1943 // If the overlay object was there and the overlay itself is
1944 // not, remove the object.
1945 getFile()->forgetObject (ovlobj);
1946 delete ovlobj;
1947 } elif (meta.img && !ovlobj)
1948 { // Inverse case: image is there but the overlay object is
1949 // not, thus create the object.
1950 ovlobj = new LDOverlay;
1951
1952 // Find a suitable position to place this object. We want to place
1953 // this into the header, which is everything up to the first scemantic
1954 // object. If we find another overlay object, place this object after
1955 // the last one found. Otherwise, place it before the first schemantic
1956 // object and put an empty object after it (though don't do this if
1957 // there was no schemantic elements at all)
1958 int i, lastOverlay = -1;
1959 bool found = false;
1960
1961 for (i = 0; i < getFile()->getObjectCount(); ++i)
1962 { LDObject* obj = getFile()->getObject (i);
1963
1964 if (obj->isScemantic())
1965 { found = true;
1966 break;
1967 }
1968
1969 if (obj->getType() == LDObject::Overlay)
1970 lastOverlay = i;
1971 }
1972
1973 if (lastOverlay != -1)
1974 getFile()->insertObj (lastOverlay + 1, ovlobj);
1975 else
1976 { getFile()->insertObj (i, ovlobj);
1977
1978 if (found)
1979 getFile()->insertObj (i + 1, new LDEmpty);
1980 }
1981 }
1982
1983 if (meta.img && ovlobj)
1984 { ovlobj->setCamera (cam);
1985 ovlobj->setFileName (meta.fname);
1986 ovlobj->setX (meta.ox);
1987 ovlobj->setY (meta.oy);
1988 ovlobj->setWidth (meta.lw);
1989 ovlobj->setHeight (meta.lh);
1990 }
1991 }
1992
1993 if (g_win->R() == this)
1994 g_win->refresh();
1995 }

mercurial