forked from id-tech-3-tools/map-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacebsp.cpp
More file actions
560 lines (451 loc) · 12.5 KB
/
Copy pathfacebsp.cpp
File metadata and controls
560 lines (451 loc) · 12.5 KB
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
/* -------------------------------------------------------------------------------
Copyright (C) 1999-2007 id Software, Inc. and contributors.
For a list of contributors, see the accompanying CONTRIBUTORS file.
This file is part of GtkRadiant.
GtkRadiant 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.
GtkRadiant 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 GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
----------------------------------------------------------------------------------
This code has been altered significantly from its original form, to support
several games based on the Quake III Arena engine, in the form of "Q3Map2."
------------------------------------------------------------------------------- */
/* marker */
#define FACEBSP_C
/* dependencies */
#include "q3map2.h"
int c_faceLeafs;
/*
================
AllocBspFace
================
*/
face_t *AllocBspFace( void ) {
face_t *f;
f = static_cast<face_t*>(safe_malloc(sizeof(*f)));
memset( f, 0, sizeof( *f ) );
return f;
}
/*
================
FreeBspFace
================
*/
void FreeBspFace( face_t *f ) {
if ( f->w ) {
FreeWinding( f->w );
}
free( f );
}
/*
SelectSplitPlaneNum()
finds the best split plane for this node
*/
static void SelectSplitPlaneNum( node_t *node, face_t *list, int *splitPlaneNum, int *compileFlags ){
face_t *split;
face_t *check;
face_t *bestSplit;
int splits, facing, front, back;
int side;
plane_t *plane;
int value, bestValue;
int i;
vec3_t normal;
float dist;
int planenum;
float sizeBias;
//int frontC,backC,splitsC,facingC;
/* ydnar: set some defaults */
*splitPlaneNum = -1; /* leaf */
*compileFlags = 0;
/* ydnar 2002-06-24: changed this to split on z-axis as well */
/* ydnar 2002-09-21: changed blocksize to be a vector, so mappers can specify a 3 element value */
/* if it is crossing a block boundary, force a split */
for ( i = 0; i < 3; i++ )
{
if ( blockSize[ i ] <= 0 ) {
continue;
}
dist = blockSize[ i ] * ( floor( node->mins[ i ] / blockSize[ i ] ) + 1 );
if ( node->maxs[ i ] > dist ) {
VectorClear( normal );
normal[ i ] = 1;
planenum = FindFloatPlane( normal, dist, 0, NULL );
*splitPlaneNum = planenum;
return;
}
}
/* pick one of the face planes */
bestValue = -99999;
bestSplit = list;
// div0: this check causes detail/structural mixes
//for( split = list; split; split = split->next )
// split->checked = qfalse;
for ( split = list; split; split = split->next )
{
//if ( split->checked )
// continue;
plane = &mapplanes[ split->planenum ];
splits = 0;
facing = 0;
front = 0;
back = 0;
for ( check = list ; check ; check = check->next ) {
if ( check->planenum == split->planenum ) {
facing++;
//check->checked = qtrue; // won't need to test this plane again
continue;
}
side = WindingOnPlaneSide( check->w, plane->normal, plane->dist );
if ( side == SIDE_CROSS ) {
splits++;
}
else if ( side == SIDE_FRONT ) {
front++;
}
else if ( side == SIDE_BACK ) {
back++;
}
}
if ( bspAlternateSplitWeights ) {
// from 27
//Bigger is better
sizeBias = WindingArea( split->w );
//Base score = 20000 perfectly balanced
value = 20000 - ( abs( front - back ) );
value -= plane->counter; // If we've already used this plane sometime in the past try not to use it again
value -= facing ; // if we're going to have alot of other surfs use this plane, we want to get it in quickly.
value -= splits * 5; //more splits = bad
value += sizeBias * 10; //We want a huge score bias based on plane size
}
else
{
value = 5 * facing - 5 * splits; // - abs(front-back);
if ( plane->type < 3 ) {
value += 5; // axial is better
}
}
value += split->priority; // prioritize hints higher
if ( value > bestValue ) {
bestValue = value;
bestSplit = split;
//frontC=front;
//backC=back;
//splitsC=splits;
//facingC=facing;
}
}
/* nothing, we have a leaf */
if ( bestValue == -99999 ) {
return;
}
//Sys_FPrintf (SYS_VRB, "F: %d B:%d S:%d FA:%ds\n",frontC,backC,splitsC,facingC );
/* set best split data */
*splitPlaneNum = bestSplit->planenum;
*compileFlags = bestSplit->compileFlags;
#if 0
if ( bestSplit->compileFlags & C_DETAIL ) {
for ( split = list; split; split = split->next )
if ( !( split->compileFlags & C_DETAIL ) ) {
Sys_FPrintf( SYS_ERR, "DON'T DO SUCH SPLITS (1)\n" );
}
}
if ( ( node->compileFlags & C_DETAIL ) && !( bestSplit->compileFlags & C_DETAIL ) ) {
Sys_FPrintf( SYS_ERR, "DON'T DO SUCH SPLITS (2)\n" );
}
#endif
if ( *splitPlaneNum > -1 ) {
mapplanes[ *splitPlaneNum ].counter++;
}
}
/*
CountFaceList()
counts bsp faces in the linked list
*/
int CountFaceList( face_t *list ){
int c;
c = 0;
for ( ; list != NULL; list = list->next )
c++;
return c;
}
/*
BuildFaceTree_r()
recursively builds the bsp, splitting on face planes
*/
void BuildFaceTree_r( node_t *node, face_t *list ){
face_t *split;
face_t *next;
int side;
plane_t *plane;
face_t *newFace;
face_t *childLists[2];
winding_t *frontWinding, *backWinding;
int i;
int splitPlaneNum, compileFlags;
#if 0
qboolean isstruct = qfalse;
#endif
/* count faces left */
i = CountFaceList( list );
/* select the best split plane */
SelectSplitPlaneNum( node, list, &splitPlaneNum, &compileFlags );
/* if we don't have any more faces, this is a node */
if ( splitPlaneNum == -1 ) {
node->planenum = PLANENUM_LEAF;
node->has_structural_children = qfalse;
c_faceLeafs++;
return;
}
/* partition the list */
node->planenum = splitPlaneNum;
node->compileFlags = compileFlags;
node->has_structural_children = (!( compileFlags & C_DETAIL ) && !node->opaque) ? qtrue : qfalse;
plane = &mapplanes[ splitPlaneNum ];
childLists[0] = NULL;
childLists[1] = NULL;
for ( split = list; split; split = next )
{
/* set next */
next = split->next;
/* don't split by identical plane */
if ( split->planenum == node->planenum ) {
FreeBspFace( split );
continue;
}
#if 0
if ( !( split->compileFlags & C_DETAIL ) ) {
isstruct = 1;
}
#endif
/* determine which side the face falls on */
side = WindingOnPlaneSide( split->w, plane->normal, plane->dist );
/* switch on side */
if ( side == SIDE_CROSS ) {
ClipWindingEpsilonStrict( split->w, plane->normal, plane->dist, CLIP_EPSILON * 2,
&frontWinding, &backWinding ); /* strict; if no winding is left, we have a "virtually identical" plane and don't want to split by it */
if ( frontWinding ) {
newFace = AllocBspFace();
newFace->w = frontWinding;
newFace->next = childLists[0];
newFace->planenum = split->planenum;
newFace->priority = split->priority;
newFace->compileFlags = split->compileFlags;
childLists[0] = newFace;
}
if ( backWinding ) {
newFace = AllocBspFace();
newFace->w = backWinding;
newFace->next = childLists[1];
newFace->planenum = split->planenum;
newFace->priority = split->priority;
newFace->compileFlags = split->compileFlags;
childLists[1] = newFace;
}
FreeBspFace( split );
}
else if ( side == SIDE_FRONT ) {
split->next = childLists[0];
childLists[0] = split;
}
else if ( side == SIDE_BACK ) {
split->next = childLists[1];
childLists[1] = split;
}
}
// recursively process children
for ( i = 0 ; i < 2 ; i++ ) {
node->children[i] = AllocNode();
node->children[i]->parent = node;
VectorCopy( node->mins, node->children[i]->mins );
VectorCopy( node->maxs, node->children[i]->maxs );
}
for ( i = 0 ; i < 3 ; i++ ) {
if ( plane->normal[i] == 1 ) {
node->children[0]->mins[i] = plane->dist;
node->children[1]->maxs[i] = plane->dist;
break;
}
if ( plane->normal[i] == -1 ) {
node->children[0]->maxs[i] = -plane->dist;
node->children[1]->mins[i] = -plane->dist;
break;
}
}
#if 0
if ( ( node->compileFlags & C_DETAIL ) && isstruct ) {
Sys_FPrintf( SYS_ERR, "I am detail, my child is structural, this is a wtf1\n", node->has_structural_children );
}
#endif
for ( i = 0 ; i < 2 ; i++ ) {
BuildFaceTree_r( node->children[i], childLists[i] );
if (!node->has_structural_children && node->children[i]->has_structural_children)
{
node->has_structural_children = qtrue;
}
}
#if 0
if ( ( node->compileFlags & C_DETAIL ) && !( node->children[0]->compileFlags & C_DETAIL ) && node->children[0]->planenum != PLANENUM_LEAF ) {
Sys_FPrintf( SYS_ERR, "I am detail, my child is structural\n", node->has_structural_children );
}
if ( ( node->compileFlags & C_DETAIL ) && isstruct ) {
Sys_FPrintf( SYS_ERR, "I am detail, my child is structural, this is a wtf2\n", node->has_structural_children );
}
#endif
}
/*
================
FaceBSP
List will be freed before returning
================
*/
tree_t *FaceBSP( face_t *list ) {
tree_t *tree;
face_t *face;
int i;
int count;
Sys_FPrintf( SYS_VRB, "--- FaceBSP ---\n" );
tree = AllocTree();
count = 0;
for ( face = list; face != NULL; face = face->next )
{
count++;
for ( i = 0; i < face->w->numpoints; i++ )
{
AddPointToBounds( face->w->p[ i ], tree->mins, tree->maxs );
}
}
Sys_FPrintf( SYS_VRB, "%9d faces\n", count );
for ( i = 0; i < nummapplanes; i++ )
{
mapplanes[ i ].counter = 0;
}
tree->headnode = AllocNode();
VectorCopy( tree->mins, tree->headnode->mins );
VectorCopy( tree->maxs, tree->headnode->maxs );
c_faceLeafs = 0;
BuildFaceTree_r( tree->headnode, list );
Sys_FPrintf( SYS_VRB, "%9d leafs\n", c_faceLeafs );
return tree;
}
/*
MakeStructuralBSPFaceList()
get structural brush faces
*/
face_t *MakeStructuralBSPFaceList( brush_t *list ){
brush_t *b;
int i;
side_t *s;
winding_t *w;
face_t *f, *flist;
flist = NULL;
for ( b = list; b != NULL; b = b->next )
{
if ( !deepBSP && b->detail ) {
continue;
}
for ( i = 0; i < b->numsides; i++ )
{
/* get side and winding */
s = &b->sides[ i ];
w = s->winding;
if ( w == NULL ) {
continue;
}
/* ydnar: skip certain faces */
if ( s->compileFlags & C_SKIP ) {
continue;
}
/* allocate a face */
f = AllocBspFace();
f->w = CopyWinding( w );
f->planenum = s->planenum & ~1;
f->compileFlags = s->compileFlags; /* ydnar */
if ( b->detail ) {
f->compileFlags |= C_DETAIL;
}
/* ydnar: set priority */
f->priority = 0;
if ( f->compileFlags & C_HINT ) {
f->priority += HINT_PRIORITY;
}
if ( f->compileFlags & C_ANTIPORTAL ) {
f->priority += ANTIPORTAL_PRIORITY;
}
if ( f->compileFlags & C_AREAPORTAL ) {
f->priority += AREAPORTAL_PRIORITY;
}
if ( f->compileFlags & C_DETAIL ) {
f->priority += DETAIL_PRIORITY;
}
/* get next face */
f->next = flist;
flist = f;
}
}
return flist;
}
/*
MakeVisibleBSPFaceList()
get visible brush faces
*/
face_t *MakeVisibleBSPFaceList( brush_t *list ){
brush_t *b;
int i;
side_t *s;
winding_t *w;
face_t *f, *flist;
flist = NULL;
for ( b = list; b != NULL; b = b->next )
{
if ( !deepBSP && b->detail ) {
continue;
}
for ( i = 0; i < b->numsides; i++ )
{
/* get side and winding */
s = &b->sides[ i ];
w = s->visibleHull;
if ( w == NULL ) {
continue;
}
/* ydnar: skip certain faces */
if ( s->compileFlags & C_SKIP ) {
continue;
}
/* allocate a face */
f = AllocBspFace();
f->w = CopyWinding( w );
f->planenum = s->planenum & ~1;
f->compileFlags = s->compileFlags; /* ydnar */
if ( b->detail ) {
f->compileFlags |= C_DETAIL;
}
/* ydnar: set priority */
f->priority = 0;
if ( f->compileFlags & C_HINT ) {
f->priority += HINT_PRIORITY;
}
if ( f->compileFlags & C_ANTIPORTAL ) {
f->priority += ANTIPORTAL_PRIORITY;
}
if ( f->compileFlags & C_AREAPORTAL ) {
f->priority += AREAPORTAL_PRIORITY;
}
if ( f->compileFlags & C_DETAIL ) {
f->priority += DETAIL_PRIORITY;
}
/* get next face */
f->next = flist;
flist = f;
}
}
return flist;
}