diff --git a/libraries/octree/src/OctreeElement.cpp b/libraries/octree/src/OctreeElement.cpp index 3f31ad928a..4d1630135c 100644 --- a/libraries/octree/src/OctreeElement.cpp +++ b/libraries/octree/src/OctreeElement.cpp @@ -1219,14 +1219,13 @@ bool OctreeElement::calculateShouldRender(const ViewFrustum* viewFrustum, float } // Calculates the distance to the furthest point of the voxel to the camera +// does as much math as possible in voxel scale and then scales up to TREE_SCALE at end float OctreeElement::furthestDistanceToCamera(const ViewFrustum& viewFrustum) const { - AABox box = getAABox(); - box.scale(TREE_SCALE); glm::vec3 furthestPoint; - viewFrustum.getFurthestPointFromCamera(box, furthestPoint); - glm::vec3 temp = viewFrustum.getPosition() - furthestPoint; + viewFrustum.getFurthestPointFromCameraVoxelScale(getAABox(), furthestPoint); + glm::vec3 temp = viewFrustum.getPositionVoxelScale() - furthestPoint; float distanceToFurthestPoint = sqrtf(glm::dot(temp, temp)); - return distanceToFurthestPoint; + return distanceToFurthestPoint * (float)TREE_SCALE; } float OctreeElement::distanceToCamera(const ViewFrustum& viewFrustum) const { diff --git a/libraries/octree/src/ViewFrustum.cpp b/libraries/octree/src/ViewFrustum.cpp index 00caafa095..da03aad697 100644 --- a/libraries/octree/src/ViewFrustum.cpp +++ b/libraries/octree/src/ViewFrustum.cpp @@ -25,6 +25,7 @@ using namespace std; ViewFrustum::ViewFrustum() : _position(0,0,0), + _positionVoxelScale(0,0,0), _orientation(), _direction(IDENTITY_FRONT), _up(IDENTITY_UP), @@ -724,3 +725,31 @@ void ViewFrustum::getFurthestPointFromCamera(const AABox& box, glm::vec3& furthe furthestPoint.z = bottomNearRight.z; } } + +void ViewFrustum::getFurthestPointFromCameraVoxelScale(const AABox& box, glm::vec3& furthestPoint) const { + const glm::vec3& bottomNearRight = box.getCorner(); + float scale = box.getScale(); + float halfScale = scale * 0.5f; + + if (_positionVoxelScale.x < bottomNearRight.x + halfScale) { + // we are to the right of the center, so the left edge is furthest + furthestPoint.x = bottomNearRight.x + scale; + } else { + furthestPoint.x = bottomNearRight.x; + } + + if (_positionVoxelScale.y < bottomNearRight.y + halfScale) { + // we are below of the center, so the top edge is furthest + furthestPoint.y = bottomNearRight.y + scale; + } else { + furthestPoint.y = bottomNearRight.y; + } + + if (_positionVoxelScale.z < bottomNearRight.z + halfScale) { + // we are to the near side of the center, so the far side edge is furthest + furthestPoint.z = bottomNearRight.z + scale; + } else { + furthestPoint.z = bottomNearRight.z; + } +} + diff --git a/libraries/octree/src/ViewFrustum.h b/libraries/octree/src/ViewFrustum.h index f841566652..a0b3a851aa 100644 --- a/libraries/octree/src/ViewFrustum.h +++ b/libraries/octree/src/ViewFrustum.h @@ -29,11 +29,12 @@ const float DEFAULT_FAR_CLIP = 50.0f * TREE_SCALE; class ViewFrustum { public: // setters for camera attributes - void setPosition(const glm::vec3& p) { _position = p; } + void setPosition(const glm::vec3& p) { _position = p; _positionVoxelScale = (p / (float)TREE_SCALE); } void setOrientation(const glm::quat& orientationAsQuaternion); // getters for camera attributes const glm::vec3& getPosition() const { return _position; } + const glm::vec3& getPositionVoxelScale() const { return _positionVoxelScale; } const glm::quat& getOrientation() const { return _orientation; } const glm::vec3& getDirection() const { return _direction; } const glm::vec3& getUp() const { return _up; } @@ -102,9 +103,11 @@ public: glm::vec2 projectPoint(glm::vec3 point, bool& pointInView) const; OctreeProjectedPolygon getProjectedPolygon(const AABox& box) const; - //glm::vec3 getFurthestPointFromCamera(const AABox& box) const; void getFurthestPointFromCamera(const AABox& box, glm::vec3& furthestPoint) const; + // assumes box is in voxel scale, not TREE_SCALE, will scale view frustum's position accordingly + void getFurthestPointFromCameraVoxelScale(const AABox& box, glm::vec3& furthestPoint) const; + private: // Used for keyhole calculations ViewFrustum::location pointInKeyhole(const glm::vec3& point) const; @@ -112,7 +115,8 @@ private: ViewFrustum::location boxInKeyhole(const AABox& box) const; // camera location/orientation attributes - glm::vec3 _position; + glm::vec3 _position; // the position in TREE_SCALE + glm::vec3 _positionVoxelScale; // the position in voxel scale glm::quat _orientation; // calculated for orientation