optimized ViewFrustum::getFurthestPointFromCamera()

This commit is contained in:
ZappoMan 2014-03-19 11:51:47 -07:00
parent cd6e0f1698
commit 252bc3b3ee
3 changed files with 16 additions and 21 deletions

View file

@ -1222,10 +1222,11 @@ bool OctreeElement::calculateShouldRender(const ViewFrustum* viewFrustum, float
float OctreeElement::furthestDistanceToCamera(const ViewFrustum& viewFrustum) const { float OctreeElement::furthestDistanceToCamera(const ViewFrustum& viewFrustum) const {
AABox box = getAABox(); AABox box = getAABox();
box.scale(TREE_SCALE); box.scale(TREE_SCALE);
glm::vec3 furthestPoint = viewFrustum.getFurthestPointFromCamera(box); glm::vec3 furthestPoint;
viewFrustum.getFurthestPointFromCamera(box, furthestPoint);
glm::vec3 temp = viewFrustum.getPosition() - furthestPoint; glm::vec3 temp = viewFrustum.getPosition() - furthestPoint;
float distanceToVoxelCenter = sqrtf(glm::dot(temp, temp)); float distanceToFurthestPoint = sqrtf(glm::dot(temp, temp));
return distanceToVoxelCenter; return distanceToFurthestPoint;
} }
float OctreeElement::distanceToCamera(const ViewFrustum& viewFrustum) const { float OctreeElement::distanceToCamera(const ViewFrustum& viewFrustum) const {

View file

@ -695,39 +695,32 @@ OctreeProjectedPolygon ViewFrustum::getProjectedPolygon(const AABox& box) const
return projectedPolygon; return projectedPolygon;
} }
// Similar strategy to getProjectedPolygon() we use the knowledge of camera position relative to the // 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 // axis-aligned voxels to determine which of the voxels vertices must be the furthest. No need for
// squares and square-roots. Just compares. // 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(); const glm::vec3& bottomNearRight = box.getCorner();
glm::vec3 center = box.calcCenter(); float scale = box.getScale();
glm::vec3 topFarLeft = box.calcTopFarLeft(); float halfScale = scale * 0.5f;
glm::vec3 furthestPoint; if (_position.x < bottomNearRight.x + halfScale) {
if (_position.x < center.x) {
// we are to the right of the center, so the left edge is furthest // we are to the right of the center, so the left edge is furthest
furthestPoint.x = topFarLeft.x; furthestPoint.x = bottomNearRight.x + scale;
} else { } else {
// we are to the left of the center, so the right edge is furthest (at center ok too)
furthestPoint.x = bottomNearRight.x; 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 // we are below of the center, so the top edge is furthest
furthestPoint.y = topFarLeft.y; furthestPoint.y = bottomNearRight.y + scale;
} else { } else {
// we are above the center, so the lower edge is furthest (at center ok too)
furthestPoint.y = bottomNearRight.y; 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 // 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 { } 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; furthestPoint.z = bottomNearRight.z;
} }
return furthestPoint;
} }

View file

@ -102,8 +102,9 @@ public:
glm::vec2 projectPoint(glm::vec3 point, bool& pointInView) const; glm::vec2 projectPoint(glm::vec3 point, bool& pointInView) const;
OctreeProjectedPolygon getProjectedPolygon(const AABox& box) 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: private:
// Used for keyhole calculations // Used for keyhole calculations
ViewFrustum::location pointInKeyhole(const glm::vec3& point) const; ViewFrustum::location pointInKeyhole(const glm::vec3& point) const;