-
Notifications
You must be signed in to change notification settings - Fork 0
/
csg.c
744 lines (673 loc) · 17.8 KB
/
csg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
#include "qe3.h"
#include "winding.h"
/*
==============
CSG_SplitBrushByFace
The incoming brush is NOT freed.
The incoming face is NOT left referenced.
==============
*/
void CSG_SplitBrushByFace (brush_t *in, face_t *f, brush_t **front, brush_t **back)
{
brush_t *b;
face_t *nf;
vec3_t temp;
b = Brush_Clone (in);
nf = Face_Clone (f);
nf->texdef = b->brush_faces->texdef;
nf->next = b->brush_faces;
b->brush_faces = nf;
Brush_Build( b );
Brush_RemoveEmptyFaces ( b );
if ( !b->brush_faces )
{ // completely clipped away
Brush_Free (b);
*back = NULL;
}
else
{
Entity_LinkBrush (in->owner, b);
*back = b;
}
b = Brush_Clone (in);
nf = Face_Clone (f);
// swap the plane winding
VectorCopy (nf->planepts[0], temp);
VectorCopy (nf->planepts[1], nf->planepts[0]);
VectorCopy (temp, nf->planepts[1]);
nf->texdef = b->brush_faces->texdef;
nf->next = b->brush_faces;
b->brush_faces = nf;
Brush_Build( b );
Brush_RemoveEmptyFaces ( b );
if ( !b->brush_faces )
{ // completely clipped away
Brush_Free (b);
*front = NULL;
}
else
{
Entity_LinkBrush (in->owner, b);
*front = b;
}
}
/*
=============
CSG_MakeHollow
=============
*/
void CSG_MakeHollow (void)
{
brush_t *b, *front, *back, *next;
face_t *f;
face_t split;
vec3_t move;
int i;
for (b = selected_brushes.next ; b != &selected_brushes ; b=next)
{
next = b->next;
if (b->owner->eclass->fixedsize || b->hiddenbrush)
continue;
for (f = b->brush_faces ; f ; f=f->next)
{
split = *f;
VectorScale (f->plane.normal, g_qeglobals.d_gridsize, move);
for (i=0 ; i<3 ; i++)
VectorSubtract (split.planepts[i], move, split.planepts[i]);
CSG_SplitBrushByFace (b, &split, &front, &back);
if (back)
Brush_Free (back);
if (front)
Brush_AddToList (front, &selected_brushes);
}
Brush_Free (b);
}
Sys_UpdateWindows (W_ALL);
}
/*
=============
Brush_Merge
Returns a new brush that is created by merging brush1 and brush2.
May return NULL if brush1 and brush2 do not create a convex brush when merged.
The input brushes brush1 and brush2 stay intact.
if onlyshape is true then the merge is allowed based on the shape only
otherwise the texture references of faces in the same plane have to
be the same as well.
=============
*/
brush_t *Brush_Merge(brush_t *brush1, brush_t *brush2, int onlyshape)
{
int i, shared;
brush_t *newbrush;
face_t *face1, *face2, *newface, *f;
// check for bounding box overlapp
for (i = 0; i < 3; i++)
{
if (brush1->mins[i] > brush2->maxs[i] + ON_EPSILON
|| brush1->maxs[i] < brush2->mins[i] - ON_EPSILON)
{
// never merge if the brushes overlap
return NULL;
}
}
//
shared = 0;
// check if the new brush would be convex... flipped planes make a brush non-convex
for (face1 = brush1->brush_faces; face1; face1 = face1->next)
{
// don't check the faces of brush 1 and 2 touching each other
for (face2 = brush2->brush_faces; face2; face2 = face2->next)
{
if (Plane_Equal(&face1->plane, &face2->plane, true))
{
shared++;
// there may only be ONE shared side
if (shared > 1)
return NULL;
break;
}
}
// if this face plane is shared
if (face2) continue;
//
for (face2 = brush2->brush_faces; face2; face2 = face2->next)
{
// don't check the faces of brush 1 and 2 touching each other
for (f = brush1->brush_faces; f; f = f->next)
{
if (Plane_Equal(&face2->plane, &f->plane, true)) break;
}
if (f)
continue;
//
if (Plane_Equal(&face1->plane, &face2->plane, false))
{
//if the texture references should be the same but are not
if (!onlyshape && stricmp(face1->texdef.name, face2->texdef.name) != 0) return NULL;
continue;
}
//
if (Winding_PlanesConcave(face1->face_winding, face2->face_winding,
face1->plane.normal, face2->plane.normal,
face1->plane.dist, face2->plane.dist))
{
return NULL;
} //end if
} //end for
} //end for
//
newbrush = Brush_Alloc();
//
for (face1 = brush1->brush_faces; face1; face1 = face1->next)
{
// don't add the faces of brush 1 and 2 touching each other
for (face2 = brush2->brush_faces; face2; face2 = face2->next)
{
if (Plane_Equal(&face1->plane, &face2->plane, true))
break;
}
if (face2)
continue;
// don't add faces with the same plane twice
for (f = newbrush->brush_faces; f; f = f->next)
{
if (Plane_Equal(&face1->plane, &f->plane, false))
break;
if (Plane_Equal(&face1->plane, &f->plane, true))
break;
}
if (f)
continue;
//
newface = Face_Alloc();
newface->texdef = face1->texdef;
VectorCopy(face1->planepts[0], newface->planepts[0]);
VectorCopy(face1->planepts[1], newface->planepts[1]);
VectorCopy(face1->planepts[2], newface->planepts[2]);
newface->plane = face1->plane;
newface->next = newbrush->brush_faces;
newbrush->brush_faces = newface;
}
//
for (face2 = brush2->brush_faces; face2; face2 = face2->next)
{
// don't add the faces of brush 1 and 2 touching each other
for (face1 = brush1->brush_faces; face1; face1 = face1->next)
{
if (Plane_Equal(&face2->plane, &face1->plane, true))
break;
}
if (face1)
continue;
// don't add faces with the same plane twice
for (f = newbrush->brush_faces; f; f = f->next)
{
if (Plane_Equal(&face2->plane, &f->plane, false))
break;
if (Plane_Equal(&face2->plane, &f->plane, true))
break;
}
if (f)
continue;
//
newface = Face_Alloc();
newface->texdef = face2->texdef;
VectorCopy(face2->planepts[0], newface->planepts[0]);
VectorCopy(face2->planepts[1], newface->planepts[1]);
VectorCopy(face2->planepts[2], newface->planepts[2]);
newface->plane = face2->plane;
newface->next = newbrush->brush_faces;
newbrush->brush_faces = newface;
}
// link the new brush to an entity
Entity_LinkBrush (brush1->owner, newbrush);
// build windings for the faces
Brush_BuildWindings(newbrush);
return newbrush;
}
/*
=============
Brush_MergeListPairs
Returns a list with merged brushes.
Tries to merge brushes pair wise.
The input list is destroyed.
Input and output should be a single linked list using .next
=============
*/
brush_t *Brush_MergeListPairs(brush_t *brushlist, int onlyshape)
{
int nummerges, merged;
brush_t *b1, *b2, *tail, *newbrush, *newbrushlist;
brush_t *lastb2;
if (!brushlist) return NULL;
nummerges = 0;
do
{
for (tail = brushlist; tail; tail = tail->next)
{
if (!tail->next) break;
}
merged = 0;
newbrushlist = NULL;
for (b1 = brushlist; b1; b1 = brushlist)
{
lastb2 = b1;
for (b2 = b1->next; b2; b2 = b2->next)
{
newbrush = Brush_Merge(b1, b2, onlyshape);
if (newbrush)
{
tail->next = newbrush;
lastb2->next = b2->next;
brushlist = brushlist->next;
b1->next = b1->prev = NULL;
b2->next = b2->prev = NULL;
Brush_Free(b1);
Brush_Free(b2);
for (tail = brushlist; tail; tail = tail->next)
{
if (!tail->next) break;
} //end for
merged++;
nummerges++;
break;
}
lastb2 = b2;
}
//if b1 can't be merged with any of the other brushes
if (!b2)
{
brushlist = brushlist->next;
//keep b1
b1->next = newbrushlist;
newbrushlist = b1;
}
}
brushlist = newbrushlist;
} while(merged);
return newbrushlist;
}
/*
=============
Brush_MergeList
Tries to merge all brushes in the list into one new brush.
The input brush list stays intact.
Returns NULL if no merged brush can be created.
To create a new brush the brushes in the list may not overlap and
the outer faces of the brushes together should make a new convex brush.
if onlyshape is true then the merge is allowed based on the shape only
otherwise the texture references of faces in the same plane have to
be the same as well.
=============
*/
brush_t *Brush_MergeList(brush_t *brushlist, int onlyshape)
{
brush_t *brush1, *brush2, *brush3, *newbrush;
face_t *face1, *face2, *face3, *newface, *f;
if (!brushlist) return NULL;
for (brush1 = brushlist; brush1; brush1 = brush1->next)
{
// check if the new brush would be convex... flipped planes make a brush concave
for (face1 = brush1->brush_faces; face1; face1 = face1->next)
{
// don't check face1 if it touches another brush
for (brush2 = brushlist; brush2; brush2 = brush2->next)
{
if (brush2 == brush1) continue;
for (face2 = brush2->brush_faces; face2; face2 = face2->next)
{
if (Plane_Equal(&face1->plane, &face2->plane, true))
{
break;
}
}
if (face2) break;
}
// if face1 touches another brush
if (brush2) continue;
//
for (brush2 = brush1->next; brush2; brush2 = brush2->next)
{
// don't check the faces of brush 2 touching another brush
for (face2 = brush2->brush_faces; face2; face2 = face2->next)
{
for (brush3 = brushlist; brush3; brush3 = brush3->next)
{
if (brush3 == brush2) continue;
for (face3 = brush3->brush_faces; face3; face3 = face3->next)
{
if (Plane_Equal(&face2->plane, &face3->plane, true)) break;
}
if (face3) break;
}
// if face2 touches another brush
if (brush3) continue;
//
if (Plane_Equal(&face1->plane, &face2->plane, false))
{
//if the texture references should be the same but are not
if (!onlyshape && stricmp(face1->texdef.name, face2->texdef.name) != 0) return NULL;
continue;
}
//
if (Winding_PlanesConcave(face1->face_winding, face2->face_winding,
face1->plane.normal, face2->plane.normal,
face1->plane.dist, face2->plane.dist))
{
return NULL;
}
}
}
}
}
//
newbrush = Brush_Alloc();
//
for (brush1 = brushlist; brush1; brush1 = brush1->next)
{
for (face1 = brush1->brush_faces; face1; face1 = face1->next)
{
// don't add face1 to the new brush if it touches another brush
for (brush2 = brushlist; brush2; brush2 = brush2->next)
{
if (brush2 == brush1) continue;
for (face2 = brush2->brush_faces; face2; face2 = face2->next)
{
if (Plane_Equal(&face1->plane, &face2->plane, true))
{
break;
}
}
if (face2) break;
}
if (brush2) continue;
// don't add faces with the same plane twice
for (f = newbrush->brush_faces; f; f = f->next)
{
if (Plane_Equal(&face1->plane, &f->plane, false))
break;
if (Plane_Equal(&face1->plane, &f->plane, true))
break;
}
if (f)
continue;
//
newface = Face_Alloc();
newface->texdef = face1->texdef;
VectorCopy(face1->planepts[0], newface->planepts[0]);
VectorCopy(face1->planepts[1], newface->planepts[1]);
VectorCopy(face1->planepts[2], newface->planepts[2]);
newface->plane = face1->plane;
newface->next = newbrush->brush_faces;
newbrush->brush_faces = newface;
}
}
// link the new brush to an entity
Entity_LinkBrush (brushlist->owner, newbrush);
// build windings for the faces
Brush_BuildWindings( newbrush);
return newbrush;
}
/*
=============
Brush_Subtract
Returns a list of brushes that remain after B is subtracted from A.
May by empty if A is contained inside B.
The originals are undisturbed.
=============
*/
brush_t *Brush_Subtract(brush_t *a, brush_t *b)
{
// a - b = out (list)
brush_t *front, *back;
brush_t *in, *out, *next;
face_t *f;
in = a;
out = NULL;
for (f = b->brush_faces; f && in; f = f->next)
{
CSG_SplitBrushByFace(in, f, &front, &back);
if (in != a) Brush_Free(in);
if (front)
{ // add to list
front->next = out;
out = front;
}
in = back;
}
//NOTE: in != a just in case brush b has no faces
if (in && in != a)
{
Brush_Free(in);
}
else
{ //didn't really intersect
for (b = out; b; b = next)
{
next = b->next;
b->next = b->prev = NULL;
Brush_Free(b);
}
return a;
}
return out;
}
/*
=============
CSG_Subtract
=============
*/
void CSG_Subtract (void)
{
brush_t *b, *s, *fragments, *nextfragment, *frag, *next, *snext;
brush_t fragmentlist;
int i, numfragments, numbrushes;
Sys_Printf ("Subtracting...\n");
if (selected_brushes.next == &selected_brushes)
{
Sys_Printf ("No brushes selected.\n");
return;
}
fragmentlist.next = &fragmentlist;
fragmentlist.prev = &fragmentlist;
numfragments = 0;
numbrushes = 0;
for (b = selected_brushes.next ; b != &selected_brushes ; b=next)
{
next = b->next;
if (b->owner->eclass->fixedsize)
continue; // can't use texture from a fixed entity, so don't subtract
// chop all fragments further up
for (s = fragmentlist.next; s != &fragmentlist; s = snext)
{
snext = s->next;
for (i=0 ; i<3 ; i++)
if (b->mins[i] >= s->maxs[i] - ON_EPSILON
|| b->maxs[i] <= s->mins[i] + ON_EPSILON)
break;
if (i != 3)
continue; // definately don't touch
fragments = Brush_Subtract(s, b);
// if the brushes did not really intersect
if (fragments == s)
continue;
// try to merge fragments
fragments = Brush_MergeListPairs(fragments, true);
// add the fragments to the list
for (frag = fragments; frag; frag = nextfragment)
{
nextfragment = frag->next;
frag->next = NULL;
frag->owner = s->owner;
Brush_AddToList(frag, &fragmentlist);
}
// free the original brush
Brush_Free(s);
}
// chop any active brushes up
for (s = active_brushes.next; s != &active_brushes; s = snext)
{
snext = s->next;
if (s->owner->eclass->fixedsize || s->hiddenbrush)
continue;
for (i=0 ; i<3 ; i++)
if (b->mins[i] >= s->maxs[i] - ON_EPSILON
|| b->maxs[i] <= s->mins[i] + ON_EPSILON)
break;
if (i != 3)
continue; // definately don't touch
fragments = Brush_Subtract(s, b);
// if the brushes did not really intersect
if (fragments == s)
continue;
// one extra brush chopped up
numbrushes++;
// try to merge fragments
fragments = Brush_MergeListPairs(fragments, true);
// add the fragments to the list
for (frag = fragments; frag; frag = nextfragment)
{
nextfragment = frag->next;
frag->next = NULL;
frag->owner = s->owner;
Brush_AddToList(frag, &fragmentlist);
}
// free the original brush
Brush_Free(s);
}
}
// move all fragments to the active brush list
for (frag = fragmentlist.next; frag != &fragmentlist; frag = nextfragment)
{
nextfragment = frag->next;
numfragments++;
Brush_RemoveFromList(frag);
Brush_AddToList(frag, &active_brushes);
}
if (numfragments == 0)
{
Sys_Printf ("Selected brush%s did not intersect with any other brushes.\n",
(selected_brushes.next->next == &selected_brushes) ? "":"es");
return;
}
Sys_Printf ("Done. (created %d fragment%s out of %d brush%s)\n", numfragments, (numfragments == 1)?"":"s",
numbrushes, (numbrushes == 1)?"":"es");
Sys_UpdateWindows(W_ALL);
}
/*
=============
CSG_Subtract old
=============
*/
/*
void CSG_Subtract (void)
{
brush_t *b, *s, *frag, *front, *back, *next, *snext;
face_t *f;
int i;
Sys_Printf ("Subtracting...\n");
for (b = selected_brushes.next ; b != &selected_brushes ; b=next)
{
next = b->next;
if (b->owner->eclass->fixedsize)
continue; // can't use texture from a fixed entity, so don't subtract
for (s=active_brushes.next ; s != &active_brushes ; s=snext)
{
snext = s->next;
if (s->owner->eclass->fixedsize)
continue;
for (i=0 ; i<3 ; i++)
if (b->mins[i] >= s->maxs[i] - ON_EPSILON
|| b->maxs[i] <= s->mins[i] + ON_EPSILON)
break;
if (i != 3)
continue; // definately don't touch
frag = s;
for (f = b->brush_faces ; f && frag ; f=f->next)
{
CSG_SplitBrushByFace (frag, f, &front, &back);
Brush_Free (frag);
frag = back;
if (front)
Brush_AddToList (front, &active_brushes);
}
if (frag)
Brush_Free (frag);
}
}
Sys_Printf ("Done.\n");
Sys_UpdateWindows (W_ALL);
}
*/
/*
=============
CSG_Merge
=============
*/
void CSG_Merge(void)
{
brush_t *b, *next, *newlist, *newbrush;
struct entity_s *owner;
Sys_Printf ("Merging...\n");
if (selected_brushes.next == &selected_brushes)
{
Sys_Printf ("No brushes selected.\n");
return;
}
if (selected_brushes.next->next == &selected_brushes)
{
Sys_Printf ("At least two brushes have to be selected.\n");
return;
}
owner = selected_brushes.next->owner;
for (b = selected_brushes.next; b != &selected_brushes; b = next)
{
next = b->next;
if (b->owner->eclass->fixedsize)
{
// can't use texture from a fixed entity, so don't subtract
Sys_Printf ("Cannot add fixed size entities.\n");
return;
}
if (b->owner != owner)
{
Sys_Printf ("Cannot add brushes from different entities.\n");
return;
}
}
newlist = NULL;
for (b = selected_brushes.next; b != &selected_brushes; b = next)
{
next = b->next;
Brush_RemoveFromList(b);
b->next = newlist;
b->prev = NULL;
newlist = b;
}
newbrush = Brush_MergeList(newlist, true);
// if the new brush would not be convex
if (!newbrush)
{
// add the brushes back into the selection
for (b = newlist; b; b = next)
{
next = b->next;
b->next = NULL;
b->prev = NULL;
Brush_AddToList(b, &selected_brushes);
}
Sys_Printf ("Cannot add a set of brushes with a concave hull.\n");
return;
}
// free the original brushes
for (b = newlist; b; b = next)
{
next = b->next;
b->next = NULL;
b->prev = NULL;
Brush_Free(b);
}
Brush_AddToList(newbrush, &selected_brushes);
Sys_Printf ("Done.\n");
Sys_UpdateWindows (W_ALL);
}