1
0
forked from 0ad/0ad
0ad/source/graphics/Frustum.cpp
janwas c0ed950657 had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).

it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.

after several hours, the code now requires fewer casts and less
guesswork.

other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.

This was SVN commit r5942.
2008-05-11 18:48:32 +00:00

166 lines
3.3 KiB
C++

/**
* =========================================================================
* File : Frustum.cpp
* Project : 0 A.D.
* Description : CFrustum is a collection of planes which define
* a viewing space.
* =========================================================================
*/
/*
Usually associated with the camera, there are 6 planes which define the
view pyramid. But we allow more planes per frustum which may be used for
portal rendering, where a portal may have 3 or more edges.
*/
#include "precompiled.h"
#include "Frustum.h"
#include "maths/Bound.h"
#include "maths/MathUtil.h"
CFrustum::CFrustum ()
{
m_NumPlanes = 0;
}
CFrustum::~CFrustum ()
{
}
void CFrustum::SetNumPlanes (size_t num)
{
m_NumPlanes = num;
//clip it
if (m_NumPlanes >= MAX_NUM_FRUSTUM_PLANES)
m_NumPlanes = MAX_NUM_FRUSTUM_PLANES-1;
}
bool CFrustum::IsPointVisible (const CVector3D &point) const
{
PLANESIDE Side;
for (size_t i=0; i<m_NumPlanes; i++)
{
Side = m_aPlanes[i].ClassifyPoint (point);
if (Side == PS_BACK)
return false;
}
return true;
}
bool CFrustum::DoesSegmentIntersect(const CVector3D& startRef, const CVector3D &endRef)
{
CVector3D start = startRef;
CVector3D end = endRef;
if(IsPointVisible(start) || IsPointVisible(end))
return true;
CVector3D intersect;
for ( size_t i = 0; i<m_NumPlanes; ++i )
{
if ( m_aPlanes[i].FindLineSegIntersection(start, end, &intersect) )
{
if ( IsPointVisible( intersect ) )
return true;
}
}
return false;
}
bool CFrustum::IsSphereVisible (const CVector3D &center, float radius) const
{
for (size_t i=0; i<m_NumPlanes; i++)
{
float Dist = m_aPlanes[i].DistanceToPlane (center);
//is it behind the plane
if (Dist < 0)
{
//if non of it falls in front its outside the
//frustum
if (-Dist > radius)
return false;
}
}
return true;
}
bool CFrustum::IsBoxVisible (const CVector3D &position,const CBound &bounds) const
{
//basically for every plane we calculate the furthest point
//in the box to that plane. If that point is beyond the plane
//then the box is not visible
CVector3D FarPoint;
PLANESIDE Side;
CVector3D Min = position+bounds[0];
CVector3D Max = position+bounds[1];
for (size_t i=0; i<m_NumPlanes; i++)
{
if (m_aPlanes[i].m_Norm.X > 0.0f)
{
if (m_aPlanes[i].m_Norm.Y > 0.0f)
{
if (m_aPlanes[i].m_Norm.Z > 0.0f)
{
FarPoint.X = Max.X; FarPoint.Y = Max.Y; FarPoint.Z = Max.Z;
}
else
{
FarPoint.X = Max.X; FarPoint.Y = Max.Y; FarPoint.Z = Min.Z;
}
}
else
{
if (m_aPlanes[i].m_Norm.Z > 0.0f)
{
FarPoint.X = Max.X; FarPoint.Y = Min.Y; FarPoint.Z = Max.Z;
}
else
{
FarPoint.X = Max.X; FarPoint.Y = Min.Y; FarPoint.Z = Min.Z;
}
}
}
else
{
if (m_aPlanes[i].m_Norm.Y > 0.0f)
{
if (m_aPlanes[i].m_Norm.Z > 0.0f)
{
FarPoint.X = Min.X; FarPoint.Y = Max.Y; FarPoint.Z = Max.Z;
}
else
{
FarPoint.X = Min.X; FarPoint.Y = Max.Y; FarPoint.Z = Min.Z;
}
}
else
{
if (m_aPlanes[i].m_Norm.Z > 0.0f)
{
FarPoint.X = Min.X; FarPoint.Y = Min.Y; FarPoint.Z = Max.Z;
}
else
{
FarPoint.X = Min.X; FarPoint.Y = Min.Y; FarPoint.Z = Min.Z;
}
}
}
Side = m_aPlanes[i].ClassifyPoint (FarPoint);
if (Side == PS_BACK)
return false;
}
return true;
}