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