doom3-bfg/neo/d3xp/Pvs.cpp

1651 lines
35 KiB
C++
Raw Normal View History

2012-11-26 18:58:24 +00:00
/*
===========================================================================
Doom 3 BFG Edition GPL Source Code
Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company.
2012-11-26 18:58:24 +00:00
This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").
2012-11-26 18:58:24 +00:00
Doom 3 BFG Edition Source Code 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 3 of the License, or
(at your option) any later version.
Doom 3 BFG Edition Source Code 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 Doom 3 BFG Edition Source Code. If not, see <http://www.gnu.org/licenses/>.
In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code. If not, please request a copy in writing from id Software at the address below.
If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
===========================================================================
*/
#include "precompiled.h"
2012-11-26 18:58:24 +00:00
#pragma hdrstop
#include "Game_local.h"
#define MAX_BOUNDS_AREAS 16
typedef struct pvsPassage_s
{
byte* canSee; // bit set for all portals that can be seen through this passage
2012-11-26 18:58:24 +00:00
} pvsPassage_t;
typedef struct pvsPortal_s
{
2012-11-26 18:58:24 +00:00
int areaNum; // area this portal leads to
idWinding* w; // winding goes counter clockwise seen from the area this portal is part of
2012-11-26 18:58:24 +00:00
idBounds bounds; // winding bounds
idPlane plane; // winding plane, normal points towards the area this portal leads to
pvsPassage_t* passages; // passages to portals in the area this portal leads to
2012-11-26 18:58:24 +00:00
bool done; // true if pvs is calculated for this portal
byte* vis; // PVS for this portal
byte* mightSee; // used during construction
2012-11-26 18:58:24 +00:00
} pvsPortal_t;
typedef struct pvsArea_s
{
2012-11-26 18:58:24 +00:00
int numPortals; // number of portals in this area
idBounds bounds; // bounds of the whole area
pvsPortal_t** portals; // array with pointers to the portals of this area
2012-11-26 18:58:24 +00:00
} pvsArea_t;
typedef struct pvsStack_s
{
struct pvsStack_s* next; // next stack entry
byte* mightSee; // bit set for all portals that might be visible through this passage/portal stack
2012-11-26 18:58:24 +00:00
} pvsStack_t;
/*
================
idPVS::idPVS
================
*/
idPVS::idPVS()
{
2012-11-26 18:58:24 +00:00
int i;
2012-11-26 18:58:24 +00:00
numAreas = 0;
numPortals = 0;
2012-11-26 18:58:24 +00:00
connectedAreas = NULL;
areaQueue = NULL;
areaPVS = NULL;
for( i = 0; i < MAX_CURRENT_PVS; i++ )
{
2012-11-26 18:58:24 +00:00
currentPVS[i].handle.i = -1;
currentPVS[i].handle.h = 0;
currentPVS[i].pvs = NULL;
}
2012-11-26 18:58:24 +00:00
pvsAreas = NULL;
pvsPortals = NULL;
}
/*
================
idPVS::~idPVS
================
*/
idPVS::~idPVS()
{
2012-11-26 18:58:24 +00:00
Shutdown();
}
/*
================
idPVS::GetPortalCount
================
*/
int idPVS::GetPortalCount() const
{
2012-11-26 18:58:24 +00:00
int i, na, np;
2012-11-26 18:58:24 +00:00
na = gameRenderWorld->NumAreas();
np = 0;
for( i = 0; i < na; i++ )
{
2012-11-26 18:58:24 +00:00
np += gameRenderWorld->NumPortalsInArea( i );
}
return np;
}
/*
================
idPVS::CreatePVSData
================
*/
void idPVS::CreatePVSData()
{
2012-11-26 18:58:24 +00:00
int i, j, n, cp;
exitPortal_t portal;
pvsArea_t* area;
pvsPortal_t* p, **portalPtrs;
if( !numPortals )
{
2012-11-26 18:58:24 +00:00
return;
}
pvsPortals = new( TAG_PVS ) pvsPortal_t[numPortals];
pvsAreas = new( TAG_PVS ) pvsArea_t[numAreas];
2012-11-26 18:58:24 +00:00
memset( pvsAreas, 0, numAreas * sizeof( *pvsAreas ) );
2012-11-26 18:58:24 +00:00
cp = 0;
portalPtrs = new( TAG_PVS ) pvsPortal_t*[numPortals];
for( i = 0; i < numAreas; i++ )
{
2012-11-26 18:58:24 +00:00
area = &pvsAreas[i];
area->bounds.Clear();
area->portals = portalPtrs + cp;
2012-11-26 18:58:24 +00:00
n = gameRenderWorld->NumPortalsInArea( i );
for( j = 0; j < n; j++ )
{
2012-11-26 18:58:24 +00:00
portal = gameRenderWorld->GetPortal( i, j );
2012-11-26 18:58:24 +00:00
p = &pvsPortals[cp++];
// the winding goes counter clockwise seen from this area
p->w = portal.w->Copy();
p->areaNum = portal.areas[1]; // area[1] is always the area the portal leads to
p->vis = new( TAG_PVS ) byte[portalVisBytes];
2012-11-26 18:58:24 +00:00
memset( p->vis, 0, portalVisBytes );
p->mightSee = new( TAG_PVS ) byte[portalVisBytes];
2012-11-26 18:58:24 +00:00
memset( p->mightSee, 0, portalVisBytes );
p->w->GetBounds( p->bounds );
p->w->GetPlane( p->plane );
// plane normal points to outside the area
p->plane = -p->plane;
// no PVS calculated for this portal yet
p->done = false;
2012-11-26 18:58:24 +00:00
area->portals[area->numPortals] = p;
area->numPortals++;
2012-11-26 18:58:24 +00:00
area->bounds += p->bounds;
}
}
}
/*
================
idPVS::DestroyPVSData
================
*/
void idPVS::DestroyPVSData()
{
2012-11-26 18:58:24 +00:00
int i;
if( !pvsAreas )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
// delete portal pointer array
delete[] pvsAreas[0].portals;
2012-11-26 18:58:24 +00:00
// delete all areas
delete[] pvsAreas;
pvsAreas = NULL;
2012-11-26 18:58:24 +00:00
// delete portal data
for( i = 0; i < numPortals; i++ )
{
2012-11-26 18:58:24 +00:00
delete[] pvsPortals[i].vis;
delete[] pvsPortals[i].mightSee;
delete pvsPortals[i].w;
}
2012-11-26 18:58:24 +00:00
// delete portals
delete[] pvsPortals;
pvsPortals = NULL;
}
/*
================
idPVS::FloodFrontPortalPVS_r
================
*/
void idPVS::FloodFrontPortalPVS_r( pvsPortal_t* portal, int areaNum ) const
{
2012-11-26 18:58:24 +00:00
int i, n;
pvsArea_t* area;
pvsPortal_t* p;
2012-11-26 18:58:24 +00:00
area = &pvsAreas[ areaNum ];
for( i = 0; i < area->numPortals; i++ )
{
2012-11-26 18:58:24 +00:00
p = area->portals[i];
n = p - pvsPortals;
// don't flood through if this portal is not at the front
if( !( portal->mightSee[ n >> 3 ] & ( 1 << ( n & 7 ) ) ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
// don't flood through if already visited this portal
if( portal->vis[ n >> 3 ] & ( 1 << ( n & 7 ) ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
// this portal might be visible
portal->vis[ n >> 3 ] |= ( 1 << ( n & 7 ) );
2012-11-26 18:58:24 +00:00
// flood through the portal
FloodFrontPortalPVS_r( portal, p->areaNum );
}
}
/*
================
idPVS::FrontPortalPVS
================
*/
void idPVS::FrontPortalPVS() const
{
2012-11-26 18:58:24 +00:00
int i, j, k, n, p, side1, side2, areaSide;
pvsPortal_t* p1, *p2;
pvsArea_t* area;
for( i = 0; i < numPortals; i++ )
{
2012-11-26 18:58:24 +00:00
p1 = &pvsPortals[i];
for( j = 0; j < numAreas; j++ )
{
2012-11-26 18:58:24 +00:00
area = &pvsAreas[j];
2012-11-26 18:58:24 +00:00
areaSide = side1 = area->bounds.PlaneSide( p1->plane );
2012-11-26 18:58:24 +00:00
// if the whole area is at the back side of the portal
if( areaSide == PLANESIDE_BACK )
{
2012-11-26 18:58:24 +00:00
continue;
}
for( p = 0; p < area->numPortals; p++ )
{
2012-11-26 18:58:24 +00:00
p2 = area->portals[p];
2012-11-26 18:58:24 +00:00
// if we the whole area is not at the front we need to check
if( areaSide != PLANESIDE_FRONT )
{
2012-11-26 18:58:24 +00:00
// if the second portal is completely at the back side of the first portal
side1 = p2->bounds.PlaneSide( p1->plane );
if( side1 == PLANESIDE_BACK )
{
2012-11-26 18:58:24 +00:00
continue;
}
}
2012-11-26 18:58:24 +00:00
// if the first portal is completely at the front of the second portal
side2 = p1->bounds.PlaneSide( p2->plane );
if( side2 == PLANESIDE_FRONT )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
// if the second portal is not completely at the front of the first portal
if( side1 != PLANESIDE_FRONT )
{
2012-11-26 18:58:24 +00:00
// more accurate check
for( k = 0; k < p2->w->GetNumPoints(); k++ )
{
2012-11-26 18:58:24 +00:00
// if more than an epsilon at the front side
if( p1->plane.Side( ( *p2->w )[k].ToVec3(), ON_EPSILON ) == PLANESIDE_FRONT )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( k >= p2->w->GetNumPoints() )
{
2012-11-26 18:58:24 +00:00
continue; // second portal is at the back of the first portal
}
}
2012-11-26 18:58:24 +00:00
// if the first portal is not completely at the back side of the second portal
if( side2 != PLANESIDE_BACK )
{
2012-11-26 18:58:24 +00:00
// more accurate check
for( k = 0; k < p1->w->GetNumPoints(); k++ )
{
2012-11-26 18:58:24 +00:00
// if more than an epsilon at the back side
if( p2->plane.Side( ( *p1->w )[k].ToVec3(), ON_EPSILON ) == PLANESIDE_BACK )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( k >= p1->w->GetNumPoints() )
{
2012-11-26 18:58:24 +00:00
continue; // first portal is at the front of the second portal
}
}
2012-11-26 18:58:24 +00:00
// the portal might be visible at the front
n = p2 - pvsPortals;
p1->mightSee[ n >> 3 ] |= 1 << ( n & 7 );
2012-11-26 18:58:24 +00:00
}
}
}
2012-11-26 18:58:24 +00:00
// flood the front portal pvs for all portals
for( i = 0; i < numPortals; i++ )
{
2012-11-26 18:58:24 +00:00
p1 = &pvsPortals[i];
FloodFrontPortalPVS_r( p1, p1->areaNum );
}
}
/*
===============
idPVS::FloodPassagePVS_r
===============
*/
pvsStack_t* idPVS::FloodPassagePVS_r( pvsPortal_t* source, const pvsPortal_t* portal, pvsStack_t* prevStack ) const
{
2012-11-26 18:58:24 +00:00
int i, j, n, m;
pvsPortal_t* p;
pvsArea_t* area;
pvsStack_t* stack;
pvsPassage_t* passage;
2012-12-12 11:11:55 +00:00
// RB: 64 bit fixes, changed long to int
int* sourceVis, *passageVis, *portalVis, *mightSee, *prevMightSee, more;
// RB end
2012-11-26 18:58:24 +00:00
area = &pvsAreas[portal->areaNum];
2012-11-26 18:58:24 +00:00
stack = prevStack->next;
// if no next stack entry allocated
if( !stack )
{
stack = reinterpret_cast<pvsStack_t*>( new byte[sizeof( pvsStack_t ) + portalVisBytes] );
stack->mightSee = ( reinterpret_cast<byte*>( stack ) ) + sizeof( pvsStack_t );
2012-11-26 18:58:24 +00:00
stack->next = NULL;
prevStack->next = stack;
}
2012-11-26 18:58:24 +00:00
// check all portals for flooding into other areas
for( i = 0; i < area->numPortals; i++ )
{
2012-11-26 18:58:24 +00:00
passage = &portal->passages[i];
2012-11-26 18:58:24 +00:00
// if this passage is completely empty
if( !passage->canSee )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
p = area->portals[i];
n = p - pvsPortals;
2012-11-26 18:58:24 +00:00
// if this portal cannot be seen through our current portal/passage stack
if( !( prevStack->mightSee[n >> 3] & ( 1 << ( n & 7 ) ) ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
// mark the portal as visible
source->vis[n >> 3] |= ( 1 << ( n & 7 ) );
2012-11-26 18:58:24 +00:00
// get pointers to vis data
2012-12-12 11:11:55 +00:00
// RB: 64 bit fixes, changed long to int
prevMightSee = reinterpret_cast<int*>( prevStack->mightSee );
passageVis = reinterpret_cast<int*>( passage->canSee );
sourceVis = reinterpret_cast<int*>( source->vis );
mightSee = reinterpret_cast<int*>( stack->mightSee );
// RB end
2012-11-26 18:58:24 +00:00
more = 0;
// use the portal PVS if it has been calculated
if( p->done )
{
2012-12-12 11:11:55 +00:00
// RB: 64 bit fixes, changed long to int
portalVis = reinterpret_cast<int*>( p->vis );
// RB end
for( j = 0; j < portalVisLongs; j++ )
{
2012-11-26 18:58:24 +00:00
// get new PVS which is decreased by going through this passage
m = *prevMightSee++ & *passageVis++ & *portalVis++;
// check if anything might be visible through this passage that wasn't yet visible
more |= ( m & ~( *sourceVis++ ) );
2012-11-26 18:58:24 +00:00
// store new PVS
*mightSee++ = m;
}
}
else
{
2012-11-26 18:58:24 +00:00
// the p->mightSee is implicitely stored in the passageVis
for( j = 0; j < portalVisLongs; j++ )
{
2012-11-26 18:58:24 +00:00
// get new PVS which is decreased by going through this passage
m = *prevMightSee++ & *passageVis++;
// check if anything might be visible through this passage that wasn't yet visible
more |= ( m & ~( *sourceVis++ ) );
2012-11-26 18:58:24 +00:00
// store new PVS
*mightSee++ = m;
}
}
2012-11-26 18:58:24 +00:00
// if nothing more can be seen
if( !more )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
// go through the portal
stack->next = FloodPassagePVS_r( source, p, stack );
}
2012-11-26 18:58:24 +00:00
return stack;
}
/*
===============
idPVS::PassagePVS
===============
*/
void idPVS::PassagePVS() const
{
2012-11-26 18:58:24 +00:00
int i;
pvsPortal_t* source;
pvsStack_t* stack, *s;
2012-11-26 18:58:24 +00:00
// create the passages
CreatePassages();
2012-11-26 18:58:24 +00:00
// allocate first stack entry
stack = reinterpret_cast<pvsStack_t*>( new byte[sizeof( pvsStack_t ) + portalVisBytes] );
stack->mightSee = ( reinterpret_cast<byte*>( stack ) ) + sizeof( pvsStack_t );
2012-11-26 18:58:24 +00:00
stack->next = NULL;
2012-11-26 18:58:24 +00:00
// calculate portal PVS by flooding through the passages
for( i = 0; i < numPortals; i++ )
{
2012-11-26 18:58:24 +00:00
source = &pvsPortals[i];
memset( source->vis, 0, portalVisBytes );
memcpy( stack->mightSee, source->mightSee, portalVisBytes );
FloodPassagePVS_r( source, source, stack );
source->done = true;
}
2012-11-26 18:58:24 +00:00
// free the allocated stack
for( s = stack; s; s = stack )
{
2012-11-26 18:58:24 +00:00
stack = stack->next;
delete[] s;
}
2012-11-26 18:58:24 +00:00
// destroy the passages
DestroyPassages();
}
/*
===============
idPVS::AddPassageBoundaries
===============
*/
void idPVS::AddPassageBoundaries( const idWinding& source, const idWinding& pass, bool flipClip, idPlane* bounds, int& numBounds, int maxBounds ) const
{
2012-11-26 18:58:24 +00:00
int i, j, k, l;
idVec3 v1, v2, normal;
float d, dist;
bool flipTest, front;
idPlane plane;
// check all combinations
for( i = 0; i < source.GetNumPoints(); i++ )
{
l = ( i + 1 ) % source.GetNumPoints();
2012-11-26 18:58:24 +00:00
v1 = source[l].ToVec3() - source[i].ToVec3();
2012-11-26 18:58:24 +00:00
// find a vertex of pass that makes a plane that puts all of the
// vertices of pass on the front side and all of the vertices of
// source on the back side
for( j = 0; j < pass.GetNumPoints(); j++ )
{
2012-11-26 18:58:24 +00:00
v2 = pass[j].ToVec3() - source[i].ToVec3();
2012-11-26 18:58:24 +00:00
normal = v1.Cross( v2 );
if( normal.Normalize() < 0.01f )
{
2012-11-26 18:58:24 +00:00
continue;
}
dist = normal * pass[j].ToVec3();
2012-11-26 18:58:24 +00:00
//
// find out which side of the generated seperating plane has the
// source portal
//
flipTest = false;
for( k = 0; k < source.GetNumPoints(); k++ )
{
if( k == i || k == l )
{
2012-11-26 18:58:24 +00:00
continue;
}
d = source[k].ToVec3() * normal - dist;
if( d < -ON_EPSILON )
{
2012-11-26 18:58:24 +00:00
// source is on the negative side, so we want all
// pass and target on the positive side
flipTest = false;
break;
}
else if( d > ON_EPSILON )
{
2012-11-26 18:58:24 +00:00
// source is on the positive side, so we want all
// pass and target on the negative side
flipTest = true;
break;
}
}
if( k == source.GetNumPoints() )
{
2012-11-26 18:58:24 +00:00
continue; // planar with source portal
}
2012-11-26 18:58:24 +00:00
// flip the normal if the source portal is backwards
if( flipTest )
{
2012-11-26 18:58:24 +00:00
normal = -normal;
dist = -dist;
}
2012-11-26 18:58:24 +00:00
// if all of the pass portal points are now on the positive side,
// this is the seperating plane
front = false;
for( k = 0; k < pass.GetNumPoints(); k++ )
{
if( k == j )
{
2012-11-26 18:58:24 +00:00
continue;
}
d = pass[k].ToVec3() * normal - dist;
if( d < -ON_EPSILON )
{
2012-11-26 18:58:24 +00:00
break;
}
else if( d > ON_EPSILON )
{
2012-11-26 18:58:24 +00:00
front = true;
}
}
if( k < pass.GetNumPoints() )
{
2012-11-26 18:58:24 +00:00
continue; // points on negative side, not a seperating plane
}
if( !front )
{
2012-11-26 18:58:24 +00:00
continue; // planar with seperating plane
}
2012-11-26 18:58:24 +00:00
// flip the normal if we want the back side
if( flipClip )
{
2012-11-26 18:58:24 +00:00
plane.SetNormal( -normal );
plane.SetDist( -dist );
}
else
{
2012-11-26 18:58:24 +00:00
plane.SetNormal( normal );
plane.SetDist( dist );
}
2012-11-26 18:58:24 +00:00
// check if the plane is already a passage boundary
for( k = 0; k < numBounds; k++ )
{
if( plane.Compare( bounds[k], 0.001f, 0.01f ) )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( k < numBounds )
{
2012-11-26 18:58:24 +00:00
break;
}
if( numBounds >= maxBounds )
{
2012-11-26 18:58:24 +00:00
gameLocal.Warning( "max passage boundaries." );
break;
}
bounds[numBounds] = plane;
numBounds++;
break;
}
}
}
/*
================
idPVS::CreatePassages
================
*/
#define MAX_PASSAGE_BOUNDS 128
void idPVS::CreatePassages() const
{
2012-11-26 18:58:24 +00:00
int i, j, l, n, numBounds, front, passageMemory, byteNum, bitNum;
int sides[MAX_PASSAGE_BOUNDS];
idPlane passageBounds[MAX_PASSAGE_BOUNDS];
pvsPortal_t* source, *target, *p;
pvsArea_t* area;
pvsPassage_t* passage;
2012-11-26 18:58:24 +00:00
idFixedWinding winding;
byte canSee, mightSee, bit;
2012-11-26 18:58:24 +00:00
passageMemory = 0;
for( i = 0; i < numPortals; i++ )
{
2012-11-26 18:58:24 +00:00
source = &pvsPortals[i];
area = &pvsAreas[source->areaNum];
source->passages = new( TAG_PVS ) pvsPassage_t[area->numPortals];
for( j = 0; j < area->numPortals; j++ )
{
2012-11-26 18:58:24 +00:00
target = area->portals[j];
n = target - pvsPortals;
2012-11-26 18:58:24 +00:00
passage = &source->passages[j];
2012-11-26 18:58:24 +00:00
// if the source portal cannot see this portal
if( !( source->mightSee[ n >> 3 ] & ( 1 << ( n & 7 ) ) ) )
{
2012-11-26 18:58:24 +00:00
// not all portals in the area have to be visible because areas are not necesarily convex
// also no passage has to be created for the portal which is the opposite of the source
passage->canSee = NULL;
continue;
}
passage->canSee = new( TAG_PVS ) byte[portalVisBytes];
2012-11-26 18:58:24 +00:00
passageMemory += portalVisBytes;
2012-11-26 18:58:24 +00:00
// boundary plane normals point inwards
numBounds = 0;
AddPassageBoundaries( *( source->w ), *( target->w ), false, passageBounds, numBounds, MAX_PASSAGE_BOUNDS );
AddPassageBoundaries( *( target->w ), *( source->w ), true, passageBounds, numBounds, MAX_PASSAGE_BOUNDS );
2012-11-26 18:58:24 +00:00
// get all portals visible through this passage
for( byteNum = 0; byteNum < portalVisBytes; byteNum++ )
{
2012-11-26 18:58:24 +00:00
canSee = 0;
mightSee = source->mightSee[byteNum] & target->mightSee[byteNum];
2012-11-26 18:58:24 +00:00
// go through eight portals at a time to speed things up
for( bitNum = 0; bitNum < 8; bitNum++ )
{
2012-11-26 18:58:24 +00:00
bit = 1 << bitNum;
if( !( mightSee & bit ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
p = &pvsPortals[( byteNum << 3 ) + bitNum];
if( p->areaNum == source->areaNum )
{
2012-11-26 18:58:24 +00:00
continue;
}
for( front = 0, l = 0; l < numBounds; l++ )
{
2012-11-26 18:58:24 +00:00
sides[l] = p->bounds.PlaneSide( passageBounds[l] );
// if completely at the back of the passage bounding plane
if( sides[l] == PLANESIDE_BACK )
{
2012-11-26 18:58:24 +00:00
break;
}
// if completely at the front
if( sides[l] == PLANESIDE_FRONT )
{
2012-11-26 18:58:24 +00:00
front++;
}
}
// if completely outside the passage
if( l < numBounds )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
// if not at the front of all bounding planes and thus not completely inside the passage
if( front != numBounds )
{
2012-11-26 18:58:24 +00:00
winding = *p->w;
for( l = 0; l < numBounds; l++ )
{
2012-11-26 18:58:24 +00:00
// only clip if the winding possibly crosses this plane
if( sides[l] != PLANESIDE_CROSS )
{
2012-11-26 18:58:24 +00:00
continue;
}
// clip away the part at the back of the bounding plane
winding.ClipInPlace( passageBounds[l] );
// if completely clipped away
if( !winding.GetNumPoints() )
{
2012-11-26 18:58:24 +00:00
break;
}
}
// if completely outside the passage
if( l < numBounds )
{
2012-11-26 18:58:24 +00:00
continue;
}
}
2012-11-26 18:58:24 +00:00
canSee |= bit;
}
2012-11-26 18:58:24 +00:00
// store results of all eight portals
passage->canSee[byteNum] = canSee;
}
2012-11-26 18:58:24 +00:00
// can always see the target portal
passage->canSee[n >> 3] |= ( 1 << ( n & 7 ) );
2012-11-26 18:58:24 +00:00
}
}
if( passageMemory < 1024 )
{
2012-11-26 18:58:24 +00:00
gameLocal.Printf( "%5d bytes passage memory used to build PVS\n", passageMemory );
}
else
{
gameLocal.Printf( "%5d KB passage memory used to build PVS\n", passageMemory >> 10 );
2012-11-26 18:58:24 +00:00
}
}
/*
================
idPVS::DestroyPassages
================
*/
void idPVS::DestroyPassages() const
{
2012-11-26 18:58:24 +00:00
int i, j;
pvsPortal_t* p;
pvsArea_t* area;
for( i = 0; i < numPortals; i++ )
{
2012-11-26 18:58:24 +00:00
p = &pvsPortals[i];
area = &pvsAreas[p->areaNum];
for( j = 0; j < area->numPortals; j++ )
{
if( p->passages[j].canSee )
{
2012-11-26 18:58:24 +00:00
delete[] p->passages[j].canSee;
}
}
delete[] p->passages;
}
}
/*
================
idPVS::CopyPortalPVSToMightSee
================
*/
void idPVS::CopyPortalPVSToMightSee() const
{
2012-11-26 18:58:24 +00:00
int i;
pvsPortal_t* p;
for( i = 0; i < numPortals; i++ )
{
2012-11-26 18:58:24 +00:00
p = &pvsPortals[i];
memcpy( p->mightSee, p->vis, portalVisBytes );
}
}
/*
================
idPVS::AreaPVSFromPortalPVS
================
*/
int idPVS::AreaPVSFromPortalPVS() const
{
2012-11-26 18:58:24 +00:00
int i, j, k, areaNum, totalVisibleAreas;
2012-12-12 11:11:55 +00:00
// RB: 64 bit fixes, changed long to int
int* p1, *p2;
// RB end
byte* pvs, *portalPVS;
pvsArea_t* area;
2012-11-26 18:58:24 +00:00
totalVisibleAreas = 0;
if( !numPortals )
{
2012-11-26 18:58:24 +00:00
return totalVisibleAreas;
}
2012-11-26 18:58:24 +00:00
memset( areaPVS, 0, numAreas * areaVisBytes );
for( i = 0; i < numAreas; i++ )
{
2012-11-26 18:58:24 +00:00
area = &pvsAreas[i];
pvs = areaPVS + i * areaVisBytes;
2012-11-26 18:58:24 +00:00
// the area is visible to itself
pvs[ i >> 3 ] |= 1 << ( i & 7 );
if( !area->numPortals )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
// store the PVS of all portals in this area at the first portal
for( j = 1; j < area->numPortals; j++ )
{
2012-12-12 11:11:55 +00:00
// RB: 64 bit fixes, changed long to int
p1 = reinterpret_cast<int*>( area->portals[0]->vis );
p2 = reinterpret_cast<int*>( area->portals[j]->vis );
// RB end
for( k = 0; k < portalVisLongs; k++ )
{
2012-11-26 18:58:24 +00:00
*p1++ |= *p2++;
}
}
2012-11-26 18:58:24 +00:00
// the portals of this area are always visible
for( j = 0; j < area->numPortals; j++ )
{
2012-11-26 18:58:24 +00:00
k = area->portals[j] - pvsPortals;
area->portals[0]->vis[ k >> 3 ] |= 1 << ( k & 7 );
2012-11-26 18:58:24 +00:00
}
2012-11-26 18:58:24 +00:00
// set all areas to visible that can be seen from the portals of this area
portalPVS = area->portals[0]->vis;
for( j = 0; j < numPortals; j++ )
{
2012-11-26 18:58:24 +00:00
// if this portal is visible
if( portalPVS[j >> 3] & ( 1 << ( j & 7 ) ) )
{
2012-11-26 18:58:24 +00:00
areaNum = pvsPortals[j].areaNum;
pvs[ areaNum >> 3 ] |= 1 << ( areaNum & 7 );
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
// count the number of visible areas
for( j = 0; j < numAreas; j++ )
{
if( pvs[j >> 3] & ( 1 << ( j & 7 ) ) )
{
2012-11-26 18:58:24 +00:00
totalVisibleAreas++;
}
}
}
return totalVisibleAreas;
}
/*
================
idPVS::Init
================
*/
void idPVS::Init()
{
2012-11-26 18:58:24 +00:00
int totalVisibleAreas;
2012-11-26 18:58:24 +00:00
Shutdown();
2012-11-26 18:58:24 +00:00
numAreas = gameRenderWorld->NumAreas();
if( numAreas <= 0 )
{
2012-11-26 18:58:24 +00:00
return;
}
connectedAreas = new( TAG_PVS ) bool[numAreas];
areaQueue = new( TAG_PVS ) int[numAreas];
areaVisBytes = ( ( ( numAreas + 31 )&~31 ) >> 3 );
2012-12-12 11:11:55 +00:00
// RB: 64 bit fixes, changed long to int
areaVisLongs = areaVisBytes / sizeof( int );
// RB end
areaPVS = new( TAG_PVS ) byte[numAreas * areaVisBytes];
2012-11-26 18:58:24 +00:00
memset( areaPVS, 0xFF, numAreas * areaVisBytes );
2012-11-26 18:58:24 +00:00
numPortals = GetPortalCount();
portalVisBytes = ( ( ( numPortals + 31 )&~31 ) >> 3 );
2012-12-12 11:11:55 +00:00
// RB: 64 bit fixes, changed long to int
portalVisLongs = portalVisBytes / sizeof( int );
// RB end
for( int i = 0; i < MAX_CURRENT_PVS; i++ )
{
2012-11-26 18:58:24 +00:00
currentPVS[i].handle.i = -1;
currentPVS[i].handle.h = 0;
currentPVS[i].pvs = new( TAG_PVS ) byte[areaVisBytes];
2012-11-26 18:58:24 +00:00
memset( currentPVS[i].pvs, 0, areaVisBytes );
}
2012-11-26 18:58:24 +00:00
idTimer timer;
timer.Start();
2012-11-26 18:58:24 +00:00
CreatePVSData();
2012-11-26 18:58:24 +00:00
FrontPortalPVS();
2012-11-26 18:58:24 +00:00
CopyPortalPVSToMightSee();
2012-11-26 18:58:24 +00:00
PassagePVS();
2012-11-26 18:58:24 +00:00
totalVisibleAreas = AreaPVSFromPortalPVS();
2012-11-26 18:58:24 +00:00
DestroyPVSData();
2012-11-26 18:58:24 +00:00
timer.Stop();
2012-11-26 18:58:24 +00:00
gameLocal.Printf( "%5.0f msec to calculate PVS\n", timer.Milliseconds() );
gameLocal.Printf( "%5d areas\n", numAreas );
gameLocal.Printf( "%5d portals\n", numPortals );
gameLocal.Printf( "%5d areas visible on average\n", totalVisibleAreas / numAreas );
if( numAreas * areaVisBytes < 1024 )
{
2012-11-26 18:58:24 +00:00
gameLocal.Printf( "%5d bytes PVS data\n", numAreas * areaVisBytes );
}
else
{
gameLocal.Printf( "%5d KB PVS data\n", ( numAreas * areaVisBytes ) >> 10 );
2012-11-26 18:58:24 +00:00
}
}
/*
================
idPVS::Shutdown
================
*/
void idPVS::Shutdown()
{
if( connectedAreas )
{
2012-11-26 18:58:24 +00:00
delete connectedAreas;
connectedAreas = NULL;
}
if( areaQueue )
{
2012-11-26 18:58:24 +00:00
delete areaQueue;
areaQueue = NULL;
}
if( areaPVS )
{
2012-11-26 18:58:24 +00:00
delete areaPVS;
areaPVS = NULL;
}
for( int i = 0; i < MAX_CURRENT_PVS; i++ )
{
2012-11-26 18:58:24 +00:00
delete currentPVS[i].pvs;
currentPVS[i].pvs = NULL;
}
}
/*
================
idPVS::GetConnectedAreas
assumes the 'areas' array is initialized to false
================
*/
void idPVS::GetConnectedAreas( int srcArea, bool* areas ) const
{
2012-11-26 18:58:24 +00:00
int curArea, nextArea;
int queueStart, queueEnd;
int i, n;
exitPortal_t portal;
2012-11-26 18:58:24 +00:00
queueStart = -1;
queueEnd = 0;
areas[srcArea] = true;
for( curArea = srcArea; queueStart < queueEnd; curArea = areaQueue[++queueStart] )
{
2012-11-26 18:58:24 +00:00
n = gameRenderWorld->NumPortalsInArea( curArea );
for( i = 0; i < n; i++ )
{
2012-11-26 18:58:24 +00:00
portal = gameRenderWorld->GetPortal( curArea, i );
if( portal.blockingBits & PS_BLOCK_VIEW )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
// area[1] is always the area the portal leads to
nextArea = portal.areas[1];
2012-11-26 18:58:24 +00:00
// if already visited this area
if( areas[nextArea] )
{
2012-11-26 18:58:24 +00:00
continue;
}
2012-11-26 18:58:24 +00:00
// add area to queue
areaQueue[queueEnd++] = nextArea;
areas[nextArea] = true;
}
}
}
/*
================
idPVS::GetPVSArea
================
*/
int idPVS::GetPVSArea( const idVec3& point ) const
{
2012-11-26 18:58:24 +00:00
return gameRenderWorld->PointInArea( point );
}
/*
================
idPVS::GetPVSAreas
================
*/
int idPVS::GetPVSAreas( const idBounds& bounds, int* areas, int maxAreas ) const
{
2012-11-26 18:58:24 +00:00
return gameRenderWorld->BoundsInAreas( bounds, areas, maxAreas );
}
/*
================
idPVS::SetupCurrentPVS
================
*/
pvsHandle_t idPVS::SetupCurrentPVS( const idVec3& source, const pvsType_t type ) const
{
2012-11-26 18:58:24 +00:00
int sourceArea;
2012-11-26 18:58:24 +00:00
sourceArea = gameRenderWorld->PointInArea( source );
2012-11-26 18:58:24 +00:00
return SetupCurrentPVS( sourceArea, type );
}
/*
================
idPVS::SetupCurrentPVS
================
*/
pvsHandle_t idPVS::SetupCurrentPVS( const idBounds& source, const pvsType_t type ) const
{
2012-11-26 18:58:24 +00:00
int numSourceAreas, sourceAreas[MAX_BOUNDS_AREAS];
2012-11-26 18:58:24 +00:00
numSourceAreas = gameRenderWorld->BoundsInAreas( source, sourceAreas, MAX_BOUNDS_AREAS );
2012-11-26 18:58:24 +00:00
return SetupCurrentPVS( sourceAreas, numSourceAreas, type );
}
/*
================
idPVS::SetupCurrentPVS
================
*/
pvsHandle_t idPVS::SetupCurrentPVS( const int sourceArea, const pvsType_t type ) const
{
2012-11-26 18:58:24 +00:00
int i;
pvsHandle_t handle;
handle = AllocCurrentPVS( *reinterpret_cast<const unsigned int*>( &sourceArea ) );
if( sourceArea < 0 || sourceArea >= numAreas )
{
2012-11-26 18:58:24 +00:00
memset( currentPVS[handle.i].pvs, 0, areaVisBytes );
return handle;
}
if( type != PVS_CONNECTED_AREAS )
{
2012-11-26 18:58:24 +00:00
memcpy( currentPVS[handle.i].pvs, areaPVS + sourceArea * areaVisBytes, areaVisBytes );
}
else
{
2012-11-26 18:58:24 +00:00
memset( currentPVS[handle.i].pvs, -1, areaVisBytes );
}
if( type == PVS_ALL_PORTALS_OPEN )
{
2012-11-26 18:58:24 +00:00
return handle;
}
2012-11-26 18:58:24 +00:00
memset( connectedAreas, 0, numAreas * sizeof( *connectedAreas ) );
2012-11-26 18:58:24 +00:00
GetConnectedAreas( sourceArea, connectedAreas );
for( i = 0; i < numAreas; i++ )
{
if( !connectedAreas[i] )
{
currentPVS[handle.i].pvs[i >> 3] &= ~( 1 << ( i & 7 ) );
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
return handle;
}
/*
================
idPVS::SetupCurrentPVS
================
*/
pvsHandle_t idPVS::SetupCurrentPVS( const int* sourceAreas, const int numSourceAreas, const pvsType_t type ) const
{
2012-11-26 18:58:24 +00:00
int i, j;
unsigned int h;
2012-12-12 11:11:55 +00:00
// RB: 64 bit fixes, changed long to int
int* vis, *pvs;
// RB end
2012-11-26 18:58:24 +00:00
pvsHandle_t handle;
2012-11-26 18:58:24 +00:00
h = 0;
for( i = 0; i < numSourceAreas; i++ )
{
h ^= *reinterpret_cast<const unsigned int*>( &sourceAreas[i] );
2012-11-26 18:58:24 +00:00
}
handle = AllocCurrentPVS( h );
if( !numSourceAreas || sourceAreas[0] < 0 || sourceAreas[0] >= numAreas )
{
2012-11-26 18:58:24 +00:00
memset( currentPVS[handle.i].pvs, 0, areaVisBytes );
return handle;
}
if( type != PVS_CONNECTED_AREAS )
{
2012-11-26 18:58:24 +00:00
// merge PVS of all areas the source is in
memcpy( currentPVS[handle.i].pvs, areaPVS + sourceAreas[0] * areaVisBytes, areaVisBytes );
for( i = 1; i < numSourceAreas; i++ )
{
2012-11-26 18:58:24 +00:00
assert( sourceAreas[i] >= 0 && sourceAreas[i] < numAreas );
2012-12-12 11:11:55 +00:00
// RB: 64 bit fixes, changed long to int
vis = reinterpret_cast<int*>( areaPVS + sourceAreas[i] * areaVisBytes );
pvs = reinterpret_cast<int*>( currentPVS[handle.i].pvs );
// RB end
for( j = 0; j < areaVisLongs; j++ )
{
2012-11-26 18:58:24 +00:00
*pvs++ |= *vis++;
}
}
}
else
{
2012-11-26 18:58:24 +00:00
memset( currentPVS[handle.i].pvs, -1, areaVisBytes );
}
if( type == PVS_ALL_PORTALS_OPEN )
{
2012-11-26 18:58:24 +00:00
return handle;
}
2012-11-26 18:58:24 +00:00
memset( connectedAreas, 0, numAreas * sizeof( *connectedAreas ) );
2012-11-26 18:58:24 +00:00
// get all areas connected to any of the source areas
for( i = 0; i < numSourceAreas; i++ )
{
if( !connectedAreas[sourceAreas[i]] )
{
2012-11-26 18:58:24 +00:00
GetConnectedAreas( sourceAreas[i], connectedAreas );
}
}
2012-11-26 18:58:24 +00:00
// remove unconnected areas from the PVS
for( i = 0; i < numAreas; i++ )
{
if( !connectedAreas[i] )
{
currentPVS[handle.i].pvs[i >> 3] &= ~( 1 << ( i & 7 ) );
2012-11-26 18:58:24 +00:00
}
}
2012-11-26 18:58:24 +00:00
return handle;
}
/*
================
idPVS::MergeCurrentPVS
================
*/
pvsHandle_t idPVS::MergeCurrentPVS( pvsHandle_t pvs1, pvsHandle_t pvs2 ) const
{
2012-11-26 18:58:24 +00:00
int i;
2012-12-12 11:11:55 +00:00
// RB: 64 bit fixes, changed long to int
int* pvs1Ptr, *pvs2Ptr, *ptr;
// RB end
2012-11-26 18:58:24 +00:00
pvsHandle_t handle = { 0 };
if( pvs1.i < 0 || pvs1.i >= MAX_CURRENT_PVS || pvs1.h != currentPVS[pvs1.i].handle.h ||
pvs2.i < 0 || pvs2.i >= MAX_CURRENT_PVS || pvs2.h != currentPVS[pvs2.i].handle.h )
{
2012-11-26 18:58:24 +00:00
gameLocal.Error( "idPVS::MergeCurrentPVS: invalid handle" );
return handle;
}
2012-11-26 18:58:24 +00:00
handle = AllocCurrentPVS( pvs1.h ^ pvs2.h );
2012-12-12 11:11:55 +00:00
// RB: 64 bit fixes, changed long to int
ptr = reinterpret_cast<int*>( currentPVS[handle.i].pvs );
pvs1Ptr = reinterpret_cast<int*>( currentPVS[pvs1.i].pvs );
pvs2Ptr = reinterpret_cast<int*>( currentPVS[pvs2.i].pvs );
// RB end
for( i = 0; i < areaVisLongs; i++ )
{
2012-11-26 18:58:24 +00:00
*ptr++ = *pvs1Ptr++ | *pvs2Ptr++;
}
2012-11-26 18:58:24 +00:00
return handle;
}
/*
================
idPVS::AllocCurrentPVS
================
*/
pvsHandle_t idPVS::AllocCurrentPVS( unsigned int h ) const
{
2012-11-26 18:58:24 +00:00
int i;
pvsHandle_t handle;
for( i = 0; i < MAX_CURRENT_PVS; i++ )
{
if( currentPVS[i].handle.i == -1 )
{
2012-11-26 18:58:24 +00:00
currentPVS[i].handle.i = i;
currentPVS[i].handle.h = h;
return currentPVS[i].handle;
}
}
2012-11-26 18:58:24 +00:00
gameLocal.Error( "idPVS::AllocCurrentPVS: no free PVS left" );
2012-11-26 18:58:24 +00:00
handle.i = -1;
handle.h = 0;
return handle;
}
/*
================
idPVS::FreeCurrentPVS
================
*/
void idPVS::FreeCurrentPVS( pvsHandle_t handle ) const
{
if( handle.i < 0 || handle.i >= MAX_CURRENT_PVS || handle.h != currentPVS[handle.i].handle.h )
{
2012-11-26 18:58:24 +00:00
gameLocal.Error( "idPVS::FreeCurrentPVS: invalid handle" );
return;
}
currentPVS[handle.i].handle.i = -1;
}
/*
================
idPVS::InCurrentPVS
================
*/
bool idPVS::InCurrentPVS( const pvsHandle_t handle, const idVec3& target ) const
{
2012-11-26 18:58:24 +00:00
int targetArea;
if( handle.i < 0 || handle.i >= MAX_CURRENT_PVS ||
handle.h != currentPVS[handle.i].handle.h )
{
2012-11-26 18:58:24 +00:00
gameLocal.Warning( "idPVS::InCurrentPVS: invalid handle" );
return false;
}
2012-11-26 18:58:24 +00:00
targetArea = gameRenderWorld->PointInArea( target );
if( targetArea == -1 )
{
2012-11-26 18:58:24 +00:00
return false;
}
return ( ( currentPVS[handle.i].pvs[targetArea >> 3] & ( 1 << ( targetArea & 7 ) ) ) != 0 );
2012-11-26 18:58:24 +00:00
}
/*
================
idPVS::InCurrentPVS
================
*/
bool idPVS::InCurrentPVS( const pvsHandle_t handle, const idBounds& target ) const
{
2012-11-26 18:58:24 +00:00
int i, numTargetAreas, targetAreas[MAX_BOUNDS_AREAS];
if( handle.i < 0 || handle.i >= MAX_CURRENT_PVS ||
handle.h != currentPVS[handle.i].handle.h )
{
2012-11-26 18:58:24 +00:00
gameLocal.Warning( "idPVS::InCurrentPVS: invalid handle" );
return false;
}
2012-11-26 18:58:24 +00:00
numTargetAreas = gameRenderWorld->BoundsInAreas( target, targetAreas, MAX_BOUNDS_AREAS );
for( i = 0; i < numTargetAreas; i++ )
{
if( currentPVS[handle.i].pvs[targetAreas[i] >> 3] & ( 1 << ( targetAreas[i] & 7 ) ) )
{
2012-11-26 18:58:24 +00:00
return true;
}
}
return false;
}
/*
================
idPVS::InCurrentPVS
================
*/
bool idPVS::InCurrentPVS( const pvsHandle_t handle, const int targetArea ) const
{
2012-11-26 18:58:24 +00:00
if( handle.i < 0 || handle.i >= MAX_CURRENT_PVS ||
handle.h != currentPVS[handle.i].handle.h )
{
2012-11-26 18:58:24 +00:00
gameLocal.Warning( "idPVS::InCurrentPVS: invalid handle" );
return false;
}
if( targetArea < 0 || targetArea >= numAreas )
{
2012-11-26 18:58:24 +00:00
return false;
}
return ( ( currentPVS[handle.i].pvs[targetArea >> 3] & ( 1 << ( targetArea & 7 ) ) ) != 0 );
2012-11-26 18:58:24 +00:00
}
/*
================
idPVS::InCurrentPVS
================
*/
bool idPVS::InCurrentPVS( const pvsHandle_t handle, const int* targetAreas, int numTargetAreas ) const
{
2012-11-26 18:58:24 +00:00
int i;
if( handle.i < 0 || handle.i >= MAX_CURRENT_PVS ||
handle.h != currentPVS[handle.i].handle.h )
{
2012-11-26 18:58:24 +00:00
gameLocal.Warning( "idPVS::InCurrentPVS: invalid handle" );
return false;
}
for( i = 0; i < numTargetAreas; i++ )
{
if( targetAreas[i] < 0 || targetAreas[i] >= numAreas )
{
2012-11-26 18:58:24 +00:00
continue;
}
if( currentPVS[handle.i].pvs[targetAreas[i] >> 3] & ( 1 << ( targetAreas[i] & 7 ) ) )
{
2012-11-26 18:58:24 +00:00
return true;
}
}
return false;
}
/*
================
idPVS::DrawPVS
================
*/
void idPVS::DrawPVS( const idVec3& source, const pvsType_t type ) const
{
2012-11-26 18:58:24 +00:00
int i, j, k, numPoints, n, sourceArea;
exitPortal_t portal;
idPlane plane;
idVec3 offset;
idVec4* color;
2012-11-26 18:58:24 +00:00
pvsHandle_t handle;
2012-11-26 18:58:24 +00:00
sourceArea = gameRenderWorld->PointInArea( source );
if( sourceArea == -1 )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
handle = SetupCurrentPVS( source, type );
for( j = 0; j < numAreas; j++ )
{
if( !( currentPVS[handle.i].pvs[j >> 3] & ( 1 << ( j & 7 ) ) ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
if( j == sourceArea )
{
2012-11-26 18:58:24 +00:00
color = &colorRed;
}
else
{
2012-11-26 18:58:24 +00:00
color = &colorCyan;
}
2012-11-26 18:58:24 +00:00
n = gameRenderWorld->NumPortalsInArea( j );
2012-11-26 18:58:24 +00:00
// draw all the portals of the area
for( i = 0; i < n; i++ )
{
2012-11-26 18:58:24 +00:00
portal = gameRenderWorld->GetPortal( j, i );
2012-11-26 18:58:24 +00:00
numPoints = portal.w->GetNumPoints();
2012-11-26 18:58:24 +00:00
portal.w->GetPlane( plane );
offset = plane.Normal() * 4.0f;
for( k = 0; k < numPoints; k++ )
{
gameRenderWorld->DebugLine( *color, ( *portal.w )[k].ToVec3() + offset, ( *portal.w )[( k + 1 ) % numPoints].ToVec3() + offset );
2012-11-26 18:58:24 +00:00
}
}
}
2012-11-26 18:58:24 +00:00
FreeCurrentPVS( handle );
}
/*
================
idPVS::DrawPVS
================
*/
void idPVS::DrawPVS( const idBounds& source, const pvsType_t type ) const
{
2012-11-26 18:58:24 +00:00
int i, j, k, numPoints, n, num, areas[MAX_BOUNDS_AREAS];
exitPortal_t portal;
idPlane plane;
idVec3 offset;
idVec4* color;
2012-11-26 18:58:24 +00:00
pvsHandle_t handle;
2012-11-26 18:58:24 +00:00
num = gameRenderWorld->BoundsInAreas( source, areas, MAX_BOUNDS_AREAS );
if( !num )
{
2012-11-26 18:58:24 +00:00
return;
}
2012-11-26 18:58:24 +00:00
handle = SetupCurrentPVS( source, type );
for( j = 0; j < numAreas; j++ )
{
if( !( currentPVS[handle.i].pvs[j >> 3] & ( 1 << ( j & 7 ) ) ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
for( i = 0; i < num; i++ )
{
if( j == areas[i] )
{
2012-11-26 18:58:24 +00:00
break;
}
}
if( i < num )
{
2012-11-26 18:58:24 +00:00
color = &colorRed;
}
else
{
2012-11-26 18:58:24 +00:00
color = &colorCyan;
}
2012-11-26 18:58:24 +00:00
n = gameRenderWorld->NumPortalsInArea( j );
2012-11-26 18:58:24 +00:00
// draw all the portals of the area
for( i = 0; i < n; i++ )
{
2012-11-26 18:58:24 +00:00
portal = gameRenderWorld->GetPortal( j, i );
2012-11-26 18:58:24 +00:00
numPoints = portal.w->GetNumPoints();
2012-11-26 18:58:24 +00:00
portal.w->GetPlane( plane );
offset = plane.Normal() * 4.0f;
for( k = 0; k < numPoints; k++ )
{
gameRenderWorld->DebugLine( *color, ( *portal.w )[k].ToVec3() + offset, ( *portal.w )[( k + 1 ) % numPoints].ToVec3() + offset );
2012-11-26 18:58:24 +00:00
}
}
}
2012-11-26 18:58:24 +00:00
FreeCurrentPVS( handle );
}
/*
================
idPVS::DrawPVS
================
*/
void idPVS::DrawCurrentPVS( const pvsHandle_t handle, const idVec3& source ) const
{
2012-11-26 18:58:24 +00:00
int i, j, k, numPoints, n, sourceArea;
exitPortal_t portal;
idPlane plane;
idVec3 offset;
idVec4* color;
if( handle.i < 0 || handle.i >= MAX_CURRENT_PVS ||
handle.h != currentPVS[handle.i].handle.h )
{
2012-11-26 18:58:24 +00:00
gameLocal.Error( "idPVS::DrawCurrentPVS: invalid handle" );
return;
}
2012-11-26 18:58:24 +00:00
sourceArea = gameRenderWorld->PointInArea( source );
if( sourceArea == -1 )
{
2012-11-26 18:58:24 +00:00
return;
}
for( j = 0; j < numAreas; j++ )
{
if( !( currentPVS[handle.i].pvs[j >> 3] & ( 1 << ( j & 7 ) ) ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
if( j == sourceArea )
{
2012-11-26 18:58:24 +00:00
color = &colorRed;
}
else
{
2012-11-26 18:58:24 +00:00
color = &colorCyan;
}
2012-11-26 18:58:24 +00:00
n = gameRenderWorld->NumPortalsInArea( j );
2012-11-26 18:58:24 +00:00
// draw all the portals of the area
for( i = 0; i < n; i++ )
{
2012-11-26 18:58:24 +00:00
portal = gameRenderWorld->GetPortal( j, i );
2012-11-26 18:58:24 +00:00
numPoints = portal.w->GetNumPoints();
2012-11-26 18:58:24 +00:00
portal.w->GetPlane( plane );
offset = plane.Normal() * 4.0f;
for( k = 0; k < numPoints; k++ )
{
gameRenderWorld->DebugLine( *color, ( *portal.w )[k].ToVec3() + offset, ( *portal.w )[( k + 1 ) % numPoints].ToVec3() + offset );
2012-11-26 18:58:24 +00:00
}
}
}
}
/*
================
idPVS::CheckAreasForPortalSky
================
*/
bool idPVS::CheckAreasForPortalSky( const pvsHandle_t handle, const idVec3& origin )
{
2012-11-26 18:58:24 +00:00
int j, sourceArea;
if( handle.i < 0 || handle.i >= MAX_CURRENT_PVS || handle.h != currentPVS[handle.i].handle.h )
{
2012-11-26 18:58:24 +00:00
return false;
}
2012-11-26 18:58:24 +00:00
sourceArea = gameRenderWorld->PointInArea( origin );
if( sourceArea == -1 )
{
2012-11-26 18:58:24 +00:00
return false;
}
for( j = 0; j < numAreas; j++ )
{
if( !( currentPVS[handle.i].pvs[j >> 3] & ( 1 << ( j & 7 ) ) ) )
{
2012-11-26 18:58:24 +00:00
continue;
}
if( gameRenderWorld->CheckAreaForPortalSky( j ) )
{
2012-11-26 18:58:24 +00:00
return true;
}
}
2012-11-26 18:58:24 +00:00
return false;
}