From d04f4d4b2b72fb2df5432f971c630012163edf0e Mon Sep 17 00:00:00 2001 From: "Anthony J. Thibault" Date: Tue, 22 Sep 2015 19:57:23 -0700 Subject: [PATCH] Added shared DebugDraw singleton. --- interface/src/avatar/MyAvatar.cpp | 4 ++ libraries/render-utils/src/AnimDebugDraw.cpp | 44 +++++++++------- libraries/render-utils/src/AnimDebugDraw.h | 6 --- libraries/shared/src/DebugDraw.cpp | 40 ++++++++++++++ libraries/shared/src/DebugDraw.h | 55 ++++++++++++++++++++ 5 files changed, 123 insertions(+), 26 deletions(-) create mode 100644 libraries/shared/src/DebugDraw.cpp create mode 100644 libraries/shared/src/DebugDraw.h diff --git a/interface/src/avatar/MyAvatar.cpp b/interface/src/avatar/MyAvatar.cpp index 4bbd0a4a0b..b2df29d464 100644 --- a/interface/src/avatar/MyAvatar.cpp +++ b/interface/src/avatar/MyAvatar.cpp @@ -47,6 +47,7 @@ #include "Recorder.h" #include "Util.h" #include "InterfaceLogging.h" +#include "DebugDraw.h" using namespace std; @@ -1358,6 +1359,9 @@ void MyAvatar::preRender(RenderArgs* renderArgs) { } } + DebugDraw::getInstance().updateMyAvatarPos(getPosition()); + DebugDraw::getInstance().updateMyAvatarRot(getOrientation()); + if (shouldDrawHead != _prevShouldDrawHead) { _skeletonModel.setCauterizeBones(!shouldDrawHead); } diff --git a/libraries/render-utils/src/AnimDebugDraw.cpp b/libraries/render-utils/src/AnimDebugDraw.cpp index 1e6428b50f..e7dc75dd57 100644 --- a/libraries/render-utils/src/AnimDebugDraw.cpp +++ b/libraries/render-utils/src/AnimDebugDraw.cpp @@ -13,6 +13,7 @@ #include "AbstractViewStateInterface.h" #include "RenderUtilsLogging.h" #include "GLMHelpers.h" +#include "DebugDraw.h" #include "AnimDebugDraw.h" @@ -67,13 +68,9 @@ namespace render { } } -static AnimDebugDraw* instance = nullptr; - AnimDebugDraw& AnimDebugDraw::getInstance() { - if (!instance) { - instance = new AnimDebugDraw(); - } - return *instance; + static AnimDebugDraw instance; + return instance; } static uint32_t toRGBA(uint8_t r, uint8_t g, uint8_t b, uint8_t a) { @@ -176,14 +173,6 @@ void AnimDebugDraw::removePoses(const std::string& key) { _poses.erase(key); } -void AnimDebugDraw::addPose(const std::string& key, const AnimPose& pose, const AnimPose& rootPose) { - _singlePoses[key] = SinglePoseInfo(pose, rootPose); -} - -void AnimDebugDraw::removePose(const std::string& key) { - _singlePoses.erase(key); -} - static const uint32_t red = toRGBA(255, 0, 0, 255); static const uint32_t green = toRGBA(0, 255, 0, 255); static const uint32_t blue = toRGBA(0, 0, 255, 255); @@ -380,7 +369,11 @@ void AnimDebugDraw::update() { } } - numVerts += _singlePoses.size() * VERTICES_PER_BONE; + // count marker verts from shared DebugDraw singleton + auto markerMap = DebugDraw::getInstance().getMarkerMap(); + numVerts += markerMap.size() * VERTICES_PER_BONE; + auto myAvatarMarkerMap = DebugDraw::getInstance().getMyAvatarMarkerMap(); + numVerts += myAvatarMarkerMap.size() * VERTICES_PER_BONE; data._vertexBuffer->resize(sizeof(Vertex) * numVerts); Vertex* verts = (Vertex*)data._vertexBuffer->editData(); @@ -486,11 +479,22 @@ void AnimDebugDraw::update() { } } - for (auto& iter : _singlePoses) { - AnimPose pose = std::get<0>(iter.second); - AnimPose rootPose = std::get<1>(iter.second); - const float radius = POSE_RADIUS / (pose.scale.x * rootPose.scale.x); - addBone(rootPose, pose, radius, v); + // draw markers from shared DebugDraw singleton + for (auto& iter : markerMap) { + glm::quat rot = std::get<0>(iter.second); + glm::vec3 pos = std::get<1>(iter.second); + glm::vec4 color = std::get<2>(iter.second); // TODO: currently ignored. + const float radius = POSE_RADIUS; + addBone(AnimPose::identity, AnimPose(glm::vec3(1), rot, pos), radius, v); + } + + AnimPose myAvatarPose(glm::vec3(1), DebugDraw::getInstance().getMyAvatarRot(), DebugDraw::getInstance().getMyAvatarPos()); + for (auto& iter : myAvatarMarkerMap) { + glm::quat rot = std::get<0>(iter.second); + glm::vec3 pos = std::get<1>(iter.second); + glm::vec4 color = std::get<2>(iter.second); // TODO: currently ignored. + const float radius = POSE_RADIUS; + addBone(myAvatarPose, AnimPose(glm::vec3(1), rot, pos), radius, v); } assert(numVerts == (v - verts)); diff --git a/libraries/render-utils/src/AnimDebugDraw.h b/libraries/render-utils/src/AnimDebugDraw.h index 5bb19097f2..c2205c1afe 100644 --- a/libraries/render-utils/src/AnimDebugDraw.h +++ b/libraries/render-utils/src/AnimDebugDraw.h @@ -39,10 +39,6 @@ public: void addPoses(const std::string& key, AnimSkeleton::ConstPointer skeleton, const AnimPoseVec& poses, const AnimPose& rootPose, const glm::vec4& color); void removePoses(const std::string& key); - // draw a single pose - void addPose(const std::string& key, const AnimPose& rootPose, const AnimPose& pose); - void removePose(const std::string& key); - void update(); protected: @@ -55,12 +51,10 @@ protected: typedef std::tuple SkeletonInfo; typedef std::tuple AnimNodeInfo; typedef std::tuple PosesInfo; - typedef std::tuple SinglePoseInfo; std::unordered_map _skeletons; std::unordered_map _animNodes; std::unordered_map _poses; - std::unordered_map _singlePoses; // no copies AnimDebugDraw(const AnimDebugDraw&) = delete; diff --git a/libraries/shared/src/DebugDraw.cpp b/libraries/shared/src/DebugDraw.cpp new file mode 100644 index 0000000000..3060e151c8 --- /dev/null +++ b/libraries/shared/src/DebugDraw.cpp @@ -0,0 +1,40 @@ +// +// DebugDraw.cpp +// +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "DebugDraw.h" + +DebugDraw& DebugDraw::getInstance() { + static DebugDraw instance; + return instance; +} + +DebugDraw::DebugDraw() { + +} + +DebugDraw::~DebugDraw() { + +} + +void DebugDraw::addMarker(const std::string& key, const glm::quat& rotation, const glm::vec3& position, const glm::vec4& color) { + _markers[key] = MarkerInfo(rotation, position, color); +} + +void DebugDraw::removeMarker(const std::string& key) { + _markers.erase(key); +} + +void DebugDraw::addMyAvatarMarker(const std::string& key, const glm::quat& rotation, const glm::vec3& position, const glm::vec4& color) { + _myAvatarMarkers[key] = MarkerInfo(rotation, position, color); +} + +void DebugDraw::removeMyAvatarMarker(const std::string& key) { + _myAvatarMarkers.erase(key); +} + diff --git a/libraries/shared/src/DebugDraw.h b/libraries/shared/src/DebugDraw.h new file mode 100644 index 0000000000..2f62f17abf --- /dev/null +++ b/libraries/shared/src/DebugDraw.h @@ -0,0 +1,55 @@ +// +// DebugDraw.h +// +// Copyright 2015 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_DebugDraw_h +#define hifi_DebugDraw_h + +#include +#include +#include +#include +#include + +class DebugDraw { +public: + static DebugDraw& getInstance(); + + DebugDraw(); + ~DebugDraw(); + + // world space maker + void addMarker(const std::string& key, const glm::quat& rotation, const glm::vec3& position, const glm::vec4& color); + void removeMarker(const std::string& key); + + // myAvatar relative marker + void addMyAvatarMarker(const std::string& key, const glm::quat& rotation, const glm::vec3& position, const glm::vec4& color); + void removeMyAvatarMarker(const std::string& key); + + using MarkerInfo = std::tuple; + using MarkerMap = std::unordered_map; + + // + // accessors used by renderer + // + + const MarkerMap& getMarkerMap() const { return _markers; } + const MarkerMap& getMyAvatarMarkerMap() const { return _myAvatarMarkers; } + void updateMyAvatarPos(const glm::vec3& pos) { _myAvatarPos = pos; } + const glm::vec3& getMyAvatarPos() const { return _myAvatarPos; } + void updateMyAvatarRot(const glm::quat& rot) { _myAvatarRot = rot; } + const glm::quat& getMyAvatarRot() const { return _myAvatarRot; } + +protected: + MarkerMap _markers; + MarkerMap _myAvatarMarkers; + glm::quat _myAvatarRot; + glm::vec3 _myAvatarPos; +}; + +#endif // hifi_DebugDraw_h