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_malloc0( size );
76 void prl( leaf_t *l ){
81 for ( i = 0 ; i < l->numportals ; i++ )
85 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] );
90 //=============================================================================
96 Sorts the portals from the least complex, so the later ones can reuse
97 the earlier information.
100 int PComp( const void *a, const void *b ){
101 if ( ( *(const vportal_t *const *)a )->nummightsee == ( *(const vportal_t *const *)b )->nummightsee ) {
104 if ( ( *(const vportal_t *const *)a )->nummightsee < ( *(const vportal_t *const *)b )->nummightsee ) {
109 void SortPortals( void ){
112 for ( i = 0 ; i < numportals * 2 ; i++ )
113 sorted_portals[i] = &portals[i];
118 qsort( sorted_portals, numportals * 2, sizeof( sorted_portals[0] ), PComp );
124 LeafVectorFromPortalVector
127 int LeafVectorFromPortalVector( byte *portalbits, byte *leafbits ){
133 for ( i = 0 ; i < numportals * 2 ; i++ )
135 if ( portalbits[i >> 3] & ( 1 << ( i & 7 ) ) ) {
137 leafbits[p->leaf >> 3] |= ( 1 << ( p->leaf & 7 ) );
141 for ( j = 0; j < portalclusters; j++ )
144 while ( leafs[leafnum].merged >= 0 )
145 leafnum = leafs[leafnum].merged;
146 //if the merged leaf is visible then the original leaf is visible
147 if ( leafbits[leafnum >> 3] & ( 1 << ( leafnum & 7 ) ) ) {
148 leafbits[j >> 3] |= ( 1 << ( j & 7 ) );
152 c_leafs = CountBits( leafbits, portalclusters );
162 Merges the portal visibility for a leaf
166 static int clustersizehistogram[MAX_MAP_LEAFS] = {0};
168 void ClusterMerge( int leafnum ){
170 byte portalvector[MAX_PORTALS / 8];
171 byte uncompressed[MAX_MAP_LEAFS / 8];
173 int numvis, mergedleafnum;
177 // OR together all the portalvis bits
179 mergedleafnum = leafnum;
180 while ( leafs[mergedleafnum].merged >= 0 )
181 mergedleafnum = leafs[mergedleafnum].merged;
183 memset( portalvector, 0, portalbytes );
184 leaf = &leafs[mergedleafnum];
185 for ( i = 0; i < leaf->numportals; i++ )
187 p = leaf->portals[i];
192 if ( p->status != stat_done ) {
193 Error( "portal not done" );
195 for ( j = 0 ; j < portallongs ; j++ )
196 ( (long *)portalvector )[j] |= ( (long *)p->portalvis )[j];
198 portalvector[pnum >> 3] |= 1 << ( pnum & 7 );
201 memset( uncompressed, 0, leafbytes );
203 uncompressed[mergedleafnum >> 3] |= ( 1 << ( mergedleafnum & 7 ) );
204 // convert portal bits to leaf bits
205 numvis = LeafVectorFromPortalVector( portalvector, uncompressed );
207 // if (uncompressed[leafnum>>3] & (1<<(leafnum&7)))
208 // Sys_FPrintf (SYS_WRN, "WARNING: Leaf portals saw into leaf\n");
210 // uncompressed[leafnum>>3] |= (1<<(leafnum&7));
212 numvis++; // count the leaf itself
214 //Sys_FPrintf( SYS_VRB,"cluster %4i : %4i visible\n", leafnum, numvis );
215 ++clustersizehistogram[numvis];
217 memcpy( bspVisBytes + VIS_HEADER_SIZE + leafnum * leafbytes, uncompressed, leafbytes );
225 void CalcPortalVis( void ){
227 Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
228 //get rid of the counter
229 RunThreadsOnIndividual( numportals * 2, qfalse, PortalFlow );
231 RunThreadsOnIndividual( numportals * 2, qtrue, PortalFlow );
241 void CalcPassageVis( void ){
245 _printf( "%6d portals out of %d", 0, numportals * 2 );
246 RunThreadsOnIndividual( numportals * 2, qfalse, CreatePassages );
248 _printf( "%6d portals out of %d", 0, numportals * 2 );
249 RunThreadsOnIndividual( numportals * 2, qfalse, PassageFlow );
252 Sys_Printf( "\n--- CreatePassages (%d) ---\n", numportals * 2 );
253 RunThreadsOnIndividual( numportals * 2, qtrue, CreatePassages );
255 Sys_Printf( "\n--- PassageFlow (%d) ---\n", numportals * 2 );
256 RunThreadsOnIndividual( numportals * 2, qtrue, PassageFlow );
265 void CalcPassagePortalVis( void ){
269 Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
270 RunThreadsOnIndividual( numportals * 2, qfalse, CreatePassages );
272 Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
273 RunThreadsOnIndividual( numportals * 2, qfalse, PassagePortalFlow );
276 Sys_Printf( "\n--- CreatePassages (%d) ---\n", numportals * 2 );
277 RunThreadsOnIndividual( numportals * 2, qtrue, CreatePassages );
279 Sys_Printf( "\n--- PassagePortalFlow (%d) ---\n", numportals * 2 );
280 RunThreadsOnIndividual( numportals * 2, qtrue, PassagePortalFlow );
289 void CalcFastVis( void ){
292 // fastvis just uses mightsee for a very loose bound
293 for ( i = 0 ; i < numportals * 2 ; i++ )
295 portals[i].portalvis = portals[i].portalflood;
296 portals[i].status = stat_done;
305 void CalcVis( void ){
306 int i, minvis, maxvis;
308 double mu, sigma, totalvis, totalvis2;
311 /* ydnar: rr2do2's farplane code */
313 value = ValueForKey( &entities[ 0 ], "_farplanedist" ); /* proper '_' prefixed key */
314 if ( value[ 0 ] == '\0' ) {
315 value = ValueForKey( &entities[ 0 ], "fogclip" ); /* wolf compatibility */
317 if ( value[ 0 ] == '\0' ) {
318 value = ValueForKey( &entities[ 0 ], "distancecull" ); /* sof2 compatibility */
320 if ( value[ 0 ] != '\0' ) {
321 farPlaneDist = atof( value );
322 if ( farPlaneDist > 0.0f ) {
323 Sys_Printf( "farplane distance = %.1f\n", farPlaneDist );
332 Sys_Printf( "\n--- BasePortalVis (%d) ---\n", numportals * 2 );
333 RunThreadsOnIndividual( numportals * 2, qtrue, BasePortalVis );
335 // RunThreadsOnIndividual (numportals*2, qtrue, BetterPortalVis);
342 else if ( noPassageVis ) {
345 else if ( passageVisOnly ) {
349 CalcPassagePortalVis();
352 // assemble the leaf vis lists by oring and compressing the portal lists
354 Sys_Printf( "creating leaf vis...\n" );
355 for ( i = 0 ; i < portalclusters ; i++ )
362 for ( i = 0; i < MAX_MAP_LEAFS; ++i )
363 if ( clustersizehistogram[i] ) {
364 if ( debugCluster ) {
365 Sys_FPrintf( SYS_VRB, "%4i clusters have exactly %4i visible clusters\n", clustersizehistogram[i], i );
367 /* cast is to prevent integer overflow */
368 totalvis += ( (double) i ) * ( (double) clustersizehistogram[i] );
369 totalvis2 += ( (double) i ) * ( (double) i ) * ( (double) clustersizehistogram[i] );
377 mu = totalvis / portalclusters;
378 sigma = sqrt( totalvis2 / portalclusters - mu * mu );
380 Sys_Printf( "Total clusters: %i\n", portalclusters );
381 Sys_Printf( "Total visible clusters: %.0f\n", totalvis );
382 Sys_Printf( "Average clusters visible: %.2f (%.3f%%/total)\n", mu, mu / portalclusters * 100.0 );
383 Sys_Printf( " Standard deviation: %.2f (%.3f%%/total, %.3f%%/avg)\n", sigma, sigma / portalclusters * 100.0, sigma / mu * 100.0 );
384 Sys_Printf( " Minimum: %i (%.3f%%/total, %.3f%%/avg)\n", minvis, minvis / (double) portalclusters * 100.0, minvis / mu * 100.0 );
385 Sys_Printf( " Maximum: %i (%.3f%%/total, %.3f%%/avg)\n", maxvis, maxvis / (double) portalclusters * 100.0, maxvis / mu * 100.0 );
393 void SetPortalSphere( vportal_t *p ){
400 VectorCopy( vec3_origin, total );
401 for ( i = 0 ; i < w->numpoints ; i++ )
403 VectorAdd( total, w->points[i], total );
406 for ( i = 0 ; i < 3 ; i++ )
407 total[i] /= w->numpoints;
410 for ( i = 0 ; i < w->numpoints ; i++ )
412 VectorSubtract( w->points[i], total, dist );
413 r = VectorLength( dist );
418 VectorCopy( total, p->origin );
424 Winding_PlanesConcave
427 #define WCONVEX_EPSILON 0.2
429 int Winding_PlanesConcave( fixedWinding_t *w1, fixedWinding_t *w2,
430 vec3_t normal1, vec3_t normal2,
431 float dist1, float dist2 ){
438 // check if one of the points of winding 1 is at the front of the plane of winding 2
439 for ( i = 0; i < w1->numpoints; i++ )
441 if ( DotProduct( normal2, w1->points[i] ) - dist2 > WCONVEX_EPSILON ) {
445 // check if one of the points of winding 2 is at the front of the plane of winding 1
446 for ( i = 0; i < w2->numpoints; i++ )
448 if ( DotProduct( normal1, w2->points[i] ) - dist1 > WCONVEX_EPSILON ) {
461 int TryMergeLeaves( int l1num, int l2num ){
462 int i, j, k, n, numportals;
463 visPlane_t plane1, plane2;
466 vportal_t *portals[MAX_PORTALS_ON_LEAF];
468 for ( k = 0; k < 2; k++ )
473 else{l1 = &faceleafs[l1num]; }
474 for ( i = 0; i < l1->numportals; i++ )
477 if ( p1->leaf == l2num ) {
480 for ( n = 0; n < 2; n++ )
485 else{l2 = &faceleafs[l2num]; }
486 for ( j = 0; j < l2->numportals; j++ )
489 if ( p2->leaf == l1num ) {
495 if ( Winding_PlanesConcave( p1->winding, p2->winding, plane1.normal, plane2.normal, plane1.dist, plane2.dist ) ) {
502 for ( k = 0; k < 2; k++ )
510 l1 = &faceleafs[l1num];
511 l2 = &faceleafs[l2num];
514 //the leaves can be merged now
515 for ( i = 0; i < l1->numportals; i++ )
518 if ( p1->leaf == l2num ) {
522 portals[numportals++] = p1;
524 for ( j = 0; j < l2->numportals; j++ )
527 if ( p2->leaf == l1num ) {
531 portals[numportals++] = p2;
533 for ( i = 0; i < numportals; i++ )
535 l2->portals[i] = portals[i];
537 l2->numportals = numportals;
548 void UpdatePortals( void ){
552 for ( i = 0; i < numportals * 2; i++ )
558 while ( leafs[p->leaf].merged >= 0 )
559 p->leaf = leafs[p->leaf].merged;
567 try to merge leaves but don't merge through hint splitters
570 void MergeLeaves( void ){
571 int i, j, nummerges, totalnummerges;
579 for ( i = 0; i < portalclusters; i++ )
582 //if this leaf is merged already
584 /* ydnar: vmods: merge all non-hint portals */
585 if ( leaf->merged >= 0 && hint == qfalse ) {
590 for ( j = 0; j < leaf->numportals; j++ )
592 p = leaf->portals[j];
597 //never merge through hint portals
601 if ( TryMergeLeaves( i, p->leaf ) ) {
608 totalnummerges += nummerges;
609 } while ( nummerges );
610 Sys_Printf( "%6d leaves merged\n", totalnummerges );
618 #define CONTINUOUS_EPSILON 0.005
620 fixedWinding_t *TryMergeWinding( fixedWinding_t *f1, fixedWinding_t *f2, vec3_t planenormal ){
621 vec_t *p1, *p2, *p3, *p4, *back;
622 fixedWinding_t *newf;
624 vec3_t normal, delta;
626 qboolean keep1, keep2;
630 // find a common edge
632 p1 = p2 = NULL; // stop compiler warning
635 for ( i = 0; i < f1->numpoints; i++ )
638 p2 = f1->points[( i + 1 ) % f1->numpoints];
639 for ( j = 0; j < f2->numpoints; j++ )
642 p4 = f2->points[( j + 1 ) % f2->numpoints];
643 for ( k = 0; k < 3; k++ )
645 if ( fabs( p1[k] - p4[k] ) > 0.1 ) { //EQUAL_EPSILON) //ME
648 if ( fabs( p2[k] - p3[k] ) > 0.1 ) { //EQUAL_EPSILON) //ME
656 if ( j < f2->numpoints ) {
661 if ( i == f1->numpoints ) {
662 return NULL; // no matching edges
666 // check slope of connected lines
667 // if the slopes are colinear, the point can be removed
669 back = f1->points[( i + f1->numpoints - 1 ) % f1->numpoints];
670 VectorSubtract( p1, back, delta );
671 CrossProduct( planenormal, delta, normal );
672 VectorNormalize( normal, normal );
674 back = f2->points[( j + 2 ) % f2->numpoints];
675 VectorSubtract( back, p1, delta );
676 dot = DotProduct( delta, normal );
677 if ( dot > CONTINUOUS_EPSILON ) {
678 return NULL; // not a convex polygon
680 keep1 = (qboolean)( dot < -CONTINUOUS_EPSILON );
682 back = f1->points[( i + 2 ) % f1->numpoints];
683 VectorSubtract( back, p2, delta );
684 CrossProduct( planenormal, delta, normal );
685 VectorNormalize( normal, normal );
687 back = f2->points[( j + f2->numpoints - 1 ) % f2->numpoints];
688 VectorSubtract( back, p2, delta );
689 dot = DotProduct( delta, normal );
690 if ( dot > CONTINUOUS_EPSILON ) {
691 return NULL; // not a convex polygon
693 keep2 = (qboolean)( dot < -CONTINUOUS_EPSILON );
696 // build the new polygon
698 newf = NewFixedWinding( f1->numpoints + f2->numpoints );
700 // copy first polygon
701 for ( k = ( i + 1 ) % f1->numpoints ; k != i ; k = ( k + 1 ) % f1->numpoints )
703 if ( k == ( i + 1 ) % f1->numpoints && !keep2 ) {
707 VectorCopy( f1->points[k], newf->points[newf->numpoints] );
711 // copy second polygon
712 for ( l = ( j + 1 ) % f2->numpoints ; l != j ; l = ( l + 1 ) % f2->numpoints )
714 if ( l == ( j + 1 ) % f2->numpoints && !keep1 ) {
717 VectorCopy( f2->points[l], newf->points[newf->numpoints] );
729 void MergeLeafPortals( void ){
730 int i, j, k, nummerges, hintsmerged;
737 for ( i = 0; i < portalclusters; i++ )
740 if ( leaf->merged >= 0 ) {
743 for ( j = 0; j < leaf->numportals; j++ )
745 p1 = leaf->portals[j];
749 for ( k = j + 1; k < leaf->numportals; k++ )
751 p2 = leaf->portals[k];
755 if ( p1->leaf == p2->leaf ) {
756 w = TryMergeWinding( p1->winding, p2->winding, p1->plane.normal );
758 free( p1->winding ); //% FreeWinding(p1->winding);
760 if ( p1->hint && p2->hint ) {
763 p1->hint |= p2->hint;
764 SetPortalSphere( p1 );
772 if ( k < leaf->numportals ) {
777 Sys_Printf( "%6d portals merged\n", nummerges );
778 Sys_Printf( "%6d hint portals merged\n", hintsmerged );
787 int CountActivePortals( void ){
793 for ( j = 0; j < numportals * 2; j++ )
804 Sys_Printf( "%6d active portals\n", num );
805 Sys_Printf( "%6d hint portals\n", hints );
814 void WriteFloat( FILE *f, vec_t v );
816 void WritePortals( char *portalpathfile ){
823 pf = fopen( portalpathfile, "w" );
825 Error( "Error opening %s", portalpathfile );
829 for ( j = 0; j < numportals * 2; j++ )
840 fprintf( pf, "%s\n", PORTALFILE );
841 fprintf( pf, "%i\n", 0 );
842 fprintf( pf, "%i\n", num ); // + numfaces);
843 fprintf( pf, "%i\n", 0 );
845 for ( j = 0; j < numportals * 2; j++ )
854 fprintf( pf,"%i %i %i ",w->numpoints, 0, 0 );
855 fprintf( pf, "%d ", p->hint );
856 for ( i = 0 ; i < w->numpoints ; i++ )
859 WriteFloat( pf, w->points[i][0] );
860 WriteFloat( pf, w->points[i][1] );
861 WriteFloat( pf, w->points[i][2] );
868 for (j = 0; j < numfaces; j++)
872 fprintf (pf,"%i %i %i ",w->numpoints, 0, 0);
874 for (i=0 ; i<w->numpoints ; i++)
877 WriteFloat (pf, w->points[i][0]);
878 WriteFloat (pf, w->points[i][1]);
879 WriteFloat (pf, w->points[i][2]);
893 void LoadPortals( char *name ){
904 if ( !strcmp( name,"-" ) ) {
909 f = fopen( name, "r" );
911 Error( "LoadPortals: couldn't read %s\n",name );
915 if ( fscanf( f,"%79s\n%i\n%i\n%i\n",magic, &portalclusters, &numportals, &numfaces ) != 4 ) {
916 Error( "LoadPortals: failed to read header" );
918 if ( strcmp( magic,PORTALFILE ) ) {
919 Error( "LoadPortals: not a portal file" );
922 Sys_Printf( "%6i portalclusters\n", portalclusters );
923 Sys_Printf( "%6i numportals\n", numportals );
924 Sys_Printf( "%6i numfaces\n", numfaces );
926 if ( numportals > MAX_PORTALS ) {
927 Error( "MAX_PORTALS" );
930 // these counts should take advantage of 64 bit systems automatically
931 leafbytes = ( ( portalclusters + 63 ) & ~63 ) >> 3;
932 leaflongs = leafbytes / sizeof( long );
934 portalbytes = ( ( numportals * 2 + 63 ) & ~63 ) >> 3;
935 portallongs = portalbytes / sizeof( long );
937 // each file portal is split into two memory portals
938 portals = safe_malloc0( 2 * numportals * sizeof( vportal_t ) );
940 leafs = safe_malloc0( portalclusters * sizeof( leaf_t ) );
942 for ( i = 0; i < portalclusters; i++ )
943 leafs[i].merged = -1;
945 numBSPVisBytes = VIS_HEADER_SIZE + portalclusters * leafbytes;
947 if ( numBSPVisBytes > MAX_MAP_VISIBILITY ) {
948 Error( "MAX_MAP_VISIBILITY exceeded" );
951 ( (int *)bspVisBytes )[0] = portalclusters;
952 ( (int *)bspVisBytes )[1] = leafbytes;
954 for ( i = 0, p = portals ; i < numportals ; i++ )
956 if ( fscanf( f, "%i %i %i ", &numpoints, &leafnums[0], &leafnums[1] ) != 3 ) {
957 Error( "LoadPortals: reading portal %i", i );
959 if ( numpoints > MAX_POINTS_ON_WINDING ) {
960 Error( "LoadPortals: portal %i has too many points", i );
962 if ( leafnums[0] > portalclusters
963 || leafnums[1] > portalclusters ) {
964 Error( "LoadPortals: reading portal %i", i );
966 if ( fscanf( f, "%i ", &flags ) != 1 ) {
967 Error( "LoadPortals: reading flags" );
970 w = p->winding = NewFixedWinding( numpoints );
971 w->numpoints = numpoints;
973 for ( j = 0 ; j < numpoints ; j++ )
978 // scanf into double, then assign to vec_t
979 // so we don't care what size vec_t is
980 if ( fscanf( f, "(%lf %lf %lf ) "
981 , &v[0], &v[1], &v[2] ) != 3 ) {
982 Error( "LoadPortals: reading portal %i", i );
984 for ( k = 0 ; k < 3 ; k++ )
985 w->points[j][k] = v[k];
987 if ( fscanf( f, "\n" ) != 0 ) {
988 // silence gcc warning
992 PlaneFromWinding( w, &plane );
994 // create forward portal
995 l = &leafs[leafnums[0]];
996 if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
997 Error( "Leaf with too many portals" );
999 l->portals[l->numportals] = p;
1003 p->hint = ((flags & 1) != 0);
1004 p->sky = ((flags & 2) != 0);
1006 VectorSubtract( vec3_origin, plane.normal, p->plane.normal );
1007 p->plane.dist = -plane.dist;
1008 p->leaf = leafnums[1];
1009 SetPortalSphere( p );
1012 // create backwards portal
1013 l = &leafs[leafnums[1]];
1014 if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
1015 Error( "Leaf with too many portals" );
1017 l->portals[l->numportals] = p;
1022 p->winding = NewFixedWinding( w->numpoints );
1023 p->winding->numpoints = w->numpoints;
1024 for ( j = 0 ; j < w->numpoints ; j++ )
1026 VectorCopy( w->points[w->numpoints - 1 - j], p->winding->points[j] );
1030 p->leaf = leafnums[0];
1031 SetPortalSphere( p );
1036 faces = safe_malloc0( 2 * numfaces * sizeof( vportal_t ) );
1038 faceleafs = safe_malloc0( portalclusters * sizeof( leaf_t ) );
1040 for ( i = 0, p = faces; i < numfaces; i++ )
1042 if ( fscanf( f, "%i %i ", &numpoints, &leafnums[0] ) != 2 ) {
1043 Error( "LoadPortals: reading portal %i", i );
1046 w = p->winding = NewFixedWinding( numpoints );
1047 w->numpoints = numpoints;
1049 for ( j = 0 ; j < numpoints ; j++ )
1054 // scanf into double, then assign to vec_t
1055 // so we don't care what size vec_t is
1056 if ( fscanf( f, "(%lf %lf %lf ) "
1057 , &v[0], &v[1], &v[2] ) != 3 ) {
1058 Error( "LoadPortals: reading portal %i", i );
1060 for ( k = 0 ; k < 3 ; k++ )
1061 w->points[j][k] = v[k];
1063 if ( fscanf( f, "\n" ) != 0 ) {
1064 // silence gcc warning
1068 PlaneFromWinding( w, &plane );
1070 l = &faceleafs[leafnums[0]];
1072 if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
1073 Error( "Leaf with too many faces" );
1075 l->portals[l->numportals] = p;
1080 // normal pointing out of the leaf
1081 VectorSubtract( vec3_origin, plane.normal, p->plane.normal );
1082 p->plane.dist = -plane.dist;
1084 SetPortalSphere( p );
1098 int VisMain( int argc, char **argv ){
1100 char portalFilePath[ 1024 ];
1101 portalFilePath[0] = 0;
1105 Sys_Printf( "--- Vis ---\n" );
1107 /* process arguments */
1108 for ( i = 1 ; i < ( argc - 1 ) ; i++ )
1110 if ( !strcmp( argv[i], "-fast" ) ) {
1111 Sys_Printf( "fastvis = true\n" );
1114 else if ( !strcmp( argv[i], "-merge" ) ) {
1115 Sys_Printf( "merge = true\n" );
1118 else if ( !strcmp( argv[i], "-mergeportals" ) ) {
1119 Sys_Printf( "mergeportals = true\n" );
1120 mergevisportals = qtrue;
1122 else if ( !strcmp( argv[i], "-nopassage" ) ) {
1123 Sys_Printf( "nopassage = true\n" );
1124 noPassageVis = qtrue;
1126 else if ( !strcmp( argv[i], "-passageOnly" ) ) {
1127 Sys_Printf( "passageOnly = true\n" );
1128 passageVisOnly = qtrue;
1130 else if ( !strcmp( argv[i],"-nosort" ) ) {
1131 Sys_Printf( "nosort = true\n" );
1134 else if ( !strcmp( argv[i],"-saveprt" ) ) {
1135 Sys_Printf( "saveprt = true\n" );
1138 else if ( !strcmp( argv[ i ], "-v" ) ) {
1139 debugCluster = qtrue;
1140 Sys_Printf( "Extra verbose mode enabled\n" );
1142 else if ( !strcmp( argv[i],"-tmpin" ) ) {
1143 strcpy( inbase, "/tmp" );
1145 else if ( !strcmp( argv[i],"-tmpout" ) ) {
1146 strcpy( outbase, "/tmp" );
1149 /* ydnar: -hint to merge all but hint portals */
1150 else if ( !strcmp( argv[ i ], "-hint" ) ) {
1151 Sys_Printf( "hint = true\n" );
1155 else if ( !strcmp( argv[ i ], "-prtfile" ) )
1157 strcpy( portalFilePath, argv[i + 1] );
1159 Sys_Printf( "Use %s as portal file\n", portalFilePath );
1163 Sys_FPrintf( SYS_WRN, "WARNING: Unknown option \"%s\"\n", argv[ i ] );
1167 if ( i != argc - 1 ) {
1168 Error( "usage: vis [-threads #] [-fast] [-v] BSPFilePath" );
1173 sprintf( source, "%s%s", inbase, ExpandArg( argv[ i ] ) );
1174 StripExtension( source );
1175 strcat( source, ".bsp" );
1176 Sys_Printf( "Loading %s\n", source );
1177 LoadBSPFile( source );
1179 if ( game->texFile )
1181 // smokinguns-like tex file
1182 StripExtension( source );
1183 strcat( source, ".tex" );
1184 LoadSurfaceFlags( source );
1185 StripExtension( source );
1186 strcat( source, ".bsp" );
1189 /* load the portal file */
1190 if (!portalFilePath[0]) {
1191 sprintf( portalFilePath, "%s%s", inbase, ExpandArg( argv[ i ] ) );
1192 StripExtension( portalFilePath );
1193 strcat( portalFilePath, ".prt" );
1195 Sys_Printf( "Loading %s\n", portalFilePath );
1196 LoadPortals( portalFilePath );
1198 /* ydnar: exit if no portals, hence no vis */
1199 if ( numportals == 0 ) {
1200 Sys_Printf( "No portals means no vis, exiting.\n" );
1204 /* ydnar: for getting far plane */
1207 /* inject command line parameters */
1208 InjectCommandLine( argv, 0, argc - 1 );
1215 if ( mergevis || mergevisportals ) {
1219 CountActivePortals();
1220 /* WritePortals( "maps/hints.prs" );*/
1222 Sys_Printf( "visdatasize:%i\n", numBSPVisBytes );
1226 /* delete the prt file */
1228 remove( portalFilePath );
1231 if ( game->texFile )
1233 // smokinguns-like tex file
1234 StripExtension( source );
1235 WriteTexFile( source );
1236 DefaultExtension( source, ".bsp" );
1239 /* write the bsp file */
1240 Sys_Printf( "Writing %s\n", source );
1241 WriteBSPFile( source );