2013-11-06 22:52:00 +01:00
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
// Created by Dan Andersson 2013
|
|
|
|
/////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "Frustrum.h"
|
2013-11-12 12:33:52 +01:00
|
|
|
#include "OysterCollision3D.h"
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-12-16 09:36:41 +01:00
|
|
|
using namespace ::Oyster::Math;
|
|
|
|
using namespace ::Oyster::Collision3D;
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
namespace PrivateStatic
|
|
|
|
{
|
2013-11-10 02:27:16 +01:00
|
|
|
inline void VP_ToPlanes( Plane &lp, Plane &rp, Plane &bp, Plane &tp, Plane &np, Plane &fp, const Float4x4 &vp )
|
|
|
|
{ /// TODO: : not verified
|
|
|
|
Float4x4 m = vp.GetTranspose();
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
// left
|
|
|
|
lp.normal = m.v[3].xyz + m.v[0].xyz;
|
2013-11-10 02:27:16 +01:00
|
|
|
lp.phasing = lp.normal.GetMagnitude();
|
2013-11-06 22:52:00 +01:00
|
|
|
lp.normal /= lp.phasing;
|
|
|
|
lp.phasing = (m.v[3].w + m.v[0].w) / lp.phasing;
|
|
|
|
|
|
|
|
// right
|
|
|
|
rp.normal = m.v[3].xyz - m.v[0].xyz;
|
2013-11-10 02:27:16 +01:00
|
|
|
rp.phasing = rp.normal.GetMagnitude();
|
2013-11-06 22:52:00 +01:00
|
|
|
rp.normal /= rp.phasing;
|
|
|
|
rp.phasing = (m.v[3].w - m.v[0].w) / rp.phasing;
|
|
|
|
|
|
|
|
// bottom
|
|
|
|
bp.normal = m.v[3].xyz + m.v[1].xyz;
|
2013-11-10 02:27:16 +01:00
|
|
|
bp.phasing = bp.normal.GetMagnitude();
|
2013-11-06 22:52:00 +01:00
|
|
|
bp.normal /= bp.phasing;
|
|
|
|
bp.phasing = (m.v[3].w + m.v[1].w) / bp.phasing;
|
|
|
|
|
|
|
|
// top
|
|
|
|
tp.normal = m.v[3].xyz - m.v[1].xyz;
|
2013-11-10 02:27:16 +01:00
|
|
|
tp.phasing = tp.normal.GetMagnitude();
|
2013-11-06 22:52:00 +01:00
|
|
|
tp.normal /= tp.phasing;
|
|
|
|
tp.phasing = (m.v[3].w - m.v[1].w) / tp.phasing;
|
|
|
|
|
|
|
|
// near leftHanded DirectX
|
|
|
|
np.normal = m.v[2].xyz;
|
2013-11-10 02:27:16 +01:00
|
|
|
np.phasing = np.normal.GetMagnitude();
|
2013-11-06 22:52:00 +01:00
|
|
|
np.normal /= np.phasing;
|
|
|
|
np.phasing = m.v[2].w / np.phasing;
|
|
|
|
|
|
|
|
// far lefthanded
|
|
|
|
fp.normal = m.v[3].xyz - m.v[2].xyz;
|
2013-11-10 02:27:16 +01:00
|
|
|
fp.phasing = fp.normal.GetMagnitude();
|
2013-11-06 22:52:00 +01:00
|
|
|
fp.normal /= fp.phasing;
|
|
|
|
fp.phasing = (m.v[3].w - m.v[2].w) / fp.phasing;
|
|
|
|
|
|
|
|
/*/ near rightHanded openGL
|
|
|
|
np.normal = m.v[3].xyz + m.v[2].xyz;
|
2013-11-10 02:27:16 +01:00
|
|
|
np.phasing = np.normal.GetMagnitude();
|
2013-11-06 22:52:00 +01:00
|
|
|
np.normal /= np.phasing;
|
|
|
|
np.phasing = -(m.v[3].w + m.v[2].w) / np.phasing;
|
|
|
|
|
|
|
|
// far rightHanded
|
|
|
|
fp.normal = m.v[3].xyz - m.v[2].xyz;
|
2013-11-10 02:27:16 +01:00
|
|
|
fp.phasing = fp.normal.GetMagnitude();
|
2013-11-06 22:52:00 +01:00
|
|
|
fp.normal /= fp.phasing;
|
|
|
|
fp.phasing = -(m.v[3].w - m.v[2].w) / fp.phasing;*/
|
|
|
|
}
|
|
|
|
|
2013-11-10 02:27:16 +01:00
|
|
|
void InterpolatePlanes( Plane &target, const Plane &planeA, const Plane &planeB, float interpolation )
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
float counterInterpol = 1.0f - interpolation;
|
|
|
|
target.normal = counterInterpol * planeA.normal;
|
|
|
|
target.normal += interpolation * planeB.normal;
|
|
|
|
target.phasing = counterInterpol * planeA.phasing;
|
|
|
|
target.phasing += interpolation * planeB.phasing;
|
2013-11-10 02:27:16 +01:00
|
|
|
target.normal.Normalize();
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-27 15:20:29 +01:00
|
|
|
Frustrum::Frustrum() : ICollideable(Type_frustrum)
|
|
|
|
{
|
2013-12-18 08:57:27 +01:00
|
|
|
this->leftPlane = Plane( Float4::standard_unit_x, -0.5f );
|
|
|
|
this->rightPlane = Plane(-Float4::standard_unit_x, 0.5f ),
|
|
|
|
this->bottomPlane = Plane( Float4::standard_unit_y, -0.5f );
|
|
|
|
this->topPlane = Plane(-Float4::standard_unit_y, 0.5f );
|
|
|
|
this->nearPlane = Plane( Float4::standard_unit_z, -0.5f );
|
|
|
|
this->farPlane = Plane(-Float4::standard_unit_z, 0.5f );
|
2013-11-27 15:20:29 +01:00
|
|
|
}
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-10 02:27:16 +01:00
|
|
|
Frustrum::Frustrum( const Float4x4 &vp ) : ICollideable(Type_frustrum)
|
2013-11-27 15:20:29 +01:00
|
|
|
{
|
|
|
|
PrivateStatic::VP_ToPlanes( this->leftPlane, this->rightPlane, this->bottomPlane,
|
|
|
|
this->topPlane, this->nearPlane, this->farPlane,
|
|
|
|
vp );
|
|
|
|
}
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-11 21:14:52 +01:00
|
|
|
Frustrum::~Frustrum() {}
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
Frustrum & Frustrum::operator = ( const Frustrum &frustrum )
|
|
|
|
{
|
2013-11-10 02:27:16 +01:00
|
|
|
this->plane[0] = frustrum.plane[0];
|
|
|
|
this->plane[1] = frustrum.plane[1];
|
|
|
|
this->plane[2] = frustrum.plane[2];
|
|
|
|
this->plane[3] = frustrum.plane[3];
|
|
|
|
this->plane[4] = frustrum.plane[4];
|
|
|
|
this->plane[5] = frustrum.plane[5];
|
2013-11-06 22:52:00 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Frustrum & Frustrum::operator = ( const Float4x4 &vp )
|
|
|
|
{
|
2013-11-10 02:27:16 +01:00
|
|
|
PrivateStatic::VP_ToPlanes( this->leftPlane, this->rightPlane, this->bottomPlane, this->topPlane, this->nearPlane, this->farPlane, vp );
|
2013-11-06 22:52:00 +01:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2013-11-10 13:34:35 +01:00
|
|
|
void Frustrum::Split( unsigned int numX, unsigned int numY, unsigned int numZ, Frustrum target[] ) const
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
float incrementX = 1.0f / numX,
|
|
|
|
incrementY = 1.0f / numY,
|
|
|
|
incrementZ = 1.0f / numZ,
|
|
|
|
interpolX = 0.0f,
|
|
|
|
interpolY = 0.0f,
|
|
|
|
interpolZ = 0.0f;
|
|
|
|
|
|
|
|
unsigned int i = 0U,
|
|
|
|
stepY = numX,
|
|
|
|
stepZ = numX * numY;
|
|
|
|
|
2013-11-10 02:27:16 +01:00
|
|
|
Plane invRight( -this->rightPlane.normal, -this->rightPlane.phasing ),
|
|
|
|
invBottom( -this->bottomPlane.normal, -this->bottomPlane.phasing ),
|
|
|
|
invFar( -this->farPlane.normal, -this->farPlane.phasing );
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
for( unsigned int iz = 0U; iz < numZ; ++iz )
|
|
|
|
{
|
|
|
|
interpolY = 0.0f;
|
|
|
|
for( unsigned int iy = 0U; iy < numY; ++iy )
|
|
|
|
{
|
|
|
|
interpolX = 0.0f;
|
|
|
|
for( unsigned int ix = 0U; ix < numX; ++ix )
|
|
|
|
{
|
|
|
|
if( ix == 0 )
|
|
|
|
target[i].leftPlane = this->leftPlane;
|
|
|
|
else
|
|
|
|
{
|
2013-11-10 02:27:16 +01:00
|
|
|
PrivateStatic::InterpolatePlanes( target[i].leftPlane, this->leftPlane, invRight, interpolX );
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
unsigned int iLeft = i - 1;
|
|
|
|
target[iLeft].rightPlane.normal = -target[i].leftPlane.normal;
|
|
|
|
target[iLeft].rightPlane.phasing = -target[i].leftPlane.phasing;
|
|
|
|
}
|
|
|
|
if( ix == numX - 1 )
|
|
|
|
target[i].rightPlane = this->rightPlane;
|
|
|
|
|
|
|
|
if( iy == 0 )
|
|
|
|
target[i].topPlane = this->topPlane;
|
|
|
|
else
|
|
|
|
{
|
2013-11-10 02:27:16 +01:00
|
|
|
PrivateStatic::InterpolatePlanes( target[i].topPlane, this->topPlane, invBottom, interpolY );
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
unsigned int iAbove = i - stepY;
|
|
|
|
target[iAbove].bottomPlane.normal = -target[i].topPlane.normal;
|
|
|
|
target[iAbove].bottomPlane.phasing = -target[i].topPlane.phasing;
|
|
|
|
}
|
|
|
|
if( iy == numY - 1 )
|
|
|
|
target[i].bottomPlane = this->bottomPlane;
|
|
|
|
|
|
|
|
if( iz == 0 )
|
|
|
|
target[i].nearPlane = this->nearPlane;
|
|
|
|
else
|
|
|
|
{
|
2013-11-10 02:27:16 +01:00
|
|
|
PrivateStatic::InterpolatePlanes( target[i].nearPlane, this->nearPlane, invFar, interpolZ );
|
2013-11-06 22:52:00 +01:00
|
|
|
|
|
|
|
unsigned int iCloser = i - stepZ;
|
|
|
|
target[iCloser].farPlane.normal = -target[i].nearPlane.normal;
|
|
|
|
target[iCloser].farPlane.phasing = -target[i].nearPlane.phasing;
|
|
|
|
}
|
|
|
|
if( iz == numZ - 1 )
|
|
|
|
target[i].farPlane = this->farPlane;
|
|
|
|
|
|
|
|
++i;
|
|
|
|
interpolX += incrementX;
|
|
|
|
}
|
|
|
|
interpolY += incrementY;
|
|
|
|
}
|
|
|
|
interpolZ += incrementZ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-10 13:34:35 +01:00
|
|
|
void Frustrum::WriteToByte( unsigned int &nextIndex, unsigned char targetMem[] ) const
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
|
|
|
Float *fMem = (Float*)&targetMem[nextIndex];
|
|
|
|
for( int p = 0; p < 6; ++p )
|
|
|
|
{
|
|
|
|
fMem[0] = this->plane[p].element[0];
|
|
|
|
fMem[1] = this->plane[p].element[1];
|
|
|
|
fMem[2] = this->plane[p].element[2];
|
|
|
|
fMem[3] = this->plane[p].element[3];
|
|
|
|
fMem = &fMem[4];
|
|
|
|
}
|
2013-11-10 02:27:16 +01:00
|
|
|
nextIndex += 6 * ::Utility::StaticArray::NumElementsOf( this->plane[0].byte );
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
|
2013-11-13 14:50:08 +01:00
|
|
|
::Utility::DynamicMemory::UniquePointer<ICollideable> Frustrum::Clone( ) const
|
2013-12-17 14:14:54 +01:00
|
|
|
{
|
|
|
|
return ::Utility::DynamicMemory::UniquePointer<ICollideable>( new Frustrum(*this) );
|
|
|
|
}
|
2013-11-06 22:52:00 +01:00
|
|
|
|
2013-11-25 14:22:38 +01:00
|
|
|
bool Frustrum::Intersects( const ICollideable &target ) const
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-25 14:22:38 +01:00
|
|
|
switch( target.type )
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-12-17 14:14:54 +01:00
|
|
|
case Type_universe: return true;
|
|
|
|
case Type_point: return Utility::Intersect( *this, (const Point&)target );
|
|
|
|
case Type_ray: return Utility::Intersect( *this, (const Ray&)target, ((const Ray&)target).collisionDistance );
|
|
|
|
case Type_sphere: return Utility::Intersect( *this, (const Sphere&)target );
|
|
|
|
case Type_plane: return Utility::Intersect( *this, (const Plane&)target );
|
|
|
|
//case Type_triangle: return false; // TODO:
|
|
|
|
case Type_box_axis_aligned: return Utility::Intersect( *this, (const BoxAxisAligned&)target );
|
|
|
|
case Type_box: return Utility::Intersect( *this, (const Box&)target );
|
|
|
|
case Type_frustrum: return Utility::Intersect( *this, (const Frustrum&)target );
|
|
|
|
//case Type_line: return false; // TODO:
|
|
|
|
default: return false;
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-18 08:57:27 +01:00
|
|
|
bool Frustrum::Intersects( const ICollideable &target, Float4 &worldPointOfContact ) const
|
2013-12-16 09:36:41 +01:00
|
|
|
{
|
|
|
|
//! @todo TODO: implement stub properly
|
|
|
|
return this->Intersects( target );
|
|
|
|
}
|
|
|
|
|
2013-11-25 14:22:38 +01:00
|
|
|
bool Frustrum::Contains( const ICollideable &target ) const
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-11-25 14:22:38 +01:00
|
|
|
switch( target.type )
|
2013-11-06 22:52:00 +01:00
|
|
|
{
|
2013-12-17 14:14:54 +01:00
|
|
|
case Type_point: return Utility::Intersect( *this, (const Point&)target );
|
|
|
|
//case Type_ray: return false; // TODO:
|
|
|
|
//case Type_sphere: return false; // TODO:
|
|
|
|
//case Type_plane: return false; // TODO:
|
|
|
|
//case Type_triangle: return false; // TODO:
|
|
|
|
//case Type_box_axis_aligned: return false; // TODO:
|
|
|
|
//case Type_box: return false; // TODO:
|
|
|
|
//case Type_frustrum: return false; // TODO:
|
|
|
|
//case Type_line: return false; // TODO:
|
|
|
|
default: return false;
|
2013-11-06 22:52:00 +01:00
|
|
|
}
|
2013-11-10 02:27:16 +01:00
|
|
|
}
|