2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* CFrustum is a collection of planes which define a viewing space.
|
2007-05-07 18:33:24 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
2004-05-29 22:56:24 +02:00
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-05-29 22:56:24 +02:00
|
|
|
#include "Frustum.h"
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "maths/Bound.h"
|
2006-08-25 21:31:01 +02:00
|
|
|
#include "maths/MathUtil.h"
|
2004-05-29 22:56:24 +02:00
|
|
|
|
|
|
|
CFrustum::CFrustum ()
|
|
|
|
{
|
|
|
|
m_NumPlanes = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CFrustum::~CFrustum ()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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 20:48:32 +02:00
|
|
|
void CFrustum::SetNumPlanes (size_t num)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
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 20:48:32 +02:00
|
|
|
for (size_t i=0; i<m_NumPlanes; i++)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
|
|
|
Side = m_aPlanes[i].ClassifyPoint (point);
|
|
|
|
|
|
|
|
if (Side == PS_BACK)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2006-08-25 21:31:01 +02:00
|
|
|
bool CFrustum::DoesSegmentIntersect(const CVector3D& startRef, const CVector3D &endRef)
|
2006-08-25 08:04:33 +02:00
|
|
|
{
|
2006-08-25 21:31:01 +02:00
|
|
|
CVector3D start = startRef;
|
|
|
|
CVector3D end = endRef;
|
|
|
|
|
|
|
|
if(IsPointVisible(start) || IsPointVisible(end))
|
|
|
|
return true;
|
|
|
|
|
2006-08-25 08:04:33 +02:00
|
|
|
CVector3D intersect;
|
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 20:48:32 +02:00
|
|
|
for ( size_t i = 0; i<m_NumPlanes; ++i )
|
2006-08-25 08:04:33 +02:00
|
|
|
{
|
|
|
|
if ( m_aPlanes[i].FindLineSegIntersection(start, end, &intersect) )
|
2006-08-25 21:31:01 +02:00
|
|
|
{
|
|
|
|
if ( IsPointVisible( intersect ) )
|
|
|
|
return true;
|
|
|
|
}
|
2006-08-25 08:04:33 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2004-05-29 22:56:24 +02:00
|
|
|
bool CFrustum::IsSphereVisible (const CVector3D ¢er, float radius) const
|
|
|
|
{
|
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 20:48:32 +02:00
|
|
|
for (size_t i=0; i<m_NumPlanes; i++)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
{
|
2005-03-18 23:30:23 +01:00
|
|
|
//basically for every plane we calculate the furthest point
|
2004-05-29 22:56:24 +02:00
|
|
|
//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];
|
|
|
|
|
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 20:48:32 +02:00
|
|
|
for (size_t i=0; i<m_NumPlanes; i++)
|
2004-05-29 22:56:24 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
|