src/gldraw.cpp

changeset 263
d7b534057d61
parent 262
56f8987f4c7c
child 264
4299b818a816
equal deleted inserted replaced
262:56f8987f4c7c 263:d7b534057d61
95 m_picking = m_rangepick = false; 95 m_picking = m_rangepick = false;
96 m_camera = (GL::Camera) gl_camera.value; 96 m_camera = (GL::Camera) gl_camera.value;
97 m_drawToolTip = false; 97 m_drawToolTip = false;
98 m_editMode = Select; 98 m_editMode = Select;
99 m_rectdraw = false; 99 m_rectdraw = false;
100 m_panning = false;
100 setFile (null); 101 setFile (null);
101 setDrawOnly (false); 102 setDrawOnly (false);
102 resetAngles (); 103 resetAngles ();
103 104
104 m_toolTipTimer = new QTimer (this); 105 m_toolTipTimer = new QTimer (this);
433 434
434 cx *= negXFac; 435 cx *= negXFac;
435 cy *= negYFac; 436 cy *= negYFac;
436 437
437 pos3d = g_origin; 438 pos3d = g_origin;
438 pos3d[axisX] = cx; 439 pos3d[axisX] = atof (fmt ("%.3f", cx));
439 pos3d[axisY] = cy; 440 pos3d[axisY] = atof (fmt ("%.3f", cy));
440 pos3d[3 - axisX - axisY] = depthValue (); 441 pos3d[3 - axisX - axisY] = depthValue ();
441 return pos3d; 442 return pos3d;
442 } 443 }
443 444
444 // ============================================================================= 445 // =============================================================================
478 m_virtHeight = (m_height * m_virtWidth) / m_width; 479 m_virtHeight = (m_height * m_virtWidth) / m_width;
479 480
480 initGLData (); 481 initGLData ();
481 drawGLScene (); 482 drawGLScene ();
482 483
483 m_hoverpos = g_origin;
484
485 QPainter paint (this); 484 QPainter paint (this);
486 QFontMetrics metrics = QFontMetrics (QFont ()); 485 QFontMetrics metrics = QFontMetrics (QFont ());
487 paint.setRenderHint (QPainter::HighQualityAntialiasing); 486 paint.setRenderHint (QPainter::HighQualityAntialiasing);
488 487
489 // If we wish to only draw the brick, stop here 488 // If we wish to only draw the brick, stop here
500 QRect targRect (v0.x (), v0.y (), abs (v1.x () - v0.x ()), abs (v1.y () - v0.y ())), 499 QRect targRect (v0.x (), v0.y (), abs (v1.x () - v0.x ()), abs (v1.y () - v0.y ())),
501 srcRect (0, 0, overlay.img->width (), overlay.img->height ()); 500 srcRect (0, 0, overlay.img->width (), overlay.img->height ());
502 paint.drawImage (targRect, *overlay.img, srcRect); 501 paint.drawImage (targRect, *overlay.img, srcRect);
503 } 502 }
504 503
505 // Calculate 3d position of the cursor
506 m_hoverpos = coordconv2_3 (m_pos, true);
507
508 // Paint the coordinates onto the screen. 504 // Paint the coordinates onto the screen.
509 str text = fmt ("X: %s, Y: %s, Z: %s", ftoa (m_hoverpos[X]).chars (), 505 str text = fmt ("X: %s, Y: %s, Z: %s", ftoa (m_hoverpos[X]).chars (),
510 ftoa (m_hoverpos[Y]).chars (), ftoa (m_hoverpos[Z]).chars ()); 506 ftoa (m_hoverpos[Y]).chars (), ftoa (m_hoverpos[Z]).chars ());
511 507
512 QFontMetrics metrics = QFontMetrics (font ()); 508 QFontMetrics metrics = QFontMetrics (font ());
518 514
519 // If we're drawing, draw the vertices onto the screen. 515 // If we're drawing, draw the vertices onto the screen.
520 if (editMode () == Draw) { 516 if (editMode () == Draw) {
521 ushort numverts; 517 ushort numverts;
522 518
519 numverts = 4;
523 if (!m_rectdraw) 520 if (!m_rectdraw)
524 numverts = m_drawedVerts.size () + 1; 521 numverts = m_drawedVerts.size () + 1;
525 else
526 numverts = (m_drawedVerts.size () > 0) ? 4 : 1;
527 522
528 const short blipsize = 8; 523 const short blipsize = 8;
529 524
530 if (numverts > 0) { 525 if (numverts > 0) {
531 QPoint* poly = new QPoint[numverts]; 526 QPoint poly[4];
527 vertex polyverts[4];
532 528
533 if (!m_rectdraw) { 529 if (!m_rectdraw) {
534 uchar i = 0; 530 uchar i = 0;
535 for (vertex& vert : m_drawedVerts) { 531 for (vertex& vert : m_drawedVerts) {
536 poly[i] = coordconv3_2 (vert); 532 poly[i] = coordconv3_2 (vert);
533 polyverts[i] = vert;
537 ++i; 534 ++i;
538 } 535 }
539 536
540 // Draw the cursor vertex as the last one in the list. 537 // Draw the cursor vertex as the last one in the list.
541 if (numverts < 5) 538 if (numverts <= 4) {
542 poly[i] = coordconv3_2 (m_hoverpos); 539 poly[i] = coordconv3_2 (m_hoverpos);
543 else 540 polyverts[i] = m_hoverpos;
541 } else {
544 numverts = 4; 542 numverts = 4;
543 }
545 } else { 544 } else {
546 if (m_drawedVerts.size () > 0) { 545 if (m_drawedVerts.size () > 0) {
547 QPoint v0 = coordconv3_2 (m_drawedVerts[0]), 546 // Get vertex information from m_rectverts
548 v1 = coordconv3_2 ((m_drawedVerts.size () >= 2) ? m_drawedVerts[1] : m_hoverpos); 547 for (ushort i = 0; i < numverts; ++i) {
549 548 polyverts[i] = m_rectverts[i];
550 poly[0] = QPoint (v0.x (), v0.y ()); 549 poly[i] = coordconv3_2 (polyverts[i]);
551 poly[1] = QPoint (v0.x (), v1.y ()); 550 }
552 poly[2] = QPoint (v1.x (), v1.y ());
553 poly[3] = QPoint (v1.x (), v0.y ());
554 } else { 551 } else {
555 poly[0] = coordconv3_2 (m_hoverpos); 552 poly[0] = coordconv3_2 (m_hoverpos);
553 polyverts[0] = m_hoverpos;
556 } 554 }
557 } 555 }
558 556
557 // Draw the polygon-to-be
559 QPen pen = m_thinBorderPen; 558 QPen pen = m_thinBorderPen;
560 pen.setWidth (1);
561 paint.setPen (pen);
562 paint.setBrush (QColor (128, 192, 0));
563
564 // Draw vertex blips
565 for (ushort i = 0; i < numverts; ++i) {
566 QPoint& blip = poly[i];
567 paint.drawEllipse (blip.x () - blipsize / 2, blip.y () - blipsize / 2,
568 blipsize, blipsize);
569 }
570
571 pen.setWidth (2); 559 pen.setWidth (2);
572 pen.setColor (luma (m_bgcolor) < 40 ? Qt::white : Qt::black); 560 pen.setColor (luma (m_bgcolor) < 40 ? Qt::white : Qt::black);
573 paint.setPen (pen); 561 paint.setPen (pen);
574 paint.setBrush (QColor (128, 192, 0, 128)); 562 paint.setBrush (QColor (128, 192, 0, 128));
575 paint.drawPolygon (poly, numverts); 563 paint.drawPolygon (poly, numverts);
576 564
577 delete[] poly; 565 // Draw vertex blips
566 pen = m_thinBorderPen;
567 pen.setWidth (1);
568 paint.setPen (pen);
569 paint.setBrush (QColor (128, 192, 0));
570
571 for (ushort i = 0; i < numverts; ++i) {
572 QPoint& blip = poly[i];
573 paint.drawEllipse (blip.x () - blipsize / 2, blip.y () - blipsize / 2,
574 blipsize, blipsize);
575
576 // Draw their coordinates
577 paint.drawText (blip.x (), blip.y () - 8, polyverts[i].stringRep (true));
578 }
578 } 579 }
579 } 580 }
580 } 581 }
581 582
582 // Camera icons 583 // Camera icons
856 // ============================================================================= 857 // =============================================================================
857 void GLRenderer::mouseReleaseEvent (QMouseEvent* ev) { 858 void GLRenderer::mouseReleaseEvent (QMouseEvent* ev) {
858 const bool wasLeft = (m_lastButtons & Qt::LeftButton) && !(ev->buttons() & Qt::LeftButton); 859 const bool wasLeft = (m_lastButtons & Qt::LeftButton) && !(ev->buttons() & Qt::LeftButton);
859 const bool wasRight = (m_lastButtons & Qt::RightButton) && !(ev->buttons() & Qt::RightButton); 860 const bool wasRight = (m_lastButtons & Qt::RightButton) && !(ev->buttons() & Qt::RightButton);
860 const bool wasMid = (m_lastButtons & Qt::MidButton) && !(ev->buttons() & Qt::MidButton); 861 const bool wasMid = (m_lastButtons & Qt::MidButton) && !(ev->buttons() & Qt::MidButton);
861 printf ("totalmove: %lu\n", m_totalmove); 862
863 if (m_panning)
864 m_panning = false;
862 865
863 if (wasLeft) { 866 if (wasLeft) {
864 // Check if we selected a camera icon 867 // Check if we selected a camera icon
865 if (!m_rangepick) { 868 if (!m_rangepick) {
866 for (CameraIcon& info : g_CameraIcons) { 869 for (CameraIcon& info : g_CameraIcons) {
867 if (info.destRect.contains (ev->pos ())) { 870 if (info.destRect.contains (ev->pos ())) {
868 setCamera (info.cam); 871 setCamera (info.cam);
869 update (); 872 goto end;
870 return;
871 } 873 }
872 } 874 }
873 } 875 }
874 876
875 switch (editMode ()) { 877 switch (editMode ()) {
889 if (m_drawedVerts.size () == 0 && ev->modifiers () & Qt::ShiftModifier) 891 if (m_drawedVerts.size () == 0 && ev->modifiers () & Qt::ShiftModifier)
890 m_rectdraw = true; 892 m_rectdraw = true;
891 } 893 }
892 894
893 addDrawnVertex (m_hoverpos); 895 addDrawnVertex (m_hoverpos);
894 update ();
895 break; 896 break;
896 897
897 case Select: 898 case Select:
898 if (!drawOnly ()) { 899 if (!drawOnly ()) {
899 if (!m_rangepick) 900 if (!m_rangepick)
900 m_addpick = (m_keymods & Qt::ControlModifier); 901 m_addpick = (m_keymods & Qt::ControlModifier);
901 902
902 if (m_totalmove < 10 || m_rangepick) 903 if (m_totalmove < 10 || m_rangepick)
903 pick (ev->x (), ev->y ()); 904 pick (ev->x (), ev->y ());
904 } 905 }
906
905 break; 907 break;
906 } 908 }
907 909
908 m_rangepick = false; 910 m_rangepick = false;
909 } 911 }
942 // Remove the last vertex 944 // Remove the last vertex
943 m_drawedVerts.erase (m_drawedVerts.size () - 1); 945 m_drawedVerts.erase (m_drawedVerts.size () - 1);
944 946
945 if (m_drawedVerts.size () == 0) 947 if (m_drawedVerts.size () == 0)
946 m_rectdraw = false; 948 m_rectdraw = false;
947 949 }
948 update (); 950
949 } 951 end:
950 952 update ();
951 m_totalmove = 0; 953 m_totalmove = 0;
952 } 954 }
953 955
954 // ============================================================================= 956 // =============================================================================
955 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 957 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
984 } 986 }
985 987
986 if (ev->buttons () & Qt::MidButton) { 988 if (ev->buttons () & Qt::MidButton) {
987 m_panX += 0.03f * dx * (zoom () / 7.5f); 989 m_panX += 0.03f * dx * (zoom () / 7.5f);
988 m_panY -= 0.03f * dy * (zoom () / 7.5f); 990 m_panY -= 0.03f * dy * (zoom () / 7.5f);
991 m_panning = true;
989 } 992 }
990 993
991 // Start the tool tip timer 994 // Start the tool tip timer
992 if (!m_drawToolTip) 995 if (!m_drawToolTip)
993 m_toolTipTimer->start (1000); 996 m_toolTipTimer->start (1000);
994 997
998 // Update 2d position
995 m_pos = ev->pos (); 999 m_pos = ev->pos ();
1000
1001 // Calculate 3d position of the cursor
1002 m_hoverpos = (camera () != Free) ? coordconv2_3 (m_pos, true) : g_origin;
1003
1004 // Update rect vertices since m_hoverpos may have changed
1005 updateRectVerts ();
1006
996 update (); 1007 update ();
997 } 1008 }
998 1009
999 // ============================================================================= 1010 // =============================================================================
1000 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 1011 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
1211 vector<vertex>& verts = m_drawedVerts; 1222 vector<vertex>& verts = m_drawedVerts;
1212 LDObject* obj = null; 1223 LDObject* obj = null;
1213 1224
1214 if (m_rectdraw) { 1225 if (m_rectdraw) {
1215 LDQuad* quad = new LDQuad; 1226 LDQuad* quad = new LDQuad;
1216 vertex v0 = m_drawedVerts[0], 1227
1217 v1 = m_drawedVerts[1]; 1228 // Copy the vertices from m_rectverts
1218 const Axis axisX = cameraAxis (false), 1229 updateRectVerts ();
1219 axisY = cameraAxis (true); 1230 memcpy (quad->coords, m_rectverts, sizeof quad->coords);
1220 1231
1221 memset (quad->coords, 0, sizeof quad->coords);
1222 quad->coords[0][axisX] = v0[axisX];
1223 quad->coords[0][axisY] = v0[axisY];
1224 quad->coords[1][axisX] = v1[axisX];
1225 quad->coords[1][axisY] = v0[axisY];
1226 quad->coords[2][axisX] = v1[axisX];
1227 quad->coords[2][axisY] = v1[axisY];
1228 quad->coords[3][axisX] = v0[axisX];
1229 quad->coords[3][axisY] = v1[axisY];
1230 quad->color = maincolor; 1232 quad->color = maincolor;
1231 obj = quad; 1233 obj = quad;
1232 } else { 1234 } else {
1233 switch (verts.size ()) { 1235 switch (verts.size ()) {
1234 case 1: 1236 case 1:
1312 1314
1313 glEndList (); 1315 glEndList ();
1314 } 1316 }
1315 1317
1316 // Mark in known vertices of this object 1318 // Mark in known vertices of this object
1317 m_knownVerts << getVertices (obj); 1319 vector<vertex> verts = getVertices (obj);
1320 m_knownVerts << verts;
1318 m_knownVerts.makeUnique (); 1321 m_knownVerts.makeUnique ();
1319 1322
1320 obj->m_glinit = true; 1323 obj->m_glinit = true;
1321 } 1324 }
1322 1325
1463 setZoom (zoom () * (inward ? 0.833f : 1.2f)); 1466 setZoom (zoom () * (inward ? 0.833f : 1.2f));
1464 else 1467 else
1465 setZoom (zoom () + (inward ? -1.2f : 1.2f)); 1468 setZoom (zoom () + (inward ? -1.2f : 1.2f));
1466 } 1469 }
1467 1470
1471 // =============================================================================
1468 void GLRenderer::zoomToFit () { 1472 void GLRenderer::zoomToFit () {
1469 if (file () == null) { 1473 if (file () == null) {
1470 setZoom (30.0f); 1474 setZoom (30.0f);
1471 return; 1475 return;
1472 } 1476 }
1539 } 1543 }
1540 1544
1541 setBackground (); 1545 setBackground ();
1542 m_picking = false; 1546 m_picking = false;
1543 } 1547 }
1548
1549 // =============================================================================
1550 void GLRenderer::updateRectVerts () {
1551 if (!m_rectdraw)
1552 return;
1553
1554 if (m_drawedVerts.size () == 0) {
1555 for (int i = 0; i < 4; ++i)
1556 m_rectverts[i] = m_hoverpos;
1557
1558 return;
1559 }
1560
1561 vertex v0 = m_drawedVerts[0],
1562 v1 = (m_drawedVerts.size () >= 2) ? m_drawedVerts[1] : m_hoverpos;
1563
1564 const Axis ax = cameraAxis (false),
1565 ay = cameraAxis (true),
1566 az = (Axis) (3 - ax - ay);
1567
1568 for (int i = 0; i < 4; ++i)
1569 m_rectverts[i][az] = depthValue ();
1570
1571 m_rectverts[0][ax] = v0[ax];
1572 m_rectverts[0][ay] = v0[ay];
1573 m_rectverts[1][ax] = v1[ax];
1574 m_rectverts[1][ay] = v0[ay];
1575 m_rectverts[2][ax] = v1[ax];
1576 m_rectverts[2][ay] = v1[ay];
1577 m_rectverts[3][ax] = v0[ax];
1578 m_rectverts[3][ay] = v1[ay];
1579 }

mercurial