optimized OctreeElement::furthestDistanceToCamera()

This commit is contained in:
ZappoMan 2014-03-19 12:48:11 -07:00
parent 252bc3b3ee
commit f3bd8925d9
3 changed files with 40 additions and 8 deletions

View file

@ -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 {

View file

@ -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;
}
}

View file

@ -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