2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
\r
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
\r
5 This file is part of GtkRadiant.
\r
7 GtkRadiant is free software; you can redistribute it and/or modify
\r
8 it under the terms of the GNU General Public License as published by
\r
9 the Free Software Foundation; either version 2 of the License, or
\r
10 (at your option) any later version.
\r
12 GtkRadiant is distributed in the hope that it will be useful,
\r
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
15 GNU General Public License for more details.
\r
17 You should have received a copy of the GNU General Public License
\r
18 along with GtkRadiant; if not, write to the Free Software
\r
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
24 #include "q2_threads.h"
\r
36 int c_portaltest, c_portalpass, c_portalcheck;
\r
38 byte *uncompressedvis;
\r
40 byte *vismap, *vismap_p, *vismap_end; // past visfile
\r
41 int originalvismapsize;
\r
43 int leafbytes; // (portalclusters+63)>>3
\r
46 int portalbytes, portallongs;
\r
55 portal_t *sorted_portals[MAX_MAP_PORTALS*2];
\r
58 //=============================================================================
\r
60 void PlaneFromWinding (winding_t *w, plane_t *plane)
\r
65 VectorSubtract (w->points[2], w->points[1], v1);
\r
66 VectorSubtract (w->points[0], w->points[1], v2);
\r
67 CrossProduct (v2, v1, plane->normal);
\r
68 VectorNormalize (plane->normal, plane->normal);
\r
69 plane->dist = DotProduct (w->points[0], plane->normal);
\r
78 winding_t *NewWinding (int points)
\r
83 if (points > MAX_POINTS_ON_WINDING)
\r
84 Error ("NewWinding: %i points", points);
\r
86 size = (int)((winding_t *)0)->points[points];
\r
88 memset (w, 0, size);
\r
95 void pw(winding_t *w)
\r
98 for (i=0 ; i<w->numpoints ; i++)
\r
99 Sys_Printf ("(%5.1f, %5.1f, %5.1f)\n",w->points[i][0], w->points[i][1],w->points[i][2]);
\r
102 void prl(leaf_t *l)
\r
108 for (i=0 ; i<l->numportals ; i++)
\r
112 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
117 //=============================================================================
\r
123 Sorts the portals from the least complex, so the later ones can reuse
\r
124 the earlier information.
\r
127 int PComp (const void *a, const void *b)
\r
129 if ( (*(portal_t **)a)->nummightsee == (*(portal_t **)b)->nummightsee)
\r
131 if ( (*(portal_t **)a)->nummightsee < (*(portal_t **)b)->nummightsee)
\r
135 void SortPortals (void)
\r
139 for (i=0 ; i<numportals*2 ; i++)
\r
140 sorted_portals[i] = &portals[i];
\r
144 qsort (sorted_portals, numportals*2, sizeof(sorted_portals[0]), PComp);
\r
150 LeafVectorFromPortalVector
\r
153 int LeafVectorFromPortalVector (byte *portalbits, byte *leafbits)
\r
160 memset (leafbits, 0, leafbytes);
\r
162 for (i=0 ; i<numportals*2 ; i++)
\r
164 if (portalbits[i>>3] & (1<<(i&7)) )
\r
167 leafbits[p->leaf>>3] |= (1<<(p->leaf&7));
\r
171 c_leafs = CountBits (leafbits, portalclusters);
\r
181 Merges the portal visibility for a leaf
\r
184 void ClusterMerge (int leafnum)
\r
187 byte portalvector[MAX_PORTALS/8];
\r
188 byte uncompressed[MAX_MAP_LEAFS/8];
\r
189 byte compressed[MAX_MAP_LEAFS/8];
\r
196 // OR together all the portalvis bits
\r
198 memset (portalvector, 0, portalbytes);
\r
199 leaf = &leafs[leafnum];
\r
200 for (i=0 ; i<leaf->numportals ; i++)
\r
202 p = leaf->portals[i];
\r
203 if (p->status != stat_done)
\r
204 Error ("portal not done");
\r
205 for (j=0 ; j<portallongs ; j++)
\r
206 ((long *)portalvector)[j] |= ((long *)p->portalvis)[j];
\r
207 pnum = p - portals;
\r
208 portalvector[pnum>>3] |= 1<<(pnum&7);
\r
211 // convert portal bits to leaf bits
\r
212 numvis = LeafVectorFromPortalVector (portalvector, uncompressed);
\r
214 if (uncompressed[leafnum>>3] & (1<<(leafnum&7)))
\r
215 Sys_Printf ("WARNING: Leaf portals saw into leaf\n");
\r
217 uncompressed[leafnum>>3] |= (1<<(leafnum&7));
\r
218 numvis++; // count the leaf itself
\r
220 // save uncompressed for PHS calculation
\r
221 memcpy (uncompressedvis + leafnum*leafbytes, uncompressed, leafbytes);
\r
224 // compress the bit string
\r
226 Sys_FPrintf( SYS_VRB, "cluster %4i : %4i visible\n", leafnum, numvis);
\r
227 totalvis += numvis;
\r
229 i = CompressVis (uncompressed, compressed);
\r
234 if (vismap_p > vismap_end)
\r
235 Error ("Vismap expansion overflow");
\r
237 dvis->bitofs[leafnum][DVIS_PVS] = dest-vismap;
\r
239 memcpy (dest, compressed, i);
\r
248 void CalcPortalVis (void)
\r
252 // fastvis just uses mightsee for a very loose bound
\r
255 for (i=0 ; i<numportals*2 ; i++)
\r
257 portals[i].portalvis = portals[i].portalflood;
\r
258 portals[i].status = stat_done;
\r
263 RunThreadsOnIndividual (numportals*2, true, PortalFlow);
\r
273 void CalcVis (void)
\r
277 RunThreadsOnIndividual (numportals*2, true, BasePortalVis);
\r
279 // RunThreadsOnIndividual (numportals*2, true, BetterPortalVis);
\r
286 // assemble the leaf vis lists by oring and compressing the portal lists
\r
288 for (i=0 ; i<portalclusters ; i++)
\r
291 Sys_Printf ("Average clusters visible: %i\n", totalvis / portalclusters);
\r
295 void SetPortalSphere (portal_t *p)
\r
298 vec3_t total, dist;
\r
303 VectorCopy (vec3_origin, total);
\r
304 for (i=0 ; i<w->numpoints ; i++)
\r
306 VectorAdd (total, w->points[i], total);
\r
309 for (i=0 ; i<3 ; i++)
\r
310 total[i] /= w->numpoints;
\r
313 for (i=0 ; i<w->numpoints ; i++)
\r
315 VectorSubtract (w->points[i], total, dist);
\r
316 r = VectorLength (dist);
\r
320 VectorCopy (total, p->origin);
\r
329 void LoadPortals (char *name)
\r
341 if (!strcmp(name,"-"))
\r
345 f = fopen(name, "r");
\r
347 Error ("LoadPortals: couldn't read %s\n",name);
\r
350 if (fscanf (f,"%79s\n%i\n%i\n",magic, &portalclusters, &numportals) != 3)
\r
351 Error ("LoadPortals: failed to read header");
\r
352 if (strcmp(magic,PORTALFILE))
\r
353 Error ("LoadPortals: not a portal file");
\r
355 Sys_Printf ("%4i portalclusters\n", portalclusters);
\r
356 Sys_Printf ("%4i numportals\n", numportals);
\r
358 // these counts should take advantage of 64 bit systems automatically
\r
359 leafbytes = ((portalclusters+63)&~63)>>3;
\r
360 leaflongs = leafbytes/sizeof(long);
\r
362 portalbytes = ((numportals*2+63)&~63)>>3;
\r
363 portallongs = portalbytes/sizeof(long);
\r
365 // each file portal is split into two memory portals
\r
366 portals = malloc(2*numportals*sizeof(portal_t));
\r
367 memset (portals, 0, 2*numportals*sizeof(portal_t));
\r
369 leafs = malloc(portalclusters*sizeof(leaf_t));
\r
370 memset (leafs, 0, portalclusters*sizeof(leaf_t));
\r
372 originalvismapsize = portalclusters*leafbytes;
\r
373 uncompressedvis = malloc(originalvismapsize);
\r
375 vismap = vismap_p = dvisdata;
\r
376 dvis->numclusters = portalclusters;
\r
377 vismap_p = (byte *)&dvis->bitofs[portalclusters];
\r
379 vismap_end = vismap + MAX_MAP_VISIBILITY;
\r
381 for (i=0, p=portals ; i<numportals ; i++)
\r
383 if (fscanf (f, "%i %i %i ", &numpoints, &leafnums[0], &leafnums[1])
\r
385 Error ("LoadPortals: reading portal %i", i);
\r
386 if (numpoints > MAX_POINTS_ON_WINDING)
\r
387 Error ("LoadPortals: portal %i has too many points", i);
\r
388 if ( (unsigned)leafnums[0] > portalclusters
\r
389 || (unsigned)leafnums[1] > portalclusters)
\r
390 Error ("LoadPortals: reading portal %i", i);
\r
392 w = p->winding = NewWinding (numpoints);
\r
393 w->original = true;
\r
394 w->numpoints = numpoints;
\r
396 for (j=0 ; j<numpoints ; j++)
\r
401 // scanf into double, then assign to vec_t
\r
402 // so we don't care what size vec_t is
\r
403 if (fscanf (f, "(%lf %lf %lf ) "
\r
404 , &v[0], &v[1], &v[2]) != 3)
\r
405 Error ("LoadPortals: reading portal %i", i);
\r
406 for (k=0 ; k<3 ; k++)
\r
407 w->points[j][k] = v[k];
\r
412 PlaneFromWinding (w, &plane);
\r
414 // create forward portal
\r
415 l = &leafs[leafnums[0]];
\r
416 if (l->numportals == MAX_PORTALS_ON_LEAF)
\r
417 Error ("Leaf with too many portals");
\r
418 l->portals[l->numportals] = p;
\r
422 VectorSubtract (vec3_origin, plane.normal, p->plane.normal);
\r
423 p->plane.dist = -plane.dist;
\r
424 p->leaf = leafnums[1];
\r
425 SetPortalSphere (p);
\r
428 // create backwards portal
\r
429 l = &leafs[leafnums[1]];
\r
430 if (l->numportals == MAX_PORTALS_ON_LEAF)
\r
431 Error ("Leaf with too many portals");
\r
432 l->portals[l->numportals] = p;
\r
435 p->winding = NewWinding(w->numpoints);
\r
436 p->winding->numpoints = w->numpoints;
\r
437 for (j=0 ; j<w->numpoints ; j++)
\r
439 VectorCopy (w->points[w->numpoints-1-j], p->winding->points[j]);
\r
443 p->leaf = leafnums[0];
\r
444 SetPortalSphere (p);
\r
457 Calculate the PHS (Potentially Hearable Set)
\r
458 by ORing together all the PVS visible from a leaf
\r
461 void CalcPHS (void)
\r
463 int i, j, k, l, index;
\r
468 byte uncompressed[MAX_MAP_LEAFS/8];
\r
469 byte compressed[MAX_MAP_LEAFS/8];
\r
471 Sys_Printf ("Building PHS...\n");
\r
474 for (i=0 ; i<portalclusters ; i++)
\r
476 scan = uncompressedvis + i*leafbytes;
\r
477 memcpy (uncompressed, scan, leafbytes);
\r
478 for (j=0 ; j<leafbytes ; j++)
\r
483 for (k=0 ; k<8 ; k++)
\r
485 if (! (bitbyte & (1<<k)) )
\r
487 // OR this pvs row into the phs
\r
488 index = ((j<<3)+k);
\r
489 if (index >= portalclusters)
\r
490 Error ("Bad bit in PVS"); // pad bits should be 0
\r
491 src = (long *)(uncompressedvis + index*leafbytes);
\r
492 dest = (long *)uncompressed;
\r
493 for (l=0 ; l<leaflongs ; l++)
\r
494 ((long *)uncompressed)[l] |= src[l];
\r
497 for (j=0 ; j<portalclusters ; j++)
\r
498 if (uncompressed[j>>3] & (1<<(j&7)) )
\r
502 // compress the bit string
\r
504 j = CompressVis (uncompressed, compressed);
\r
506 dest = (long *)vismap_p;
\r
509 if (vismap_p > vismap_end)
\r
510 Error ("Vismap expansion overflow");
\r
512 dvis->bitofs[i][DVIS_PHS] = (byte *)dest-vismap;
\r
514 memcpy (dest, compressed, j);
\r
517 Sys_Printf ("Average clusters hearable: %i\n", count/portalclusters);
\r
527 char portalfile[1024];
\r
531 int total_vis_time;
\r
533 Sys_Printf ("\n----- VIS ----\n\n");
\r
535 //if (i != argc - 1)
\r
536 // Error ("usage: vis [-threads #] [-level 0-4] [-fast] [-v] bspfile");
\r
538 start = I_FloatTime ();
\r
540 ThreadSetDefault ();
\r
542 SetQdirFromPath (mapname);
\r
543 strcpy (source, ExpandArg(mapname));
\r
544 StripExtension (source);
\r
545 DefaultExtension (source, ".bsp");
\r
547 sprintf (name, "%s%s", inbase, source);
\r
548 Sys_Printf ("reading %s\n", name);
\r
549 LoadBSPFile (name);
\r
550 if (numnodes == 0 || numfaces == 0)
\r
551 Error ("Empty map");
\r
553 sprintf (portalfile, "%s%s", inbase, ExpandArg(mapname));
\r
554 StripExtension (portalfile);
\r
555 strcat (portalfile, ".prt");
\r
557 Sys_Printf ("reading %s\n", portalfile);
\r
558 LoadPortals (portalfile);
\r
564 visdatasize = vismap_p - dvisdata;
\r
565 Sys_Printf ("visdatasize:%i compressed from %i\n", visdatasize, originalvismapsize*2);
\r
567 sprintf (name, "%s%s", outbase, source);
\r
568 Sys_Printf ("writing %s\n", name);
\r
569 WriteBSPFile (name);
\r
571 end = I_FloatTime ();
\r
572 total_vis_time = (int) (end-start);
\r
573 Sys_Printf("\nVIS Time: ");
\r
574 if ( total_vis_time > 59 )
\r
575 Sys_Printf("%d Minutes ", total_vis_time/60 );
\r
576 Sys_Printf( "%d Seconds\n", total_vis_time%60 );
\r