|
@@ -6,7 +6,6 @@
|
|
|
#include "anki/collision/Frustum.h"
|
|
#include "anki/collision/Frustum.h"
|
|
|
#include "anki/collision/LineSegment.h"
|
|
#include "anki/collision/LineSegment.h"
|
|
|
#include "anki/collision/Aabb.h"
|
|
#include "anki/collision/Aabb.h"
|
|
|
-#include <limits>
|
|
|
|
|
|
|
|
|
|
namespace anki {
|
|
namespace anki {
|
|
|
|
|
|
|
@@ -20,7 +19,7 @@ Frustum& Frustum::operator=(const Frustum& b)
|
|
|
ANKI_ASSERT(m_type == b.m_type);
|
|
ANKI_ASSERT(m_type == b.m_type);
|
|
|
m_near = b.m_near;
|
|
m_near = b.m_near;
|
|
|
m_far = b.m_far;
|
|
m_far = b.m_far;
|
|
|
- m_planes = b.m_planes;
|
|
|
|
|
|
|
+ m_planesL = b.m_planesL;
|
|
|
m_planesW = b.m_planesW;
|
|
m_planesW = b.m_planesW;
|
|
|
m_trf = b.m_trf;
|
|
m_trf = b.m_trf;
|
|
|
m_frustumDirty = b.m_frustumDirty;
|
|
m_frustumDirty = b.m_frustumDirty;
|
|
@@ -74,27 +73,8 @@ Bool Frustum::insideFrustum(const CollisionShape& b)
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
void Frustum::transform(const Transform& trf)
|
|
void Frustum::transform(const Transform& trf)
|
|
|
{
|
|
{
|
|
|
- m_trf = m_trf.combineTransformations(trf);
|
|
|
|
|
-
|
|
|
|
|
- if(m_frustumDirty)
|
|
|
|
|
- {
|
|
|
|
|
- // Update everything
|
|
|
|
|
- updateInternal();
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- // Update only the other shapes
|
|
|
|
|
- recalculate(false, true);
|
|
|
|
|
-
|
|
|
|
|
- // Transform the compound
|
|
|
|
|
- CompoundShape::transform(m_trf);
|
|
|
|
|
-
|
|
|
|
|
- // Transform the planes
|
|
|
|
|
- for(U i = 0; i < m_planes.getSize(); ++i)
|
|
|
|
|
- {
|
|
|
|
|
- m_planesW[i] = m_planes[i].getTransformed(m_trf);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ Transform trfa = m_trf.combineTransformations(trf);
|
|
|
|
|
+ resetTransform(trfa);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
@@ -109,16 +89,13 @@ void Frustum::resetTransform(const Transform& trf)
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- // Update only the other shapes
|
|
|
|
|
- recalculate(false, true);
|
|
|
|
|
-
|
|
|
|
|
- // Transform the compound
|
|
|
|
|
- CompoundShape::transform(m_trf);
|
|
|
|
|
|
|
+ // Inform the child about the change
|
|
|
|
|
+ onTransform();
|
|
|
|
|
|
|
|
// Transform the planes
|
|
// Transform the planes
|
|
|
- for(U i = 0; i < m_planes.getSize(); ++i)
|
|
|
|
|
|
|
+ for(U i = 0; i < m_planesL.getSize(); ++i)
|
|
|
{
|
|
{
|
|
|
- m_planesW[i] = m_planes[i].getTransformed(m_trf);
|
|
|
|
|
|
|
+ m_planesW[i] = m_planesL[i].getTransformed(m_trf);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -138,15 +115,15 @@ void Frustum::updateInternal()
|
|
|
{
|
|
{
|
|
|
ANKI_ASSERT(m_frustumDirty);
|
|
ANKI_ASSERT(m_frustumDirty);
|
|
|
m_frustumDirty = false;
|
|
m_frustumDirty = false;
|
|
|
- recalculate(true, true);
|
|
|
|
|
|
|
+ recalculate();
|
|
|
|
|
|
|
|
- // Transform the compound
|
|
|
|
|
- CompoundShape::transform(m_trf);
|
|
|
|
|
|
|
+ // Transform derived
|
|
|
|
|
+ onTransform();
|
|
|
|
|
|
|
|
// Transform the planes
|
|
// Transform the planes
|
|
|
- for(U i = 0; i < m_planes.getSize(); ++i)
|
|
|
|
|
|
|
+ for(U i = 0; i < m_planesL.getSize(); ++i)
|
|
|
{
|
|
{
|
|
|
- m_planesW[i] = m_planes[i].getTransformed(m_trf);
|
|
|
|
|
|
|
+ m_planesW[i] = m_planesL[i].getTransformed(m_trf);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -159,7 +136,7 @@ PerspectiveFrustum::PerspectiveFrustum()
|
|
|
: Frustum(Type::PERSPECTIVE)
|
|
: Frustum(Type::PERSPECTIVE)
|
|
|
{
|
|
{
|
|
|
addShape(&m_hull);
|
|
addShape(&m_hull);
|
|
|
- m_hull.initStorage(&m_points[0], m_points.getSize());
|
|
|
|
|
|
|
+ m_hull.initStorage(&m_pointsW[0], m_pointsW.getSize());
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
@@ -168,52 +145,63 @@ PerspectiveFrustum& PerspectiveFrustum::operator=(const PerspectiveFrustum& b)
|
|
|
Frustum::operator=(b);
|
|
Frustum::operator=(b);
|
|
|
m_fovX = b.m_fovX;
|
|
m_fovX = b.m_fovX;
|
|
|
m_fovY = b.m_fovY;
|
|
m_fovY = b.m_fovY;
|
|
|
- m_points = b.m_points;
|
|
|
|
|
|
|
+ m_pointsL = b.m_pointsL;
|
|
|
|
|
+ m_pointsW = b.m_pointsW;
|
|
|
return *this;
|
|
return *this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
-void PerspectiveFrustum::recalculate(Bool planes, Bool other)
|
|
|
|
|
|
|
+void PerspectiveFrustum::onTransform()
|
|
|
{
|
|
{
|
|
|
- if(planes)
|
|
|
|
|
|
|
+ // Eye
|
|
|
|
|
+ m_pointsW[0] = m_trf.getOrigin();
|
|
|
|
|
+
|
|
|
|
|
+ for(U i = 1; i < 5; ++i)
|
|
|
{
|
|
{
|
|
|
- F32 c, s; // cos & sine
|
|
|
|
|
-
|
|
|
|
|
- sinCos(getPi<F32>() + m_fovX / 2.0, s, c);
|
|
|
|
|
- // right
|
|
|
|
|
- m_planes[(U)PlaneType::RIGHT] = Plane(Vec4(c, 0.0, s, 0.0), 0.0);
|
|
|
|
|
- // left
|
|
|
|
|
- m_planes[(U)PlaneType::LEFT] = Plane(Vec4(-c, 0.0, s, 0.0), 0.0);
|
|
|
|
|
-
|
|
|
|
|
- sinCos((getPi<F32>() + m_fovY) * 0.5, s, c);
|
|
|
|
|
- // bottom
|
|
|
|
|
- m_planes[(U)PlaneType::BOTTOM] = Plane(Vec4(0.0, s, c, 0.0), 0.0);
|
|
|
|
|
- // top
|
|
|
|
|
- m_planes[(U)PlaneType::TOP] = Plane(Vec4(0.0, -s, c, 0.0), 0.0);
|
|
|
|
|
-
|
|
|
|
|
- // near
|
|
|
|
|
- m_planes[(U)PlaneType::NEAR] = Plane(Vec4(0.0, 0.0, -1.0, 0.0), m_near);
|
|
|
|
|
- // far
|
|
|
|
|
- m_planes[(U)PlaneType::FAR] = Plane(Vec4(0.0, 0.0, 1.0, 0.0), -m_far);
|
|
|
|
|
|
|
+ m_pointsW[i] = m_trf.transform(m_pointsL[i - 1]);
|
|
|
}
|
|
}
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- if(other)
|
|
|
|
|
|
|
+//==============================================================================
|
|
|
|
|
+void PerspectiveFrustum::recalculate()
|
|
|
|
|
+{
|
|
|
|
|
+ // Planes
|
|
|
|
|
+ //
|
|
|
|
|
+ F32 c, s; // cos & sine
|
|
|
|
|
+
|
|
|
|
|
+ sinCos(getPi<F32>() + m_fovX / 2.0, s, c);
|
|
|
|
|
+ // right
|
|
|
|
|
+ m_planesL[(U)PlaneType::RIGHT] = Plane(Vec4(c, 0.0, s, 0.0), 0.0);
|
|
|
|
|
+ // left
|
|
|
|
|
+ m_planesL[(U)PlaneType::LEFT] = Plane(Vec4(-c, 0.0, s, 0.0), 0.0);
|
|
|
|
|
+
|
|
|
|
|
+ sinCos((getPi<F32>() + m_fovY) * 0.5, s, c);
|
|
|
|
|
+ // bottom
|
|
|
|
|
+ m_planesL[(U)PlaneType::BOTTOM] = Plane(Vec4(0.0, s, c, 0.0), 0.0);
|
|
|
|
|
+ // top
|
|
|
|
|
+ m_planesL[(U)PlaneType::TOP] = Plane(Vec4(0.0, -s, c, 0.0), 0.0);
|
|
|
|
|
+
|
|
|
|
|
+ // near
|
|
|
|
|
+ m_planesL[(U)PlaneType::NEAR] = Plane(Vec4(0.0, 0.0, -1.0, 0.0), m_near);
|
|
|
|
|
+ // far
|
|
|
|
|
+ m_planesL[(U)PlaneType::FAR] = Plane(Vec4(0.0, 0.0, 1.0, 0.0), -m_far);
|
|
|
|
|
+
|
|
|
|
|
+ // Points
|
|
|
|
|
+ //
|
|
|
|
|
+ Vec4 eye = Vec4(0.0, 0.0, -m_near, 0.0);
|
|
|
|
|
+ for(Vec4& p : m_pointsL)
|
|
|
{
|
|
{
|
|
|
- Vec4 eye = Vec4(0.0, 0.0, -m_near, 0.0);
|
|
|
|
|
- for(Vec4& p : m_points)
|
|
|
|
|
- {
|
|
|
|
|
- p = eye;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ p = eye;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- F32 x = m_far / tan((getPi<F32>() - m_fovX) / 2.0);
|
|
|
|
|
- F32 y = tan(m_fovY / 2.0) * m_far;
|
|
|
|
|
- F32 z = -m_far;
|
|
|
|
|
|
|
+ F32 x = m_far / tan((getPi<F32>() - m_fovX) / 2.0);
|
|
|
|
|
+ F32 y = tan(m_fovY / 2.0) * m_far;
|
|
|
|
|
+ F32 z = -m_far;
|
|
|
|
|
|
|
|
- m_points[1] += Vec4(x, y, z - m_near, 0.0); // top right
|
|
|
|
|
- m_points[2] += Vec4(-x, y, z - m_near, 0.0); // top left
|
|
|
|
|
- m_points[3] += Vec4(-x, -y, z - m_near, 0.0); // bot left
|
|
|
|
|
- m_points[4] += Vec4(x, -y, z - m_near, 0.0); // bot right
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ m_pointsL[0] += Vec4(x, y, z - m_near, 0.0); // top right
|
|
|
|
|
+ m_pointsL[1] += Vec4(-x, y, z - m_near, 0.0); // top left
|
|
|
|
|
+ m_pointsL[2] += Vec4(-x, -y, z - m_near, 0.0); // bot left
|
|
|
|
|
+ m_pointsL[3] += Vec4(x, -y, z - m_near, 0.0); // bot right
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
@@ -272,7 +260,7 @@ Mat4 PerspectiveFrustum::calculateProjectionMatrix() const
|
|
|
OrthographicFrustum::OrthographicFrustum()
|
|
OrthographicFrustum::OrthographicFrustum()
|
|
|
: Frustum(Type::ORTHOGRAPHIC)
|
|
: Frustum(Type::ORTHOGRAPHIC)
|
|
|
{
|
|
{
|
|
|
- addShape(&m_obb);
|
|
|
|
|
|
|
+ addShape(&m_obbW);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
@@ -284,7 +272,8 @@ OrthographicFrustum& OrthographicFrustum::operator=(
|
|
|
m_right = b.m_right;
|
|
m_right = b.m_right;
|
|
|
m_top = b.m_top;
|
|
m_top = b.m_top;
|
|
|
m_bottom = b.m_bottom;
|
|
m_bottom = b.m_bottom;
|
|
|
- m_obb = b.m_obb;
|
|
|
|
|
|
|
+ m_obbL = b.m_obbL;
|
|
|
|
|
+ m_obbW = b.m_obbW;
|
|
|
return *this;
|
|
return *this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -322,36 +311,36 @@ Mat4 OrthographicFrustum::calculateProjectionMatrix() const
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
-void OrthographicFrustum::recalculate(Bool planes, Bool other)
|
|
|
|
|
|
|
+void OrthographicFrustum::recalculate()
|
|
|
{
|
|
{
|
|
|
- if(planes)
|
|
|
|
|
- {
|
|
|
|
|
- // Planes
|
|
|
|
|
- m_planes[(U)PlaneType::LEFT] =
|
|
|
|
|
- Plane(Vec4(1.0, 0.0, 0.0, 0.0), m_left);
|
|
|
|
|
- m_planes[(U)PlaneType::RIGHT] =
|
|
|
|
|
- Plane(Vec4(-1.0, 0.0, 0.0, 0.0), -m_right);
|
|
|
|
|
-
|
|
|
|
|
- m_planes[(U)PlaneType::NEAR] =
|
|
|
|
|
- Plane(Vec4(0.0, 0.0, -1.0, 0.0), m_near);
|
|
|
|
|
- m_planes[(U)PlaneType::FAR] =
|
|
|
|
|
- Plane(Vec4(0.0, 0.0, 1.0, 0.0), -m_far);
|
|
|
|
|
- m_planes[(U)PlaneType::TOP] =
|
|
|
|
|
- Plane(Vec4(0.0, -1.0, 0.0, 0.0), -m_top);
|
|
|
|
|
- m_planes[(U)PlaneType::BOTTOM] =
|
|
|
|
|
- Plane(Vec4(0.0, 1.0, 0.0, 0.0), m_bottom);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // Planes
|
|
|
|
|
+ m_planesL[(U)PlaneType::LEFT] =
|
|
|
|
|
+ Plane(Vec4(1.0, 0.0, 0.0, 0.0), m_left);
|
|
|
|
|
+ m_planesL[(U)PlaneType::RIGHT] =
|
|
|
|
|
+ Plane(Vec4(-1.0, 0.0, 0.0, 0.0), -m_right);
|
|
|
|
|
+
|
|
|
|
|
+ m_planesL[(U)PlaneType::NEAR] =
|
|
|
|
|
+ Plane(Vec4(0.0, 0.0, -1.0, 0.0), m_near);
|
|
|
|
|
+ m_planesL[(U)PlaneType::FAR] =
|
|
|
|
|
+ Plane(Vec4(0.0, 0.0, 1.0, 0.0), -m_far);
|
|
|
|
|
+ m_planesL[(U)PlaneType::TOP] =
|
|
|
|
|
+ Plane(Vec4(0.0, -1.0, 0.0, 0.0), -m_top);
|
|
|
|
|
+ m_planesL[(U)PlaneType::BOTTOM] =
|
|
|
|
|
+ Plane(Vec4(0.0, 1.0, 0.0, 0.0), m_bottom);
|
|
|
|
|
+
|
|
|
|
|
+ // OBB
|
|
|
|
|
+ Vec4 c((m_right + m_left) * 0.5,
|
|
|
|
|
+ (m_top + m_bottom) * 0.5,
|
|
|
|
|
+ -(m_far + m_near) * 0.5,
|
|
|
|
|
+ 0.0);
|
|
|
|
|
+ Vec4 e = Vec4(m_right, m_top, -m_far, 0.0) - c;
|
|
|
|
|
+ m_obbL = Obb(c, Mat3x4::getIdentity(), e);
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- if(other)
|
|
|
|
|
- {
|
|
|
|
|
- // OBB
|
|
|
|
|
- Vec4 c((m_right + m_left) * 0.5,
|
|
|
|
|
- (m_top + m_bottom) * 0.5,
|
|
|
|
|
- -(m_far + m_near) * 0.5,
|
|
|
|
|
- 0.0);
|
|
|
|
|
- Vec4 e = Vec4(m_right, m_top, -m_far, 0.0) - c;
|
|
|
|
|
- m_obb = Obb(c, Mat3x4::getIdentity(), e);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+//==============================================================================
|
|
|
|
|
+void OrthographicFrustum::onTransform()
|
|
|
|
|
+{
|
|
|
|
|
+ m_obbW = m_obbL.getTransformed(m_trf);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} // end namespace anki
|
|
} // end namespace anki
|