| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "vis.h" |
| 24 |
#include "threads.h" |
| 25 |
#include "stdlib.h" |
| 26 |
|
| 27 |
int numportals; |
| 28 |
int portalclusters; |
| 29 |
|
| 30 |
char inbase[32]; |
| 31 |
char outbase[32]; |
| 32 |
|
| 33 |
portal_t *portals; |
| 34 |
leaf_t *leafs; |
| 35 |
|
| 36 |
int c_portaltest, c_portalpass, c_portalcheck; |
| 37 |
|
| 38 |
byte *uncompressedvis; |
| 39 |
|
| 40 |
byte *vismap, *vismap_p, *vismap_end; |
| 41 |
int originalvismapsize; |
| 42 |
|
| 43 |
int leafbytes; |
| 44 |
int leaflongs; |
| 45 |
|
| 46 |
int portalbytes, portallongs; |
| 47 |
|
| 48 |
qboolean fastvis; |
| 49 |
qboolean nosort; |
| 50 |
|
| 51 |
int testlevel = 2; |
| 52 |
|
| 53 |
int totalvis; |
| 54 |
|
| 55 |
portal_t *sorted_portals[MAX_MAP_PORTALS*2]; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
void PlaneFromWinding (winding_t *w, plane_t *plane) |
| 61 |
{ |
| 62 |
vec3_t v1, v2; |
| 63 |
|
| 64 |
|
| 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); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
winding_t *NewWinding (int points) |
| 79 |
{ |
| 80 |
winding_t *w; |
| 81 |
int size; |
| 82 |
|
| 83 |
if (points > MAX_POINTS_ON_WINDING) |
| 84 |
Error ("NewWinding: %i points", points); |
| 85 |
|
| 86 |
size = (int)((winding_t *)0)->points[points]; |
| 87 |
w = malloc (size); |
| 88 |
memset (w, 0, size); |
| 89 |
|
| 90 |
return w; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
void pw(winding_t *w) |
| 96 |
{ |
| 97 |
int i; |
| 98 |
for (i=0 ; i<w->numpoints ; i++) |
| 99 |
printf ("(%5.1f, %5.1f, %5.1f)\n",w->points[i][0], w->points[i][1],w->points[i][2]); |
| 100 |
} |
| 101 |
|
| 102 |
void prl(leaf_t *l) |
| 103 |
{ |
| 104 |
int i; |
| 105 |
portal_t *p; |
| 106 |
plane_t pl; |
| 107 |
|
| 108 |
for (i=0 ; i<l->numportals ; i++) |
| 109 |
{ |
| 110 |
p = l->portals[i]; |
| 111 |
pl = p->plane; |
| 112 |
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]); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
int PComp (const void *a, const void *b) |
| 128 |
{ |
| 129 |
if ( (*(portal_t **)a)->nummightsee == (*(portal_t **)b)->nummightsee) |
| 130 |
return 0; |
| 131 |
if ( (*(portal_t **)a)->nummightsee < (*(portal_t **)b)->nummightsee) |
| 132 |
return -1; |
| 133 |
return 1; |
| 134 |
} |
| 135 |
void SortPortals (void) |
| 136 |
{ |
| 137 |
int i; |
| 138 |
|
| 139 |
for (i=0 ; i<numportals*2 ; i++) |
| 140 |
sorted_portals[i] = &portals[i]; |
| 141 |
|
| 142 |
if (nosort) |
| 143 |
return; |
| 144 |
qsort (sorted_portals, numportals*2, sizeof(sorted_portals[0]), PComp); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
int LeafVectorFromPortalVector (byte *portalbits, byte *leafbits) |
| 154 |
{ |
| 155 |
int i; |
| 156 |
portal_t *p; |
| 157 |
int c_leafs; |
| 158 |
|
| 159 |
|
| 160 |
memset (leafbits, 0, leafbytes); |
| 161 |
|
| 162 |
for (i=0 ; i<numportals*2 ; i++) |
| 163 |
{ |
| 164 |
if (portalbits[i>>3] & (1<<(i&7)) ) |
| 165 |
{ |
| 166 |
p = portals+i; |
| 167 |
leafbits[p->leaf>>3] |= (1<<(p->leaf&7)); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
c_leafs = CountBits (leafbits, portalclusters); |
| 172 |
|
| 173 |
return c_leafs; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
void ClusterMerge (int leafnum) |
| 185 |
{ |
| 186 |
leaf_t *leaf; |
| 187 |
byte portalvector[MAX_PORTALS/8]; |
| 188 |
byte uncompressed[MAX_MAP_LEAFS/8]; |
| 189 |
byte compressed[MAX_MAP_LEAFS/8]; |
| 190 |
int i, j; |
| 191 |
int numvis; |
| 192 |
byte *dest; |
| 193 |
portal_t *p; |
| 194 |
int pnum; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
memset (portalvector, 0, portalbytes); |
| 199 |
leaf = &leafs[leafnum]; |
| 200 |
for (i=0 ; i<leaf->numportals ; i++) |
| 201 |
{ |
| 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]; |
| 207 |
pnum = p - portals; |
| 208 |
portalvector[pnum>>3] |= 1<<(pnum&7); |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
numvis = LeafVectorFromPortalVector (portalvector, uncompressed); |
| 213 |
|
| 214 |
if (uncompressed[leafnum>>3] & (1<<(leafnum&7))) |
| 215 |
printf ("WARNING: Leaf portals saw into leaf\n"); |
| 216 |
|
| 217 |
uncompressed[leafnum>>3] |= (1<<(leafnum&7)); |
| 218 |
numvis++; |
| 219 |
|
| 220 |
|
| 221 |
memcpy (uncompressedvis + leafnum*leafbytes, uncompressed, leafbytes); |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
qprintf ("cluster %4i : %4i visible\n", leafnum, numvis); |
| 227 |
totalvis += numvis; |
| 228 |
|
| 229 |
i = CompressVis (uncompressed, compressed); |
| 230 |
|
| 231 |
dest = vismap_p; |
| 232 |
vismap_p += i; |
| 233 |
|
| 234 |
if (vismap_p > vismap_end) |
| 235 |
Error ("Vismap expansion overflow"); |
| 236 |
|
| 237 |
dvis->bitofs[leafnum][DVIS_PVS] = dest-vismap; |
| 238 |
|
| 239 |
memcpy (dest, compressed, i); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
void CalcPortalVis (void) |
| 249 |
{ |
| 250 |
int i; |
| 251 |
|
| 252 |
|
| 253 |
if (fastvis) |
| 254 |
{ |
| 255 |
for (i=0 ; i<numportals*2 ; i++) |
| 256 |
{ |
| 257 |
portals[i].portalvis = portals[i].portalflood; |
| 258 |
portals[i].status = stat_done; |
| 259 |
} |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
RunThreadsOnIndividual (numportals*2, true, PortalFlow); |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
void CalcVis (void) |
| 274 |
{ |
| 275 |
int i; |
| 276 |
|
| 277 |
RunThreadsOnIndividual (numportals*2, true, BasePortalVis); |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
SortPortals (); |
| 282 |
|
| 283 |
CalcPortalVis (); |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
for (i=0 ; i<portalclusters ; i++) |
| 289 |
ClusterMerge (i); |
| 290 |
|
| 291 |
printf ("Average clusters visible: %i\n", totalvis / portalclusters); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
void SetPortalSphere (portal_t *p) |
| 296 |
{ |
| 297 |
int i; |
| 298 |
vec3_t total, dist; |
| 299 |
winding_t *w; |
| 300 |
float r, bestr; |
| 301 |
|
| 302 |
w = p->winding; |
| 303 |
VectorCopy (vec3_origin, total); |
| 304 |
for (i=0 ; i<w->numpoints ; i++) |
| 305 |
{ |
| 306 |
VectorAdd (total, w->points[i], total); |
| 307 |
} |
| 308 |
|
| 309 |
for (i=0 ; i<3 ; i++) |
| 310 |
total[i] /= w->numpoints; |
| 311 |
|
| 312 |
bestr = 0; |
| 313 |
for (i=0 ; i<w->numpoints ; i++) |
| 314 |
{ |
| 315 |
VectorSubtract (w->points[i], total, dist); |
| 316 |
r = VectorLength (dist); |
| 317 |
if (r > bestr) |
| 318 |
bestr = r; |
| 319 |
} |
| 320 |
VectorCopy (total, p->origin); |
| 321 |
p->radius = bestr; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
void LoadPortals (char *name) |
| 330 |
{ |
| 331 |
int i, j; |
| 332 |
portal_t *p; |
| 333 |
leaf_t *l; |
| 334 |
char magic[80]; |
| 335 |
FILE *f; |
| 336 |
int numpoints; |
| 337 |
winding_t *w; |
| 338 |
int leafnums[2]; |
| 339 |
plane_t plane; |
| 340 |
|
| 341 |
if (!strcmp(name,"-")) |
| 342 |
f = stdin; |
| 343 |
else |
| 344 |
{ |
| 345 |
f = fopen(name, "r"); |
| 346 |
if (!f) |
| 347 |
Error ("LoadPortals: couldn't read %s\n",name); |
| 348 |
} |
| 349 |
|
| 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"); |
| 354 |
|
| 355 |
printf ("%4i portalclusters\n", portalclusters); |
| 356 |
printf ("%4i numportals\n", numportals); |
| 357 |
|
| 358 |
|
| 359 |
leafbytes = ((portalclusters+63)&~63)>>3; |
| 360 |
leaflongs = leafbytes/sizeof(long); |
| 361 |
|
| 362 |
portalbytes = ((numportals*2+63)&~63)>>3; |
| 363 |
portallongs = portalbytes/sizeof(long); |
| 364 |
|
| 365 |
|
| 366 |
portals = malloc(2*numportals*sizeof(portal_t)); |
| 367 |
memset (portals, 0, 2*numportals*sizeof(portal_t)); |
| 368 |
|
| 369 |
leafs = malloc(portalclusters*sizeof(leaf_t)); |
| 370 |
memset (leafs, 0, portalclusters*sizeof(leaf_t)); |
| 371 |
|
| 372 |
originalvismapsize = portalclusters*leafbytes; |
| 373 |
uncompressedvis = malloc(originalvismapsize); |
| 374 |
|
| 375 |
vismap = vismap_p = dvisdata; |
| 376 |
dvis->numclusters = portalclusters; |
| 377 |
vismap_p = (byte *)&dvis->bitofs[portalclusters]; |
| 378 |
|
| 379 |
vismap_end = vismap + MAX_MAP_VISIBILITY; |
| 380 |
|
| 381 |
for (i=0, p=portals ; i<numportals ; i++) |
| 382 |
{ |
| 383 |
if (fscanf (f, "%i %i %i ", &numpoints, &leafnums[0], &leafnums[1]) |
| 384 |
!= 3) |
| 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); |
| 391 |
|
| 392 |
w = p->winding = NewWinding (numpoints); |
| 393 |
w->original = true; |
| 394 |
w->numpoints = numpoints; |
| 395 |
|
| 396 |
for (j=0 ; j<numpoints ; j++) |
| 397 |
{ |
| 398 |
double v[3]; |
| 399 |
int k; |
| 400 |
|
| 401 |
|
| 402 |
|
| 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]; |
| 408 |
} |
| 409 |
fscanf (f, "\n"); |
| 410 |
|
| 411 |
|
| 412 |
PlaneFromWinding (w, &plane); |
| 413 |
|
| 414 |
|
| 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; |
| 419 |
l->numportals++; |
| 420 |
|
| 421 |
p->winding = w; |
| 422 |
VectorSubtract (vec3_origin, plane.normal, p->plane.normal); |
| 423 |
p->plane.dist = -plane.dist; |
| 424 |
p->leaf = leafnums[1]; |
| 425 |
SetPortalSphere (p); |
| 426 |
p++; |
| 427 |
|
| 428 |
|
| 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; |
| 433 |
l->numportals++; |
| 434 |
|
| 435 |
p->winding = NewWinding(w->numpoints); |
| 436 |
p->winding->numpoints = w->numpoints; |
| 437 |
for (j=0 ; j<w->numpoints ; j++) |
| 438 |
{ |
| 439 |
VectorCopy (w->points[w->numpoints-1-j], p->winding->points[j]); |
| 440 |
} |
| 441 |
|
| 442 |
p->plane = plane; |
| 443 |
p->leaf = leafnums[0]; |
| 444 |
SetPortalSphere (p); |
| 445 |
p++; |
| 446 |
|
| 447 |
} |
| 448 |
|
| 449 |
fclose (f); |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
void CalcPHS (void) |
| 462 |
{ |
| 463 |
int i, j, k, l, index; |
| 464 |
int bitbyte; |
| 465 |
long *dest, *src; |
| 466 |
byte *scan; |
| 467 |
int count; |
| 468 |
byte uncompressed[MAX_MAP_LEAFS/8]; |
| 469 |
byte compressed[MAX_MAP_LEAFS/8]; |
| 470 |
|
| 471 |
printf ("Building PHS...\n"); |
| 472 |
|
| 473 |
count = 0; |
| 474 |
for (i=0 ; i<portalclusters ; i++) |
| 475 |
{ |
| 476 |
scan = uncompressedvis + i*leafbytes; |
| 477 |
memcpy (uncompressed, scan, leafbytes); |
| 478 |
for (j=0 ; j<leafbytes ; j++) |
| 479 |
{ |
| 480 |
bitbyte = scan[j]; |
| 481 |
if (!bitbyte) |
| 482 |
continue; |
| 483 |
for (k=0 ; k<8 ; k++) |
| 484 |
{ |
| 485 |
if (! (bitbyte & (1<<k)) ) |
| 486 |
continue; |
| 487 |
|
| 488 |
index = ((j<<3)+k); |
| 489 |
if (index >= portalclusters) |
| 490 |
Error ("Bad bit in PVS"); |
| 491 |
src = (long *)(uncompressedvis + index*leafbytes); |
| 492 |
dest = (long *)uncompressed; |
| 493 |
for (l=0 ; l<leaflongs ; l++) |
| 494 |
((long *)uncompressed)[l] |= src[l]; |
| 495 |
} |
| 496 |
} |
| 497 |
for (j=0 ; j<portalclusters ; j++) |
| 498 |
if (uncompressed[j>>3] & (1<<(j&7)) ) |
| 499 |
count++; |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
j = CompressVis (uncompressed, compressed); |
| 505 |
|
| 506 |
dest = (long *)vismap_p; |
| 507 |
vismap_p += j; |
| 508 |
|
| 509 |
if (vismap_p > vismap_end) |
| 510 |
Error ("Vismap expansion overflow"); |
| 511 |
|
| 512 |
dvis->bitofs[i][DVIS_PHS] = (byte *)dest-vismap; |
| 513 |
|
| 514 |
memcpy (dest, compressed, j); |
| 515 |
} |
| 516 |
|
| 517 |
printf ("Average clusters hearable: %i\n", count/portalclusters); |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
int main (int argc, char **argv) |
| 526 |
{ |
| 527 |
char portalfile[1024]; |
| 528 |
char source[1024]; |
| 529 |
char name[1024]; |
| 530 |
int i; |
| 531 |
double start, end; |
| 532 |
|
| 533 |
printf ("---- vis ----\n"); |
| 534 |
|
| 535 |
verbose = false; |
| 536 |
for (i=1 ; i<argc ; i++) |
| 537 |
{ |
| 538 |
if (!strcmp(argv[i],"-threads")) |
| 539 |
{ |
| 540 |
numthreads = atoi (argv[i+1]); |
| 541 |
i++; |
| 542 |
} |
| 543 |
else if (!strcmp(argv[i], "-fast")) |
| 544 |
{ |
| 545 |
printf ("fastvis = true\n"); |
| 546 |
fastvis = true; |
| 547 |
} |
| 548 |
else if (!strcmp(argv[i], "-level")) |
| 549 |
{ |
| 550 |
testlevel = atoi(argv[i+1]); |
| 551 |
printf ("testlevel = %i\n", testlevel); |
| 552 |
i++; |
| 553 |
} |
| 554 |
else if (!strcmp(argv[i], "-v")) |
| 555 |
{ |
| 556 |
printf ("verbose = true\n"); |
| 557 |
verbose = true; |
| 558 |
} |
| 559 |
else if (!strcmp (argv[i],"-nosort")) |
| 560 |
{ |
| 561 |
printf ("nosort = true\n"); |
| 562 |
nosort = true; |
| 563 |
} |
| 564 |
else if (!strcmp (argv[i],"-tmpin")) |
| 565 |
strcpy (inbase, "/tmp"); |
| 566 |
else if (!strcmp (argv[i],"-tmpout")) |
| 567 |
strcpy (outbase, "/tmp"); |
| 568 |
else if (argv[i][0] == '-') |
| 569 |
Error ("Unknown option \"%s\"", argv[i]); |
| 570 |
else |
| 571 |
break; |
| 572 |
} |
| 573 |
|
| 574 |
if (i != argc - 1) |
| 575 |
Error ("usage: vis [-threads #] [-level 0-4] [-fast] [-v] bspfile"); |
| 576 |
|
| 577 |
start = I_FloatTime (); |
| 578 |
|
| 579 |
ThreadSetDefault (); |
| 580 |
|
| 581 |
SetQdirFromPath (argv[i]); |
| 582 |
strcpy (source, ExpandArg(argv[i])); |
| 583 |
StripExtension (source); |
| 584 |
DefaultExtension (source, ".bsp"); |
| 585 |
|
| 586 |
sprintf (name, "%s%s", inbase, source); |
| 587 |
printf ("reading %s\n", name); |
| 588 |
LoadBSPFile (name); |
| 589 |
if (numnodes == 0 || numfaces == 0) |
| 590 |
Error ("Empty map"); |
| 591 |
|
| 592 |
sprintf (portalfile, "%s%s", inbase, ExpandArg(argv[i])); |
| 593 |
StripExtension (portalfile); |
| 594 |
strcat (portalfile, ".prt"); |
| 595 |
|
| 596 |
printf ("reading %s\n", portalfile); |
| 597 |
LoadPortals (portalfile); |
| 598 |
|
| 599 |
CalcVis (); |
| 600 |
|
| 601 |
CalcPHS (); |
| 602 |
|
| 603 |
visdatasize = vismap_p - dvisdata; |
| 604 |
printf ("visdatasize:%i compressed from %i\n", visdatasize, originalvismapsize*2); |
| 605 |
|
| 606 |
sprintf (name, "%s%s", outbase, source); |
| 607 |
printf ("writing %s\n", name); |
| 608 |
WriteBSPFile (name); |
| 609 |
|
| 610 |
end = I_FloatTime (); |
| 611 |
printf ("%5.1f seconds elapsed\n", end-start); |
| 612 |
|
| 613 |
return 0; |
| 614 |
} |
| 615 |
|