2007-11-04 03:34:51 +00:00
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
Copyright (C) 1999-2007 id Software, Inc. and contributors.
|
|
|
|
For a list of contributors, see the accompanying CONTRIBUTORS file.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This file is part of GtkRadiant.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
GtkRadiant 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.
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with GtkRadiant; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2007-11-04 03:34:51 +00:00
|
|
|
// qvis.c
|
|
|
|
|
|
|
|
#include "qvis.h"
|
|
|
|
#include "q2_threads.h"
|
|
|
|
#include "stdlib.h"
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int numportals;
|
|
|
|
int portalclusters;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
char inbase[32];
|
|
|
|
char outbase[32];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
portal_t *portals;
|
|
|
|
leaf_t *leafs;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int c_portaltest, c_portalpass, c_portalcheck;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
byte *uncompressedvis;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
byte *vismap, *vismap_p, *vismap_end; // past visfile
|
|
|
|
int originalvismapsize;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int leafbytes; // (portalclusters+63)>>3
|
|
|
|
int leaflongs;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int portalbytes, portallongs;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
qboolean fastvis;
|
|
|
|
qboolean nosort;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int testlevel = 2;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
int totalvis;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
portal_t *sorted_portals[MAX_MAP_PORTALS * 2];
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void PlaneFromWinding( winding_t *w, plane_t *plane ){
|
|
|
|
vec3_t v1, v2;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// calc plane
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorSubtract( w->points[2], w->points[1], v1 );
|
|
|
|
VectorSubtract( w->points[0], w->points[1], v2 );
|
|
|
|
CrossProduct( v2, v1, plane->normal );
|
|
|
|
VectorNormalize( plane->normal, plane->normal );
|
|
|
|
plane->dist = DotProduct( w->points[0], plane->normal );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
NewWinding
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
winding_t *NewWinding( int points ){
|
|
|
|
winding_t *w;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if ( points > MAX_POINTS_ON_WINDING ) {
|
|
|
|
Error( "NewWinding: %i points", points );
|
|
|
|
}
|
|
|
|
|
|
|
|
size = (int)( (winding_t *)0 )->points[points];
|
|
|
|
w = malloc( size );
|
|
|
|
memset( w, 0, size );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
return w;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
void pw(winding_t *w)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i=0 ; i<w->numpoints ; i++)
|
|
|
|
Sys_Printf ("(%5.1f, %5.1f, %5.1f)\n",w->points[i][0], w->points[i][1],w->points[i][2]);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
void prl( leaf_t *l ){
|
|
|
|
int i;
|
|
|
|
portal_t *p;
|
|
|
|
plane_t pl;
|
|
|
|
|
|
|
|
for ( i = 0 ; i < l->numportals ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p = l->portals[i];
|
|
|
|
pl = p->plane;
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "portal %4i to leaf %4i : %7.1f : (%4.1f, %4.1f, %4.1f)\n",(int)( p - portals ),p->leaf,pl.dist, pl.normal[0], pl.normal[1], pl.normal[2] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//=============================================================================
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
=============
|
|
|
|
SortPortals
|
|
|
|
|
|
|
|
Sorts the portals from the least complex, so the later ones can reuse
|
|
|
|
the earlier information.
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
int PComp( const void *a, const void *b ){
|
|
|
|
if ( ( *(portal_t **)a )->nummightsee == ( *(portal_t **)b )->nummightsee ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( ( *(portal_t **)a )->nummightsee < ( *(portal_t **)b )->nummightsee ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
void SortPortals( void ){
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for ( i = 0 ; i < numportals * 2 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
sorted_portals[i] = &portals[i];
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( nosort ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
qsort( sorted_portals, numportals * 2, sizeof( sorted_portals[0] ), PComp );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==============
|
|
|
|
LeafVectorFromPortalVector
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
int LeafVectorFromPortalVector( byte *portalbits, byte *leafbits ){
|
|
|
|
int i;
|
|
|
|
portal_t *p;
|
|
|
|
int c_leafs;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( leafbits, 0, leafbytes );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < numportals * 2 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( portalbits[i >> 3] & ( 1 << ( i & 7 ) ) ) {
|
|
|
|
p = portals + i;
|
|
|
|
leafbits[p->leaf >> 3] |= ( 1 << ( p->leaf & 7 ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
c_leafs = CountBits( leafbits, portalclusters );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
return c_leafs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===============
|
|
|
|
ClusterMerge
|
|
|
|
|
|
|
|
Merges the portal visibility for a leaf
|
|
|
|
===============
|
|
|
|
*/
|
|
|
|
void ClusterMerge( int leafnum ){
|
|
|
|
leaf_t *leaf;
|
|
|
|
byte portalvector[MAX_PORTALS / 8];
|
|
|
|
byte uncompressed[MAX_MAP_LEAFS / 8];
|
|
|
|
byte compressed[MAX_MAP_LEAFS / 8];
|
|
|
|
int i, j;
|
|
|
|
int numvis;
|
|
|
|
byte *dest;
|
|
|
|
portal_t *p;
|
|
|
|
int pnum;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// OR together all the portalvis bits
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( portalvector, 0, portalbytes );
|
2007-11-04 03:34:51 +00:00
|
|
|
leaf = &leafs[leafnum];
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < leaf->numportals ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p = leaf->portals[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->status != stat_done ) {
|
|
|
|
Error( "portal not done" );
|
|
|
|
}
|
|
|
|
for ( j = 0 ; j < portallongs ; j++ )
|
|
|
|
( (long *)portalvector )[j] |= ( (long *)p->portalvis )[j];
|
2007-11-04 03:34:51 +00:00
|
|
|
pnum = p - portals;
|
2012-03-17 20:01:54 +00:00
|
|
|
portalvector[pnum >> 3] |= 1 << ( pnum & 7 );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert portal bits to leaf bits
|
2012-03-17 20:01:54 +00:00
|
|
|
numvis = LeafVectorFromPortalVector( portalvector, uncompressed );
|
|
|
|
|
|
|
|
if ( uncompressed[leafnum >> 3] & ( 1 << ( leafnum & 7 ) ) ) {
|
|
|
|
Sys_Printf( "WARNING: Leaf portals saw into leaf\n" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
uncompressed[leafnum >> 3] |= ( 1 << ( leafnum & 7 ) );
|
|
|
|
numvis++; // count the leaf itself
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// save uncompressed for PHS calculation
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( uncompressedvis + leafnum * leafbytes, uncompressed, leafbytes );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// compress the bit string
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_FPrintf( SYS_VRB, "cluster %4i : %4i visible\n", leafnum, numvis );
|
2007-11-04 03:34:51 +00:00
|
|
|
totalvis += numvis;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
i = CompressVis( uncompressed, compressed );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
dest = vismap_p;
|
|
|
|
vismap_p += i;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( vismap_p > vismap_end ) {
|
|
|
|
Error( "Vismap expansion overflow" );
|
|
|
|
}
|
|
|
|
|
|
|
|
dvis->bitofs[leafnum][DVIS_PVS] = dest - vismap;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( dest, compressed, i );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
CalcPortalVis
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void CalcPortalVis( void ){
|
|
|
|
int i;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// fastvis just uses mightsee for a very loose bound
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fastvis ) {
|
|
|
|
for ( i = 0 ; i < numportals * 2 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
portals[i].portalvis = portals[i].portalflood;
|
|
|
|
portals[i].status = stat_done;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
RunThreadsOnIndividual( numportals * 2, true, PortalFlow );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
CalcVis
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void CalcVis( void ){
|
|
|
|
int i;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
RunThreadsOnIndividual( numportals * 2, true, BasePortalVis );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// RunThreadsOnIndividual (numportals*2, true, BetterPortalVis);
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
SortPortals();
|
|
|
|
|
|
|
|
CalcPortalVis();
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// assemble the leaf vis lists by oring and compressing the portal lists
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < portalclusters ; i++ )
|
|
|
|
ClusterMerge( i );
|
|
|
|
|
|
|
|
Sys_Printf( "Average clusters visible: %i\n", totalvis / portalclusters );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void SetPortalSphere( portal_t *p ){
|
|
|
|
int i;
|
|
|
|
vec3_t total, dist;
|
|
|
|
winding_t *w;
|
|
|
|
float r, bestr;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
w = p->winding;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( vec3_origin, total );
|
|
|
|
for ( i = 0 ; i < w->numpoints ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorAdd( total, w->points[i], total );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
for ( i = 0 ; i < 3 ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
total[i] /= w->numpoints;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
bestr = 0;
|
|
|
|
for ( i = 0 ; i < w->numpoints ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorSubtract( w->points[i], total, dist );
|
|
|
|
r = VectorLength( dist );
|
|
|
|
if ( r > bestr ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
bestr = r;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( total, p->origin );
|
2007-11-04 03:34:51 +00:00
|
|
|
p->radius = bestr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
LoadPortals
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void LoadPortals( char *name ){
|
|
|
|
int i, j;
|
|
|
|
portal_t *p;
|
|
|
|
leaf_t *l;
|
|
|
|
char magic[80];
|
|
|
|
FILE *f;
|
|
|
|
int numpoints;
|
|
|
|
winding_t *w;
|
|
|
|
int leafnums[2];
|
|
|
|
plane_t plane;
|
|
|
|
|
|
|
|
if ( !strcmp( name,"-" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
f = stdin;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
else
|
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
f = fopen( name, "r" );
|
|
|
|
if ( !f ) {
|
|
|
|
Error( "LoadPortals: couldn't read %s\n",name );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fscanf( f,"%79s\n%i\n%i\n",magic, &portalclusters, &numportals ) != 3 ) {
|
|
|
|
Error( "LoadPortals: failed to read header" );
|
|
|
|
}
|
|
|
|
if ( strcmp( magic,PORTALFILE ) ) {
|
|
|
|
Error( "LoadPortals: not a portal file" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "%4i portalclusters\n", portalclusters );
|
|
|
|
Sys_Printf( "%4i numportals\n", numportals );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// these counts should take advantage of 64 bit systems automatically
|
2012-03-17 20:01:54 +00:00
|
|
|
leafbytes = ( ( portalclusters + 63 ) & ~63 ) >> 3;
|
|
|
|
leaflongs = leafbytes / sizeof( long );
|
|
|
|
|
|
|
|
portalbytes = ( ( numportals * 2 + 63 ) & ~63 ) >> 3;
|
|
|
|
portallongs = portalbytes / sizeof( long );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// each file portal is split into two memory portals
|
2012-03-17 20:01:54 +00:00
|
|
|
portals = malloc( 2 * numportals * sizeof( portal_t ) );
|
|
|
|
memset( portals, 0, 2 * numportals * sizeof( portal_t ) );
|
|
|
|
|
|
|
|
leafs = malloc( portalclusters * sizeof( leaf_t ) );
|
|
|
|
memset( leafs, 0, portalclusters * sizeof( leaf_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
originalvismapsize = portalclusters * leafbytes;
|
|
|
|
uncompressedvis = malloc( originalvismapsize );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
vismap = vismap_p = dvisdata;
|
|
|
|
dvis->numclusters = portalclusters;
|
|
|
|
vismap_p = (byte *)&dvis->bitofs[portalclusters];
|
|
|
|
|
|
|
|
vismap_end = vismap + MAX_MAP_VISIBILITY;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
for ( i = 0, p = portals ; i < numportals ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fscanf( f, "%i %i %i ", &numpoints, &leafnums[0], &leafnums[1] )
|
|
|
|
!= 3 ) {
|
|
|
|
Error( "LoadPortals: reading portal %i", i );
|
|
|
|
}
|
|
|
|
if ( numpoints > MAX_POINTS_ON_WINDING ) {
|
|
|
|
Error( "LoadPortals: portal %i has too many points", i );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
if ( (unsigned)leafnums[0] > portalclusters
|
2012-03-17 20:01:54 +00:00
|
|
|
|| (unsigned)leafnums[1] > portalclusters ) {
|
|
|
|
Error( "LoadPortals: reading portal %i", i );
|
|
|
|
}
|
|
|
|
|
|
|
|
w = p->winding = NewWinding( numpoints );
|
2007-11-04 03:34:51 +00:00
|
|
|
w->original = true;
|
|
|
|
w->numpoints = numpoints;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
for ( j = 0 ; j < numpoints ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
double v[3];
|
|
|
|
int k;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// scanf into double, then assign to vec_t
|
|
|
|
// so we don't care what size vec_t is
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fscanf( f, "(%lf %lf %lf ) "
|
|
|
|
, &v[0], &v[1], &v[2] ) != 3 ) {
|
|
|
|
Error( "LoadPortals: reading portal %i", i );
|
|
|
|
}
|
|
|
|
for ( k = 0 ; k < 3 ; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
w->points[j][k] = v[k];
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
fscanf( f, "\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
// calc plane
|
|
|
|
PlaneFromWinding( w, &plane );
|
|
|
|
|
|
|
|
// create forward portal
|
2007-11-04 03:34:51 +00:00
|
|
|
l = &leafs[leafnums[0]];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
|
|
|
|
Error( "Leaf with too many portals" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
l->portals[l->numportals] = p;
|
|
|
|
l->numportals++;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
p->winding = w;
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorSubtract( vec3_origin, plane.normal, p->plane.normal );
|
2007-11-04 03:34:51 +00:00
|
|
|
p->plane.dist = -plane.dist;
|
|
|
|
p->leaf = leafnums[1];
|
2012-03-17 20:01:54 +00:00
|
|
|
SetPortalSphere( p );
|
2007-11-04 03:34:51 +00:00
|
|
|
p++;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
// create backwards portal
|
2007-11-04 03:34:51 +00:00
|
|
|
l = &leafs[leafnums[1]];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
|
|
|
|
Error( "Leaf with too many portals" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
l->portals[l->numportals] = p;
|
|
|
|
l->numportals++;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
p->winding = NewWinding( w->numpoints );
|
2007-11-04 03:34:51 +00:00
|
|
|
p->winding->numpoints = w->numpoints;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < w->numpoints ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
VectorCopy( w->points[w->numpoints - 1 - j], p->winding->points[j] );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p->plane = plane;
|
|
|
|
p->leaf = leafnums[0];
|
2012-03-17 20:01:54 +00:00
|
|
|
SetPortalSphere( p );
|
2007-11-04 03:34:51 +00:00
|
|
|
p++;
|
|
|
|
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
fclose( f );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
================
|
|
|
|
CalcPHS
|
|
|
|
|
|
|
|
Calculate the PHS (Potentially Hearable Set)
|
|
|
|
by ORing together all the PVS visible from a leaf
|
|
|
|
================
|
|
|
|
*/
|
|
|
|
void CalcPHS( void ){
|
|
|
|
int i, j, k, l, index;
|
|
|
|
int bitbyte;
|
|
|
|
long *dest, *src;
|
|
|
|
byte *scan;
|
|
|
|
int count;
|
|
|
|
byte uncompressed[MAX_MAP_LEAFS / 8];
|
|
|
|
byte compressed[MAX_MAP_LEAFS / 8];
|
|
|
|
|
|
|
|
Sys_Printf( "Building PHS...\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
count = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0 ; i < portalclusters ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
scan = uncompressedvis + i * leafbytes;
|
|
|
|
memcpy( uncompressed, scan, leafbytes );
|
|
|
|
for ( j = 0 ; j < leafbytes ; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
bitbyte = scan[j];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !bitbyte ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
for ( k = 0 ; k < 8 ; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !( bitbyte & ( 1 << k ) ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
// OR this pvs row into the phs
|
2012-03-17 20:01:54 +00:00
|
|
|
index = ( ( j << 3 ) + k );
|
|
|
|
if ( index >= portalclusters ) {
|
|
|
|
Error( "Bad bit in PVS" ); // pad bits should be 0
|
|
|
|
}
|
|
|
|
src = (long *)( uncompressedvis + index * leafbytes );
|
2007-11-04 03:34:51 +00:00
|
|
|
dest = (long *)uncompressed;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( l = 0 ; l < leaflongs ; l++ )
|
|
|
|
( (long *)uncompressed )[l] |= src[l];
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0 ; j < portalclusters ; j++ )
|
|
|
|
if ( uncompressed[j >> 3] & ( 1 << ( j & 7 ) ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
count++;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
//
|
|
|
|
// compress the bit string
|
|
|
|
//
|
|
|
|
j = CompressVis( uncompressed, compressed );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
dest = (long *)vismap_p;
|
|
|
|
vismap_p += j;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( vismap_p > vismap_end ) {
|
|
|
|
Error( "Vismap expansion overflow" );
|
|
|
|
}
|
|
|
|
|
|
|
|
dvis->bitofs[i][DVIS_PHS] = (byte *)dest - vismap;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( dest, compressed, j );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "Average clusters hearable: %i\n", count / portalclusters );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===========
|
|
|
|
main
|
|
|
|
===========
|
|
|
|
*/
|
|
|
|
int VIS_Main(){
|
|
|
|
char portalfile[1024];
|
|
|
|
char source[1024];
|
|
|
|
char name[1024];
|
|
|
|
double start, end;
|
|
|
|
int total_vis_time;
|
|
|
|
|
|
|
|
Sys_Printf( "\n----- VIS ----\n\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//if (i != argc - 1)
|
|
|
|
// Error ("usage: vis [-threads #] [-level 0-4] [-fast] [-v] bspfile");
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
start = I_FloatTime();
|
|
|
|
|
|
|
|
ThreadSetDefault();
|
|
|
|
|
|
|
|
SetQdirFromPath( mapname );
|
|
|
|
strcpy( source, ExpandArg( mapname ) );
|
|
|
|
StripExtension( source );
|
|
|
|
DefaultExtension( source, ".bsp" );
|
|
|
|
|
|
|
|
sprintf( name, "%s%s", inbase, source );
|
|
|
|
Sys_Printf( "reading %s\n", name );
|
|
|
|
LoadBSPFile( name );
|
|
|
|
if ( numnodes == 0 || numfaces == 0 ) {
|
|
|
|
Error( "Empty map" );
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf( portalfile, "%s%s", inbase, ExpandArg( mapname ) );
|
|
|
|
StripExtension( portalfile );
|
|
|
|
strcat( portalfile, ".prt" );
|
|
|
|
|
|
|
|
Sys_Printf( "reading %s\n", portalfile );
|
|
|
|
LoadPortals( portalfile );
|
|
|
|
|
|
|
|
CalcVis();
|
|
|
|
|
|
|
|
CalcPHS();
|
|
|
|
|
|
|
|
visdatasize = vismap_p - dvisdata;
|
|
|
|
Sys_Printf( "visdatasize:%i compressed from %i\n", visdatasize, originalvismapsize * 2 );
|
|
|
|
|
|
|
|
sprintf( name, "%s%s", outbase, source );
|
|
|
|
Sys_Printf( "writing %s\n", name );
|
|
|
|
WriteBSPFile( name );
|
|
|
|
|
|
|
|
end = I_FloatTime();
|
|
|
|
total_vis_time = (int) ( end - start );
|
|
|
|
Sys_Printf( "\nVIS Time: " );
|
|
|
|
if ( total_vis_time > 59 ) {
|
|
|
|
Sys_Printf( "%d Minutes ", total_vis_time / 60 );
|
|
|
|
}
|
|
|
|
Sys_Printf( "%d Seconds\n", total_vis_time % 60 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|