zeus/src/CAABox.cpp

40 lines
1.1 KiB
C++
Raw Normal View History

2016-03-04 15:03:26 -08:00
#include "zeus/CAABox.hpp"
2016-04-16 14:49:09 -07:00
#include "zeus/CVector3f.hpp"
2018-12-07 21:23:50 -08:00
namespace zeus {
2016-04-16 14:49:09 -07:00
const CAABox CAABox::skInvertedBox = CAABox();
const CAABox CAABox::skNullBox = CAABox(CVector3f::skZero, CVector3f::skZero);
2019-01-19 22:41:51 -08:00
static const int ProjWindings[6][4] = {
{2, 0, 4, 6}, // -X
{0, 1, 5, 4}, // -Y
{2, 3, 1, 0}, // -Z
{1, 3, 7, 5}, // +X
{3, 2, 6, 7}, // +Y
{4, 5, 7, 6}, // +Z
};
static bool testProjectedLine(const CVector2f& a, const CVector2f& b, const CVector2f& point) {
zeus::CVector2f normal = (b - a).perpendicularVector().normalized();
return point.dot(normal) >= a.dot(normal);
}
bool CAABox::projectedPointTest(const CMatrix4f& mvp, const CVector2f& point) const {
CVector2f projPoints[8];
for (int i = 0; i < 8; ++i)
projPoints[i] = mvp.multiplyOneOverW(getPoint(i)).toVec2f();
for (int i = 0, j; i < 6; ++i) {
for (j = 0; j < 3; ++j) {
if (!testProjectedLine(projPoints[ProjWindings[i][j]], projPoints[ProjWindings[i][j + 1]], point))
break;
}
if (j == 3 && testProjectedLine(projPoints[ProjWindings[i][3]], projPoints[ProjWindings[i][0]], point))
return true;
}
return false;
}
2018-12-07 21:23:50 -08:00
} // namespace zeus