Add getCorners to ViewFrustum

This commit is contained in:
Zach Pomerantz 2016-01-13 22:49:00 -08:00
parent 1436b4f6b1
commit ed5c53f23c
2 changed files with 29 additions and 1 deletions

View file

@ -736,6 +736,23 @@ void ViewFrustum::getFurthestPointFromCamera(const AACube& box, glm::vec3& furth
}
}
const ViewFrustum::Corners ViewFrustum::getCorners(const float& depth) {
glm::vec3 normal = glm::normalize(_direction);
auto getCorner = [&](enum::BoxVertex nearCorner, enum::BoxVertex farCorner) {
const auto dir = glm::normalize(_cornersWorld[nearCorner] - _cornersWorld[farCorner]);
const auto factor = depth / glm::dot(dir, normal);
return _position + factor * dir;
};
return Corners{
getCorner(TOP_LEFT_NEAR, TOP_LEFT_FAR),
getCorner(TOP_RIGHT_NEAR, TOP_RIGHT_FAR),
getCorner(BOTTOM_LEFT_NEAR, BOTTOM_LEFT_FAR),
getCorner(BOTTOM_RIGHT_NEAR, BOTTOM_RIGHT_FAR)
};
}
float ViewFrustum::distanceToCamera(const glm::vec3& point) const {
glm::vec3 temp = getPosition() - point;
float distanceToPoint = sqrtf(glm::dot(temp, temp));

View file

@ -61,11 +61,22 @@ public:
float getFarClip() const { return _farClip; }
float getFocalLength() const { return _focalLength; }
const class Corners{
public:
Corners(glm::vec3&& topLeft, glm::vec3&& topRight, glm::vec3&& bottomLeft, glm::vec3&& bottomRight)
: topLeft{ topLeft }, topRight{ topRight }, bottomLeft{ bottomLeft }, bottomRight{ bottomRight } {}
glm::vec3 topLeft;
glm::vec3 topRight;
glm::vec3 bottomLeft;
glm::vec3 bottomRight;
// Get the corners depth units from frustum position, along frustum orientation
} getCorners(const float& depth);
// getters for corners
const glm::vec3& getFarTopLeft() const { return _cornersWorld[TOP_LEFT_FAR]; }
const glm::vec3& getFarTopRight() const { return _cornersWorld[TOP_RIGHT_FAR]; }
const glm::vec3& getFarBottomLeft() const { return _cornersWorld[BOTTOM_LEFT_FAR]; }
const glm::vec3& getFarBottomRight() const { return _cornersWorld[BOTTOM_RIGHT_FAR]; }
const glm::vec3& getNearTopLeft() const { return _cornersWorld[TOP_LEFT_NEAR]; }
const glm::vec3& getNearTopRight() const { return _cornersWorld[TOP_RIGHT_NEAR]; }
const glm::vec3& getNearBottomLeft() const { return _cornersWorld[BOTTOM_LEFT_NEAR]; }