From 07d7db97c24212454c2b8b13110194c469767c03 Mon Sep 17 00:00:00 2001 From: Wellinator Date: Mon, 18 Jul 2022 20:27:32 -0300 Subject: [PATCH] feat: implements min and max bbox methods --- engine/inc/renderer/3d/bbox/bbox.hpp | 14 +++++- engine/src/renderer/3d/bbox/bbox.cpp | 64 ++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/engine/inc/renderer/3d/bbox/bbox.hpp b/engine/inc/renderer/3d/bbox/bbox.hpp index 35fdd0c..06feaeb 100644 --- a/engine/inc/renderer/3d/bbox/bbox.hpp +++ b/engine/inc/renderer/3d/bbox/bbox.hpp @@ -41,9 +41,21 @@ class BBox : public CoreBBox { /** @returns the bottom face (nearer on y-axis) */ const BBoxFace& getBottomFace() { return _bottomFace; } + /** @returns the lower (x, y, z) boundary of the box. */ + const Vec4 min(); + /** @returns the upper (x, y, z) boundary of the box. */ + const Vec4 max(); + /** + * @returns void; + * @param res_min Vec4 to store the min result + * @param res_max Vec4 to store the min result + * @brief Calc and stores the min and max points of box at a single loop + * */ + void calcMinMax(Vec4& res_min, Vec4& res_max); + protected: float _height, _depth, _width; - Vec4 _centerVector; + Vec4 _centerVector, _min; /** Front face (further on z-axis) */ BBoxFace _frontFace; /** Back face (nearer on z-axis) */ diff --git a/engine/src/renderer/3d/bbox/bbox.cpp b/engine/src/renderer/3d/bbox/bbox.cpp index 64e5d20..07a0219 100644 --- a/engine/src/renderer/3d/bbox/bbox.cpp +++ b/engine/src/renderer/3d/bbox/bbox.cpp @@ -50,4 +50,68 @@ void BBox::setData() { _bottomFace = BBoxFace(_vertices[0], _vertices[5], _vertices[0].y); } +const Vec4 BBox::min() { + Vec4 temp, _min; + u8 isInitialized = 0; + + const Vec4* vertices = getVertices(); + for (u8 i = 0; i < 8; i++) { + temp.set(vertices[i].x, vertices[i].y, vertices[i].z, 1.0F); + if (isInitialized == 0) { + isInitialized = 1; + _min.set(temp); + } + + if (_min.x > temp.x) _min.x = temp.x; + if (_min.y > temp.y) _min.y = temp.y; + if (_min.z > temp.z) _min.z = temp.z; + } + + return _min; +} + +const Vec4 BBox::max() { + Vec4 temp, _max; + u8 isInitialized = 0; + + const Vec4* vertices = getVertices(); + for (u8 i = 0; i < 8; i++) { + temp.set(vertices[i].x, vertices[i].y, vertices[i].z, 1.0F); + if (isInitialized == 0) { + isInitialized = 1; + _max.set(temp); + } + + if (temp.x > _max.x) _max.x = temp.x; + if (temp.y > _max.y) _max.y = temp.y; + if (temp.z > _max.z) _max.z = temp.z; + } + + return _max; +} + +void BBox::calcMinMax(Vec4& res_min, Vec4& res_max) { + Vec4 temp = Vec4(); + + u8 isInitialized = 0; + const Vec4* vertices = getVertices(); + for (u8 i = 0; i < 8; i++) { + temp.set(vertices[i].x, vertices[i].y, vertices[i].z, 1.0F); + if (isInitialized == 0) { + isInitialized = 1; + res_min.set(temp); + res_max.set(temp); + } + + if (res_min.x > temp.x) res_min.x = temp.x; + if (temp.x > res_max.x) res_max.x = temp.x; + + if (res_min.y > temp.y) res_min.y = temp.y; + if (temp.y > res_max.y) res_max.y = temp.y; + + if (res_min.z > temp.z) res_min.z = temp.z; + if (temp.z > res_max.z) res_max.z = temp.z; + } +} + } // namespace Tyra