|
@@ -6,78 +6,83 @@
|
|
|
namespace anki {
|
|
namespace anki {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+//==============================================================================
|
|
|
|
|
+// Frustum =
|
|
|
|
|
+//==============================================================================
|
|
|
|
|
+
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
Frustum& Frustum::operator=(const Frustum& b)
|
|
Frustum& Frustum::operator=(const Frustum& b)
|
|
|
{
|
|
{
|
|
|
- type = b.type;
|
|
|
|
|
planes = b.planes;
|
|
planes = b.planes;
|
|
|
-
|
|
|
|
|
- eye = b.eye;
|
|
|
|
|
- dirs = b.dirs;
|
|
|
|
|
-
|
|
|
|
|
- obb = b.obb;
|
|
|
|
|
|
|
+ zNear = b.zNear;
|
|
|
|
|
+ zFar = b.zFar;
|
|
|
return *this;
|
|
return *this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
-float Frustum::testPlane(const Plane& p) const
|
|
|
|
|
|
|
+bool Frustum::insideFrustum(const CollisionShape& b) const
|
|
|
{
|
|
{
|
|
|
- if(type == FT_ORTHOGRAPHIC)
|
|
|
|
|
- {
|
|
|
|
|
- return obb.testPlane(p);
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
|
|
+ BOOST_FOREACH(const Plane& plane, planes)
|
|
|
{
|
|
{
|
|
|
- float o = 0.0;
|
|
|
|
|
-
|
|
|
|
|
- BOOST_FOREACH(const Vec3& dir, dirs)
|
|
|
|
|
|
|
+ if(b.testPlane(plane) < 0.0)
|
|
|
{
|
|
{
|
|
|
- LineSegment ls(eye, dir);
|
|
|
|
|
- float t = ls.testPlane(p);
|
|
|
|
|
-
|
|
|
|
|
- if(t == 0)
|
|
|
|
|
- {
|
|
|
|
|
- return 0.0;
|
|
|
|
|
- }
|
|
|
|
|
- else if(t < 0.0)
|
|
|
|
|
- {
|
|
|
|
|
- o = std::max(o, t);
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- o = std::min(o, t);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- return o;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
-Frustum Frustum::getTransformed(const Transform& trf) const
|
|
|
|
|
|
|
+void Frustum::transform(const Transform& trf)
|
|
|
{
|
|
{
|
|
|
- Frustum o;
|
|
|
|
|
-
|
|
|
|
|
// Planes
|
|
// Planes
|
|
|
for(uint i = 0; i < planes.size(); ++i)
|
|
for(uint i = 0; i < planes.size(); ++i)
|
|
|
{
|
|
{
|
|
|
- o.planes[i] = planes[i].getTransformed(trf);
|
|
|
|
|
|
|
+ planes[i].transform(trf);
|
|
|
}
|
|
}
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- // Other
|
|
|
|
|
- if(type == FT_ORTHOGRAPHIC)
|
|
|
|
|
- {
|
|
|
|
|
- o.obb = obb.getTransformed(trf);
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
|
|
+
|
|
|
|
|
+//==============================================================================
|
|
|
|
|
+// PerspectiveFrustum =
|
|
|
|
|
+//==============================================================================
|
|
|
|
|
+
|
|
|
|
|
+//==============================================================================
|
|
|
|
|
+PerspectiveFrustum& PerspectiveFrustum::operator=(const PerspectiveFrustum& b)
|
|
|
|
|
+{
|
|
|
|
|
+ Frustum::operator=(*this);
|
|
|
|
|
+ eye = b.eye;
|
|
|
|
|
+ dirs = b.dirs;
|
|
|
|
|
+ fovX = b.fovX;
|
|
|
|
|
+ fovY = b.fovY;
|
|
|
|
|
+ return *this;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+//==============================================================================
|
|
|
|
|
+float PerspectiveFrustum::testPlane(const Plane& p) const
|
|
|
|
|
+{
|
|
|
|
|
+ float o = 0.0;
|
|
|
|
|
+
|
|
|
|
|
+ BOOST_FOREACH(const Vec3& dir, dirs)
|
|
|
{
|
|
{
|
|
|
- o.eye = eye.getTransformed(trf);
|
|
|
|
|
|
|
+ LineSegment ls(eye, dir);
|
|
|
|
|
+ float t = ls.testPlane(p);
|
|
|
|
|
|
|
|
- for(uint i = 0; i < dirs.size(); i++)
|
|
|
|
|
|
|
+ if(t == 0)
|
|
|
|
|
+ {
|
|
|
|
|
+ return 0.0;
|
|
|
|
|
+ }
|
|
|
|
|
+ else if(t < 0.0)
|
|
|
|
|
+ {
|
|
|
|
|
+ o = std::max(o, t);
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
{
|
|
{
|
|
|
- o.dirs[i] = trf.getRotation() * dirs[i];
|
|
|
|
|
|
|
+ o = std::min(o, t);
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -86,11 +91,45 @@ Frustum Frustum::getTransformed(const Transform& trf) const
|
|
|
|
|
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
-void Frustum::setPerspective(float fovX, float fovY,
|
|
|
|
|
- float zNear, float zFar, const Transform& trf)
|
|
|
|
|
|
|
+void PerspectiveFrustum::transform(const Transform& trf)
|
|
|
{
|
|
{
|
|
|
- type = FT_PERSPECTIVE;
|
|
|
|
|
|
|
+ Frustum::transform(trf);
|
|
|
|
|
|
|
|
|
|
+ eye.transform(trf);
|
|
|
|
|
+
|
|
|
|
|
+ for(uint i = 0; i < dirs.size(); i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ dirs[i] = trf.getRotation() * dirs[i];
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+//==============================================================================
|
|
|
|
|
+void PerspectiveFrustum::recalculate()
|
|
|
|
|
+{
|
|
|
|
|
+ // Planes
|
|
|
|
|
+ //
|
|
|
|
|
+ float c, s; // cos & sine
|
|
|
|
|
+
|
|
|
|
|
+ Math::sinCos(Math::PI + fovX / 2, s, c);
|
|
|
|
|
+ // right
|
|
|
|
|
+ planes[FP_RIGHT] = Plane(Vec3(c, 0.0, s), 0.0);
|
|
|
|
|
+ // left
|
|
|
|
|
+ planes[FP_LEFT] = Plane(Vec3(-c, 0.0, s), 0.0);
|
|
|
|
|
+
|
|
|
|
|
+ Math::sinCos((3 * Math::PI - fovY) * 0.5, s, c);
|
|
|
|
|
+ // top
|
|
|
|
|
+ planes[FP_TOP] = Plane(Vec3(0.0, s, c), 0.0);
|
|
|
|
|
+ // bottom
|
|
|
|
|
+ planes[FP_BOTTOM] = Plane(Vec3(0.0, -s, c), 0.0);
|
|
|
|
|
+
|
|
|
|
|
+ // near
|
|
|
|
|
+ planes[FP_NEAR] = Plane(Vec3(0.0, 0.0, -1.0), zNear);
|
|
|
|
|
+ // far
|
|
|
|
|
+ planes[FP_FAR] = Plane(Vec3(0.0, 0.0, 1.0), -zFar);
|
|
|
|
|
+
|
|
|
|
|
+ // Rest
|
|
|
|
|
+ //
|
|
|
eye = Vec3(0.0, 0.0, -zNear);
|
|
eye = Vec3(0.0, 0.0, -zNear);
|
|
|
|
|
|
|
|
float x = zFar / tan((Math::PI - fovX) / 2.0);
|
|
float x = zFar / tan((Math::PI - fovX) / 2.0);
|
|
@@ -101,27 +140,6 @@ void Frustum::setPerspective(float fovX, float fovY,
|
|
|
dirs[1] = Vec3(-x, y, z - zNear); // top left
|
|
dirs[1] = Vec3(-x, y, z - zNear); // top left
|
|
|
dirs[2] = Vec3(-x, -y, z - zNear); // bottom left
|
|
dirs[2] = Vec3(-x, -y, z - zNear); // bottom left
|
|
|
dirs[3] = Vec3(x, -y, z - zNear); // bottom right
|
|
dirs[3] = Vec3(x, -y, z - zNear); // bottom right
|
|
|
-
|
|
|
|
|
- eye.transform(trf);
|
|
|
|
|
- BOOST_FOREACH(Vec3& dir, dirs)
|
|
|
|
|
- {
|
|
|
|
|
- dir = trf.getRotation() * dir;
|
|
|
|
|
- }
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-//==============================================================================
|
|
|
|
|
-bool Frustum::insideFrustum(const CollisionShape& b) const
|
|
|
|
|
-{
|
|
|
|
|
- BOOST_FOREACH(const Plane& plane, planes)
|
|
|
|
|
- {
|
|
|
|
|
- if(b.testPlane(plane) < 0.0)
|
|
|
|
|
- {
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return true;
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|