1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ----------------------------------------------------------------------------------
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
27 ------------------------------------------------------------------------------- */
42 void PlaneFromWinding( fixedWinding_t *w, visPlane_t *plane ){
46 VectorSubtract( w->points[2], w->points[1], v1 );
47 VectorSubtract( w->points[0], w->points[1], v2 );
48 CrossProduct( v2, v1, plane->normal );
49 VectorNormalize( plane->normal, plane->normal );
50 plane->dist = DotProduct( w->points[0], plane->normal );
56 returns a new fixed winding
57 ydnar: altered this a bit to reconcile multiply-defined winding_t
60 fixedWinding_t *NewFixedWinding( int points ){
64 if ( points > MAX_POINTS_ON_WINDING ) {
65 Error( "NewWinding: %i points", points );
68 size = (int)( (size_t)( (fixedWinding_t *)0 )->points[points] );
69 w = safe_malloc( size );
77 void prl( leaf_t *l ){
82 for ( i = 0 ; i < l->numportals ; i++ )
86 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] );
91 //=============================================================================
97 Sorts the portals from the least complex, so the later ones can reuse
98 the earlier information.
101 int PComp( const void *a, const void *b ){
102 if ( ( *(const vportal_t *const *)a )->nummightsee == ( *(const vportal_t *const *)b )->nummightsee ) {
105 if ( ( *(const vportal_t *const *)a )->nummightsee < ( *(const vportal_t *const *)b )->nummightsee ) {
110 void SortPortals( void ){
113 for ( i = 0 ; i < numportals * 2 ; i++ )
114 sorted_portals[i] = &portals[i];
119 qsort( sorted_portals, numportals * 2, sizeof( sorted_portals[0] ), PComp );
125 LeafVectorFromPortalVector
128 int LeafVectorFromPortalVector( byte *portalbits, byte *leafbits ){
134 for ( i = 0 ; i < numportals * 2 ; i++ )
136 if ( portalbits[i >> 3] & ( 1 << ( i & 7 ) ) ) {
138 leafbits[p->leaf >> 3] |= ( 1 << ( p->leaf & 7 ) );
142 for ( j = 0; j < portalclusters; j++ )
145 while ( leafs[leafnum].merged >= 0 )
146 leafnum = leafs[leafnum].merged;
147 //if the merged leaf is visible then the original leaf is visible
148 if ( leafbits[leafnum >> 3] & ( 1 << ( leafnum & 7 ) ) ) {
149 leafbits[j >> 3] |= ( 1 << ( j & 7 ) );
153 c_leafs = CountBits( leafbits, portalclusters );
163 Merges the portal visibility for a leaf
167 static int clustersizehistogram[MAX_MAP_LEAFS] = {0};
169 void ClusterMerge( int leafnum ){
171 byte portalvector[MAX_PORTALS / 8];
172 byte uncompressed[MAX_MAP_LEAFS / 8];
174 int numvis, mergedleafnum;
178 // OR together all the portalvis bits
180 mergedleafnum = leafnum;
181 while ( leafs[mergedleafnum].merged >= 0 )
182 mergedleafnum = leafs[mergedleafnum].merged;
184 memset( portalvector, 0, portalbytes );
185 leaf = &leafs[mergedleafnum];
186 for ( i = 0; i < leaf->numportals; i++ )
188 p = leaf->portals[i];
193 if ( p->status != stat_done ) {
194 Error( "portal not done" );
196 for ( j = 0 ; j < portallongs ; j++ )
197 ( (long *)portalvector )[j] |= ( (long *)p->portalvis )[j];
199 portalvector[pnum >> 3] |= 1 << ( pnum & 7 );
202 memset( uncompressed, 0, leafbytes );
204 uncompressed[mergedleafnum >> 3] |= ( 1 << ( mergedleafnum & 7 ) );
205 // convert portal bits to leaf bits
206 numvis = LeafVectorFromPortalVector( portalvector, uncompressed );
208 // if (uncompressed[leafnum>>3] & (1<<(leafnum&7)))
209 // Sys_FPrintf (SYS_WRN, "WARNING: Leaf portals saw into leaf\n");
211 // uncompressed[leafnum>>3] |= (1<<(leafnum&7));
213 numvis++; // count the leaf itself
215 //Sys_FPrintf( SYS_VRB,"cluster %4i : %4i visible\n", leafnum, numvis );
216 ++clustersizehistogram[numvis];
218 memcpy( bspVisBytes + VIS_HEADER_SIZE + leafnum * leafbytes, uncompressed, leafbytes );
226 void CalcPortalVis( void ){
228 Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
229 //get rid of the counter
230 RunThreadsOnIndividual( numportals * 2, qfalse, PortalFlow );
232 RunThreadsOnIndividual( numportals * 2, qtrue, PortalFlow );
242 void CalcPassageVis( void ){
246 _printf( "%6d portals out of %d", 0, numportals * 2 );
247 RunThreadsOnIndividual( numportals * 2, qfalse, CreatePassages );
249 _printf( "%6d portals out of %d", 0, numportals * 2 );
250 RunThreadsOnIndividual( numportals * 2, qfalse, PassageFlow );
253 Sys_Printf( "\n--- CreatePassages (%d) ---\n", numportals * 2 );
254 RunThreadsOnIndividual( numportals * 2, qtrue, CreatePassages );
256 Sys_Printf( "\n--- PassageFlow (%d) ---\n", numportals * 2 );
257 RunThreadsOnIndividual( numportals * 2, qtrue, PassageFlow );
266 void CalcPassagePortalVis( void ){
270 Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
271 RunThreadsOnIndividual( numportals * 2, qfalse, CreatePassages );
273 Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
274 RunThreadsOnIndividual( numportals * 2, qfalse, PassagePortalFlow );
277 Sys_Printf( "\n--- CreatePassages (%d) ---\n", numportals * 2 );
278 RunThreadsOnIndividual( numportals * 2, qtrue, CreatePassages );
280 Sys_Printf( "\n--- PassagePortalFlow (%d) ---\n", numportals * 2 );
281 RunThreadsOnIndividual( numportals * 2, qtrue, PassagePortalFlow );
290 void CalcFastVis( void ){
293 // fastvis just uses mightsee for a very loose bound
294 for ( i = 0 ; i < numportals * 2 ; i++ )
296 portals[i].portalvis = portals[i].portalflood;
297 portals[i].status = stat_done;
306 void CalcVis( void ){
307 int i, minvis, maxvis;
309 double mu, sigma, totalvis, totalvis2;
312 /* ydnar: rr2do2's farplane code */
314 value = ValueForKey( &entities[ 0 ], "_farplanedist" ); /* proper '_' prefixed key */
315 if ( value[ 0 ] == '\0' ) {
316 value = ValueForKey( &entities[ 0 ], "fogclip" ); /* wolf compatibility */
318 if ( value[ 0 ] == '\0' ) {
319 value = ValueForKey( &entities[ 0 ], "distancecull" ); /* sof2 compatibility */
321 if ( value[ 0 ] != '\0' ) {
322 farPlaneDist = atof( value );
323 farPlaneDistMode = value[strlen(value) - 1 ];
324 if ( farPlaneDist != 0.0f ) {
325 Sys_Printf( "farplane distance = %.1f\n", farPlaneDist );
327 if ( farPlaneDist != 0.0f && farPlaneDistMode == 'o' ) {
328 Sys_Printf( "farplane Origin2Origin mode on\n" );
330 if ( farPlaneDist != 0.0f && farPlaneDistMode == 'r' ) {
331 Sys_Printf( "farplane Radius+Radius mode on\n" );
333 if ( farPlaneDist != 0.0f && farPlaneDistMode == 'e' ) {
334 Sys_Printf( "farplane Exact distance mode on\n" );
342 Sys_Printf( "\n--- BasePortalVis (%d) ---\n", numportals * 2 );
343 RunThreadsOnIndividual( numportals * 2, qtrue, BasePortalVis );
345 // RunThreadsOnIndividual (numportals*2, qtrue, BetterPortalVis);
352 else if ( noPassageVis ) {
355 else if ( passageVisOnly ) {
359 CalcPassagePortalVis();
362 // assemble the leaf vis lists by oring and compressing the portal lists
364 Sys_Printf( "creating leaf vis...\n" );
365 for ( i = 0 ; i < portalclusters ; i++ )
372 for ( i = 0; i < MAX_MAP_LEAFS; ++i )
373 if ( clustersizehistogram[i] ) {
374 if ( debugCluster ) {
375 Sys_FPrintf( SYS_VRB, "%4i clusters have exactly %4i visible clusters\n", clustersizehistogram[i], i );
377 /* cast is to prevent integer overflow */
378 totalvis += ( (double) i ) * ( (double) clustersizehistogram[i] );
379 totalvis2 += ( (double) i ) * ( (double) i ) * ( (double) clustersizehistogram[i] );
387 mu = totalvis / portalclusters;
388 sigma = sqrt( totalvis2 / portalclusters - mu * mu );
390 Sys_Printf( "Total clusters: %i\n", portalclusters );
391 Sys_Printf( "Total visible clusters: %.0f\n", totalvis );
392 Sys_Printf( "Average clusters visible: %.2f (%.3f%%/total)\n", mu, mu / portalclusters * 100.0 );
393 Sys_Printf( " Standard deviation: %.2f (%.3f%%/total, %.3f%%/avg)\n", sigma, sigma / portalclusters * 100.0, sigma / mu * 100.0 );
394 Sys_Printf( " Minimum: %i (%.3f%%/total, %.3f%%/avg)\n", minvis, minvis / (double) portalclusters * 100.0, minvis / mu * 100.0 );
395 Sys_Printf( " Maximum: %i (%.3f%%/total, %.3f%%/avg)\n", maxvis, maxvis / (double) portalclusters * 100.0, maxvis / mu * 100.0 );
403 void SetPortalSphere( vportal_t *p ){
410 VectorCopy( vec3_origin, total );
411 for ( i = 0 ; i < w->numpoints ; i++ )
413 VectorAdd( total, w->points[i], total );
416 for ( i = 0 ; i < 3 ; i++ )
417 total[i] /= w->numpoints;
420 for ( i = 0 ; i < w->numpoints ; i++ )
422 VectorSubtract( w->points[i], total, dist );
423 r = VectorLength( dist );
428 VectorCopy( total, p->origin );
434 Winding_PlanesConcave
437 #define WCONVEX_EPSILON 0.2
439 int Winding_PlanesConcave( fixedWinding_t *w1, fixedWinding_t *w2,
440 vec3_t normal1, vec3_t normal2,
441 float dist1, float dist2 ){
448 // check if one of the points of winding 1 is at the front of the plane of winding 2
449 for ( i = 0; i < w1->numpoints; i++ )
451 if ( DotProduct( normal2, w1->points[i] ) - dist2 > WCONVEX_EPSILON ) {
455 // check if one of the points of winding 2 is at the front of the plane of winding 1
456 for ( i = 0; i < w2->numpoints; i++ )
458 if ( DotProduct( normal1, w2->points[i] ) - dist1 > WCONVEX_EPSILON ) {
471 int TryMergeLeaves( int l1num, int l2num ){
472 int i, j, k, n, numportals;
473 visPlane_t plane1, plane2;
476 vportal_t *portals[MAX_PORTALS_ON_LEAF];
478 for ( k = 0; k < 2; k++ )
483 else{l1 = &faceleafs[l1num]; }
484 for ( i = 0; i < l1->numportals; i++ )
487 if ( p1->leaf == l2num ) {
490 for ( n = 0; n < 2; n++ )
495 else{l2 = &faceleafs[l2num]; }
496 for ( j = 0; j < l2->numportals; j++ )
499 if ( p2->leaf == l1num ) {
505 if ( Winding_PlanesConcave( p1->winding, p2->winding, plane1.normal, plane2.normal, plane1.dist, plane2.dist ) ) {
512 for ( k = 0; k < 2; k++ )
520 l1 = &faceleafs[l1num];
521 l2 = &faceleafs[l2num];
524 //the leaves can be merged now
525 for ( i = 0; i < l1->numportals; i++ )
528 if ( p1->leaf == l2num ) {
532 portals[numportals++] = p1;
534 for ( j = 0; j < l2->numportals; j++ )
537 if ( p2->leaf == l1num ) {
541 portals[numportals++] = p2;
543 for ( i = 0; i < numportals; i++ )
545 l2->portals[i] = portals[i];
547 l2->numportals = numportals;
558 void UpdatePortals( void ){
562 for ( i = 0; i < numportals * 2; i++ )
568 while ( leafs[p->leaf].merged >= 0 )
569 p->leaf = leafs[p->leaf].merged;
577 try to merge leaves but don't merge through hint splitters
580 void MergeLeaves( void ){
581 int i, j, nummerges, totalnummerges;
589 for ( i = 0; i < portalclusters; i++ )
592 //if this leaf is merged already
594 /* ydnar: vmods: merge all non-hint portals */
595 if ( leaf->merged >= 0 && hint == qfalse ) {
600 for ( j = 0; j < leaf->numportals; j++ )
602 p = leaf->portals[j];
607 //never merge through hint portals
611 if ( TryMergeLeaves( i, p->leaf ) ) {
618 totalnummerges += nummerges;
619 } while ( nummerges );
620 Sys_Printf( "%6d leaves merged\n", totalnummerges );
628 #define CONTINUOUS_EPSILON 0.005
630 fixedWinding_t *TryMergeWinding( fixedWinding_t *f1, fixedWinding_t *f2, vec3_t planenormal ){
631 vec_t *p1, *p2, *p3, *p4, *back;
632 fixedWinding_t *newf;
634 vec3_t normal, delta;
636 qboolean keep1, keep2;
640 // find a common edge
642 p1 = p2 = NULL; // stop compiler warning
645 for ( i = 0; i < f1->numpoints; i++ )
648 p2 = f1->points[( i + 1 ) % f1->numpoints];
649 for ( j = 0; j < f2->numpoints; j++ )
652 p4 = f2->points[( j + 1 ) % f2->numpoints];
653 for ( k = 0; k < 3; k++ )
655 if ( fabs( p1[k] - p4[k] ) > 0.1 ) { //EQUAL_EPSILON) //ME
658 if ( fabs( p2[k] - p3[k] ) > 0.1 ) { //EQUAL_EPSILON) //ME
666 if ( j < f2->numpoints ) {
671 if ( i == f1->numpoints ) {
672 return NULL; // no matching edges
676 // check slope of connected lines
677 // if the slopes are colinear, the point can be removed
679 back = f1->points[( i + f1->numpoints - 1 ) % f1->numpoints];
680 VectorSubtract( p1, back, delta );
681 CrossProduct( planenormal, delta, normal );
682 VectorNormalize( normal, normal );
684 back = f2->points[( j + 2 ) % f2->numpoints];
685 VectorSubtract( back, p1, delta );
686 dot = DotProduct( delta, normal );
687 if ( dot > CONTINUOUS_EPSILON ) {
688 return NULL; // not a convex polygon
690 keep1 = (qboolean)( dot < -CONTINUOUS_EPSILON );
692 back = f1->points[( i + 2 ) % f1->numpoints];
693 VectorSubtract( back, p2, delta );
694 CrossProduct( planenormal, delta, normal );
695 VectorNormalize( normal, normal );
697 back = f2->points[( j + f2->numpoints - 1 ) % f2->numpoints];
698 VectorSubtract( back, p2, delta );
699 dot = DotProduct( delta, normal );
700 if ( dot > CONTINUOUS_EPSILON ) {
701 return NULL; // not a convex polygon
703 keep2 = (qboolean)( dot < -CONTINUOUS_EPSILON );
706 // build the new polygon
708 newf = NewFixedWinding( f1->numpoints + f2->numpoints );
710 // copy first polygon
711 for ( k = ( i + 1 ) % f1->numpoints ; k != i ; k = ( k + 1 ) % f1->numpoints )
713 if ( k == ( i + 1 ) % f1->numpoints && !keep2 ) {
717 VectorCopy( f1->points[k], newf->points[newf->numpoints] );
721 // copy second polygon
722 for ( l = ( j + 1 ) % f2->numpoints ; l != j ; l = ( l + 1 ) % f2->numpoints )
724 if ( l == ( j + 1 ) % f2->numpoints && !keep1 ) {
727 VectorCopy( f2->points[l], newf->points[newf->numpoints] );
739 void MergeLeafPortals( void ){
740 int i, j, k, nummerges, hintsmerged;
747 for ( i = 0; i < portalclusters; i++ )
750 if ( leaf->merged >= 0 ) {
753 for ( j = 0; j < leaf->numportals; j++ )
755 p1 = leaf->portals[j];
759 for ( k = j + 1; k < leaf->numportals; k++ )
761 p2 = leaf->portals[k];
765 if ( p1->leaf == p2->leaf ) {
766 w = TryMergeWinding( p1->winding, p2->winding, p1->plane.normal );
768 free( p1->winding ); //% FreeWinding(p1->winding);
770 if ( p1->hint && p2->hint ) {
773 p1->hint |= p2->hint;
774 SetPortalSphere( p1 );
782 if ( k < leaf->numportals ) {
787 Sys_Printf( "%6d portals merged\n", nummerges );
788 Sys_Printf( "%6d hint portals merged\n", hintsmerged );
797 int CountActivePortals( void ){
803 for ( j = 0; j < numportals * 2; j++ )
814 Sys_Printf( "%6d active portals\n", num );
815 Sys_Printf( "%6d hint portals\n", hints );
824 void WriteFloat( FILE *f, vec_t v );
826 void WritePortals( char *portalpathfile ){
833 pf = fopen( portalpathfile, "w" );
835 Error( "Error opening %s", portalpathfile );
839 for ( j = 0; j < numportals * 2; j++ )
850 fprintf( pf, "%s\n", PORTALFILE );
851 fprintf( pf, "%i\n", 0 );
852 fprintf( pf, "%i\n", num ); // + numfaces);
853 fprintf( pf, "%i\n", 0 );
855 for ( j = 0; j < numportals * 2; j++ )
864 fprintf( pf,"%i %i %i ",w->numpoints, 0, 0 );
865 fprintf( pf, "%d ", p->hint );
866 for ( i = 0 ; i < w->numpoints ; i++ )
869 WriteFloat( pf, w->points[i][0] );
870 WriteFloat( pf, w->points[i][1] );
871 WriteFloat( pf, w->points[i][2] );
878 for (j = 0; j < numfaces; j++)
882 fprintf (pf,"%i %i %i ",w->numpoints, 0, 0);
884 for (i=0 ; i<w->numpoints ; i++)
887 WriteFloat (pf, w->points[i][0]);
888 WriteFloat (pf, w->points[i][1]);
889 WriteFloat (pf, w->points[i][2]);
903 void LoadPortals( char *name ){
914 if ( !strcmp( name,"-" ) ) {
919 f = fopen( name, "r" );
921 Error( "LoadPortals: couldn't read %s\n",name );
925 if ( fscanf( f,"%79s\n%i\n%i\n%i\n",magic, &portalclusters, &numportals, &numfaces ) != 4 ) {
926 Error( "LoadPortals: failed to read header" );
928 if ( strcmp( magic,PORTALFILE ) ) {
929 Error( "LoadPortals: not a portal file" );
932 Sys_Printf( "%6i portalclusters\n", portalclusters );
933 Sys_Printf( "%6i numportals\n", numportals );
934 Sys_Printf( "%6i numfaces\n", numfaces );
936 if ( numportals > MAX_PORTALS ) {
937 Error( "MAX_PORTALS" );
940 // these counts should take advantage of 64 bit systems automatically
941 leafbytes = ( ( portalclusters + 63 ) & ~63 ) >> 3;
942 leaflongs = leafbytes / sizeof( long );
944 portalbytes = ( ( numportals * 2 + 63 ) & ~63 ) >> 3;
945 portallongs = portalbytes / sizeof( long );
947 // each file portal is split into two memory portals
948 portals = safe_malloc( 2 * numportals * sizeof( vportal_t ) );
949 memset( portals, 0, 2 * numportals * sizeof( vportal_t ) );
951 leafs = safe_malloc( portalclusters * sizeof( leaf_t ) );
952 memset( leafs, 0, portalclusters * sizeof( leaf_t ) );
954 for ( i = 0; i < portalclusters; i++ )
955 leafs[i].merged = -1;
957 numBSPVisBytes = VIS_HEADER_SIZE + portalclusters * leafbytes;
959 if ( numBSPVisBytes > MAX_MAP_VISIBILITY ) {
960 Error( "MAX_MAP_VISIBILITY exceeded" );
963 ( (int *)bspVisBytes )[0] = portalclusters;
964 ( (int *)bspVisBytes )[1] = leafbytes;
966 for ( i = 0, p = portals ; i < numportals ; i++ )
968 if ( fscanf( f, "%i %i %i ", &numpoints, &leafnums[0], &leafnums[1] ) != 3 ) {
969 Error( "LoadPortals: reading portal %i", i );
971 if ( numpoints > MAX_POINTS_ON_WINDING ) {
972 Error( "LoadPortals: portal %i has too many points", i );
974 if ( leafnums[0] > portalclusters
975 || leafnums[1] > portalclusters ) {
976 Error( "LoadPortals: reading portal %i", i );
978 if ( fscanf( f, "%i ", &flags ) != 1 ) {
979 Error( "LoadPortals: reading flags" );
982 w = p->winding = NewFixedWinding( numpoints );
983 w->numpoints = numpoints;
985 for ( j = 0 ; j < numpoints ; j++ )
990 // scanf into double, then assign to vec_t
991 // so we don't care what size vec_t is
992 if ( fscanf( f, "(%lf %lf %lf ) "
993 , &v[0], &v[1], &v[2] ) != 3 ) {
994 Error( "LoadPortals: reading portal %i", i );
996 for ( k = 0 ; k < 3 ; k++ )
997 w->points[j][k] = v[k];
999 if ( fscanf( f, "\n" ) != 0 ) {
1000 // silence gcc warning
1004 PlaneFromWinding( w, &plane );
1006 // create forward portal
1007 l = &leafs[leafnums[0]];
1008 if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
1009 Error( "Leaf with too many portals" );
1011 l->portals[l->numportals] = p;
1015 p->hint = ((flags & 1) != 0);
1016 p->sky = ((flags & 2) != 0);
1018 VectorSubtract( vec3_origin, plane.normal, p->plane.normal );
1019 p->plane.dist = -plane.dist;
1020 p->leaf = leafnums[1];
1021 SetPortalSphere( p );
1024 // create backwards portal
1025 l = &leafs[leafnums[1]];
1026 if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
1027 Error( "Leaf with too many portals" );
1029 l->portals[l->numportals] = p;
1034 p->winding = NewFixedWinding( w->numpoints );
1035 p->winding->numpoints = w->numpoints;
1036 for ( j = 0 ; j < w->numpoints ; j++ )
1038 VectorCopy( w->points[w->numpoints - 1 - j], p->winding->points[j] );
1042 p->leaf = leafnums[0];
1043 SetPortalSphere( p );
1048 faces = safe_malloc( 2 * numfaces * sizeof( vportal_t ) );
1049 memset( faces, 0, 2 * numfaces * sizeof( vportal_t ) );
1051 faceleafs = safe_malloc( portalclusters * sizeof( leaf_t ) );
1052 memset( faceleafs, 0, portalclusters * sizeof( leaf_t ) );
1054 for ( i = 0, p = faces; i < numfaces; i++ )
1056 if ( fscanf( f, "%i %i ", &numpoints, &leafnums[0] ) != 2 ) {
1057 Error( "LoadPortals: reading portal %i", i );
1060 w = p->winding = NewFixedWinding( numpoints );
1061 w->numpoints = numpoints;
1063 for ( j = 0 ; j < numpoints ; j++ )
1068 // scanf into double, then assign to vec_t
1069 // so we don't care what size vec_t is
1070 if ( fscanf( f, "(%lf %lf %lf ) "
1071 , &v[0], &v[1], &v[2] ) != 3 ) {
1072 Error( "LoadPortals: reading portal %i", i );
1074 for ( k = 0 ; k < 3 ; k++ )
1075 w->points[j][k] = v[k];
1077 if ( fscanf( f, "\n" ) != 0 ) {
1078 // silence gcc warning
1082 PlaneFromWinding( w, &plane );
1084 l = &faceleafs[leafnums[0]];
1086 if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
1087 Error( "Leaf with too many faces" );
1089 l->portals[l->numportals] = p;
1094 // normal pointing out of the leaf
1095 VectorSubtract( vec3_origin, plane.normal, p->plane.normal );
1096 p->plane.dist = -plane.dist;
1098 SetPortalSphere( p );
1112 int VisMain( int argc, char **argv ){
1114 char portalFilePath[ 1024 ];
1115 portalFilePath[0] = 0;
1119 Sys_Printf( "--- Vis ---\n" );
1121 /* process arguments */
1122 for ( i = 1 ; i < ( argc - 1 ) ; i++ )
1124 if ( !strcmp( argv[i], "-fast" ) ) {
1125 Sys_Printf( "fastvis = true\n" );
1128 else if ( !strcmp( argv[i], "-merge" ) ) {
1129 Sys_Printf( "merge = true\n" );
1132 else if ( !strcmp( argv[i], "-mergeportals" ) ) {
1133 Sys_Printf( "mergeportals = true\n" );
1134 mergevisportals = qtrue;
1136 else if ( !strcmp( argv[i], "-nopassage" ) ) {
1137 Sys_Printf( "nopassage = true\n" );
1138 noPassageVis = qtrue;
1140 else if ( !strcmp( argv[i], "-passageOnly" ) ) {
1141 Sys_Printf( "passageOnly = true\n" );
1142 passageVisOnly = qtrue;
1144 else if ( !strcmp( argv[i],"-nosort" ) ) {
1145 Sys_Printf( "nosort = true\n" );
1148 else if ( !strcmp( argv[i],"-saveprt" ) ) {
1149 Sys_Printf( "saveprt = true\n" );
1152 else if ( !strcmp( argv[ i ], "-v" ) ) {
1153 debugCluster = qtrue;
1154 Sys_Printf( "Extra verbous mode enabled\n" );
1156 else if ( !strcmp( argv[i],"-tmpin" ) ) {
1157 strcpy( inbase, "/tmp" );
1159 else if ( !strcmp( argv[i],"-tmpout" ) ) {
1160 strcpy( outbase, "/tmp" );
1163 /* ydnar: -hint to merge all but hint portals */
1164 else if ( !strcmp( argv[ i ], "-hint" ) ) {
1165 Sys_Printf( "hint = true\n" );
1169 else if ( !strcmp( argv[ i ], "-prtfile" ) )
1171 strcpy( portalFilePath, argv[i + 1] );
1173 Sys_Printf( "Use %s as portal file\n", portalFilePath );
1177 Sys_FPrintf( SYS_WRN, "WARNING: Unknown option \"%s\"\n", argv[ i ] );
1181 if ( i != argc - 1 ) {
1182 Error( "usage: vis [-threads #] [-fast] [-v] BSPFilePath" );
1187 sprintf( source, "%s%s", inbase, ExpandArg( argv[ i ] ) );
1188 StripExtension( source );
1189 strcat( source, ".bsp" );
1190 Sys_Printf( "Loading %s\n", source );
1191 LoadBSPFile( source );
1193 /* load the portal file */
1194 if (!portalFilePath[0]) {
1195 sprintf( portalFilePath, "%s%s", inbase, ExpandArg( argv[ i ] ) );
1196 StripExtension( portalFilePath );
1197 strcat( portalFilePath, ".prt" );
1199 Sys_Printf( "Loading %s\n", portalFilePath );
1200 LoadPortals( portalFilePath );
1202 /* ydnar: exit if no portals, hence no vis */
1203 if ( numportals == 0 ) {
1204 Sys_Printf( "No portals means no vis, exiting.\n" );
1208 /* ydnar: for getting far plane */
1211 /* inject command line parameters */
1212 InjectCommandLine( argv, 0, argc - 1 );
1219 if ( mergevis || mergevisportals ) {
1223 CountActivePortals();
1224 /* WritePortals( "maps/hints.prs" );*/
1226 Sys_Printf( "visdatasize:%i\n", numBSPVisBytes );
1230 /* delete the prt file */
1232 remove( portalFilePath );
1235 /* write the bsp file */
1236 Sys_Printf( "Writing %s\n", source );
1237 WriteBSPFile( source );