src/gldraw.cc

changeset 574
10874674fe30
parent 568
2fb6c500ff9e
child 577
17a88e2470c9
equal deleted inserted replaced
573:74d71c6fdc20 574:10874674fe30
46 {{ -1, 0, 0 }, X, Z, false, true }, 46 {{ -1, 0, 0 }, X, Z, false, true },
47 {{ 0, 0, 0 }, X, Y, true, true }, 47 {{ 0, 0, 0 }, X, Y, true, true },
48 {{ 0, -1, 0 }, Z, Y, false, true }, 48 {{ 0, -1, 0 }, Z, Y, false, true },
49 }; 49 };
50 50
51 // Matrix templates for circle drawing. 2 is substituted with
52 // the scale value, 1 is inverted to -1 if needed.
51 static const matrix g_circleDrawTransforms[3] = 53 static const matrix g_circleDrawTransforms[3] =
52 { { 2, 0, 0, 0, 1, 0, 0, 0, 2 }, 54 { { 2, 0, 0, 0, 1, 0, 0, 0, 2 },
53 { 2, 0, 0, 0, 0, 2, 0, 1, 0 }, 55 { 2, 0, 0, 0, 0, 2, 0, 1, 0 },
54 { 0, 1, 0, 2, 0, 0, 0, 0, 2 }, 56 { 0, 1, 0, 2, 0, 0, 0, 0, 2 },
55 }; 57 };
56 58
57 cfg (String, gl_bgcolor, "#CCCCD9"); 59 cfg (String, gl_bgcolor, "#CCCCD9");
58 cfg (String, gl_maincolor, "#707078"); 60 cfg (String, gl_maincolor, "#707078");
59 cfg (Float, gl_maincolor_alpha, 1.0); 61 cfg (Float, gl_maincolor_alpha, 1.0);
60 cfg (Int, gl_linethickness, 2); 62 cfg (Int, gl_linethickness, 2);
61 cfg (Bool, gl_colorbfc, false); 63 cfg (Bool, gl_colorbfc, false);
62 cfg (Int, gl_camera, GLRenderer::EFreeCamera); 64 cfg (Int, gl_camera, GLRenderer::EFreeCamera);
63 cfg (Bool, gl_blackedges, false); 65 cfg (Bool, gl_blackedges, false);
64 cfg (Bool, gl_axes, false); 66 cfg (Bool, gl_axes, false);
65 cfg (Bool, gl_wireframe, false); 67 cfg (Bool, gl_wireframe, false);
66 cfg (Bool, gl_logostuds, false); 68 cfg (Bool, gl_logostuds, false);
67 cfg (Bool, gl_aa, true); 69 cfg (Bool, gl_aa, true);
70 cfg (Bool, gl_linelengths, true);
68 71
69 // argh 72 // argh
70 const char* g_CameraNames[7] = 73 const char* g_CameraNames[7] =
71 { QT_TRANSLATE_NOOP ("GLRenderer", "Top"), 74 { QT_TRANSLATE_NOOP ("GLRenderer", "Top"),
72 QT_TRANSLATE_NOOP ("GLRenderer", "Front"), 75 QT_TRANSLATE_NOOP ("GLRenderer", "Front"),
85 GL::EBackCamera, 88 GL::EBackCamera,
86 GL::ERightCamera, 89 GL::ERightCamera,
87 GL::EFreeCamera 90 GL::EFreeCamera
88 }; 91 };
89 92
93 // Definitions for visual axes, drawn on the screen
90 const struct LDGLAxis 94 const struct LDGLAxis
91 { const QColor col; 95 { const QColor col;
92 const vertex vert; 96 const vertex vert;
93 } g_GLAxes[3] = 97 } g_GLAxes[3] =
94 { { QColor (255, 0, 0), vertex (10000, 0, 0) }, 98 { { QColor (255, 0, 0), vertex (10000, 0, 0) }, // X
95 { QColor (80, 192, 0), vertex (0, 10000, 0) }, 99 { QColor (80, 192, 0), vertex (0, 10000, 0) }, // Y
96 { QColor (0, 160, 192), vertex (0, 0, 10000) }, 100 { QColor (0, 160, 192), vertex (0, 0, 10000) }, // Z
97 }; 101 };
98 102
99 static bool g_glInvert = false; 103 static bool g_glInvert = false;
100 static QList<int> g_warnedColors; 104 static QList<int> g_warnedColors;
101 105
487 } 491 }
488 492
489 cx *= negXFac; 493 cx *= negXFac;
490 cy *= negYFac; 494 cy *= negYFac;
491 495
492 str tmp; 496 roundToDecimals (cx, 4);
497 roundToDecimals (cy, 4);
498
493 // Create the vertex from the coordinates 499 // Create the vertex from the coordinates
494 pos3d[axisX] = tmp.sprintf ("%.3f", cx).toDouble(); 500 pos3d[axisX] = cx;
495 pos3d[axisY] = tmp.sprintf ("%.3f", cy).toDouble(); 501 pos3d[axisY] = cy;
496 pos3d[3 - axisX - axisY] = getDepthValue(); 502 pos3d[3 - axisX - axisY] = getDepthValue();
497 return pos3d; 503 return pos3d;
498 } 504 }
499 505
500 // ============================================================================= 506 // =============================================================================
501 // ----------------------------------------------------------------------------- 507 // -----------------------------------------------------------------------------
502 // Inverse operation for the above - convert a 3D position to a 2D screen 508 // Inverse operation for the above - convert a 3D position to a 2D screen
503 // position 509 // position. Don't ask me how this code manages to work, I don't even know.
504 // ----------------------------------------------------------------------------- 510 // -----------------------------------------------------------------------------
505 QPoint GLRenderer::coordconv3_2 (const vertex& pos3d) const 511 QPoint GLRenderer::coordconv3_2 (const vertex& pos3d) const
506 { GLfloat m[16]; 512 { GLfloat m[16];
507 const LDFixedCameraInfo* cam = &g_FixedCameras[m_camera]; 513 const LDFixedCameraInfo* cam = &g_FixedCameras[m_camera];
508 const Axis axisX = cam->axisX; 514 const Axis axisX = cam->axisX;
537 m_virtHeight = (m_height * m_virtWidth) / m_width; 543 m_virtHeight = (m_height * m_virtWidth) / m_width;
538 544
539 initGLData(); 545 initGLData();
540 drawGLScene(); 546 drawGLScene();
541 547
542 const QPen textpen = getTextPen(); 548 const QPen textpen (m_darkbg ? Qt::white : Qt::black);
543 const QBrush polybrush (QColor (64, 192, 0, 128)); 549 const QBrush polybrush (QColor (64, 192, 0, 128));
544 QPainter paint (this); 550 QPainter paint (this);
545 QFontMetrics metrics = QFontMetrics (QFont()); 551 QFontMetrics metrics = QFontMetrics (QFont());
546 paint.setRenderHint (QPainter::HighQualityAntialiasing); 552 paint.setRenderHint (QPainter::HighQualityAntialiasing);
547 553
566 str text = fmt (tr ("X: %1, Y: %2, Z: %3"), m_hoverpos[X], m_hoverpos[Y], m_hoverpos[Z]); 572 str text = fmt (tr ("X: %1, Y: %2, Z: %3"), m_hoverpos[X], m_hoverpos[Y], m_hoverpos[Z]);
567 573
568 QFontMetrics metrics = QFontMetrics (font()); 574 QFontMetrics metrics = QFontMetrics (font());
569 QRect textSize = metrics.boundingRect (0, 0, m_width, m_height, Qt::AlignCenter, text); 575 QRect textSize = metrics.boundingRect (0, 0, m_width, m_height, Qt::AlignCenter, text);
570 576
571 paint.setPen (getTextPen()); 577 paint.setPen (textpen);
572 paint.drawText (m_width - textSize.width(), m_height - 16, textSize.width(), 578 paint.drawText (m_width - textSize.width(), m_height - 16, textSize.width(),
573 textSize.height(), Qt::AlignCenter, text); 579 textSize.height(), Qt::AlignCenter, text);
574 580
575 QPen linepen = m_thinBorderPen; 581 QPen linepen = m_thinBorderPen;
576 linepen.setWidth (2); 582 linepen.setWidth (2);
583 if (!m_rectdraw) 589 if (!m_rectdraw)
584 numverts = m_drawedVerts.size() + 1; 590 numverts = m_drawedVerts.size() + 1;
585 591
586 if (numverts > 0) 592 if (numverts > 0)
587 { QPoint poly[4]; 593 { QPoint poly[4];
588 vertex polyverts[4]; 594 vertex poly3d[4];
589 595
590 if (!m_rectdraw) 596 if (!m_rectdraw)
591 { uchar i = 0; 597 { int i = 0;
592 598
593 for (vertex& vert : m_drawedVerts) 599 for (vertex& vert : m_drawedVerts)
594 { poly[i] = coordconv3_2 (vert); 600 poly3d[i++] = vert;
595 polyverts[i] = vert;
596 ++i;
597 }
598 601
599 // Draw the cursor vertex as the last one in the list. 602 // Draw the cursor vertex as the last one in the list.
600 if (numverts <= 4) 603 if (numverts <= 4)
601 { poly[i] = coordconv3_2 (m_hoverpos); 604 poly3d[i] = m_hoverpos;
602 polyverts[i] = m_hoverpos;
603 }
604 else 605 else
605 { numverts = 4; 606 numverts = 4;
606 }
607 } 607 }
608 else 608 else
609 { if (m_drawedVerts.size() > 0) 609 { // Get vertex information from m_rectverts
610 { // Get vertex information from m_rectverts 610 if (m_drawedVerts.size() > 0)
611 for (int i = 0; i < numverts; ++i) 611 for (int i = 0; i < numverts; ++i)
612 { polyverts[i] = m_rectverts[i]; 612 poly3d[i] = m_rectverts[i];
613 poly[i] = coordconv3_2 (polyverts[i]);
614 }
615 }
616 else 613 else
617 { poly[0] = coordconv3_2 (m_hoverpos); 614 poly3d[0] = m_hoverpos;
618 polyverts[0] = m_hoverpos;
619 }
620 } 615 }
616
617 // convert to 2d
618 for (int i = 0; i < numverts; ++i)
619 poly[i] = coordconv3_2 (poly3d[i]);
621 620
622 // Draw the polygon-to-be 621 // Draw the polygon-to-be
623 paint.setPen (linepen); 622 paint.setPen (linepen);
624 paint.setBrush (polybrush); 623 paint.setBrush (polybrush);
625 paint.drawPolygon (poly, numverts); 624 paint.drawPolygon (poly, numverts);
628 for (int i = 0; i < numverts; ++i) 627 for (int i = 0; i < numverts; ++i)
629 { QPoint& blip = poly[i]; 628 { QPoint& blip = poly[i];
630 drawBlip (paint, blip); 629 drawBlip (paint, blip);
631 630
632 // Draw their coordinates 631 // Draw their coordinates
633 paint.drawText (blip.x(), blip.y() - 8, polyverts[i].stringRep (true)); 632 paint.drawText (blip.x(), blip.y() - 8, poly3d[i].stringRep (true));
633 }
634
635 // Draw line lengths
636 if (numverts >= 2 && gl_linelengths)
637 { int numlines = (m_drawedVerts.size() == 1) ? 1 : m_drawedVerts.size() + 1;
638
639 for (int i = 0; i < numlines; ++i)
640 { const int j = (i + 1 < numverts) ? i + 1 : 0;
641 const str label = str::number (poly3d[i].distanceTo (poly3d[j]));
642 paint.setPen (textpen);
643 paint.drawText (QLineF (poly[i], poly[j]).pointAt (0.5), label);
644 }
634 } 645 }
635 } 646 }
636 } 647 }
637 elif (getEditMode() == ECircleMode) 648 elif (getEditMode() == ECircleMode)
638 { // If we have not specified the center point of the circle yet, preview it on the screen. 649 { // If we have not specified the center point of the circle yet, preview it on the screen.
759 770
760 // Message log 771 // Message log
761 if (getMessageLog()) 772 if (getMessageLog())
762 { int y = 0; 773 { int y = 0;
763 const int margin = 2; 774 const int margin = 2;
764 QColor penColor = getTextPen(); 775 QColor penColor = textpen.color();
765 776
766 for (const MessageManager::Line& line : getMessageLog()->getLines()) 777 for (const MessageManager::Line& line : getMessageLog()->getLines())
767 { penColor.setAlphaF (line.alpha); 778 { penColor.setAlphaF (line.alpha);
768 paint.setPen (penColor); 779 paint.setPen (penColor);
769 paint.drawText (QPoint (margin, y + margin + metrics.ascent()), line.text); 780 paint.drawText (QPoint (margin, y + margin + metrics.ascent()), line.text);
795 const int blipsize = 8; 806 const int blipsize = 8;
796 pen.setWidth (1); 807 pen.setWidth (1);
797 paint.setPen (pen); 808 paint.setPen (pen);
798 paint.setBrush (QColor (64, 192, 0)); 809 paint.setBrush (QColor (64, 192, 0));
799 paint.drawEllipse (pos.x() - blipsize / 2, pos.y() - blipsize / 2, blipsize, blipsize); 810 paint.drawEllipse (pos.x() - blipsize / 2, pos.y() - blipsize / 2, blipsize, blipsize);
800 }
801
802 // =============================================================================
803 // -----------------------------------------------------------------------------
804 QColor GLRenderer::getTextPen () const
805 { return m_darkbg ? Qt::white : Qt::black;
806 } 811 }
807 812
808 // ============================================================================= 813 // =============================================================================
809 // ----------------------------------------------------------------------------- 814 // -----------------------------------------------------------------------------
810 void GLRenderer::compileAllObjects() 815 void GLRenderer::compileAllObjects()

mercurial