forked from bnoordhuis/bspc
-
Notifications
You must be signed in to change notification settings - Fork 20
/
brushbsp.c
1869 lines (1676 loc) · 45.4 KB
/
brushbsp.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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "qbsp.h"
#include "l_mem.h"
#include "botlib/aasfile.h"
#include "aas_store.h"
#include "aas_cfg.h"
#include <stddef.h>
#include <assert.h>
/*
each side has a count of the other sides it splits
the best split will be the one that minimizes the total split counts
of all remaining sides
precalc side on plane table
evaluate split side
{
cost = 0
for all sides
for all sides
get
if side splits side and splitside is on same child
cost++;
}
*/
int c_nodes;
int c_nonvis;
int c_active_brushes;
int c_solidleafnodes;
int c_totalsides;
int c_brushmemory;
int c_peak_brushmemory;
int c_nodememory;
int c_peak_totalbspmemory;
// if a brush just barely pokes onto the other side,
// let it slide by without chopping
#define PLANESIDE_EPSILON 0.001
//0.1
//#ifdef DEBUG
typedef struct cname_s
{
int value;
char *name;
} cname_t;
cname_t contentnames[] =
{
{CONTENTS_SOLID,"CONTENTS_SOLID"},
{CONTENTS_WINDOW,"CONTENTS_WINDOW"},
{CONTENTS_AUX,"CONTENTS_AUX"},
{CONTENTS_LAVA,"CONTENTS_LAVA"},
{CONTENTS_SLIME,"CONTENTS_SLIME"},
{CONTENTS_WATER,"CONTENTS_WATER"},
{CONTENTS_MIST,"CONTENTS_MIST"},
{LAST_VISIBLE_CONTENTS,"LAST_VISIBLE_CONTENTS"},
{CONTENTS_AREAPORTAL,"CONTENTS_AREAPORTAL"},
{CONTENTS_PLAYERCLIP,"CONTENTS_PLAYERCLIP"},
{CONTENTS_MONSTERCLIP,"CONTENTS_MONSTERCLIP"},
{CONTENTS_CURRENT_0,"CONTENTS_CURRENT_0"},
{CONTENTS_CURRENT_90,"CONTENTS_CURRENT_90"},
{CONTENTS_CURRENT_180,"CONTENTS_CURRENT_180"},
{CONTENTS_CURRENT_270,"CONTENTS_CURRENT_270"},
{CONTENTS_CURRENT_UP,"CONTENTS_CURRENT_UP"},
{CONTENTS_CURRENT_DOWN,"CONTENTS_CURRENT_DOWN"},
{CONTENTS_ORIGIN,"CONTENTS_ORIGIN"},
{CONTENTS_MONSTER,"CONTENTS_MONSTER"},
{CONTENTS_DEADMONSTER,"CONTENTS_DEADMONSTER"},
{CONTENTS_DETAIL,"CONTENTS_DETAIL"},
{CONTENTS_Q2TRANSLUCENT,"CONTENTS_TRANSLUCENT"},
{CONTENTS_LADDER,"CONTENTS_LADDER"},
{0, 0}
};
void PrintContents(int contents)
{
int i;
for (i = 0; contentnames[i].value; i++)
{
if (contents & contentnames[i].value)
{
Log_Write("%s,", contentnames[i].name);
} //end if
} //end for
} //end of the function PrintContents
//#endif
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void ResetBrushBSP(void)
{
c_nodes = 0;
c_nonvis = 0;
c_active_brushes = 0;
c_solidleafnodes = 0;
c_totalsides = 0;
c_brushmemory = 0;
c_peak_brushmemory = 0;
c_nodememory = 0;
c_peak_totalbspmemory = 0;
} //end of the function ResetBrushBSP
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void FindBrushInTree (node_t *node, int brushnum)
{
bspbrush_t *b;
if (node->planenum == PLANENUM_LEAF)
{
for (b=node->brushlist ; b ; b=b->next)
if (b->original->brushnum == brushnum)
Log_Print ("here\n");
return;
}
FindBrushInTree(node->children[0], brushnum);
FindBrushInTree(node->children[1], brushnum);
} //end of the function FindBrushInTree
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void DrawBrushList (bspbrush_t *brush, node_t *node)
{
int i;
side_t *s;
GLS_BeginScene ();
for ( ; brush ; brush=brush->next)
{
for (i=0 ; i<brush->numsides ; i++)
{
s = &brush->sides[i];
if (!s->winding)
continue;
if (s->texinfo == TEXINFO_NODE)
GLS_Winding (s->winding, 1);
else if (!(s->flags & SFL_VISIBLE))
GLS_Winding (s->winding, 2);
else
GLS_Winding (s->winding, 0);
}
}
GLS_EndScene ();
} //end of the function DrawBrushList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void WriteBrushList (char *name, bspbrush_t *brush, qboolean onlyvis)
{
int i;
side_t *s;
FILE *f;
qprintf ("writing %s\n", name);
f = SafeOpenWrite (name);
for ( ; brush ; brush=brush->next)
{
for (i=0 ; i<brush->numsides ; i++)
{
s = &brush->sides[i];
if (!s->winding)
continue;
if (onlyvis && !(s->flags & SFL_VISIBLE))
continue;
OutputWinding (brush->sides[i].winding, f);
}
}
fclose (f);
} //end of the function WriteBrushList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void PrintBrush (bspbrush_t *brush)
{
int i;
printf ("brush: %p\n", brush);
for (i=0;i<brush->numsides ; i++)
{
pw(brush->sides[i].winding);
printf ("\n");
} //end for
} //end of the function PrintBrush
//===========================================================================
// Sets the mins/maxs based on the windings
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void BoundBrush (bspbrush_t *brush)
{
int i, j;
winding_t *w;
ClearBounds (brush->mins, brush->maxs);
for (i=0 ; i<brush->numsides ; i++)
{
w = brush->sides[i].winding;
if (!w)
continue;
for (j=0 ; j<w->numpoints ; j++)
AddPointToBounds (w->p[j], brush->mins, brush->maxs);
}
} //end of the function BoundBrush
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void CreateBrushWindings (bspbrush_t *brush)
{
int i, j;
winding_t *w;
side_t *side;
plane_t *plane;
for (i=0 ; i<brush->numsides ; i++)
{
side = &brush->sides[i];
plane = &mapplanes[side->planenum];
w = BaseWindingForPlane (plane->normal, plane->dist);
for (j=0 ; j<brush->numsides && w; j++)
{
if (i == j)
continue;
if (brush->sides[j].flags & SFL_BEVEL)
continue;
plane = &mapplanes[brush->sides[j].planenum^1];
ChopWindingInPlace (&w, plane->normal, plane->dist, 0); //CLIP_EPSILON);
}
side->winding = w;
}
BoundBrush (brush);
} //end of the function CreateBrushWindings
//===========================================================================
// Creates a new axial brush
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *BrushFromBounds (vec3_t mins, vec3_t maxs)
{
bspbrush_t *b;
int i;
vec3_t normal;
vec_t dist;
b = AllocBrush (6);
b->numsides = 6;
for (i=0 ; i<3 ; i++)
{
VectorClear (normal);
normal[i] = 1;
dist = maxs[i];
b->sides[i].planenum = FindFloatPlane (normal, dist);
normal[i] = -1;
dist = -mins[i];
b->sides[3+i].planenum = FindFloatPlane (normal, dist);
}
CreateBrushWindings (b);
return b;
} //end of the function BrushFromBounds
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int BrushOutOfBounds(bspbrush_t *brush, vec3_t mins, vec3_t maxs, float epsilon)
{
int i, j, n;
winding_t *w;
side_t *side;
for (i = 0; i < brush->numsides; i++)
{
side = &brush->sides[i];
w = side->winding;
for (j = 0; j < w->numpoints; j++)
{
for (n = 0; n < 3; n++)
{
if (w->p[j][n] < (mins[n] + epsilon) || w->p[j][n] > (maxs[n] - epsilon)) return true;
} //end for
} //end for
} //end for
return false;
} //end of the function BrushOutOfBounds
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
vec_t BrushVolume (bspbrush_t *brush)
{
int i;
winding_t *w;
vec3_t corner;
vec_t d, area, volume;
plane_t *plane;
if (!brush) return 0;
// grab the first valid point as the corner
w = NULL;
for (i = 0; i < brush->numsides; i++)
{
w = brush->sides[i].winding;
if (w) break;
} //end for
if (!w) return 0;
VectorCopy (w->p[0], corner);
// make tetrahedrons to all other faces
volume = 0;
for ( ; i < brush->numsides; i++)
{
w = brush->sides[i].winding;
if (!w) continue;
plane = &mapplanes[brush->sides[i].planenum];
d = -(DotProduct (corner, plane->normal) - plane->dist);
area = WindingArea(w);
volume += d * area;
} //end for
volume /= 3;
return volume;
} //end of the function BrushVolume
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int CountBrushList (bspbrush_t *brushes)
{
int c;
c = 0;
for ( ; brushes; brushes = brushes->next) c++;
return c;
} //end of the function CountBrushList
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
node_t *AllocNode (void)
{
node_t *node;
node = GetMemory(sizeof(*node));
memset (node, 0, sizeof(*node));
if (numthreads == 1)
{
c_nodememory += MemorySize(node);
} //end if
return node;
} //end of the function AllocNode
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *AllocBrush (int numsides)
{
bspbrush_t *bb;
size_t c;
c = sizeof(*bb) + sizeof(*bb->sides) * numsides;
bb = GetMemory(c);
memset (bb, 0, c);
if (numthreads == 1)
{
c_active_brushes++;
c_brushmemory += MemorySize(bb);
if (c_brushmemory > c_peak_brushmemory)
c_peak_brushmemory = c_brushmemory;
} //end if
return bb;
} //end of the function AllocBrush
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void FreeBrush (bspbrush_t *brushes)
{
int i;
for (i=0 ; i<brushes->numsides ; i++)
if (brushes->sides[i].winding)
FreeWinding(brushes->sides[i].winding);
if (numthreads == 1)
{
c_active_brushes--;
c_brushmemory -= MemorySize(brushes);
if (c_brushmemory < 0) c_brushmemory = 0;
} //end if
FreeMemory(brushes);
} //end of the function FreeBrush
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void FreeBrushList (bspbrush_t *brushes)
{
bspbrush_t *next;
for ( ; brushes; brushes = next)
{
next = brushes->next;
FreeBrush(brushes);
} //end for
} //end of the function FreeBrushList
//===========================================================================
// Duplicates the brush, the sides, and the windings
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
bspbrush_t *CopyBrush (bspbrush_t *brush)
{
bspbrush_t *newbrush;
size_t size;
int i;
size = sizeof(*newbrush) + sizeof(*brush->sides) * brush->numsides;
newbrush = AllocBrush (brush->numsides);
memcpy (newbrush, brush, size);
for (i=0 ; i<brush->numsides ; i++)
{
if (brush->sides[i].winding)
newbrush->sides[i].winding = CopyWinding (brush->sides[i].winding);
}
return newbrush;
} //end of the function CopyBrush
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
node_t *PointInLeaf (node_t *node, vec3_t point)
{
vec_t d;
plane_t *plane;
while (node->planenum != PLANENUM_LEAF)
{
plane = &mapplanes[node->planenum];
d = DotProduct (point, plane->normal) - plane->dist;
if (d > 0)
node = node->children[0];
else
node = node->children[1];
}
return node;
} //end of the function PointInLeaf
//===========================================================================
// Returns PSIDE_FRONT, PSIDE_BACK, or PSIDE_BOTH
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
#if 0
int BoxOnPlaneSide (vec3_t mins, vec3_t maxs, plane_t *plane)
{
int side;
int i;
vec3_t corners[2];
vec_t dist1, dist2;
// axial planes are easy
if (plane->type < 3)
{
side = 0;
if (maxs[plane->type] > plane->dist+PLANESIDE_EPSILON)
side |= PSIDE_FRONT;
if (mins[plane->type] < plane->dist-PLANESIDE_EPSILON)
side |= PSIDE_BACK;
return side;
}
// create the proper leading and trailing verts for the box
for (i=0 ; i<3 ; i++)
{
if (plane->normal[i] < 0)
{
corners[0][i] = mins[i];
corners[1][i] = maxs[i];
}
else
{
corners[1][i] = mins[i];
corners[0][i] = maxs[i];
}
}
dist1 = DotProduct (plane->normal, corners[0]) - plane->dist;
dist2 = DotProduct (plane->normal, corners[1]) - plane->dist;
side = 0;
if (dist1 >= PLANESIDE_EPSILON)
side = PSIDE_FRONT;
if (dist2 < PLANESIDE_EPSILON)
side |= PSIDE_BACK;
return side;
}
#else
int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, plane_t *p)
{
float dist1, dist2;
int sides;
// axial planes are easy
if (p->type < 3)
{
sides = 0;
if (emaxs[p->type] > p->dist+PLANESIDE_EPSILON) sides |= PSIDE_FRONT;
if (emins[p->type] < p->dist-PLANESIDE_EPSILON) sides |= PSIDE_BACK;
return sides;
} //end if
// general case
switch (p->signbits)
{
case 0:
dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
break;
case 1:
dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
break;
case 2:
dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
break;
case 3:
dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
break;
case 4:
dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
break;
case 5:
dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
break;
case 6:
dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
break;
case 7:
dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
break;
default:
dist1 = dist2 = 0; // shut up compiler
// assert( 0 );
break;
}
sides = 0;
if (dist1 - p->dist >= PLANESIDE_EPSILON) sides = PSIDE_FRONT;
if (dist2 - p->dist < PLANESIDE_EPSILON) sides |= PSIDE_BACK;
// assert(sides != 0);
return sides;
}
#endif
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int QuickTestBrushToPlanenum (bspbrush_t *brush, int planenum, int *numsplits)
{
int i, num;
plane_t *plane;
int s;
*numsplits = 0;
plane = &mapplanes[planenum];
#ifdef ME
//fast axial cases
if (plane->type < 3)
{
if (plane->dist + PLANESIDE_EPSILON < brush->mins[plane->type])
return PSIDE_FRONT;
if (plane->dist - PLANESIDE_EPSILON > brush->maxs[plane->type])
return PSIDE_BACK;
} //end if
#endif //ME*/
// if the brush actually uses the planenum,
// we can tell the side for sure
for (i = 0; i < brush->numsides; i++)
{
num = brush->sides[i].planenum;
if (num >= MAX_MAPFILE_PLANES)
Error ("bad planenum");
if (num == planenum)
return PSIDE_BACK|PSIDE_FACING;
if (num == (planenum ^ 1) )
return PSIDE_FRONT|PSIDE_FACING;
}
// box on plane side
s = BoxOnPlaneSide (brush->mins, brush->maxs, plane);
// if both sides, count the visible faces split
if (s == PSIDE_BOTH)
{
*numsplits += 3;
}
return s;
} //end of the function QuickTestBrushToPlanenum
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
int TestBrushToPlanenum (bspbrush_t *brush, int planenum,
int *numsplits, qboolean *hintsplit, int *epsilonbrush)
{
int i, j, num;
plane_t *plane;
int s = 0;
winding_t *w;
vec_t d, d_front, d_back;
int front, back;
int type;
float dist;
*numsplits = 0;
*hintsplit = false;
plane = &mapplanes[planenum];
#ifdef ME
//fast axial cases
type = plane->type;
if (type < 3)
{
dist = plane->dist;
if (dist + PLANESIDE_EPSILON < brush->mins[type]) return PSIDE_FRONT;
if (dist - PLANESIDE_EPSILON > brush->maxs[type]) return PSIDE_BACK;
if (brush->mins[type] < dist - PLANESIDE_EPSILON &&
brush->maxs[type] > dist + PLANESIDE_EPSILON) s = PSIDE_BOTH;
} //end if
if (s != PSIDE_BOTH)
#endif //ME
{
// if the brush actually uses the planenum,
// we can tell the side for sure
for (i = 0; i < brush->numsides; i++)
{
num = brush->sides[i].planenum;
if (num >= MAX_MAPFILE_PLANES) Error ("bad planenum");
if (num == planenum)
{
//we don't need to test this side plane again
brush->sides[i].flags |= SFL_TESTED;
return PSIDE_BACK|PSIDE_FACING;
} //end if
if (num == (planenum ^ 1) )
{
//we don't need to test this side plane again
brush->sides[i].flags |= SFL_TESTED;
return PSIDE_FRONT|PSIDE_FACING;
} //end if
} //end for
// box on plane side
s = BoxOnPlaneSide (brush->mins, brush->maxs, plane);
if (s != PSIDE_BOTH) return s;
} //end if
// if both sides, count the visible faces split
d_front = d_back = 0;
for (i = 0; i < brush->numsides; i++)
{
if (brush->sides[i].texinfo == TEXINFO_NODE)
continue; // on node, don't worry about splits
if (!(brush->sides[i].flags & SFL_VISIBLE))
continue; // we don't care about non-visible
w = brush->sides[i].winding;
if (!w) continue;
front = back = 0;
for (j = 0; j < w->numpoints; j++)
{
d = DotProduct(w->p[j], plane->normal) - plane->dist;
if (d > d_front) d_front = d;
if (d < d_back) d_back = d;
if (d > 0.1) // PLANESIDE_EPSILON)
front = 1;
if (d < -0.1) // PLANESIDE_EPSILON)
back = 1;
} //end for
if (front && back)
{
if ( !(brush->sides[i].surf & SURF_SKIP) )
{
(*numsplits)++;
if (brush->sides[i].surf & SURF_HINT)
{
*hintsplit = true;
} //end if
} //end if
} //end if
} //end for
if ( (d_front > 0.0 && d_front < 1.0)
|| (d_back < 0.0 && d_back > -1.0) )
(*epsilonbrush)++;
#if 0
if (*numsplits == 0)
{ // didn't really need to be split
if (front) s = PSIDE_FRONT;
else if (back) s = PSIDE_BACK;
else s = 0;
}
#endif
return s;
} //end of the function TestBrushToPlanenum
//===========================================================================
// Returns true if the winding would be crunched out of
// existance by the vertex snapping.
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
#define EDGE_LENGTH 0.2
qboolean WindingIsTiny (winding_t *w)
{
#if 0
if (WindingArea (w) < 1)
return true;
return false;
#else
int i, j;
vec_t len;
vec3_t delta;
int edges;
edges = 0;
for (i=0 ; i<w->numpoints ; i++)
{
j = i == w->numpoints - 1 ? 0 : i+1;
VectorSubtract (w->p[j], w->p[i], delta);
len = VectorLength (delta);
if (len > EDGE_LENGTH)
{
if (++edges == 3)
return false;
}
}
return true;
#endif
} //end of the function WindingIsTiny
//===========================================================================
// Returns true if the winding still has one of the points
// from basewinding for plane
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean WindingIsHuge (winding_t *w)
{
int i, j;
for (i=0 ; i<w->numpoints ; i++)
{
for (j=0 ; j<3 ; j++)
if (w->p[i][j] < -BOGUS_RANGE+1 || w->p[i][j] > BOGUS_RANGE-1)
return true;
}
return false;
} //end of the function WindingIsHuge
//===========================================================================
// creates a leaf out of the given nodes with the given brushes
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void LeafNode(node_t *node, bspbrush_t *brushes)
{
bspbrush_t *b;
int i;
node->side = NULL;
node->planenum = PLANENUM_LEAF;
node->contents = 0;
for (b = brushes; b; b = b->next)
{
// if the brush is solid and all of its sides are on nodes,
// it eats everything
if (b->original->contents & CONTENTS_SOLID)
{
for (i=0 ; i<b->numsides ; i++)
if (b->sides[i].texinfo != TEXINFO_NODE)
break;
if (i == b->numsides)
{
node->contents = CONTENTS_SOLID;
break;
} //end if
} //end if
node->contents |= b->original->contents;
} //end for
if (create_aas)
{
node->expansionbboxes = 0;
node->contents = 0;
for (b = brushes; b; b = b->next)
{
node->expansionbboxes |= b->original->expansionbbox;
node->contents |= b->original->contents;
if (b->original->modelnum)
node->modelnum = b->original->modelnum;
} //end for
if (node->contents & CONTENTS_SOLID)
{
if (node->expansionbboxes != cfg.allpresencetypes)
{
node->contents &= ~CONTENTS_SOLID;
} //end if
} //end if
} //end if
node->brushlist = brushes;
} //end of the function LeafNode
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
void CheckPlaneAgainstParents (int pnum, node_t *node)
{
node_t *p;
for (p = node->parent; p; p = p->parent)
{
if (p->planenum == pnum) Error("Tried parent");
} //end for
} //end of the function CheckPlaneAgainstParants
//===========================================================================
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
qboolean CheckPlaneAgainstVolume (int pnum, node_t *node)
{
bspbrush_t *front, *back;
qboolean good;
SplitBrush (node->volume, pnum, &front, &back);
good = (front && back);
if (front) FreeBrush (front);
if (back) FreeBrush (back);
return good;
} //end of the function CheckPlaneAgaintsVolume
//===========================================================================
// Using a hueristic, choses one of the sides out of the brushlist
// to partition the brushes with.
// Returns NULL if there are no valid planes to split with..
//
// Parameter: -
// Returns: -
// Changes Globals: -
//===========================================================================
side_t *SelectSplitSide (bspbrush_t *brushes, node_t *node)
{
int value, bestvalue;
bspbrush_t *brush, *test;
side_t *side, *bestside;
int i, pass, numpasses;
int pnum;
int s;
int front, back, both, facing, splits;
int bsplits;
int epsilonbrush;
qboolean hintsplit = false;
bestside = NULL;
bestvalue = -99999;
// the search order goes: visible-structural, visible-detail,
// nonvisible-structural, nonvisible-detail.
// If any valid plane is available in a pass, no further
// passes will be tried.
numpasses = 2;
for (pass = 0; pass < numpasses; pass++)
{
for (brush = brushes; brush; brush = brush->next)
{
// only check detail the second pass
// if ( (pass & 1) && !(brush->original->contents & CONTENTS_DETAIL) )
// continue;
// if ( !(pass & 1) && (brush->original->contents & CONTENTS_DETAIL) )
// continue;
for (i = 0; i < brush->numsides; i++)
{
side = brush->sides + i;
// if (side->flags & SFL_BEVEL)
// continue; // never use a bevel as a spliter