|
@@ -11,20 +11,16 @@ namespace anki {
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
Frustum& Frustum::operator=(const Frustum& b)
|
|
Frustum& Frustum::operator=(const Frustum& b)
|
|
|
{
|
|
{
|
|
|
- ANKI_ASSERT(type == b.type);
|
|
|
|
|
- zNear = b.zNear;
|
|
|
|
|
- zFar = b.zFar;
|
|
|
|
|
- trf = b.trf;
|
|
|
|
|
planes = b.planes;
|
|
planes = b.planes;
|
|
|
|
|
+ near = b.near;
|
|
|
|
|
+ far = b.far;
|
|
|
|
|
+ trf = b.trf;
|
|
|
return *this;
|
|
return *this;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
bool Frustum::insideFrustum(const CollisionShape& b) const
|
|
bool Frustum::insideFrustum(const CollisionShape& b) const
|
|
|
{
|
|
{
|
|
|
- recalculatePlanes();
|
|
|
|
|
- transformPlanes();
|
|
|
|
|
-
|
|
|
|
|
for(const Plane& plane : planes)
|
|
for(const Plane& plane : planes)
|
|
|
{
|
|
{
|
|
|
if(b.testPlane(plane) < 0.0)
|
|
if(b.testPlane(plane) < 0.0)
|
|
@@ -36,6 +32,18 @@ bool Frustum::insideFrustum(const CollisionShape& b) const
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+//==============================================================================
|
|
|
|
|
+void Frustum::transform(const Transform& trf_)
|
|
|
|
|
+{
|
|
|
|
|
+ trf.transform(trf_);
|
|
|
|
|
+
|
|
|
|
|
+ // Planes
|
|
|
|
|
+ for(Plane& p : planes)
|
|
|
|
|
+ {
|
|
|
|
|
+ p.transform(trf);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
// PerspectiveFrustum =
|
|
// PerspectiveFrustum =
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
@@ -54,9 +62,6 @@ PerspectiveFrustum& PerspectiveFrustum::operator=(const PerspectiveFrustum& b)
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
float PerspectiveFrustum::testPlane(const Plane& p) const
|
|
float PerspectiveFrustum::testPlane(const Plane& p) const
|
|
|
{
|
|
{
|
|
|
- recalculateShape();
|
|
|
|
|
- transformShape();
|
|
|
|
|
-
|
|
|
|
|
float o = 0.0;
|
|
float o = 0.0;
|
|
|
|
|
|
|
|
for(const Vec3& dir : dirs)
|
|
for(const Vec3& dir : dirs)
|
|
@@ -64,7 +69,7 @@ float PerspectiveFrustum::testPlane(const Plane& p) const
|
|
|
LineSegment ls(eye, dir);
|
|
LineSegment ls(eye, dir);
|
|
|
float t = ls.testPlane(p);
|
|
float t = ls.testPlane(p);
|
|
|
|
|
|
|
|
- if(t == 0.0)
|
|
|
|
|
|
|
+ if(t == 0)
|
|
|
{
|
|
{
|
|
|
return 0.0;
|
|
return 0.0;
|
|
|
}
|
|
}
|
|
@@ -84,14 +89,8 @@ float PerspectiveFrustum::testPlane(const Plane& p) const
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
void PerspectiveFrustum::transform(const Transform& trf_)
|
|
void PerspectiveFrustum::transform(const Transform& trf_)
|
|
|
{
|
|
{
|
|
|
- trf.transform(trf_);
|
|
|
|
|
- transformPlanes();
|
|
|
|
|
- transformShape();
|
|
|
|
|
-}
|
|
|
|
|
|
|
+ Frustum::transform(trf_);
|
|
|
|
|
|
|
|
-//==============================================================================
|
|
|
|
|
-void PerspectiveFrustum::transformShape() const
|
|
|
|
|
-{
|
|
|
|
|
eye.transform(trf);
|
|
eye.transform(trf);
|
|
|
|
|
|
|
|
for(Vec3& dir : dirs)
|
|
for(Vec3& dir : dirs)
|
|
@@ -103,17 +102,16 @@ void PerspectiveFrustum::transformShape() const
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
void PerspectiveFrustum::getAabb(Aabb& aabb) const
|
|
void PerspectiveFrustum::getAabb(Aabb& aabb) const
|
|
|
{
|
|
{
|
|
|
- recalculateShape();
|
|
|
|
|
- transformShape();
|
|
|
|
|
-
|
|
|
|
|
aabb.set(dirs);
|
|
aabb.set(dirs);
|
|
|
aabb.getMin() += eye;
|
|
aabb.getMin() += eye;
|
|
|
aabb.getMax() += eye;
|
|
aabb.getMax() += eye;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
-void PerspectiveFrustum::recalculatePlanes() const
|
|
|
|
|
|
|
+void PerspectiveFrustum::recalculate()
|
|
|
{
|
|
{
|
|
|
|
|
+ // Planes
|
|
|
|
|
+ //
|
|
|
float c, s; // cos & sine
|
|
float c, s; // cos & sine
|
|
|
|
|
|
|
|
Math::sinCos(Math::PI + fovX / 2, s, c);
|
|
Math::sinCos(Math::PI + fovX / 2, s, c);
|
|
@@ -129,24 +127,28 @@ void PerspectiveFrustum::recalculatePlanes() const
|
|
|
planes[FP_BOTTOM] = Plane(Vec3(0.0, -s, c), 0.0);
|
|
planes[FP_BOTTOM] = Plane(Vec3(0.0, -s, c), 0.0);
|
|
|
|
|
|
|
|
// near
|
|
// near
|
|
|
- planes[FP_NEAR] = Plane(Vec3(0.0, 0.0, -1.0), zNear);
|
|
|
|
|
|
|
+ planes[FP_NEAR] = Plane(Vec3(0.0, 0.0, -1.0), near);
|
|
|
// far
|
|
// far
|
|
|
- planes[FP_FAR] = Plane(Vec3(0.0, 0.0, 1.0), -zFar);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-//==============================================================================
|
|
|
|
|
-void PerspectiveFrustum::recalculateShape() const
|
|
|
|
|
-{
|
|
|
|
|
- eye = Vec3(0.0, 0.0, -zNear);
|
|
|
|
|
-
|
|
|
|
|
- float x = zFar / tan((Math::PI - fovX) / 2.0);
|
|
|
|
|
- float y = tan(fovY / 2.0) * zFar;
|
|
|
|
|
- float z = -zFar;
|
|
|
|
|
-
|
|
|
|
|
- dirs[0] = Vec3(x, y, z - zNear); // top right
|
|
|
|
|
- dirs[1] = Vec3(-x, y, z - zNear); // top left
|
|
|
|
|
- dirs[2] = Vec3(-x, -y, z - zNear); // bottom left
|
|
|
|
|
- dirs[3] = Vec3(x, -y, z - zNear); // bottom right
|
|
|
|
|
|
|
+ planes[FP_FAR] = Plane(Vec3(0.0, 0.0, 1.0), -far);
|
|
|
|
|
+
|
|
|
|
|
+ // Rest
|
|
|
|
|
+ //
|
|
|
|
|
+ eye = Vec3(0.0, 0.0, -near);
|
|
|
|
|
+
|
|
|
|
|
+ float x = far / tan((Math::PI - fovX) / 2.0);
|
|
|
|
|
+ float y = tan(fovY / 2.0) * far;
|
|
|
|
|
+ float z = -far;
|
|
|
|
|
+
|
|
|
|
|
+ dirs[0] = Vec3(x, y, z - near); // top right
|
|
|
|
|
+ dirs[1] = Vec3(-x, y, z - near); // top left
|
|
|
|
|
+ dirs[2] = Vec3(-x, -y, z - near); // bottom left
|
|
|
|
|
+ dirs[3] = Vec3(x, -y, z - near); // bottom right
|
|
|
|
|
+
|
|
|
|
|
+ // Transform
|
|
|
|
|
+ //
|
|
|
|
|
+ Transform tmptrf = trf;
|
|
|
|
|
+ trf.setIdentity();
|
|
|
|
|
+ transform(tmptrf);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
@@ -166,8 +168,8 @@ Mat4 PerspectiveFrustum::calculateProjectionMatrix() const
|
|
|
projectionMat(1, 3) = 0.0;
|
|
projectionMat(1, 3) = 0.0;
|
|
|
projectionMat(2, 0) = 0.0;
|
|
projectionMat(2, 0) = 0.0;
|
|
|
projectionMat(2, 1) = 0.0;
|
|
projectionMat(2, 1) = 0.0;
|
|
|
- projectionMat(2, 2) = (zFar + zNear) / (zNear - zFar);
|
|
|
|
|
- projectionMat(2, 3) = (2.0 * zFar * zNear) / (zNear - zFar);
|
|
|
|
|
|
|
+ projectionMat(2, 2) = (far + near) / (near - far);
|
|
|
|
|
+ projectionMat(2, 3) = (2.0 * far * near) / (near - far);
|
|
|
projectionMat(3, 0) = 0.0;
|
|
projectionMat(3, 0) = 0.0;
|
|
|
projectionMat(3, 1) = 0.0;
|
|
projectionMat(3, 1) = 0.0;
|
|
|
projectionMat(3, 2) = -1.0;
|
|
projectionMat(3, 2) = -1.0;
|
|
@@ -197,24 +199,19 @@ OrthographicFrustum& OrthographicFrustum::operator=(
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
float OrthographicFrustum::testPlane(const Plane& p) const
|
|
float OrthographicFrustum::testPlane(const Plane& p) const
|
|
|
{
|
|
{
|
|
|
- recalculateShape();
|
|
|
|
|
- transformShape();
|
|
|
|
|
return obb.testPlane(p);
|
|
return obb.testPlane(p);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
void OrthographicFrustum::transform(const Transform& trf_)
|
|
void OrthographicFrustum::transform(const Transform& trf_)
|
|
|
{
|
|
{
|
|
|
- trf.transform(trf_);
|
|
|
|
|
- transformPlanes();
|
|
|
|
|
- transformShape();
|
|
|
|
|
|
|
+ Frustum::transform(trf_);
|
|
|
|
|
+ obb.transform(trf);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
void OrthographicFrustum::getAabb(Aabb& aabb) const
|
|
void OrthographicFrustum::getAabb(Aabb& aabb) const
|
|
|
{
|
|
{
|
|
|
- recalculateShape();
|
|
|
|
|
- transformShape();
|
|
|
|
|
obb.getAabb(aabb);
|
|
obb.getAabb(aabb);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -223,15 +220,15 @@ Mat4 OrthographicFrustum::calculateProjectionMatrix() const
|
|
|
{
|
|
{
|
|
|
float difx = right - left;
|
|
float difx = right - left;
|
|
|
float dify = top - bottom;
|
|
float dify = top - bottom;
|
|
|
- float difz = zFar - zNear;
|
|
|
|
|
|
|
+ float difz = far - near;
|
|
|
float tx = -(right + left) / difx;
|
|
float tx = -(right + left) / difx;
|
|
|
float ty = -(top + bottom) / dify;
|
|
float ty = -(top + bottom) / dify;
|
|
|
- float tz = -(zFar + zNear) / difz;
|
|
|
|
|
|
|
+ float tz = -(far + near) / difz;
|
|
|
Mat4 m;
|
|
Mat4 m;
|
|
|
|
|
|
|
|
m(0, 0) = 2.0 / difx;
|
|
m(0, 0) = 2.0 / difx;
|
|
|
m(0, 1) = 0.0;
|
|
m(0, 1) = 0.0;
|
|
|
- m(0, 2) = 0.0;
|
|
|
|
|
|
|
+ m(0, 2) = 0.0:
|
|
|
m(0, 3) = tx;
|
|
m(0, 3) = tx;
|
|
|
m(1, 0) = 0.0;
|
|
m(1, 0) = 0.0;
|
|
|
m(1, 1) = 2.0 / dify;
|
|
m(1, 1) = 2.0 / dify;
|
|
@@ -250,22 +247,28 @@ Mat4 OrthographicFrustum::calculateProjectionMatrix() const
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
//==============================================================================
|
|
//==============================================================================
|
|
|
-void OrthographicFrustum::recalculatePlanes() const
|
|
|
|
|
|
|
+void OrthographicFrustum::recalculate()
|
|
|
{
|
|
{
|
|
|
|
|
+ // Planes
|
|
|
|
|
+ //
|
|
|
planes[FP_LEFT] = Plane(Vec3(1.0, 0.0, 0.0), left);
|
|
planes[FP_LEFT] = Plane(Vec3(1.0, 0.0, 0.0), left);
|
|
|
planes[FP_RIGHT] = Plane(Vec3(-1.0, 0.0, 0.0), -right);
|
|
planes[FP_RIGHT] = Plane(Vec3(-1.0, 0.0, 0.0), -right);
|
|
|
- planes[FP_NEAR] = Plane(Vec3(0.0, 0.0, -1.0), zNear);
|
|
|
|
|
- planes[FP_FAR] = Plane(Vec3(0.0, 0.0, 1.0), -zFar);
|
|
|
|
|
|
|
+ planes[FP_NEAR] = Plane(Vec3(0.0, 0.0, -1.0), near);
|
|
|
|
|
+ planes[FP_FAR] = Plane(Vec3(0.0, 0.0, 1.0), -far);
|
|
|
planes[FP_TOP] = Plane(Vec3(0.0, -1.0, 0.0), -top);
|
|
planes[FP_TOP] = Plane(Vec3(0.0, -1.0, 0.0), -top);
|
|
|
planes[FP_BOTTOM] = Plane(Vec3(0.0, 1.0, 0.0), bottom);
|
|
planes[FP_BOTTOM] = Plane(Vec3(0.0, 1.0, 0.0), bottom);
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
-//==============================================================================
|
|
|
|
|
-void OrthographicFrustum::recalculateShape() const
|
|
|
|
|
-{
|
|
|
|
|
- Vec3 c((right + left) * 0.5, (top + bottom) * 0.5, - (zFar + zNear) * 0.5);
|
|
|
|
|
- Vec3 e = Vec3(right, top, -zFar) - c;
|
|
|
|
|
|
|
+ // OBB
|
|
|
|
|
+ //
|
|
|
|
|
+ Vec3 c((right + left) * 0.5, (top + bottom) * 0.5, - (far + near) * 0.5);
|
|
|
|
|
+ Vec3 e = Vec3(right, top, -far) - c;
|
|
|
obb = Obb(c, Mat3::getIdentity(), e);
|
|
obb = Obb(c, Mat3::getIdentity(), e);
|
|
|
|
|
+
|
|
|
|
|
+ // Transform
|
|
|
|
|
+ //
|
|
|
|
|
+ Transform tmptrf = trf;
|
|
|
|
|
+ trf.setIdentity();
|
|
|
|
|
+ transform(tmptrf);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
} // end namespace
|
|
} // end namespace
|