Skip to content
Snippets Groups Projects
Verified Commit 2bf5952e authored by Tobias Frisch's avatar Tobias Frisch
Browse files

Separated different usage of transforming bounds

parent c04513d2
Branches
Tags
1 merge request!97Resolve "Dokumentation vervollständigen"
...@@ -32,6 +32,13 @@ namespace vkcv::scene { ...@@ -32,6 +32,13 @@ namespace vkcv::scene {
* box. * box.
*/ */
Bounds(); Bounds();
/**
* Constructor creating a zero volume axis aligned bounding
* box around a certain point.
* @param[in] point Center of the box as 3D vector
*/
Bounds(const glm::vec3& point);
/** /**
* Constructor creating an axis aligned bounding box with given * Constructor creating an axis aligned bounding box with given
......
...@@ -9,6 +9,16 @@ namespace vkcv::scene { ...@@ -9,6 +9,16 @@ namespace vkcv::scene {
* @addtogroup vkcv_scene * @addtogroup vkcv_scene
* @{ * @{
*/ */
/**
* Transform a given axis aligned bounding box into frustum coordinates
* using a given 4x4 transformation matrix to return a new axis aligned
* bounding box containing that transformed box.
* @param[in] transform Frustum defining 4x4 matrix
* @param[in] bounds Axis aligned bounding box
* @return Bounds containing box in frustum coordinates
*/
Bounds transformBounds(const glm::mat4& transform, const Bounds& bounds);
/** /**
* Transform a given axis aligned bounding box into frustum coordinates * Transform a given axis aligned bounding box into frustum coordinates
...@@ -16,10 +26,10 @@ namespace vkcv::scene { ...@@ -16,10 +26,10 @@ namespace vkcv::scene {
* bounding box containing that transformed box. * bounding box containing that transformed box.
* @param[in] transform Frustum defining 4x4 matrix * @param[in] transform Frustum defining 4x4 matrix
* @param[in] bounds Axis aligned bounding box * @param[in] bounds Axis aligned bounding box
* @param[out] negative_w Flag if w-coordinate is negative (optional) * @param[out] negative_w Flag if w-coordinate is negative
* @return Bounds containing box in frustum coordinates * @return Bounds containing box in frustum coordinates
*/ */
Bounds transformBounds(const glm::mat4& transform, const Bounds& bounds, bool* negative_w = nullptr); Bounds transformBounds(const glm::mat4& transform, const Bounds& bounds, bool& negative_w);
/** /**
* Check if an axis aligned bounding box is intersecting a given frustum * Check if an axis aligned bounding box is intersecting a given frustum
......
...@@ -7,6 +7,11 @@ namespace vkcv::scene { ...@@ -7,6 +7,11 @@ namespace vkcv::scene {
m_min(glm::vec3(0)), m_min(glm::vec3(0)),
m_max(glm::vec3(0)) {} m_max(glm::vec3(0)) {}
Bounds::Bounds(const glm::vec3 &point) :
m_min(point),
m_max(point)
{}
Bounds::Bounds(const glm::vec3 &min, const glm::vec3 &max) : Bounds::Bounds(const glm::vec3 &min, const glm::vec3 &max) :
m_min(min), m_min(min),
m_max(max) m_max(max)
......
...@@ -3,10 +3,9 @@ ...@@ -3,10 +3,9 @@
namespace vkcv::scene { namespace vkcv::scene {
static glm::vec3 transformPoint(const glm::mat4& transform, const glm::vec3& point, bool* negative_w) { static glm::vec3 transformPoint(const glm::mat4& transform, const glm::vec3& point, bool& negative_w) {
const glm::vec4 position = transform * glm::vec4(point, 1.0f); const glm::vec4 position = transform * glm::vec4(point, 1.0f);
/* /*
* We divide by the absolute of the 4th coorditnate because * We divide by the absolute of the 4th coorditnate because
* clipping is weird and points have to move to the other * clipping is weird and points have to move to the other
...@@ -16,39 +15,35 @@ namespace vkcv::scene { ...@@ -16,39 +15,35 @@ namespace vkcv::scene {
* to know if all corners are behind the camera. So these can * to know if all corners are behind the camera. So these can
* be culled as well * be culled as well
*/ */
if (negative_w) { const float perspective = std::abs(position[3]);
const float perspective = std::abs(position[3]); negative_w &= (position[3] < 0.0f);
return glm::vec3(
*negative_w &= (position[3] < 0.0f); position[0] / perspective,
position[1] / perspective,
return glm::vec3( position[2] / perspective
position[0] / perspective, );
position[1] / perspective,
position[2] / perspective
);
} else {
return glm::vec3(
position[0],
position[1],
position[2]
);
}
} }
Bounds transformBounds(const glm::mat4& transform, const Bounds& bounds, bool* negative_w) { Bounds transformBounds(const glm::mat4& transform, const Bounds& bounds) {
const auto corners = bounds.getCorners(); const auto corners = bounds.getCorners();
if (negative_w) { Bounds result (glm::vec3(transform * glm::vec4(corners[0], 1.0f)));
*negative_w = true;
for (size_t j = 1; j < corners.size(); j++) {
result.extend(glm::vec3(transform * glm::vec4(corners[j], 1.0f)));
} }
auto projected = transformPoint(transform, corners[0], negative_w); return result;
}
Bounds transformBounds(const glm::mat4& transform, const Bounds& bounds, bool& negative_w) {
const auto corners = bounds.getCorners();
negative_w = true;
Bounds result (projected, projected); Bounds result (transformPoint(transform, corners[0], negative_w));
for (size_t j = 1; j < corners.size(); j++) { for (size_t j = 1; j < corners.size(); j++) {
projected = transformPoint(transform, corners[j], negative_w); result.extend(transformPoint(transform, corners[j], negative_w));
result.extend(projected);
} }
return result; return result;
...@@ -61,7 +56,7 @@ namespace vkcv::scene { ...@@ -61,7 +56,7 @@ namespace vkcv::scene {
); );
bool negative_w; bool negative_w;
auto box = transformBounds(transform, bounds, &negative_w); auto box = transformBounds(transform, bounds, negative_w);
if (negative_w) { if (negative_w) {
return false; return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment