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
166 static int clustersizehistogram[MAX_MAP_LEAFS] = {0};
167 void ClusterMerge( int leafnum ){
169 byte portalvector[MAX_PORTALS / 8];
170 byte uncompressed[MAX_MAP_LEAFS / 8];
172 int numvis, mergedleafnum;
176 // OR together all the portalvis bits
178 mergedleafnum = leafnum;
179 while ( leafs[mergedleafnum].merged >= 0 )
180 mergedleafnum = leafs[mergedleafnum].merged;
182 memset( portalvector, 0, portalbytes );
183 leaf = &leafs[mergedleafnum];
184 for ( i = 0; i < leaf->numportals; i++ )
186 p = leaf->portals[i];
191 if ( p->status != stat_done ) {
192 Error( "portal not done" );
194 for ( j = 0 ; j < portallongs ; j++ )
195 ( (long *)portalvector )[j] |= ( (long *)p->portalvis )[j];
197 portalvector[pnum >> 3] |= 1 << ( pnum & 7 );
200 memset( uncompressed, 0, leafbytes );
202 uncompressed[mergedleafnum >> 3] |= ( 1 << ( mergedleafnum & 7 ) );
203 // convert portal bits to leaf bits
204 numvis = LeafVectorFromPortalVector( portalvector, uncompressed );
206 // if (uncompressed[leafnum>>3] & (1<<(leafnum&7)))
207 // Sys_Printf ("WARNING: Leaf portals saw into leaf\n");
209 // uncompressed[leafnum>>3] |= (1<<(leafnum&7));
211 numvis++; // count the leaf itself
213 //Sys_FPrintf( SYS_VRB,"cluster %4i : %4i visible\n", leafnum, numvis );
214 ++clustersizehistogram[numvis];
216 memcpy( bspVisBytes + VIS_HEADER_SIZE + leafnum * leafbytes, uncompressed, leafbytes );
224 void CalcPortalVis( void ){
226 Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
227 //get rid of the counter
228 RunThreadsOnIndividual( numportals * 2, qfalse, PortalFlow );
230 RunThreadsOnIndividual( numportals * 2, qtrue, PortalFlow );
240 void CalcPassageVis( void ){
244 _printf( "%6d portals out of %d", 0, numportals * 2 );
245 RunThreadsOnIndividual( numportals * 2, qfalse, CreatePassages );
247 _printf( "%6d portals out of %d", 0, numportals * 2 );
248 RunThreadsOnIndividual( numportals * 2, qfalse, PassageFlow );
251 Sys_Printf( "\n--- CreatePassages (%d) ---\n", numportals * 2 );
252 RunThreadsOnIndividual( numportals * 2, qtrue, CreatePassages );
254 Sys_Printf( "\n--- PassageFlow (%d) ---\n", numportals * 2 );
255 RunThreadsOnIndividual( numportals * 2, qtrue, PassageFlow );
264 void CalcPassagePortalVis( void ){
268 Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
269 RunThreadsOnIndividual( numportals * 2, qfalse, CreatePassages );
271 Sys_Printf( "%6d portals out of %d", 0, numportals * 2 );
272 RunThreadsOnIndividual( numportals * 2, qfalse, PassagePortalFlow );
275 Sys_Printf( "\n--- CreatePassages (%d) ---\n", numportals * 2 );
276 RunThreadsOnIndividual( numportals * 2, qtrue, CreatePassages );
278 Sys_Printf( "\n--- PassagePortalFlow (%d) ---\n", numportals * 2 );
279 RunThreadsOnIndividual( numportals * 2, qtrue, PassagePortalFlow );
288 void CalcFastVis( void ){
291 // fastvis just uses mightsee for a very loose bound
292 for ( i = 0 ; i < numportals * 2 ; i++ )
294 portals[i].portalvis = portals[i].portalflood;
295 portals[i].status = stat_done;
304 void CalcVis( void ){
305 int i, minvis, maxvis;
307 double mu, sigma, totalvis, totalvis2;
310 /* ydnar: rr2do2's farplane code */
312 value = ValueForKey( &entities[ 0 ], "_farplanedist" ); /* proper '_' prefixed key */
313 if ( value[ 0 ] == '\0' ) {
314 value = ValueForKey( &entities[ 0 ], "fogclip" ); /* wolf compatibility */
316 if ( value[ 0 ] == '\0' ) {
317 value = ValueForKey( &entities[ 0 ], "distancecull" ); /* sof2 compatibility */
319 if ( value[ 0 ] != '\0' ) {
320 farPlaneDist = atof( value );
321 if ( farPlaneDist > 0.0f ) {
322 Sys_Printf( "farplane distance = %.1f\n", farPlaneDist );
331 Sys_Printf( "\n--- BasePortalVis (%d) ---\n", numportals * 2 );
332 RunThreadsOnIndividual( numportals * 2, qtrue, BasePortalVis );
334 // RunThreadsOnIndividual (numportals*2, qtrue, BetterPortalVis);
341 else if ( noPassageVis ) {
344 else if ( passageVisOnly ) {
348 CalcPassagePortalVis();
351 // assemble the leaf vis lists by oring and compressing the portal lists
353 Sys_Printf( "creating leaf vis...\n" );
354 for ( i = 0 ; i < portalclusters ; i++ )
361 for ( i = 0; i < MAX_MAP_LEAFS; ++i )
362 if ( clustersizehistogram[i] ) {
363 if ( debugCluster ) {
364 Sys_FPrintf( SYS_VRB, "%4i clusters have exactly %4i visible clusters\n", clustersizehistogram[i], i );
366 /* cast is to prevent integer overflow */
367 totalvis += ( (double) i ) * ( (double) clustersizehistogram[i] );
368 totalvis2 += ( (double) i ) * ( (double) i ) * ( (double) clustersizehistogram[i] );
376 mu = totalvis / portalclusters;
377 sigma = sqrt( totalvis2 / portalclusters - mu * mu );
379 Sys_Printf( "Total clusters: %i\n", portalclusters );
380 Sys_Printf( "Total visible clusters: %.0f\n", totalvis );
381 Sys_Printf( "Average clusters visible: %.2f (%.3f%%/total)\n", mu, mu / portalclusters * 100.0 );
382 Sys_Printf( " Standard deviation: %.2f (%.3f%%/total, %.3f%%/avg)\n", sigma, sigma / portalclusters * 100.0, sigma / mu * 100.0 );
383 Sys_Printf( " Minimum: %i (%.3f%%/total, %.3f%%/avg)\n", minvis, minvis / (double) portalclusters * 100.0, minvis / mu * 100.0 );
384 Sys_Printf( " Maximum: %i (%.3f%%/total, %.3f%%/avg)\n", maxvis, maxvis / (double) portalclusters * 100.0, maxvis / mu * 100.0 );
392 void SetPortalSphere( vportal_t *p ){
399 VectorCopy( vec3_origin, total );
400 for ( i = 0 ; i < w->numpoints ; i++ )
402 VectorAdd( total, w->points[i], total );
405 for ( i = 0 ; i < 3 ; i++ )
406 total[i] /= w->numpoints;
409 for ( i = 0 ; i < w->numpoints ; i++ )
411 VectorSubtract( w->points[i], total, dist );
412 r = VectorLength( dist );
417 VectorCopy( total, p->origin );
423 Winding_PlanesConcave
426 #define WCONVEX_EPSILON 0.2
428 int Winding_PlanesConcave( fixedWinding_t *w1, fixedWinding_t *w2,
429 vec3_t normal1, vec3_t normal2,
430 float dist1, float dist2 ){
437 // check if one of the points of winding 1 is at the front of the plane of winding 2
438 for ( i = 0; i < w1->numpoints; i++ )
440 if ( DotProduct( normal2, w1->points[i] ) - dist2 > WCONVEX_EPSILON ) {
444 // check if one of the points of winding 2 is at the front of the plane of winding 1
445 for ( i = 0; i < w2->numpoints; i++ )
447 if ( DotProduct( normal1, w2->points[i] ) - dist1 > WCONVEX_EPSILON ) {
460 int TryMergeLeaves( int l1num, int l2num ){
461 int i, j, k, n, numportals;
462 visPlane_t plane1, plane2;
465 vportal_t *portals[MAX_PORTALS_ON_LEAF];
467 for ( k = 0; k < 2; k++ )
472 else{l1 = &faceleafs[l1num]; }
473 for ( i = 0; i < l1->numportals; i++ )
476 if ( p1->leaf == l2num ) {
479 for ( n = 0; n < 2; n++ )
484 else{l2 = &faceleafs[l2num]; }
485 for ( j = 0; j < l2->numportals; j++ )
488 if ( p2->leaf == l1num ) {
494 if ( Winding_PlanesConcave( p1->winding, p2->winding, plane1.normal, plane2.normal, plane1.dist, plane2.dist ) ) {
501 for ( k = 0; k < 2; k++ )
509 l1 = &faceleafs[l1num];
510 l2 = &faceleafs[l2num];
513 //the leaves can be merged now
514 for ( i = 0; i < l1->numportals; i++ )
517 if ( p1->leaf == l2num ) {
521 portals[numportals++] = p1;
523 for ( j = 0; j < l2->numportals; j++ )
526 if ( p2->leaf == l1num ) {
530 portals[numportals++] = p2;
532 for ( i = 0; i < numportals; i++ )
534 l2->portals[i] = portals[i];
536 l2->numportals = numportals;
547 void UpdatePortals( void ){
551 for ( i = 0; i < numportals * 2; i++ )
557 while ( leafs[p->leaf].merged >= 0 )
558 p->leaf = leafs[p->leaf].merged;
566 try to merge leaves but don't merge through hint splitters
569 void MergeLeaves( void ){
570 int i, j, nummerges, totalnummerges;
578 for ( i = 0; i < portalclusters; i++ )
581 //if this leaf is merged already
583 /* ydnar: vmods: merge all non-hint portals */
584 if ( leaf->merged >= 0 && hint == qfalse ) {
589 for ( j = 0; j < leaf->numportals; j++ )
591 p = leaf->portals[j];
596 //never merge through hint portals
600 if ( TryMergeLeaves( i, p->leaf ) ) {
607 totalnummerges += nummerges;
608 } while ( nummerges );
609 Sys_Printf( "%6d leaves merged\n", totalnummerges );
617 #define CONTINUOUS_EPSILON 0.005
619 fixedWinding_t *TryMergeWinding( fixedWinding_t *f1, fixedWinding_t *f2, vec3_t planenormal ){
620 vec_t *p1, *p2, *p3, *p4, *back;
621 fixedWinding_t *newf;
623 vec3_t normal, delta;
625 qboolean keep1, keep2;
629 // find a common edge
631 p1 = p2 = NULL; // stop compiler warning
634 for ( i = 0; i < f1->numpoints; i++ )
637 p2 = f1->points[( i + 1 ) % f1->numpoints];
638 for ( j = 0; j < f2->numpoints; j++ )
641 p4 = f2->points[( j + 1 ) % f2->numpoints];
642 for ( k = 0; k < 3; k++ )
644 if ( fabs( p1[k] - p4[k] ) > 0.1 ) { //EQUAL_EPSILON) //ME
647 if ( fabs( p2[k] - p3[k] ) > 0.1 ) { //EQUAL_EPSILON) //ME
655 if ( j < f2->numpoints ) {
660 if ( i == f1->numpoints ) {
661 return NULL; // no matching edges
665 // check slope of connected lines
666 // if the slopes are colinear, the point can be removed
668 back = f1->points[( i + f1->numpoints - 1 ) % f1->numpoints];
669 VectorSubtract( p1, back, delta );
670 CrossProduct( planenormal, delta, normal );
671 VectorNormalize( normal, normal );
673 back = f2->points[( j + 2 ) % f2->numpoints];
674 VectorSubtract( back, p1, delta );
675 dot = DotProduct( delta, normal );
676 if ( dot > CONTINUOUS_EPSILON ) {
677 return NULL; // not a convex polygon
679 keep1 = (qboolean)( dot < -CONTINUOUS_EPSILON );
681 back = f1->points[( i + 2 ) % f1->numpoints];
682 VectorSubtract( back, p2, delta );
683 CrossProduct( planenormal, delta, normal );
684 VectorNormalize( normal, normal );
686 back = f2->points[( j + f2->numpoints - 1 ) % f2->numpoints];
687 VectorSubtract( back, p2, delta );
688 dot = DotProduct( delta, normal );
689 if ( dot > CONTINUOUS_EPSILON ) {
690 return NULL; // not a convex polygon
692 keep2 = (qboolean)( dot < -CONTINUOUS_EPSILON );
695 // build the new polygon
697 newf = NewFixedWinding( f1->numpoints + f2->numpoints );
699 // copy first polygon
700 for ( k = ( i + 1 ) % f1->numpoints ; k != i ; k = ( k + 1 ) % f1->numpoints )
702 if ( k == ( i + 1 ) % f1->numpoints && !keep2 ) {
706 VectorCopy( f1->points[k], newf->points[newf->numpoints] );
710 // copy second polygon
711 for ( l = ( j + 1 ) % f2->numpoints ; l != j ; l = ( l + 1 ) % f2->numpoints )
713 if ( l == ( j + 1 ) % f2->numpoints && !keep1 ) {
716 VectorCopy( f2->points[l], newf->points[newf->numpoints] );
728 void MergeLeafPortals( void ){
729 int i, j, k, nummerges, hintsmerged;
736 for ( i = 0; i < portalclusters; i++ )
739 if ( leaf->merged >= 0 ) {
742 for ( j = 0; j < leaf->numportals; j++ )
744 p1 = leaf->portals[j];
748 for ( k = j + 1; k < leaf->numportals; k++ )
750 p2 = leaf->portals[k];
754 if ( p1->leaf == p2->leaf ) {
755 w = TryMergeWinding( p1->winding, p2->winding, p1->plane.normal );
757 free( p1->winding ); //% FreeWinding(p1->winding);
759 if ( p1->hint && p2->hint ) {
762 p1->hint |= p2->hint;
763 SetPortalSphere( p1 );
771 if ( k < leaf->numportals ) {
776 Sys_Printf( "%6d portals merged\n", nummerges );
777 Sys_Printf( "%6d hint portals merged\n", hintsmerged );
786 int CountActivePortals( void ){
792 for ( j = 0; j < numportals * 2; j++ )
803 Sys_Printf( "%6d active portals\n", num );
804 Sys_Printf( "%6d hint portals\n", hints );
813 void WriteFloat( FILE *f, vec_t v );
815 void WritePortals( char *portalpathfile ){
822 pf = fopen( portalpathfile, "w" );
824 Error( "Error opening %s", portalpathfile );
828 for ( j = 0; j < numportals * 2; j++ )
839 fprintf( pf, "%s\n", PORTALFILE );
840 fprintf( pf, "%i\n", 0 );
841 fprintf( pf, "%i\n", num ); // + numfaces);
842 fprintf( pf, "%i\n", 0 );
844 for ( j = 0; j < numportals * 2; j++ )
853 fprintf( pf,"%i %i %i ",w->numpoints, 0, 0 );
854 fprintf( pf, "%d ", p->hint );
855 for ( i = 0 ; i < w->numpoints ; i++ )
858 WriteFloat( pf, w->points[i][0] );
859 WriteFloat( pf, w->points[i][1] );
860 WriteFloat( pf, w->points[i][2] );
867 for (j = 0; j < numfaces; j++)
871 fprintf (pf,"%i %i %i ",w->numpoints, 0, 0);
873 for (i=0 ; i<w->numpoints ; i++)
876 WriteFloat (pf, w->points[i][0]);
877 WriteFloat (pf, w->points[i][1]);
878 WriteFloat (pf, w->points[i][2]);
892 void LoadPortals( char *name ){
903 if ( !strcmp( name,"-" ) ) {
908 f = fopen( name, "r" );
910 Error( "LoadPortals: couldn't read %s\n",name );
914 if ( fscanf( f,"%79s\n%i\n%i\n%i\n",magic, &portalclusters, &numportals, &numfaces ) != 4 ) {
915 Error( "LoadPortals: failed to read header" );
917 if ( strcmp( magic,PORTALFILE ) ) {
918 Error( "LoadPortals: not a portal file" );
921 Sys_Printf( "%6i portalclusters\n", portalclusters );
922 Sys_Printf( "%6i numportals\n", numportals );
923 Sys_Printf( "%6i numfaces\n", numfaces );
925 if ( numportals > MAX_PORTALS ) {
926 Error( "MAX_PORTALS" );
929 // these counts should take advantage of 64 bit systems automatically
930 leafbytes = ( ( portalclusters + 63 ) & ~63 ) >> 3;
931 leaflongs = leafbytes / sizeof( long );
933 portalbytes = ( ( numportals * 2 + 63 ) & ~63 ) >> 3;
934 portallongs = portalbytes / sizeof( long );
936 // each file portal is split into two memory portals
937 portals = safe_malloc( 2 * numportals * sizeof( vportal_t ) );
938 memset( portals, 0, 2 * numportals * sizeof( vportal_t ) );
940 leafs = safe_malloc( portalclusters * sizeof( leaf_t ) );
941 memset( leafs, 0, portalclusters * sizeof( leaf_t ) );
943 for ( i = 0; i < portalclusters; i++ )
944 leafs[i].merged = -1;
946 numBSPVisBytes = VIS_HEADER_SIZE + portalclusters * leafbytes;
948 if ( numBSPVisBytes > MAX_MAP_VISIBILITY ) {
949 Error( "MAX_MAP_VISIBILITY exceeded" );
952 ( (int *)bspVisBytes )[0] = portalclusters;
953 ( (int *)bspVisBytes )[1] = leafbytes;
955 for ( i = 0, p = portals ; i < numportals ; i++ )
957 if ( fscanf( f, "%i %i %i ", &numpoints, &leafnums[0], &leafnums[1] ) != 3 ) {
958 Error( "LoadPortals: reading portal %i", i );
960 if ( numpoints > MAX_POINTS_ON_WINDING ) {
961 Error( "LoadPortals: portal %i has too many points", i );
963 if ( leafnums[0] > portalclusters
964 || leafnums[1] > portalclusters ) {
965 Error( "LoadPortals: reading portal %i", i );
967 if ( fscanf( f, "%i ", &flags ) != 1 ) {
968 Error( "LoadPortals: reading flags" );
971 w = p->winding = NewFixedWinding( numpoints );
972 w->numpoints = numpoints;
974 for ( j = 0 ; j < numpoints ; j++ )
979 // scanf into double, then assign to vec_t
980 // so we don't care what size vec_t is
981 if ( fscanf( f, "(%lf %lf %lf ) "
982 , &v[0], &v[1], &v[2] ) != 3 ) {
983 Error( "LoadPortals: reading portal %i", i );
985 for ( k = 0 ; k < 3 ; k++ )
986 w->points[j][k] = v[k];
988 if ( fscanf( f, "\n" ) != 0 ) {
989 // silence gcc warning
993 PlaneFromWinding( w, &plane );
995 // create forward portal
996 l = &leafs[leafnums[0]];
997 if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
998 Error( "Leaf with too many portals" );
1000 l->portals[l->numportals] = p;
1004 p->hint = ((flags & 1) != 0);
1005 p->sky = ((flags & 2) != 0);
1007 VectorSubtract( vec3_origin, plane.normal, p->plane.normal );
1008 p->plane.dist = -plane.dist;
1009 p->leaf = leafnums[1];
1010 SetPortalSphere( p );
1013 // create backwards portal
1014 l = &leafs[leafnums[1]];
1015 if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
1016 Error( "Leaf with too many portals" );
1018 l->portals[l->numportals] = p;
1023 p->winding = NewFixedWinding( w->numpoints );
1024 p->winding->numpoints = w->numpoints;
1025 for ( j = 0 ; j < w->numpoints ; j++ )
1027 VectorCopy( w->points[w->numpoints - 1 - j], p->winding->points[j] );
1031 p->leaf = leafnums[0];
1032 SetPortalSphere( p );
1037 faces = safe_malloc( 2 * numfaces * sizeof( vportal_t ) );
1038 memset( faces, 0, 2 * numfaces * sizeof( vportal_t ) );
1040 faceleafs = safe_malloc( portalclusters * sizeof( leaf_t ) );
1041 memset( faceleafs, 0, portalclusters * sizeof( leaf_t ) );
1043 for ( i = 0, p = faces; i < numfaces; i++ )
1045 if ( fscanf( f, "%i %i ", &numpoints, &leafnums[0] ) != 2 ) {
1046 Error( "LoadPortals: reading portal %i", i );
1049 w = p->winding = NewFixedWinding( numpoints );
1050 w->numpoints = numpoints;
1052 for ( j = 0 ; j < numpoints ; j++ )
1057 // scanf into double, then assign to vec_t
1058 // so we don't care what size vec_t is
1059 if ( fscanf( f, "(%lf %lf %lf ) "
1060 , &v[0], &v[1], &v[2] ) != 3 ) {
1061 Error( "LoadPortals: reading portal %i", i );
1063 for ( k = 0 ; k < 3 ; k++ )
1064 w->points[j][k] = v[k];
1066 if ( fscanf( f, "\n" ) != 0 ) {
1067 // silence gcc warning
1071 PlaneFromWinding( w, &plane );
1073 l = &faceleafs[leafnums[0]];
1075 if ( l->numportals == MAX_PORTALS_ON_LEAF ) {
1076 Error( "Leaf with too many faces" );
1078 l->portals[l->numportals] = p;
1083 // normal pointing out of the leaf
1084 VectorSubtract( vec3_origin, plane.normal, p->plane.normal );
1085 p->plane.dist = -plane.dist;
1087 SetPortalSphere( p );
1101 int VisMain( int argc, char **argv ){
1103 char portalFilePath[ 1024 ];
1104 portalFilePath[0] = 0;
1108 Sys_Printf( "--- Vis ---\n" );
1110 /* process arguments */
1111 for ( i = 1 ; i < ( argc - 1 ) ; i++ )
1113 if ( !strcmp( argv[i], "-fast" ) ) {
1114 Sys_Printf( "fastvis = true\n" );
1117 else if ( !strcmp( argv[i], "-merge" ) ) {
1118 Sys_Printf( "merge = true\n" );
1121 else if ( !strcmp( argv[i], "-mergeportals" ) ) {
1122 Sys_Printf( "mergeportals = true\n" );
1123 mergevisportals = qtrue;
1125 else if ( !strcmp( argv[i], "-nopassage" ) ) {
1126 Sys_Printf( "nopassage = true\n" );
1127 noPassageVis = qtrue;
1129 else if ( !strcmp( argv[i], "-passageOnly" ) ) {
1130 Sys_Printf( "passageOnly = true\n" );
1131 passageVisOnly = qtrue;
1133 else if ( !strcmp( argv[i],"-nosort" ) ) {
1134 Sys_Printf( "nosort = true\n" );
1137 else if ( !strcmp( argv[i],"-saveprt" ) ) {
1138 Sys_Printf( "saveprt = true\n" );
1141 else if ( !strcmp( argv[ i ], "-v" ) ) {
1142 debugCluster = qtrue;
1143 Sys_Printf( "Extra verbous mode enabled\n" );
1145 else if ( !strcmp( argv[i],"-tmpin" ) ) {
1146 strcpy( inbase, "/tmp" );
1148 else if ( !strcmp( argv[i],"-tmpout" ) ) {
1149 strcpy( outbase, "/tmp" );
1152 /* ydnar: -hint to merge all but hint portals */
1153 else if ( !strcmp( argv[ i ], "-hint" ) ) {
1154 Sys_Printf( "hint = true\n" );
1158 else if ( !strcmp( argv[ i ], "-prtfile" ) )
1160 strcpy( portalFilePath, argv[i + 1] );
1162 Sys_Printf( "Use %s as portal file\n", portalFilePath );
1166 Sys_FPrintf( SYS_WRN, "WARNING: Unknown option \"%s\"\n", argv[ i ] );
1170 if ( i != argc - 1 ) {
1171 Error( "usage: vis [-threads #] [-fast] [-v] BSPFilePath" );
1176 sprintf( source, "%s%s", inbase, ExpandArg( argv[ i ] ) );
1177 StripExtension( source );
1178 strcat( source, ".bsp" );
1179 Sys_Printf( "Loading %s\n", source );
1180 LoadBSPFile( source );
1182 /* load the portal file */
1183 if (!portalFilePath[0]) {
1184 sprintf( portalFilePath, "%s%s", inbase, ExpandArg( argv[ i ] ) );
1185 StripExtension( portalFilePath );
1186 strcat( portalFilePath, ".prt" );
1187 Sys_Printf( "Loading %s\n", portalFilePath );
1188 LoadPortals( portalFilePath );
1191 /* ydnar: exit if no portals, hence no vis */
1192 if ( numportals == 0 ) {
1193 Sys_Printf( "No portals means no vis, exiting.\n" );
1197 /* ydnar: for getting far plane */
1200 /* inject command line parameters */
1201 InjectCommandLine( argv, 0, argc - 1 );
1208 if ( mergevis || mergevisportals ) {
1212 CountActivePortals();
1213 /* WritePortals( "maps/hints.prs" );*/
1215 Sys_Printf( "visdatasize:%i\n", numBSPVisBytes );
1219 /* delete the prt file */
1221 remove( portalFilePath );
1224 /* write the bsp file */
1225 Sys_Printf( "Writing %s\n", source );
1226 WriteBSPFile( source );