src/glRenderer.cc

changeset 742
f10de1bf22e7
parent 741
9580b6e344b4
child 752
b80ccc2a24e4
equal deleted inserted replaced
741:9580b6e344b4 742:f10de1bf22e7
44 #include "misc/ringFinder.h" 44 #include "misc/ringFinder.h"
45 #include "glCompiler.h" 45 #include "glCompiler.h"
46 46
47 static const LDFixedCameraInfo g_FixedCameras[6] = 47 static const LDFixedCameraInfo g_FixedCameras[6] =
48 { 48 {
49 {{ 1, 0, 0 }, X, Z, false, false }, 49 {{ 1, 0, 0 }, X, Z, false, false, false }, // top
50 {{ 0, 0, 0 }, X, Y, false, true }, 50 {{ 0, 0, 0 }, X, Y, false, true, false }, // front
51 {{ 0, 1, 0 }, Z, Y, true, true }, 51 {{ 0, 1, 0 }, Z, Y, true, true, false }, // left
52 {{ -1, 0, 0 }, X, Z, false, true }, 52 {{ -1, 0, 0 }, X, Z, false, true, true }, // bottom
53 {{ 0, 0, 0 }, X, Y, true, true }, 53 {{ 0, 0, 0 }, X, Y, true, true, true }, // back
54 {{ 0, -1, 0 }, Z, Y, false, true }, 54 {{ 0, -1, 0 }, Z, Y, false, true, true }, // right
55 }; 55 };
56 56
57 // Matrix templates for circle drawing. 2 is substituted with 57 // Matrix templates for circle drawing. 2 is substituted with
58 // the scale value, 1 is inverted to -1 if needed. 58 // the scale value, 1 is inverted to -1 if needed.
59 static const Matrix g_circleDrawMatrixTemplates[3] = 59 static const Matrix g_circleDrawMatrixTemplates[3] =
976 if (m_drawedVerts.size() == 2) 976 if (m_drawedVerts.size() == 2)
977 { 977 {
978 endDraw (true); 978 endDraw (true);
979 return; 979 return;
980 } 980 }
981 } else 981 }
982 else
982 { 983 {
983 // If we have 4 verts, stop drawing. 984 // If we have 4 verts, stop drawing.
984 if (m_drawedVerts.size() >= 4) 985 if (m_drawedVerts.size() >= 4)
985 { 986 {
986 endDraw (true); 987 endDraw (true);
993 updateRectVerts(); 994 updateRectVerts();
994 } 995 }
995 } 996 }
996 997
997 addDrawnVertex (m_hoverpos); 998 addDrawnVertex (m_hoverpos);
998 } break; 999 break;
1000 }
999 1001
1000 case ECircleMode: 1002 case ECircleMode:
1001 { 1003 {
1002 if (m_drawedVerts.size() == 3) 1004 if (m_drawedVerts.size() == 3)
1003 { 1005 {
1004 endDraw (true); 1006 endDraw (true);
1005 return; 1007 return;
1006 } 1008 }
1007 1009
1008 addDrawnVertex (m_hoverpos); 1010 addDrawnVertex (m_hoverpos);
1009 } break; 1011 break;
1012 }
1010 1013
1011 case ESelectMode: 1014 case ESelectMode:
1012 { 1015 {
1013 if (not isDrawOnly()) 1016 if (not isDrawOnly())
1014 { 1017 {
1019 m_addpick = (m_keymods & Qt::ControlModifier); 1022 m_addpick = (m_keymods & Qt::ControlModifier);
1020 1023
1021 if (m_totalmove < 10 || m_rangepick) 1024 if (m_totalmove < 10 || m_rangepick)
1022 pick (ev->x(), ev->y()); 1025 pick (ev->x(), ev->y());
1023 } 1026 }
1024 } break; 1027 break;
1028 }
1025 } 1029 }
1026 1030
1027 m_rangepick = false; 1031 m_rangepick = false;
1028 } 1032 }
1029 1033
1030 if (wasMid && editMode() != ESelectMode && m_drawedVerts.size() < 4 && m_totalmove < 10) 1034 if (wasMid && editMode() != ESelectMode && m_drawedVerts.size() < 4 && m_totalmove < 10)
1031 { 1035 {
1032 // Find the closest vertex to our cursor 1036 // Find the closest vertex to our cursor
1033 double mindist = 1024.0f; 1037 double minimumDistance = 1024.0;
1034 Vertex closest; 1038 const Vertex* closest = null;
1035 bool valid = false; 1039 Vertex cursorPosition = coordconv2_3 (m_pos, false);
1036 1040 QPoint cursorPosition2D (m_pos);
1037 QPoint curspos = coordconv3_2 (m_hoverpos); 1041 const Axis relZ = getRelativeZ();
1042 QList<Vertex> vertices;
1038 1043
1039 for (auto it = document()->vertices().begin(); it != document()->vertices().end(); ++it) 1044 for (auto it = document()->vertices().begin(); it != document()->vertices().end(); ++it)
1040 { 1045 vertices << it.key();
1041 QPoint pos2d = coordconv3_2 (it.key()); 1046
1042 1047 // Sort the vertices in order of distance to camera
1043 // Measure squared distance 1048 std::sort (vertices.begin(), vertices.end(), [&](const Vertex& a, const Vertex& b) -> bool
1044 const double dx = abs (pos2d.x() - curspos.x()), 1049 {
1045 dy = abs (pos2d.y() - curspos.y()), 1050 if (g_FixedCameras[camera()].negatedDepth)
1046 distsq = (dx * dx) + (dy * dy); 1051 return a[relZ] > b[relZ];
1047 1052
1048 if (distsq >= 1024.0f) // 32.0f ** 2 1053 return a[relZ] < b[relZ];
1049 continue; // too far away 1054 });
1050 1055
1051 if (distsq < mindist) 1056 for (const Vertex& vrt : vertices)
1052 { 1057 {
1053 mindist = distsq; 1058 // If the vertex in 2d space is very close to the cursor then we use
1054 closest = it.key(); 1059 // it regardless of depth.
1055 valid = true; 1060 QPoint vect2d = coordconv3_2 (vrt) - cursorPosition2D;
1056 1061 const double distance2DSquared = std::pow (vect2d.x(), 2) + std::pow (vect2d.y(), 2);
1057 // If it's only 4 pixels away, I think we found our vertex now. 1062 if (distance2DSquared < 16.0 * 16.0)
1058 if (distsq <= 16.0f) // 4.0f ** 2 1063 {
1059 break; 1064 closest = &vrt;
1060 } 1065 break;
1061 } 1066 }
1062 1067
1063 if (valid) 1068 // Check if too far away from the cursor.
1064 addDrawnVertex (closest); 1069 if (distance2DSquared > 64.0 * 64.0)
1070 continue;
1071
1072 // Not very close to the cursor. Compare using true distance,
1073 // including depth.
1074 const double distanceSquared = (vrt - cursorPosition).lengthSquared();
1075
1076 if (distanceSquared < minimumDistance)
1077 {
1078 minimumDistance = distanceSquared;
1079 closest = &vrt;
1080 }
1081 }
1082
1083 if (closest != null)
1084 addDrawnVertex (*closest);
1065 } 1085 }
1066 1086
1067 if (wasRight && not m_drawedVerts.isEmpty()) 1087 if (wasRight && not m_drawedVerts.isEmpty())
1068 { 1088 {
1069 // Remove the last vertex 1089 // Remove the last vertex
1616 relY = cam->axisY; 1636 relY = cam->axisY;
1617 } 1637 }
1618 1638
1619 // ============================================================================= 1639 // =============================================================================
1620 // 1640 //
1641 Axis GLRenderer::getRelativeZ() const
1642 {
1643 const LDFixedCameraInfo* cam = &g_FixedCameras[camera()];
1644 return (Axis) (3 - cam->axisX - cam->axisY);
1645 }
1646
1647 // =============================================================================
1648 //
1621 static QList<Vertex> getVertices (LDObject* obj) 1649 static QList<Vertex> getVertices (LDObject* obj)
1622 { 1650 {
1623 QList<Vertex> verts; 1651 QList<Vertex> verts;
1624 1652
1625 if (obj->vertices() >= 2) 1653 if (obj->vertices() >= 2)

mercurial