From 252bc3b3eee61ee52b099694234b1cae1b096b76 Mon Sep 17 00:00:00 2001 From: ZappoMan Date: Wed, 19 Mar 2014 11:51:47 -0700 Subject: [PATCH] optimized ViewFrustum::getFurthestPointFromCamera() --- libraries/octree/src/OctreeElement.cpp | 7 ++++--- libraries/octree/src/ViewFrustum.cpp | 25 +++++++++---------------- libraries/octree/src/ViewFrustum.h | 5 +++-- 3 files changed, 16 insertions(+), 21 deletions(-) diff --git a/libraries/octree/src/OctreeElement.cpp b/libraries/octree/src/OctreeElement.cpp index 72ac5b14d6..3f31ad928a 100644 --- a/libraries/octree/src/OctreeElement.cpp +++ b/libraries/octree/src/OctreeElement.cpp @@ -1222,10 +1222,11 @@ bool OctreeElement::calculateShouldRender(const ViewFrustum* viewFrustum, float float OctreeElement::furthestDistanceToCamera(const ViewFrustum& viewFrustum) const { AABox box = getAABox(); box.scale(TREE_SCALE); - glm::vec3 furthestPoint = viewFrustum.getFurthestPointFromCamera(box); + glm::vec3 furthestPoint; + viewFrustum.getFurthestPointFromCamera(box, furthestPoint); glm::vec3 temp = viewFrustum.getPosition() - furthestPoint; - float distanceToVoxelCenter = sqrtf(glm::dot(temp, temp)); - return distanceToVoxelCenter; + float distanceToFurthestPoint = sqrtf(glm::dot(temp, temp)); + return distanceToFurthestPoint; } float OctreeElement::distanceToCamera(const ViewFrustum& viewFrustum) const { diff --git a/libraries/octree/src/ViewFrustum.cpp b/libraries/octree/src/ViewFrustum.cpp index 66f1445ddc..00caafa095 100644 --- a/libraries/octree/src/ViewFrustum.cpp +++ b/libraries/octree/src/ViewFrustum.cpp @@ -695,39 +695,32 @@ OctreeProjectedPolygon ViewFrustum::getProjectedPolygon(const AABox& box) const return projectedPolygon; } - // Similar strategy to getProjectedPolygon() we use the knowledge of camera position relative to the // axis-aligned voxels to determine which of the voxels vertices must be the furthest. No need for // squares and square-roots. Just compares. -glm::vec3 ViewFrustum::getFurthestPointFromCamera(const AABox& box) const { +void ViewFrustum::getFurthestPointFromCamera(const AABox& box, glm::vec3& furthestPoint) const { const glm::vec3& bottomNearRight = box.getCorner(); - glm::vec3 center = box.calcCenter(); - glm::vec3 topFarLeft = box.calcTopFarLeft(); + float scale = box.getScale(); + float halfScale = scale * 0.5f; - glm::vec3 furthestPoint; - if (_position.x < center.x) { + if (_position.x < bottomNearRight.x + halfScale) { // we are to the right of the center, so the left edge is furthest - furthestPoint.x = topFarLeft.x; + furthestPoint.x = bottomNearRight.x + scale; } else { - // we are to the left of the center, so the right edge is furthest (at center ok too) furthestPoint.x = bottomNearRight.x; } - if (_position.y < center.y) { + if (_position.y < bottomNearRight.y + halfScale) { // we are below of the center, so the top edge is furthest - furthestPoint.y = topFarLeft.y; + furthestPoint.y = bottomNearRight.y + scale; } else { - // we are above the center, so the lower edge is furthest (at center ok too) furthestPoint.y = bottomNearRight.y; } - if (_position.z < center.z) { + if (_position.z < bottomNearRight.z + halfScale) { // we are to the near side of the center, so the far side edge is furthest - furthestPoint.z = topFarLeft.z; + furthestPoint.z = bottomNearRight.z + scale; } else { - // we are to the far side of the center, so the near side edge is furthest (at center ok too) furthestPoint.z = bottomNearRight.z; } - - return furthestPoint; } diff --git a/libraries/octree/src/ViewFrustum.h b/libraries/octree/src/ViewFrustum.h index da4a6997f1..f841566652 100644 --- a/libraries/octree/src/ViewFrustum.h +++ b/libraries/octree/src/ViewFrustum.h @@ -102,8 +102,9 @@ public: glm::vec2 projectPoint(glm::vec3 point, bool& pointInView) const; OctreeProjectedPolygon getProjectedPolygon(const AABox& box) const; - glm::vec3 getFurthestPointFromCamera(const AABox& box) const; - + //glm::vec3 getFurthestPointFromCamera(const AABox& box) const; + void getFurthestPointFromCamera(const AABox& box, glm::vec3& furthestPoint) const; + private: // Used for keyhole calculations ViewFrustum::location pointInKeyhole(const glm::vec3& point) const;