From 94c2540db0af315d452c280b20b3247a74063db0 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 1 Oct 2014 09:45:20 -0700 Subject: [PATCH] add dashed line drawing to Base3DOverlay and 3d line and rectangle overlays --- interface/src/ui/overlays/Base3DOverlay.cpp | 34 ++++++++++++++++++- interface/src/ui/overlays/Base3DOverlay.h | 2 ++ interface/src/ui/overlays/Line3DOverlay.cpp | 8 +++-- .../src/ui/overlays/Rectangle3DOverlay.cpp | 19 +++++------ 4 files changed, 50 insertions(+), 13 deletions(-) diff --git a/interface/src/ui/overlays/Base3DOverlay.cpp b/interface/src/ui/overlays/Base3DOverlay.cpp index bf0fa43f35..9c67f56e0b 100644 --- a/interface/src/ui/overlays/Base3DOverlay.cpp +++ b/interface/src/ui/overlays/Base3DOverlay.cpp @@ -110,6 +110,38 @@ void Base3DOverlay::setProperties(const QScriptValue& properties) { setIsDashedLine(properties.property("isDashedLine").toVariant().toBool()); } if (properties.property("dashed").isValid()) { - setIsDashedLine(!properties.property("dashed").toVariant().toBool()); + setIsDashedLine(properties.property("dashed").toVariant().toBool()); } } + +void Base3DOverlay::drawDashedLine(const glm::vec3& start, const glm::vec3& end) { + + glBegin(GL_LINES); + + // draw each line segment with appropriate gaps + const float dashLength = 0.05f; + const float gapLength = 0.025f; + const float segmentLength = dashLength + gapLength; + float length = glm::distance(start, end); + float segmentCount = length / segmentLength; + int segmentCountFloor = (int)glm::floor(segmentCount); + + glm::vec3 segmentVector = (end - start) / segmentCount; + glm::vec3 dashVector = segmentVector / segmentLength * dashLength; + glm::vec3 gapVector = segmentVector / segmentLength * gapLength; + + glm::vec3 point = start; + glVertex3f(point.x, point.y, point.z); + for (int i = 0; i < segmentCountFloor; i++) { + point += dashVector; + glVertex3f(point.x, point.y, point.z); + + point += gapVector; + glVertex3f(point.x, point.y, point.z); + } + glVertex3f(end.x, end.y, end.z); + + glEnd(); + +} + diff --git a/interface/src/ui/overlays/Base3DOverlay.h b/interface/src/ui/overlays/Base3DOverlay.h index b2d8a808fc..b6094be08f 100644 --- a/interface/src/ui/overlays/Base3DOverlay.h +++ b/interface/src/ui/overlays/Base3DOverlay.h @@ -43,6 +43,8 @@ public: virtual void setProperties(const QScriptValue& properties); protected: + void drawDashedLine(const glm::vec3& start, const glm::vec3& end); + glm::vec3 _position; float _lineWidth; glm::quat _rotation; diff --git a/interface/src/ui/overlays/Line3DOverlay.cpp b/interface/src/ui/overlays/Line3DOverlay.cpp index 4f09eb55e9..b35a22d4d8 100644 --- a/interface/src/ui/overlays/Line3DOverlay.cpp +++ b/interface/src/ui/overlays/Line3DOverlay.cpp @@ -40,10 +40,14 @@ void Line3DOverlay::render() { const float MAX_COLOR = 255; glColor4f(color.red / MAX_COLOR, color.green / MAX_COLOR, color.blue / MAX_COLOR, alpha); - glBegin(GL_LINES); + if (getIsDashedLine()) { + drawDashedLine(_position, _end); + } else { + glBegin(GL_LINES); glVertex3f(_position.x, _position.y, _position.z); glVertex3f(_end.x, _end.y, _end.z); - glEnd(); + glEnd(); + } glEnable(GL_LIGHTING); if (glower) { diff --git a/interface/src/ui/overlays/Rectangle3DOverlay.cpp b/interface/src/ui/overlays/Rectangle3DOverlay.cpp index b2caaf60a6..fd41aa012c 100644 --- a/interface/src/ui/overlays/Rectangle3DOverlay.cpp +++ b/interface/src/ui/overlays/Rectangle3DOverlay.cpp @@ -70,17 +70,17 @@ void Rectangle3DOverlay::render() { glEnd(); } else { if (getIsDashedLine()) { - - // TODO: change this to be dashed! - glBegin(GL_LINE_STRIP); - glVertex3f(-halfDimensions.x, 0.0f, -halfDimensions.y); - glVertex3f(halfDimensions.x, 0.0f, -halfDimensions.y); - glVertex3f(halfDimensions.x, 0.0f, halfDimensions.y); - glVertex3f(-halfDimensions.x, 0.0f, halfDimensions.y); - glVertex3f(-halfDimensions.x, 0.0f, -halfDimensions.y); + glm::vec3 point1(-halfDimensions.x, 0.0f, -halfDimensions.y); + glm::vec3 point2(halfDimensions.x, 0.0f, -halfDimensions.y); + glm::vec3 point3(halfDimensions.x, 0.0f, halfDimensions.y); + glm::vec3 point4(-halfDimensions.x, 0.0f, halfDimensions.y); - glEnd(); + drawDashedLine(point1, point2); + drawDashedLine(point2, point3); + drawDashedLine(point3, point4); + drawDashedLine(point4, point1); + } else { glBegin(GL_LINE_STRIP); @@ -112,4 +112,3 @@ void Rectangle3DOverlay::setProperties(const QScriptValue &properties) { -