2008-07-25 07:31:37 +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
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
----------------------------------------------------------------------------------
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
This code has been altered significantly from its original form, to support
|
|
|
|
several games based on the Quake III Arena engine, in the form of "Q3Map2."
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
------------------------------------------------------------------------------- */
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* marker */
|
|
|
|
#define VIS_C
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* dependencies */
|
|
|
|
#include "q3map2.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
void PlaneFromWinding( fixedWinding_t *w, visPlane_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
|
|
|
NewFixedWinding()
|
|
|
|
returns a new fixed winding
|
|
|
|
ydnar: altered this a bit to reconcile multiply-defined winding_t
|
|
|
|
*/
|
|
|
|
|
|
|
|
fixedWinding_t *NewFixedWinding( int points ){
|
|
|
|
fixedWinding_t *w;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if ( points > MAX_POINTS_ON_WINDING ) {
|
|
|
|
Error( "NewWinding: %i points", points );
|
|
|
|
}
|
|
|
|
|
|
|
|
size = (int)( (size_t)( (fixedWinding_t *)0 )->points[points] );
|
|
|
|
w = safe_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 prl( leaf_t *l ){
|
|
|
|
int i;
|
|
|
|
vportal_t *p;
|
|
|
|
visPlane_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 ( ( *(vportal_t **)a )->nummightsee == ( *(vportal_t **)b )->nummightsee ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( ( *(vportal_t **)a )->nummightsee < ( *(vportal_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, j, leafnum;
|
|
|
|
vportal_t *p;
|
|
|
|
int c_leafs;
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
for ( j = 0; j < portalclusters; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
leafnum = j;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( leafs[leafnum].merged >= 0 )
|
2007-11-04 03:34:51 +00:00
|
|
|
leafnum = leafs[leafnum].merged;
|
|
|
|
//if the merged leaf is visible then the original leaf is visible
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( leafbits[leafnum >> 3] & ( 1 << ( leafnum & 7 ) ) ) {
|
|
|
|
leafbits[j >> 3] |= ( 1 << ( j & 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];
|
|
|
|
int i, j;
|
|
|
|
int numvis, mergedleafnum;
|
|
|
|
vportal_t *p;
|
|
|
|
int pnum;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// OR together all the portalvis bits
|
|
|
|
|
|
|
|
mergedleafnum = leafnum;
|
2012-03-17 20:01:54 +00:00
|
|
|
while ( leafs[mergedleafnum].merged >= 0 )
|
2007-11-04 03:34:51 +00:00
|
|
|
mergedleafnum = leafs[mergedleafnum].merged;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( portalvector, 0, portalbytes );
|
2007-11-04 03:34:51 +00:00
|
|
|
leaf = &leafs[mergedleafnum];
|
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->removed ) {
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memset( uncompressed, 0, leafbytes );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
uncompressed[mergedleafnum >> 3] |= ( 1 << ( mergedleafnum & 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 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// if (uncompressed[leafnum>>3] & (1<<(leafnum&7)))
|
|
|
|
// Sys_Printf ("WARNING: Leaf portals saw into leaf\n");
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// uncompressed[leafnum>>3] |= (1<<(leafnum&7));
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
numvis++; // count the leaf itself
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
totalvis += numvis;
|
|
|
|
|
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
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
memcpy( bspVisBytes + VIS_HEADER_SIZE + leafnum * leafbytes, uncompressed, leafbytes );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
CalcPortalVis
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void CalcPortalVis( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
#ifdef MREDEBUG
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
|
2007-11-04 03:34:51 +00:00
|
|
|
//get rid of the counter
|
2012-03-17 20:01:54 +00:00
|
|
|
RunThreadsOnIndividual( numportals * 2, qfalse, PortalFlow );
|
2007-11-04 03:34:51 +00:00
|
|
|
#else
|
2012-03-17 20:01:54 +00:00
|
|
|
RunThreadsOnIndividual( numportals * 2, qtrue, PortalFlow );
|
2007-11-04 03:34:51 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
CalcPassageVis
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void CalcPassageVis( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
PassageMemory();
|
|
|
|
|
|
|
|
#ifdef MREDEBUG
|
2012-03-17 20:01:54 +00:00
|
|
|
_printf( "%6d portals out of %d", 0, numportals * 2 );
|
|
|
|
RunThreadsOnIndividual( numportals * 2, qfalse, CreatePassages );
|
|
|
|
_printf( "\n" );
|
|
|
|
_printf( "%6d portals out of %d", 0, numportals * 2 );
|
|
|
|
RunThreadsOnIndividual( numportals * 2, qfalse, PassageFlow );
|
|
|
|
_printf( "\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#else
|
|
|
|
Sys_Printf( "\n--- CreatePassages (%d) ---\n", numportals * 2 );
|
2012-03-17 20:01:54 +00:00
|
|
|
RunThreadsOnIndividual( numportals * 2, qtrue, CreatePassages );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
Sys_Printf( "\n--- PassageFlow (%d) ---\n", numportals * 2 );
|
|
|
|
RunThreadsOnIndividual( numportals * 2, qtrue, PassageFlow );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
CalcPassagePortalVis
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void CalcPassagePortalVis( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
PassageMemory();
|
|
|
|
|
|
|
|
#ifdef MREDEBUG
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
|
|
|
|
RunThreadsOnIndividual( numportals * 2, qfalse, CreatePassages );
|
|
|
|
Sys_Printf( "\n" );
|
|
|
|
Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
|
|
|
|
RunThreadsOnIndividual( numportals * 2, qfalse, PassagePortalFlow );
|
|
|
|
Sys_Printf( "\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
#else
|
|
|
|
Sys_Printf( "\n--- CreatePassages (%d) ---\n", numportals * 2 );
|
2012-03-17 20:01:54 +00:00
|
|
|
RunThreadsOnIndividual( numportals * 2, qtrue, CreatePassages );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
Sys_Printf( "\n--- PassagePortalFlow (%d) ---\n", numportals * 2 );
|
|
|
|
RunThreadsOnIndividual( numportals * 2, qtrue, PassagePortalFlow );
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
CalcFastVis
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void CalcFastVis( 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
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
==================
|
|
|
|
CalcVis
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void CalcVis( void ){
|
|
|
|
int i;
|
|
|
|
const char *value;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar: rr2do2's farplane code */
|
|
|
|
farPlaneDist = 0.0f;
|
2012-03-17 20:01:54 +00:00
|
|
|
value = ValueForKey( &entities[ 0 ], "_farplanedist" ); /* proper '_' prefixed key */
|
|
|
|
if ( value[ 0 ] == '\0' ) {
|
|
|
|
value = ValueForKey( &entities[ 0 ], "fogclip" ); /* wolf compatibility */
|
|
|
|
}
|
|
|
|
if ( value[ 0 ] == '\0' ) {
|
|
|
|
value = ValueForKey( &entities[ 0 ], "distancecull" ); /* sof2 compatibility */
|
|
|
|
}
|
|
|
|
if ( value[ 0 ] != '\0' ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
farPlaneDist = atof( value );
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( farPlaneDist > 0.0f ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
Sys_Printf( "farplane distance = %.1f\n", farPlaneDist );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
farPlaneDist = 0.0f;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
Sys_Printf( "\n--- BasePortalVis (%d) ---\n", numportals * 2 );
|
|
|
|
RunThreadsOnIndividual( numportals * 2, qtrue, BasePortalVis );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// RunThreadsOnIndividual (numportals*2, qtrue, BetterPortalVis);
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
SortPortals();
|
|
|
|
|
|
|
|
if ( fastvis ) {
|
|
|
|
CalcFastVis();
|
|
|
|
}
|
|
|
|
else if ( noPassageVis ) {
|
|
|
|
CalcPortalVis();
|
|
|
|
}
|
|
|
|
else if ( passageVisOnly ) {
|
|
|
|
CalcPassageVis();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
CalcPassagePortalVis();
|
|
|
|
}
|
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
|
|
|
Sys_Printf( "creating leaf vis...\n" );
|
|
|
|
for ( i = 0 ; i < portalclusters ; i++ )
|
|
|
|
ClusterMerge( i );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "Total visible clusters: %i\n", totalvis );
|
|
|
|
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
|
|
|
==================
|
|
|
|
SetPortalSphere
|
|
|
|
==================
|
|
|
|
*/
|
|
|
|
void SetPortalSphere( vportal_t *p ){
|
|
|
|
int i;
|
|
|
|
vec3_t total, dist;
|
|
|
|
fixedWinding_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
|
|
|
=============
|
|
|
|
Winding_PlanesConcave
|
|
|
|
=============
|
|
|
|
*/
|
|
|
|
#define WCONVEX_EPSILON 0.2
|
|
|
|
|
|
|
|
int Winding_PlanesConcave( fixedWinding_t *w1, fixedWinding_t *w2,
|
|
|
|
vec3_t normal1, vec3_t normal2,
|
|
|
|
float dist1, float dist2 ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int i;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !w1 || !w2 ) {
|
|
|
|
return qfalse;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// check if one of the points of winding 1 is at the front of the plane of winding 2
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < w1->numpoints; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( DotProduct( normal2, w1->points[i] ) - dist2 > WCONVEX_EPSILON ) {
|
|
|
|
return qtrue;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
// check if one of the points of winding 2 is at the front of the plane of winding 1
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < w2->numpoints; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( DotProduct( normal1, w2->points[i] ) - dist1 > WCONVEX_EPSILON ) {
|
|
|
|
return qtrue;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
TryMergeLeaves
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
int TryMergeLeaves( int l1num, int l2num ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int i, j, k, n, numportals;
|
|
|
|
visPlane_t plane1, plane2;
|
|
|
|
leaf_t *l1, *l2;
|
|
|
|
vportal_t *p1, *p2;
|
|
|
|
vportal_t *portals[MAX_PORTALS_ON_LEAF];
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < 2; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( k ) {
|
|
|
|
l1 = &leafs[l1num];
|
|
|
|
}
|
|
|
|
else{l1 = &faceleafs[l1num]; }
|
|
|
|
for ( i = 0; i < l1->numportals; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p1 = l1->portals[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p1->leaf == l2num ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for ( n = 0; n < 2; n++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( n ) {
|
|
|
|
l2 = &leafs[l2num];
|
|
|
|
}
|
|
|
|
else{l2 = &faceleafs[l2num]; }
|
|
|
|
for ( j = 0; j < l2->numportals; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p2 = l2->portals[j];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p2->leaf == l1num ) {
|
|
|
|
continue;
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
//
|
|
|
|
plane1 = p1->plane;
|
|
|
|
plane2 = p2->plane;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( Winding_PlanesConcave( p1->winding, p2->winding, plane1.normal, plane2.normal, plane1.dist, plane2.dist ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
return qfalse;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = 0; k < 2; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( k ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
l1 = &leafs[l1num];
|
|
|
|
l2 = &leafs[l2num];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
l1 = &faceleafs[l1num];
|
|
|
|
l2 = &faceleafs[l2num];
|
|
|
|
}
|
|
|
|
numportals = 0;
|
|
|
|
//the leaves can be merged now
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < l1->numportals; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p1 = l1->portals[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p1->leaf == l2num ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
p1->removed = qtrue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
portals[numportals++] = p1;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < l2->numportals; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p2 = l2->portals[j];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p2->leaf == l1num ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
p2->removed = qtrue;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
portals[numportals++] = p2;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < numportals; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
l2->portals[i] = portals[i];
|
|
|
|
}
|
|
|
|
l2->numportals = numportals;
|
|
|
|
l1->merged = l2num;
|
|
|
|
}
|
|
|
|
return qtrue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
UpdatePortals
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void UpdatePortals( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int i;
|
|
|
|
vportal_t *p;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < numportals * 2; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p = &portals[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->removed ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
while ( leafs[p->leaf].merged >= 0 )
|
2007-11-04 03:34:51 +00:00
|
|
|
p->leaf = leafs[p->leaf].merged;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
MergeLeaves
|
|
|
|
|
|
|
|
try to merge leaves but don't merge through hint splitters
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void MergeLeaves( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int i, j, nummerges, totalnummerges;
|
|
|
|
leaf_t *leaf;
|
|
|
|
vportal_t *p;
|
|
|
|
|
|
|
|
totalnummerges = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
nummerges = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < portalclusters; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
leaf = &leafs[i];
|
|
|
|
//if this leaf is merged already
|
|
|
|
|
|
|
|
/* ydnar: vmods: merge all non-hint portals */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( leaf->merged >= 0 && hint == qfalse ) {
|
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
|
|
|
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < leaf->numportals; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p = leaf->portals[j];
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->removed ) {
|
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
|
|
|
//never merge through hint portals
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->hint ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( TryMergeLeaves( i, p->leaf ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
UpdatePortals();
|
|
|
|
nummerges++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
totalnummerges += nummerges;
|
2012-03-17 20:01:54 +00:00
|
|
|
} while ( nummerges );
|
|
|
|
Sys_Printf( "%6d leaves merged\n", totalnummerges );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
TryMergeWinding
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
#define CONTINUOUS_EPSILON 0.005
|
|
|
|
|
|
|
|
fixedWinding_t *TryMergeWinding( fixedWinding_t *f1, fixedWinding_t *f2, vec3_t planenormal ){
|
|
|
|
vec_t *p1, *p2, *p3, *p4, *back;
|
|
|
|
fixedWinding_t *newf;
|
|
|
|
int i, j, k, l;
|
|
|
|
vec3_t normal, delta;
|
|
|
|
vec_t dot;
|
|
|
|
qboolean keep1, keep2;
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// find a common edge
|
2012-03-17 20:01:54 +00:00
|
|
|
//
|
|
|
|
p1 = p2 = NULL; // stop compiler warning
|
|
|
|
j = 0; //
|
|
|
|
|
|
|
|
for ( i = 0; i < f1->numpoints; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p1 = f1->points[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
p2 = f1->points[( i + 1 ) % f1->numpoints];
|
|
|
|
for ( j = 0; j < f2->numpoints; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p3 = f2->points[j];
|
2012-03-17 20:01:54 +00:00
|
|
|
p4 = f2->points[( j + 1 ) % f2->numpoints];
|
|
|
|
for ( k = 0; k < 3; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fabs( p1[k] - p4[k] ) > 0.1 ) { //EQUAL_EPSILON) //ME
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( fabs( p2[k] - p3[k] ) > 0.1 ) { //EQUAL_EPSILON) //ME
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end for
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( k == 3 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end for
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( j < f2->numpoints ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
} //end for
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i == f1->numpoints ) {
|
|
|
|
return NULL; // no matching edges
|
|
|
|
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
//
|
|
|
|
// check slope of connected lines
|
|
|
|
// if the slopes are colinear, the point can be removed
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
back = f1->points[( i + f1->numpoints - 1 ) % f1->numpoints];
|
|
|
|
VectorSubtract( p1, back, delta );
|
|
|
|
CrossProduct( planenormal, delta, normal );
|
|
|
|
VectorNormalize( normal, normal );
|
|
|
|
|
|
|
|
back = f2->points[( j + 2 ) % f2->numpoints];
|
|
|
|
VectorSubtract( back, p1, delta );
|
|
|
|
dot = DotProduct( delta, normal );
|
|
|
|
if ( dot > CONTINUOUS_EPSILON ) {
|
|
|
|
return NULL; // not a convex polygon
|
|
|
|
}
|
|
|
|
keep1 = (qboolean)( dot < -CONTINUOUS_EPSILON );
|
|
|
|
|
|
|
|
back = f1->points[( i + 2 ) % f1->numpoints];
|
|
|
|
VectorSubtract( back, p2, delta );
|
|
|
|
CrossProduct( planenormal, delta, normal );
|
|
|
|
VectorNormalize( normal, normal );
|
|
|
|
|
|
|
|
back = f2->points[( j + f2->numpoints - 1 ) % f2->numpoints];
|
|
|
|
VectorSubtract( back, p2, delta );
|
|
|
|
dot = DotProduct( delta, normal );
|
|
|
|
if ( dot > CONTINUOUS_EPSILON ) {
|
|
|
|
return NULL; // not a convex polygon
|
|
|
|
}
|
|
|
|
keep2 = (qboolean)( dot < -CONTINUOUS_EPSILON );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// build the new polygon
|
|
|
|
//
|
2012-03-17 20:01:54 +00:00
|
|
|
newf = NewFixedWinding( f1->numpoints + f2->numpoints );
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// copy first polygon
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( k = ( i + 1 ) % f1->numpoints ; k != i ; k = ( k + 1 ) % f1->numpoints )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( k == ( i + 1 ) % f1->numpoints && !keep2 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VectorCopy( f1->points[k], newf->points[newf->numpoints] );
|
2007-11-04 03:34:51 +00:00
|
|
|
newf->numpoints++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// copy second polygon
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( l = ( j + 1 ) % f2->numpoints ; l != j ; l = ( l + 1 ) % f2->numpoints )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( l == ( j + 1 ) % f2->numpoints && !keep1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
VectorCopy( f2->points[l], newf->points[newf->numpoints] );
|
2007-11-04 03:34:51 +00:00
|
|
|
newf->numpoints++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
MergeLeafPortals
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void MergeLeafPortals( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int i, j, k, nummerges, hintsmerged;
|
|
|
|
leaf_t *leaf;
|
|
|
|
vportal_t *p1, *p2;
|
|
|
|
fixedWinding_t *w;
|
|
|
|
|
|
|
|
nummerges = 0;
|
|
|
|
hintsmerged = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0; i < portalclusters; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
leaf = &leafs[i];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( leaf->merged >= 0 ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for ( j = 0; j < leaf->numportals; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p1 = leaf->portals[j];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p1->removed ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
for ( k = j + 1; k < leaf->numportals; k++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p2 = leaf->portals[k];
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p2->removed ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( p1->leaf == p2->leaf ) {
|
|
|
|
w = TryMergeWinding( p1->winding, p2->winding, p1->plane.normal );
|
|
|
|
if ( w ) {
|
|
|
|
free( p1->winding ); //% FreeWinding(p1->winding);
|
2007-11-04 03:34:51 +00:00
|
|
|
p1->winding = w;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p1->hint && p2->hint ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
hintsmerged++;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
p1->hint |= p2->hint;
|
2012-03-17 20:01:54 +00:00
|
|
|
SetPortalSphere( p1 );
|
2007-11-04 03:34:51 +00:00
|
|
|
p2->removed = qtrue;
|
|
|
|
nummerges++;
|
|
|
|
i--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( k < leaf->numportals ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
break;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "%6d portals merged\n", nummerges );
|
|
|
|
Sys_Printf( "%6d hint portals merged\n", hintsmerged );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
WritePortals
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
int CountActivePortals( void ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int num, hints, j;
|
|
|
|
vportal_t *p;
|
|
|
|
|
|
|
|
num = 0;
|
|
|
|
hints = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < numportals * 2; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p = portals + j;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->removed ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
continue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
if ( p->hint ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
hints++;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
num++;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
Sys_Printf( "%6d active portals\n", num );
|
|
|
|
Sys_Printf( "%6d hint portals\n", hints );
|
2007-11-04 03:34:51 +00:00
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
WritePortals
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void WriteFloat( FILE *f, vec_t v );
|
|
|
|
|
|
|
|
void WritePortals( char *filename ){
|
2007-11-04 03:34:51 +00:00
|
|
|
int i, j, num;
|
|
|
|
FILE *pf;
|
|
|
|
vportal_t *p;
|
|
|
|
fixedWinding_t *w;
|
|
|
|
|
|
|
|
// write the file
|
2012-03-17 20:01:54 +00:00
|
|
|
pf = fopen( filename, "w" );
|
|
|
|
if ( !pf ) {
|
|
|
|
Error( "Error opening %s", filename );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
num = 0;
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < numportals * 2; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p = portals + j;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->removed ) {
|
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
|
|
|
// if (!p->hint)
|
|
|
|
// continue;
|
|
|
|
num++;
|
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
fprintf( pf, "%s\n", PORTALFILE );
|
|
|
|
fprintf( pf, "%i\n", 0 );
|
|
|
|
fprintf( pf, "%i\n", num ); // + numfaces);
|
|
|
|
fprintf( pf, "%i\n", 0 );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( j = 0; j < numportals * 2; j++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
|
|
|
p = portals + j;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( p->removed ) {
|
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
|
|
|
// if (!p->hint)
|
|
|
|
// continue;
|
|
|
|
w = p->winding;
|
2012-03-17 20:01:54 +00:00
|
|
|
fprintf( pf,"%i %i %i ",w->numpoints, 0, 0 );
|
|
|
|
fprintf( pf, "%d ", p->hint );
|
|
|
|
for ( i = 0 ; i < w->numpoints ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
fprintf( pf,"(" );
|
|
|
|
WriteFloat( pf, w->points[i][0] );
|
|
|
|
WriteFloat( pf, w->points[i][1] );
|
|
|
|
WriteFloat( pf, w->points[i][2] );
|
|
|
|
fprintf( pf,") " );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
fprintf( pf,"\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
for (j = 0; j < numfaces; j++)
|
|
|
|
{
|
|
|
|
p = faces + j;
|
|
|
|
w = p->winding;
|
|
|
|
fprintf (pf,"%i %i %i ",w->numpoints, 0, 0);
|
|
|
|
fprintf (pf, "0 ");
|
|
|
|
for (i=0 ; i<w->numpoints ; i++)
|
|
|
|
{
|
|
|
|
fprintf (pf,"(");
|
|
|
|
WriteFloat (pf, w->points[i][0]);
|
|
|
|
WriteFloat (pf, w->points[i][1]);
|
|
|
|
WriteFloat (pf, w->points[i][2]);
|
|
|
|
fprintf (pf,") ");
|
|
|
|
}
|
|
|
|
fprintf (pf,"\n");
|
|
|
|
}*/
|
|
|
|
|
|
|
|
fclose( pf );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
============
|
|
|
|
LoadPortals
|
|
|
|
============
|
|
|
|
*/
|
|
|
|
void LoadPortals( char *name ){
|
|
|
|
int i, j, hint;
|
|
|
|
vportal_t *p;
|
|
|
|
leaf_t *l;
|
|
|
|
char magic[80];
|
|
|
|
FILE *f;
|
|
|
|
int numpoints;
|
|
|
|
fixedWinding_t *w;
|
|
|
|
int leafnums[2];
|
|
|
|
visPlane_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%i\n",magic, &portalclusters, &numportals, &numfaces ) != 4 ) {
|
|
|
|
Error( "LoadPortals: failed to read header" );
|
|
|
|
}
|
|
|
|
if ( strcmp( magic,PORTALFILE ) ) {
|
|
|
|
Error( "LoadPortals: not a portal file" );
|
|
|
|
}
|
|
|
|
|
|
|
|
Sys_Printf( "%6i portalclusters\n", portalclusters );
|
|
|
|
Sys_Printf( "%6i numportals\n", numportals );
|
|
|
|
Sys_Printf( "%6i numfaces\n", numfaces );
|
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 = safe_malloc( 2 * numportals * sizeof( vportal_t ) );
|
|
|
|
memset( portals, 0, 2 * numportals * sizeof( vportal_t ) );
|
|
|
|
|
|
|
|
leafs = safe_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
|
|
|
for ( i = 0; i < portalclusters; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
leafs[i].merged = -1;
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
numBSPVisBytes = VIS_HEADER_SIZE + portalclusters * leafbytes;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numBSPVisBytes > MAX_MAP_VISIBILITY ) {
|
|
|
|
Error( "MAX_MAP_VISIBILITY exceeded" );
|
|
|
|
}
|
|
|
|
|
|
|
|
( (int *)bspVisBytes )[0] = portalclusters;
|
|
|
|
( (int *)bspVisBytes )[1] = leafbytes;
|
2007-11-04 03:34:51 +00:00
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
if ( fscanf( f, "%i ", &hint ) != 1 ) {
|
|
|
|
Error( "LoadPortals: reading hint state" );
|
|
|
|
}
|
|
|
|
|
|
|
|
w = p->winding = NewFixedWinding( numpoints );
|
2007-11-04 03:34:51 +00:00
|
|
|
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
|
|
|
// calc plane
|
2012-03-17 20:01:54 +00:00
|
|
|
PlaneFromWinding( w, &plane );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
// create forward portal
|
|
|
|
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
|
|
|
|
|
|
|
p->num = i + 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
p->hint = hint;
|
|
|
|
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
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
// create backwards portal
|
|
|
|
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->num = i + 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
p->hint = hint;
|
2012-03-17 20:01:54 +00:00
|
|
|
p->winding = NewFixedWinding( 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
|
|
|
faces = safe_malloc( 2 * numfaces * sizeof( vportal_t ) );
|
|
|
|
memset( faces, 0, 2 * numfaces * sizeof( vportal_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
faceleafs = safe_malloc( portalclusters * sizeof( leaf_t ) );
|
|
|
|
memset( faceleafs, 0, portalclusters * sizeof( leaf_t ) );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 0, p = faces; i < numfaces; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( fscanf( f, "%i %i ", &numpoints, &leafnums[0] ) != 2 ) {
|
|
|
|
Error( "LoadPortals: reading portal %i", i );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
w = p->winding = NewFixedWinding( numpoints );
|
2007-11-04 03:34:51 +00:00
|
|
|
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
|
|
|
// calc plane
|
2012-03-17 20:01:54 +00:00
|
|
|
PlaneFromWinding( w, &plane );
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
l = &faceleafs[leafnums[0]];
|
|
|
|
l->merged = -1;
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
|
|
|
|
Error( "Leaf with too many faces" );
|
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
l->portals[l->numportals] = p;
|
|
|
|
l->numportals++;
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
p->num = i + 1;
|
2007-11-04 03:34:51 +00:00
|
|
|
p->winding = w;
|
|
|
|
// normal pointing out of the leaf
|
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 = -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
|
|
|
|
|
|
|
fclose( f );
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2012-03-17 20:01:54 +00:00
|
|
|
===========
|
|
|
|
VisMain
|
|
|
|
===========
|
|
|
|
*/
|
|
|
|
int VisMain( int argc, char **argv ){
|
|
|
|
char portalfile[1024];
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* note it */
|
|
|
|
Sys_Printf( "--- Vis ---\n" );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* process arguments */
|
2012-03-17 20:01:54 +00:00
|
|
|
for ( i = 1 ; i < ( argc - 1 ) ; i++ )
|
2007-11-04 03:34:51 +00:00
|
|
|
{
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !strcmp( argv[i], "-fast" ) ) {
|
|
|
|
Sys_Printf( "fastvis = true\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
fastvis = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( !strcmp( argv[i], "-merge" ) ) {
|
|
|
|
Sys_Printf( "merge = true\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
mergevis = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( !strcmp( argv[i], "-nopassage" ) ) {
|
|
|
|
Sys_Printf( "nopassage = true\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
noPassageVis = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( !strcmp( argv[i], "-passageOnly" ) ) {
|
|
|
|
Sys_Printf( "passageOnly = true\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
passageVisOnly = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( !strcmp( argv[i],"-nosort" ) ) {
|
|
|
|
Sys_Printf( "nosort = true\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
nosort = qtrue;
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
else if ( !strcmp( argv[i],"-saveprt" ) ) {
|
|
|
|
Sys_Printf( "saveprt = true\n" );
|
2007-11-04 03:34:51 +00:00
|
|
|
saveprt = qtrue;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !strcmp( argv[i],"-tmpin" ) ) {
|
|
|
|
strcpy( inbase, "/tmp" );
|
|
|
|
}
|
|
|
|
else if ( !strcmp( argv[i],"-tmpout" ) ) {
|
|
|
|
strcpy( outbase, "/tmp" );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar: -hint to merge all but hint portals */
|
2012-03-17 20:01:54 +00:00
|
|
|
else if ( !strcmp( argv[ i ], "-hint" ) ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
Sys_Printf( "hint = true\n" );
|
|
|
|
hint = qtrue;
|
|
|
|
mergevis = qtrue;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
else{
|
2007-11-04 03:34:51 +00:00
|
|
|
Sys_Printf( "WARNING: Unknown option \"%s\"\n", argv[ i ] );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
}
|
|
|
|
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( i != argc - 1 ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
Error( "usage: vis [-threads #] [-level 0-4] [-fast] [-v] bspfile" );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* load the bsp */
|
|
|
|
sprintf( source, "%s%s", inbase, ExpandArg( argv[ i ] ) );
|
|
|
|
StripExtension( source );
|
|
|
|
strcat( source, ".bsp" );
|
|
|
|
Sys_Printf( "Loading %s\n", source );
|
|
|
|
LoadBSPFile( source );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* load the portal file */
|
|
|
|
sprintf( portalfile, "%s%s", inbase, ExpandArg( argv[ i ] ) );
|
|
|
|
StripExtension( portalfile );
|
|
|
|
strcat( portalfile, ".prt" );
|
|
|
|
Sys_Printf( "Loading %s\n", portalfile );
|
|
|
|
LoadPortals( portalfile );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2008-07-25 07:31:37 +00:00
|
|
|
/* ydnar: exit if no portals, hence no vis */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( numportals == 0 ) {
|
2008-07-25 07:31:37 +00:00
|
|
|
Sys_Printf( "No portals means no vis, exiting.\n" );
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* ydnar: for getting far plane */
|
|
|
|
ParseEntities();
|
2012-03-17 20:01:54 +00:00
|
|
|
|
|
|
|
if ( mergevis ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
MergeLeaves();
|
|
|
|
MergeLeafPortals();
|
|
|
|
}
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
CountActivePortals();
|
|
|
|
/* WritePortals( "maps/hints.prs" );*/
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
Sys_Printf( "visdatasize:%i\n", numBSPVisBytes );
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
CalcVis();
|
2012-03-17 20:01:54 +00:00
|
|
|
|
2007-11-04 03:34:51 +00:00
|
|
|
/* delete the prt file */
|
2012-03-17 20:01:54 +00:00
|
|
|
if ( !saveprt ) {
|
2007-11-04 03:34:51 +00:00
|
|
|
remove( portalfile );
|
2012-03-17 20:01:54 +00:00
|
|
|
}
|
2007-11-04 03:34:51 +00:00
|
|
|
|
|
|
|
/* write the bsp file */
|
|
|
|
Sys_Printf( "Writing %s\n", source );
|
|
|
|
WriteBSPFile( source );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|