1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
8 GtkRadiant is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 GtkRadiant is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GtkRadiant; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 ----------------------------------------------------------------------------------
24 This code has been altered significantly from its original form, to support
25 several games based on the Quake III Arena engine, in the form of "Q3Map2."
27 ------------------------------------------------------------------------------- */
49 face_t *AllocBspFace( void ) {
52 f = safe_malloc( sizeof( *f ) );
53 memset( f, 0, sizeof( *f ) );
65 void FreeBspFace( face_t *f ) {
76 finds the best split plane for this node
79 static void SelectSplitPlaneNum( node_t *node, face_t *list, int *splitPlaneNum, int *compileFlags ){
83 int splits, facing, front, back;
93 /* ydnar: set some defaults */
94 *splitPlaneNum = -1; /* leaf */
97 /* ydnar 2002-06-24: changed this to split on z-axis as well */
98 /* ydnar 2002-09-21: changed blocksize to be a vector, so mappers can specify a 3 element value */
100 /* if it is crossing a block boundary, force a split */
101 for ( i = 0; i < 3; i++ )
103 if ( blockSize[ i ] <= 0 ) {
106 dist = blockSize[ i ] * ( floor( node->mins[ i ] / blockSize[ i ] ) + 1 );
107 if ( node->maxs[ i ] > dist ) {
108 VectorClear( normal );
110 planenum = FindFloatPlane( normal, dist, 0, NULL );
111 *splitPlaneNum = planenum;
116 /* pick one of the face planes */
121 // div0: this check causes detail/structural mixes
122 //for( split = list; split; split = split->next )
123 // split->checked = qfalse;
125 for ( split = list; split; split = split->next )
127 //if ( split->checked )
130 plane = &mapplanes[ split->planenum ];
135 for ( check = list ; check ; check = check->next ) {
136 if ( check->planenum == split->planenum ) {
138 //check->checked = qtrue; // won't need to test this plane again
141 side = WindingOnPlaneSide( check->w, plane->normal, plane->dist );
142 if ( side == SIDE_CROSS ) {
145 else if ( side == SIDE_FRONT ) {
148 else if ( side == SIDE_BACK ) {
153 if ( bspAlternateSplitWeights ) {
157 sizeBias = WindingArea( split->w );
159 //Base score = 20000 perfectly balanced
160 value = 20000 - ( abs( front - back ) );
161 value -= plane->counter; // If we've already used this plane sometime in the past try not to use it again
162 value -= facing ; // if we're going to have alot of other surfs use this plane, we want to get it in quickly.
163 value -= splits * 5; //more splits = bad
164 value += sizeBias * 10; //We want a huge score bias based on plane size
168 value = 5 * facing - 5 * splits; // - abs(front-back);
169 if ( plane->type < 3 ) {
170 value += 5; // axial is better
174 value += split->priority; // prioritize hints higher
176 if ( value > bestValue ) {
182 /* nothing, we have a leaf */
183 if ( bestValue == -99999 ) {
187 /* set best split data */
188 *splitPlaneNum = bestSplit->planenum;
189 *compileFlags = bestSplit->compileFlags;
191 if ( *splitPlaneNum > -1 ) {
192 mapplanes[ *splitPlaneNum ].counter++;
200 counts bsp faces in the linked list
203 int CountFaceList( face_t *list ){
208 for ( ; list != NULL; list = list->next )
217 recursively builds the bsp, splitting on face planes
220 void BuildFaceTree_r( node_t *node, face_t *list ){
226 face_t *childLists[2];
227 winding_t *frontWinding, *backWinding;
229 int splitPlaneNum, compileFlags;
232 /* count faces left */
233 i = CountFaceList( list );
235 /* select the best split plane */
236 SelectSplitPlaneNum( node, list, &splitPlaneNum, &compileFlags );
238 /* if we don't have any more faces, this is a node */
239 if ( splitPlaneNum == -1 ) {
240 node->planenum = PLANENUM_LEAF;
241 node->has_structural_children = qfalse;
246 /* partition the list */
247 node->planenum = splitPlaneNum;
248 node->compileFlags = compileFlags;
249 node->has_structural_children = !( compileFlags & C_DETAIL ) && !node->opaque;
250 plane = &mapplanes[ splitPlaneNum ];
251 childLists[0] = NULL;
252 childLists[1] = NULL;
254 for ( split = list; split; split = next )
259 /* don't split by identical plane */
260 if ( split->planenum == node->planenum ) {
261 FreeBspFace( split );
265 /* determine which side the face falls on */
266 side = WindingOnPlaneSide( split->w, plane->normal, plane->dist );
269 if ( side == SIDE_CROSS ) {
270 /* strict; if no winding is left, we have a "virtually identical" plane and don't want to split by it */
271 ClipWindingEpsilonStrict( split->w, plane->normal, plane->dist, CLIP_EPSILON * 2,
272 &frontWinding, &backWinding );
273 if ( frontWinding ) {
274 newFace = AllocBspFace();
275 newFace->w = frontWinding;
276 newFace->next = childLists[0];
277 newFace->planenum = split->planenum;
278 newFace->priority = split->priority;
279 newFace->compileFlags = split->compileFlags;
280 childLists[0] = newFace;
283 newFace = AllocBspFace();
284 newFace->w = backWinding;
285 newFace->next = childLists[1];
286 newFace->planenum = split->planenum;
287 newFace->priority = split->priority;
288 newFace->compileFlags = split->compileFlags;
289 childLists[1] = newFace;
291 FreeBspFace( split );
293 else if ( side == SIDE_FRONT ) {
294 split->next = childLists[0];
295 childLists[0] = split;
297 else if ( side == SIDE_BACK ) {
298 split->next = childLists[1];
299 childLists[1] = split;
304 // recursively process children
305 for ( i = 0 ; i < 2 ; i++ ) {
306 node->children[i] = AllocNode();
307 node->children[i]->parent = node;
308 VectorCopy( node->mins, node->children[i]->mins );
309 VectorCopy( node->maxs, node->children[i]->maxs );
312 for ( i = 0 ; i < 3 ; i++ ) {
313 if ( plane->normal[i] == 1 ) {
314 node->children[0]->mins[i] = plane->dist;
315 node->children[1]->maxs[i] = plane->dist;
318 if ( plane->normal[i] == -1 ) {
319 node->children[0]->maxs[i] = -plane->dist;
320 node->children[1]->mins[i] = -plane->dist;
325 for ( i = 0 ; i < 2 ; i++ ) {
326 BuildFaceTree_r( node->children[i], childLists[i] );
327 node->has_structural_children |= node->children[i]->has_structural_children;
336 List will be freed before returning
339 tree_t *FaceBSP( face_t *list ) {
345 Sys_FPrintf( SYS_VRB, "--- FaceBSP ---\n" );
350 for ( face = list; face != NULL; face = face->next )
353 for ( i = 0; i < face->w->numpoints; i++ )
355 AddPointToBounds( face->w->p[ i ], tree->mins, tree->maxs );
358 Sys_FPrintf( SYS_VRB, "%9d faces\n", count );
360 for ( i = 0; i < nummapplanes; i++ )
362 mapplanes[ i ].counter = 0;
365 tree->headnode = AllocNode();
366 VectorCopy( tree->mins, tree->headnode->mins );
367 VectorCopy( tree->maxs, tree->headnode->maxs );
370 BuildFaceTree_r( tree->headnode, list );
372 Sys_FPrintf( SYS_VRB, "%9d leafs\n", c_faceLeafs );
380 MakeStructuralBSPFaceList()
381 get structural brush faces
384 face_t *MakeStructuralBSPFaceList( brush_t *list ){
393 for ( b = list; b != NULL; b = b->next )
395 if ( !deepBSP && b->detail ) {
399 for ( i = 0; i < b->numsides; i++ )
401 /* get side and winding */
408 /* ydnar: skip certain faces */
409 if ( s->compileFlags & C_SKIP ) {
413 /* allocate a face */
415 f->w = CopyWinding( w );
416 f->planenum = s->planenum & ~1;
417 f->compileFlags = s->compileFlags; /* ydnar */
419 f->compileFlags |= C_DETAIL;
422 /* ydnar: set priority */
424 if ( f->compileFlags & C_HINT ) {
425 f->priority += HINT_PRIORITY;
427 if ( f->compileFlags & C_ANTIPORTAL ) {
428 f->priority += ANTIPORTAL_PRIORITY;
430 if ( f->compileFlags & C_AREAPORTAL ) {
431 f->priority += AREAPORTAL_PRIORITY;
433 if ( f->compileFlags & C_DETAIL ) {
434 f->priority += DETAIL_PRIORITY;
449 MakeVisibleBSPFaceList()
450 get visible brush faces
453 face_t *MakeVisibleBSPFaceList( brush_t *list ){
462 for ( b = list; b != NULL; b = b->next )
464 if ( !deepBSP && b->detail ) {
468 for ( i = 0; i < b->numsides; i++ )
470 /* get side and winding */
477 /* ydnar: skip certain faces */
478 if ( s->compileFlags & C_SKIP ) {
482 /* allocate a face */
484 f->w = CopyWinding( w );
485 f->planenum = s->planenum & ~1;
486 f->compileFlags = s->compileFlags; /* ydnar */
488 f->compileFlags |= C_DETAIL;
491 /* ydnar: set priority */
493 if ( f->compileFlags & C_HINT ) {
494 f->priority += HINT_PRIORITY;
496 if ( f->compileFlags & C_ANTIPORTAL ) {
497 f->priority += ANTIPORTAL_PRIORITY;
499 if ( f->compileFlags & C_AREAPORTAL ) {
500 f->priority += AREAPORTAL_PRIORITY;
502 if ( f->compileFlags & C_DETAIL ) {
503 f->priority += DETAIL_PRIORITY;