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