Debian Codegraph @

tools/quake2/extra/bsp/qbsp3/csg.c

raw symbols SCIP
1 /*
2 ===========================================================================
3 Copyright (C) 1997-2006 Id Software, Inc.
4
5 This file is part of Quake 2 Tools source code.
6
7 Quake 2 Tools source code is free software; you can redistribute it
8 and/or modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 Quake 2 Tools source code is distributed in the hope that it will be
13 useful, 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.
16
17 You should have received a copy of the GNU General Public License
18 along with Quake 2 Tools source code; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 ===========================================================================
21 */
22
23 #include "qbsp.h"
24
25 /*
26
27 tag all brushes with original contents
28 brushes may contain multiple contents
29 there will be no brush overlap after csg phase
30
31
32
33
34 each side has a count of the other sides it splits
35
36 the best split will be the one that minimizes the total split counts
37 of all remaining sides
38
39 precalc side on plane table
40
41 evaluate split side
42 {
43 cost = 0
44 for all sides
45 for all sides
46 get
47 if side splits side and splitside is on same child
48 cost++;
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; // not -1
61 if (*back && (*back)->sides[(*back)->numsides-1].texinfo == -1)
62 (*back)->sides[(*back)->numsides-1].texinfo = (*back)->sides[0].texinfo; // not -1
63 #endif
64 }
65
66 /*
67 ===============
68 SubtractBrush
69
70 Returns a list of brushes that remain after B is subtracted from A.
71 May by empty if A is contained inside B.
72
73 The originals are undisturbed.
74 ===============
75 */
76 bspbrush_t *SubtractBrush (bspbrush_t *a, bspbrush_t *b)
77 { // a - b = out (list)
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 { // add to list
91 front->next = out;
92 out = front;
93 }
94 in = back;
95 }
96 if (in)
97 FreeBrush (in);
98 else
99 { // didn't really intersect
100 FreeBrushList (out);
101 return a;
102 }
103 return out;
104 }
105
106 /*
107 ===============
108 IntersectBrush
109
110 Returns a single brush made up by the intersection of the
111 two provided brushes, or NULL if they are disjoint.
112
113 The originals are undisturbed.
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 BrushesDisjoint
144
145 Returns true if the two brushes definately do not intersect.
146 There will be false negatives for some non-axial combinations.
147 ===============
148 */
149 qboolean BrushesDisjoint (bspbrush_t *a, bspbrush_t *b)
150 {
151 int i, j;
152
153 // check bounding boxes
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; // bounding boxes don't overlap
158
159 // check for opposing planes
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; // opposite planes, so not touching
167 }
168 }
169
170 return false; // might intersect
171 }
172
173 /*
174 ===============
175 IntersectionContents
176
177 Returns a content word for the intersection of two brushes.
178 Some combinations will generate a combination (water + clip),
179 but most will be the stronger of the two contents.
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 ClipBrushToBox
201
202 Any planes shared with the box edge will be set to no texinfo
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 // remove any colinear faces
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 MakeBspBrushList
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 // make sure the brush has at least one face showing
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; // no faces at all
295 #endif
296 // if the brush is outside the clip area, skip it
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 // make a copy of the brush
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; // hints are always visible
317 }
318 VectorCopy (mb->mins, newbrush->mins);
319 VectorCopy (mb->maxs, newbrush->maxs);
320
321 //
322 // carve off anything outside the clip box
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 AddBspBrushListToTail
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 { // add to end of list
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 CullList
361
362 Builds a new list that doesn't hold the given brush
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 WriteBrushMap
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 BrushGE
431
432 Returns true if b1 is allowed to bite b2
433 ==================
434 */
435 qboolean BrushGE (bspbrush_t *b1, bspbrush_t *b2)
436 {
437 // detail brushes never bite structural brushes
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 ChopBrushes
449
450 Carves any intersecting solid brushes into the minimum number
451 of non-intersecting brushes.
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 // find tail
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; // didn't really intersect
496 if (!sub)
497 { // b1 is swallowed by b2
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; // didn't really intersect
509 if (!sub2)
510 { // b2 is swallowed by b1
511 FreeBrushList (sub);
512 head = CullList (b1, b2);
513 goto newlist;
514 }
515 c2 = CountBrushList (sub2);
516 }
517
518 if (!sub && !sub2)
519 continue; // neither one can bite
520
521 // only accept if it didn't fragment
522 // (commening this out allows full fragmentation)
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 { // b1 is no longer intersecting anything, so keep it
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 InitialBrushList
571 =================
572 */
573 bspbrush_t *InitialBrushList (bspbrush_t *list)
574 {
575 bspbrush_t *b;
576 bspbrush_t *out, *newb;
577 int i;
578
579 // only return brushes that have visible faces
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 // clear visible, so it must be set by MarkVisibleFaces_r
595 // to be used in the optimized list
596 for (i=0 ; i<b->numsides ; i++)
597 {
598 newb->sides[i].original = &b->sides[i];
599 // newb->sides[i].visible = true;
600 b->sides[i].visible = false;
601 }
602 }
603
604 return out;
605 }
606
607 /*
608 =================
609 OptimizedBrushList
610 =================
611 */
612 bspbrush_t *OptimizedBrushList (bspbrush_t *list)
613 {
614 bspbrush_t *b;
615 bspbrush_t *out, *newb;
616 int i;
617
618 // only return brushes that have visible faces
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 // WriteBrushList ("vis.gl", out, true);
633
634 return out;
635 }