| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "qbsp.h" |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
void SplitBrush2 (bspbrush_t *brush, int planenum, |
| 55 |
bspbrush_t **front, bspbrush_t **back) |
| 56 |
{ |
| 57 |
SplitBrush (brush, planenum, front, back); |
| 58 |
#if 0 |
| 59 |
if (*front && (*front)->sides[(*front)->numsides-1].texinfo == -1) |
| 60 |
(*front)->sides[(*front)->numsides-1].texinfo = (*front)->sides[0].texinfo; |
| 61 |
if (*back && (*back)->sides[(*back)->numsides-1].texinfo == -1) |
| 62 |
(*back)->sides[(*back)->numsides-1].texinfo = (*back)->sides[0].texinfo; |
| 63 |
#endif |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
bspbrush_t *SubtractBrush (bspbrush_t *a, bspbrush_t *b) |
| 77 |
{ |
| 78 |
int i; |
| 79 |
bspbrush_t *front, *back; |
| 80 |
bspbrush_t *out, *in; |
| 81 |
|
| 82 |
in = a; |
| 83 |
out = NULL; |
| 84 |
for (i=0 ; i<b->numsides && in ; i++) |
| 85 |
{ |
| 86 |
SplitBrush2 (in, b->sides[i].planenum, &front, &back); |
| 87 |
if (in != a) |
| 88 |
FreeBrush (in); |
| 89 |
if (front) |
| 90 |
{ |
| 91 |
front->next = out; |
| 92 |
out = front; |
| 93 |
} |
| 94 |
in = back; |
| 95 |
} |
| 96 |
if (in) |
| 97 |
FreeBrush (in); |
| 98 |
else |
| 99 |
{ |
| 100 |
FreeBrushList (out); |
| 101 |
return a; |
| 102 |
} |
| 103 |
return out; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
bspbrush_t *IntersectBrush (bspbrush_t *a, bspbrush_t *b) |
| 117 |
{ |
| 118 |
int i; |
| 119 |
bspbrush_t *front, *back; |
| 120 |
bspbrush_t *in; |
| 121 |
|
| 122 |
in = a; |
| 123 |
for (i=0 ; i<b->numsides && in ; i++) |
| 124 |
{ |
| 125 |
SplitBrush2 (in, b->sides[i].planenum, &front, &back); |
| 126 |
if (in != a) |
| 127 |
FreeBrush (in); |
| 128 |
if (front) |
| 129 |
FreeBrush (front); |
| 130 |
in = back; |
| 131 |
} |
| 132 |
|
| 133 |
if (in == a) |
| 134 |
return NULL; |
| 135 |
|
| 136 |
in->next = NULL; |
| 137 |
return in; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
qboolean BrushesDisjoint (bspbrush_t *a, bspbrush_t *b) |
| 150 |
{ |
| 151 |
int i, j; |
| 152 |
|
| 153 |
|
| 154 |
for (i=0 ; i<3 ; i++) |
| 155 |
if (a->mins[i] >= b->maxs[i] |
| 156 |
|| a->maxs[i] <= b->mins[i]) |
| 157 |
return true; |
| 158 |
|
| 159 |
|
| 160 |
for (i=0 ; i<a->numsides ; i++) |
| 161 |
{ |
| 162 |
for (j=0 ; j<b->numsides ; j++) |
| 163 |
{ |
| 164 |
if (a->sides[i].planenum == |
| 165 |
(b->sides[j].planenum^1) ) |
| 166 |
return true; |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
return false; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
int IntersectionContents (int c1, int c2) |
| 183 |
{ |
| 184 |
int out; |
| 185 |
|
| 186 |
out = c1 | c2; |
| 187 |
|
| 188 |
if (out & CONTENTS_SOLID) |
| 189 |
out = CONTENTS_SOLID; |
| 190 |
|
| 191 |
return out; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
int minplanenums[3]; |
| 196 |
int maxplanenums[3]; |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
bspbrush_t *ClipBrushToBox (bspbrush_t *brush, vec3_t clipmins, vec3_t clipmaxs) |
| 206 |
{ |
| 207 |
int i, j; |
| 208 |
bspbrush_t *front, *back; |
| 209 |
int p; |
| 210 |
|
| 211 |
for (j=0 ; j<2 ; j++) |
| 212 |
{ |
| 213 |
if (brush->maxs[j] > clipmaxs[j]) |
| 214 |
{ |
| 215 |
SplitBrush (brush, maxplanenums[j], &front, &back); |
| 216 |
if (front) |
| 217 |
FreeBrush (front); |
| 218 |
brush = back; |
| 219 |
if (!brush) |
| 220 |
return NULL; |
| 221 |
} |
| 222 |
if (brush->mins[j] < clipmins[j]) |
| 223 |
{ |
| 224 |
SplitBrush (brush, minplanenums[j], &front, &back); |
| 225 |
if (back) |
| 226 |
FreeBrush (back); |
| 227 |
brush = front; |
| 228 |
if (!brush) |
| 229 |
return NULL; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
for (i=0 ; i<brush->numsides ; i++) |
| 236 |
{ |
| 237 |
p = brush->sides[i].planenum & ~1; |
| 238 |
if (p == maxplanenums[0] || p == maxplanenums[1] |
| 239 |
|| p == minplanenums[0] || p == minplanenums[1]) |
| 240 |
{ |
| 241 |
brush->sides[i].texinfo = TEXINFO_NODE; |
| 242 |
brush->sides[i].visible = false; |
| 243 |
} |
| 244 |
} |
| 245 |
return brush; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
bspbrush_t *MakeBspBrushList (int startbrush, int endbrush, |
| 254 |
vec3_t clipmins, vec3_t clipmaxs) |
| 255 |
{ |
| 256 |
mapbrush_t *mb; |
| 257 |
bspbrush_t *brushlist, *newbrush; |
| 258 |
int i, j; |
| 259 |
int c_faces; |
| 260 |
int c_brushes; |
| 261 |
int numsides; |
| 262 |
int vis; |
| 263 |
vec3_t normal; |
| 264 |
float dist; |
| 265 |
|
| 266 |
for (i=0 ; i<2 ; i++) |
| 267 |
{ |
| 268 |
VectorClear (normal); |
| 269 |
normal[i] = 1; |
| 270 |
dist = clipmaxs[i]; |
| 271 |
maxplanenums[i] = FindFloatPlane (normal, dist); |
| 272 |
dist = clipmins[i]; |
| 273 |
minplanenums[i] = FindFloatPlane (normal, dist); |
| 274 |
} |
| 275 |
|
| 276 |
brushlist = NULL; |
| 277 |
c_faces = 0; |
| 278 |
c_brushes = 0; |
| 279 |
|
| 280 |
for (i=startbrush ; i<endbrush ; i++) |
| 281 |
{ |
| 282 |
mb = &mapbrushes[i]; |
| 283 |
|
| 284 |
numsides = mb->numsides; |
| 285 |
if (!numsides) |
| 286 |
continue; |
| 287 |
|
| 288 |
vis = 0; |
| 289 |
for (j=0 ; j<numsides ; j++) |
| 290 |
if (mb->original_sides[j].visible && mb->original_sides[j].winding) |
| 291 |
vis++; |
| 292 |
#if 0 |
| 293 |
if (!vis) |
| 294 |
continue; |
| 295 |
#endif |
| 296 |
|
| 297 |
for (j=0 ; j<3 ; j++) |
| 298 |
if (mb->mins[j] >= clipmaxs[j] |
| 299 |
|| mb->maxs[j] <= clipmins[j]) |
| 300 |
break; |
| 301 |
if (j != 3) |
| 302 |
continue; |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
newbrush = AllocBrush (mb->numsides); |
| 308 |
newbrush->original = mb; |
| 309 |
newbrush->numsides = mb->numsides; |
| 310 |
memcpy (newbrush->sides, mb->original_sides, numsides*sizeof(side_t)); |
| 311 |
for (j=0 ; j<numsides ; j++) |
| 312 |
{ |
| 313 |
if (newbrush->sides[j].winding) |
| 314 |
newbrush->sides[j].winding = CopyWinding (newbrush->sides[j].winding); |
| 315 |
if (newbrush->sides[j].surf & SURF_HINT) |
| 316 |
newbrush->sides[j].visible = true; |
| 317 |
} |
| 318 |
VectorCopy (mb->mins, newbrush->mins); |
| 319 |
VectorCopy (mb->maxs, newbrush->maxs); |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
newbrush = ClipBrushToBox (newbrush, clipmins, clipmaxs); |
| 325 |
if (!newbrush) |
| 326 |
continue; |
| 327 |
|
| 328 |
c_faces += vis; |
| 329 |
c_brushes++; |
| 330 |
|
| 331 |
newbrush->next = brushlist; |
| 332 |
brushlist = newbrush; |
| 333 |
} |
| 334 |
|
| 335 |
return brushlist; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
bspbrush_t *AddBrushListToTail (bspbrush_t *list, bspbrush_t *tail) |
| 344 |
{ |
| 345 |
bspbrush_t *walk, *next; |
| 346 |
|
| 347 |
for (walk=list ; walk ; walk=next) |
| 348 |
{ |
| 349 |
next = walk->next; |
| 350 |
walk->next = NULL; |
| 351 |
tail->next = walk; |
| 352 |
tail = walk; |
| 353 |
} |
| 354 |
|
| 355 |
return tail; |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
bspbrush_t *CullList (bspbrush_t *list, bspbrush_t *skip1) |
| 366 |
{ |
| 367 |
bspbrush_t *newlist; |
| 368 |
bspbrush_t *next; |
| 369 |
|
| 370 |
newlist = NULL; |
| 371 |
|
| 372 |
for ( ; list ; list = next) |
| 373 |
{ |
| 374 |
next = list->next; |
| 375 |
if (list == skip1) |
| 376 |
{ |
| 377 |
FreeBrush (list); |
| 378 |
continue; |
| 379 |
} |
| 380 |
list->next = newlist; |
| 381 |
newlist = list; |
| 382 |
} |
| 383 |
return newlist; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
void WriteBrushMap (char *name, bspbrush_t *list) |
| 393 |
{ |
| 394 |
FILE *f; |
| 395 |
side_t *s; |
| 396 |
int i; |
| 397 |
winding_t *w; |
| 398 |
|
| 399 |
printf ("writing %s\n", name); |
| 400 |
f = fopen (name, "wb"); |
| 401 |
if (!f) |
| 402 |
Error ("Can't write %s\b", name); |
| 403 |
|
| 404 |
fprintf (f, "{\n\"classname\" \"worldspawn\"\n"); |
| 405 |
|
| 406 |
for ( ; list ; list=list->next ) |
| 407 |
{ |
| 408 |
fprintf (f, "{\n"); |
| 409 |
for (i=0,s=list->sides ; i<list->numsides ; i++,s++) |
| 410 |
{ |
| 411 |
w = BaseWindingForPlane (mapplanes[s->planenum].normal, mapplanes[s->planenum].dist); |
| 412 |
|
| 413 |
fprintf (f,"( %i %i %i ) ", (int)w->p[0][0], (int)w->p[0][1], (int)w->p[0][2]); |
| 414 |
fprintf (f,"( %i %i %i ) ", (int)w->p[1][0], (int)w->p[1][1], (int)w->p[1][2]); |
| 415 |
fprintf (f,"( %i %i %i ) ", (int)w->p[2][0], (int)w->p[2][1], (int)w->p[2][2]); |
| 416 |
|
| 417 |
fprintf (f, "%s 0 0 0 1 1\n", texinfo[s->texinfo].texture); |
| 418 |
FreeWinding (w); |
| 419 |
} |
| 420 |
fprintf (f, "}\n"); |
| 421 |
} |
| 422 |
fprintf (f, "}\n"); |
| 423 |
|
| 424 |
fclose (f); |
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
qboolean BrushGE (bspbrush_t *b1, bspbrush_t *b2) |
| 436 |
{ |
| 437 |
|
| 438 |
if ( (b1->original->contents & CONTENTS_DETAIL) |
| 439 |
&& !(b2->original->contents & CONTENTS_DETAIL) ) |
| 440 |
return false; |
| 441 |
if (b1->original->contents & CONTENTS_SOLID) |
| 442 |
return true; |
| 443 |
return false; |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
bspbrush_t *ChopBrushes (bspbrush_t *head) |
| 455 |
{ |
| 456 |
bspbrush_t *b1, *b2, *next; |
| 457 |
bspbrush_t *tail; |
| 458 |
bspbrush_t *keep; |
| 459 |
bspbrush_t *sub, *sub2; |
| 460 |
int c1, c2; |
| 461 |
|
| 462 |
qprintf ("---- ChopBrushes ----\n"); |
| 463 |
qprintf ("original brushes: %i\n", CountBrushList (head)); |
| 464 |
|
| 465 |
#if 0 |
| 466 |
if (startbrush == 0) |
| 467 |
WriteBrushList ("before.gl", head, false); |
| 468 |
#endif |
| 469 |
keep = NULL; |
| 470 |
|
| 471 |
newlist: |
| 472 |
|
| 473 |
if (!head) |
| 474 |
return NULL; |
| 475 |
for (tail=head ; tail->next ; tail=tail->next) |
| 476 |
; |
| 477 |
|
| 478 |
for (b1=head ; b1 ; b1=next) |
| 479 |
{ |
| 480 |
next = b1->next; |
| 481 |
for (b2=b1->next ; b2 ; b2 = b2->next) |
| 482 |
{ |
| 483 |
if (BrushesDisjoint (b1, b2)) |
| 484 |
continue; |
| 485 |
|
| 486 |
sub = NULL; |
| 487 |
sub2 = NULL; |
| 488 |
c1 = 999999; |
| 489 |
c2 = 999999; |
| 490 |
|
| 491 |
if ( BrushGE (b2, b1) ) |
| 492 |
{ |
| 493 |
sub = SubtractBrush (b1, b2); |
| 494 |
if (sub == b1) |
| 495 |
continue; |
| 496 |
if (!sub) |
| 497 |
{ |
| 498 |
head = CullList (b1, b1); |
| 499 |
goto newlist; |
| 500 |
} |
| 501 |
c1 = CountBrushList (sub); |
| 502 |
} |
| 503 |
|
| 504 |
if ( BrushGE (b1, b2) ) |
| 505 |
{ |
| 506 |
sub2 = SubtractBrush (b2, b1); |
| 507 |
if (sub2 == b2) |
| 508 |
continue; |
| 509 |
if (!sub2) |
| 510 |
{ |
| 511 |
FreeBrushList (sub); |
| 512 |
head = CullList (b1, b2); |
| 513 |
goto newlist; |
| 514 |
} |
| 515 |
c2 = CountBrushList (sub2); |
| 516 |
} |
| 517 |
|
| 518 |
if (!sub && !sub2) |
| 519 |
continue; |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
if (c1 > 1 && c2 > 1) |
| 524 |
{ |
| 525 |
if (sub2) |
| 526 |
FreeBrushList (sub2); |
| 527 |
if (sub) |
| 528 |
FreeBrushList (sub); |
| 529 |
continue; |
| 530 |
} |
| 531 |
|
| 532 |
if (c1 < c2) |
| 533 |
{ |
| 534 |
if (sub2) |
| 535 |
FreeBrushList (sub2); |
| 536 |
tail = AddBrushListToTail (sub, tail); |
| 537 |
head = CullList (b1, b1); |
| 538 |
goto newlist; |
| 539 |
} |
| 540 |
else |
| 541 |
{ |
| 542 |
if (sub) |
| 543 |
FreeBrushList (sub); |
| 544 |
tail = AddBrushListToTail (sub2, tail); |
| 545 |
head = CullList (b1, b2); |
| 546 |
goto newlist; |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
if (!b2) |
| 551 |
{ |
| 552 |
b1->next = keep; |
| 553 |
keep = b1; |
| 554 |
} |
| 555 |
} |
| 556 |
|
| 557 |
qprintf ("output brushes: %i\n", CountBrushList (keep)); |
| 558 |
#if 0 |
| 559 |
{ |
| 560 |
WriteBrushList ("after.gl", keep, false); |
| 561 |
WriteBrushMap ("after.map", keep); |
| 562 |
} |
| 563 |
#endif |
| 564 |
return keep; |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
bspbrush_t *InitialBrushList (bspbrush_t *list) |
| 574 |
{ |
| 575 |
bspbrush_t *b; |
| 576 |
bspbrush_t *out, *newb; |
| 577 |
int i; |
| 578 |
|
| 579 |
|
| 580 |
out = NULL; |
| 581 |
for (b=list ; b ; b=b->next) |
| 582 |
{ |
| 583 |
#if 0 |
| 584 |
for (i=0 ; i<b->numsides ; i++) |
| 585 |
if (b->sides[i].visible) |
| 586 |
break; |
| 587 |
if (i == b->numsides) |
| 588 |
continue; |
| 589 |
#endif |
| 590 |
newb = CopyBrush (b); |
| 591 |
newb->next = out; |
| 592 |
out = newb; |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
for (i=0 ; i<b->numsides ; i++) |
| 597 |
{ |
| 598 |
newb->sides[i].original = &b->sides[i]; |
| 599 |
|
| 600 |
b->sides[i].visible = false; |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
return out; |
| 605 |
} |
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
bspbrush_t *OptimizedBrushList (bspbrush_t *list) |
| 613 |
{ |
| 614 |
bspbrush_t *b; |
| 615 |
bspbrush_t *out, *newb; |
| 616 |
int i; |
| 617 |
|
| 618 |
|
| 619 |
out = NULL; |
| 620 |
for (b=list ; b ; b=b->next) |
| 621 |
{ |
| 622 |
for (i=0 ; i<b->numsides ; i++) |
| 623 |
if (b->sides[i].visible) |
| 624 |
break; |
| 625 |
if (i == b->numsides) |
| 626 |
continue; |
| 627 |
newb = CopyBrush (b); |
| 628 |
newb->next = out; |
| 629 |
out = newb; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
return out; |
| 635 |
} |