refactor: moves isInFrustum from RenderBBox to CoreBBox

This commit is contained in:
Wellinator
2022-07-18 16:11:46 -03:00
parent f651ec76ac
commit 85f4f2e666
4 changed files with 36 additions and 36 deletions
@@ -36,6 +36,9 @@ class CoreBBox {
void print(const std::string& name) const { print(name.c_str()); } void print(const std::string& name) const { print(name.c_str()); }
std::string getPrint(const char* name = nullptr) const; std::string getPrint(const char* name = nullptr) const;
CoreBBoxFrustum isInFrustum(const Plane* frustumPlanes, const M4x4& model,
const float* margins = nullptr) const;
protected: protected:
Vec4 _vertices[8]; Vec4 _vertices[8];
}; };
@@ -25,9 +25,6 @@ class RenderBBox : public CoreBBox {
explicit RenderBBox(const std::vector<CoreBBox>& t_bboxes, explicit RenderBBox(const std::vector<CoreBBox>& t_bboxes,
const u32& startIndex, const u32& stopIndex); const u32& startIndex, const u32& stopIndex);
CoreBBoxFrustum isInFrustum(const Plane* frustumPlanes, const M4x4& model,
const float* margins = nullptr) const;
CoreBBoxFrustum clipIsInFrustum(const Plane* frustumPlanes, CoreBBoxFrustum clipIsInFrustum(const Plane* frustumPlanes,
const M4x4& model) const; const M4x4& model) const;
}; };
@@ -166,4 +166,37 @@ std::string CoreBBox::getPrint(const char* name) const {
return res.str(); return res.str();
} }
CoreBBoxFrustum CoreBBox::isInFrustum(const Plane* frustumPlanes,
const M4x4& model,
const float* margins) const {
CoreBBoxFrustum result = IN_FRUSTUM;
Vec4 boxCalcTemp;
u8 boxIn = 0, boxOut = 0;
for (u8 i = 0; i < 6; i++) {
const auto margin = margins == nullptr ? 0.0F : margins[i];
boxOut = 0;
boxIn = 0;
// for each corner of the box do ...
// get out of the cycle as soon as a box as corners
// both inside and out of the frustum
for (u8 y = 0; y < 8 && (boxIn == 0 || boxOut == 0); y++) {
boxCalcTemp = model * _vertices[y];
auto isOut = frustumPlanes[i].distanceTo(boxCalcTemp) < margin;
if (isOut)
boxOut++;
else
boxIn++;
}
// if all corners are out
if (!boxIn)
return OUTSIDE_FRUSTUM;
else if (boxOut)
result = PARTIALLY_IN_FRUSTUM;
}
return result;
}
} // namespace Tyra } // namespace Tyra
@@ -58,37 +58,4 @@ CoreBBoxFrustum RenderBBox::clipIsInFrustum(const Plane* frustumPlanes,
return isInFrustum(frustumPlanes, model, margins); // Let's check it again return isInFrustum(frustumPlanes, model, margins); // Let's check it again
} }
CoreBBoxFrustum RenderBBox::isInFrustum(const Plane* frustumPlanes,
const M4x4& model,
const float* margins) const {
CoreBBoxFrustum result = IN_FRUSTUM;
Vec4 boxCalcTemp;
u8 boxIn = 0, boxOut = 0;
for (u8 i = 0; i < 6; i++) {
const auto margin = margins == nullptr ? 0.0F : margins[i];
boxOut = 0;
boxIn = 0;
// for each corner of the box do ...
// get out of the cycle as soon as a box as corners
// both inside and out of the frustum
for (u8 y = 0; y < 8 && (boxIn == 0 || boxOut == 0); y++) {
boxCalcTemp = model * _vertices[y];
auto isOut = frustumPlanes[i].distanceTo(boxCalcTemp) < margin;
if (isOut)
boxOut++;
else
boxIn++;
}
// if all corners are out
if (!boxIn)
return OUTSIDE_FRUSTUM;
else if (boxOut)
result = PARTIALLY_IN_FRUSTUM;
}
return result;
}
} // namespace Tyra } // namespace Tyra