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 )
84 int splits, facing, front, back;
94 //int frontC,backC,splitsC,facingC;
97 /* ydnar: set some defaults */
98 *splitPlaneNum = -1; /* leaf */
101 /* ydnar 2002-06-24: changed this to split on z-axis as well */
102 /* ydnar 2002-09-21: changed blocksize to be a vector, so mappers can specify a 3 element value */
104 /* if it is crossing a block boundary, force a split */
105 for( i = 0; i < 3; i++ )
107 if( blockSize[ i ] <= 0 )
109 dist = blockSize[ i ] * (floor( node->mins[ i ] / blockSize[ i ] ) + 1);
110 if( node->maxs[ i ] > dist )
112 VectorClear( normal );
114 planenum = FindFloatPlane( normal, dist, 0, NULL );
115 *splitPlaneNum = planenum;
120 /* pick one of the face planes */
125 // div0: this check causes detail/structural mixes
126 //for( split = list; split; split = split->next )
127 // split->checked = qfalse;
129 for( split = list; split; split = split->next )
131 //if ( split->checked )
134 plane = &mapplanes[ split->planenum ];
139 for ( check = list ; check ; check = check->next ) {
140 if ( check->planenum == split->planenum ) {
142 //check->checked = qtrue; // won't need to test this plane again
145 side = WindingOnPlaneSide( check->w, plane->normal, plane->dist );
146 if ( side == SIDE_CROSS ) {
148 } else if ( side == SIDE_FRONT ) {
150 } else if ( side == SIDE_BACK ) {
155 if(bspAlternateSplitWeights)
160 sizeBias=WindingArea(split->w);
162 //Base score = 20000 perfectly balanced
163 value = 20000-(abs(front-back));
164 value -= plane->counter;// If we've already used this plane sometime in the past try not to use it again
165 value -= facing ; // if we're going to have alot of other surfs use this plane, we want to get it in quickly.
166 value -= splits*5; //more splits = bad
167 value += sizeBias*10; //We want a huge score bias based on plane size
171 value = 5*facing - 5*splits; // - abs(front-back);
172 if ( plane->type < 3 ) {
173 value+=5; // axial is better
177 value += split->priority; // prioritize hints higher
179 if ( value > bestValue ) {
189 /* nothing, we have a leaf */
190 if( bestValue == -99999 )
193 //Sys_FPrintf (SYS_VRB, "F: %d B:%d S:%d FA:%ds\n",frontC,backC,splitsC,facingC );
195 /* set best split data */
196 *splitPlaneNum = bestSplit->planenum;
197 *compileFlags = bestSplit->compileFlags;
200 if(bestSplit->compileFlags & C_DETAIL)
201 for( split = list; split; split = split->next )
202 if(!(split->compileFlags & C_DETAIL))
203 Sys_FPrintf(SYS_ERR, "DON'T DO SUCH SPLITS (1)\n");
204 if((node->compileFlags & C_DETAIL) && !(bestSplit->compileFlags & C_DETAIL))
205 Sys_FPrintf(SYS_ERR, "DON'T DO SUCH SPLITS (2)\n");
208 if (*splitPlaneNum>-1) mapplanes[ *splitPlaneNum ].counter++;
215 counts bsp faces in the linked list
218 int CountFaceList( face_t *list )
224 for( ; list != NULL; list = list->next )
233 recursively builds the bsp, splitting on face planes
236 void BuildFaceTree_r( node_t *node, face_t *list )
243 face_t *childLists[2];
244 winding_t *frontWinding, *backWinding;
246 int splitPlaneNum, compileFlags;
247 qboolean isstruct = qfalse;
250 /* count faces left */
251 i = CountFaceList( list );
253 /* select the best split plane */
254 SelectSplitPlaneNum( node, list, &splitPlaneNum, &compileFlags );
256 /* if we don't have any more faces, this is a node */
257 if ( splitPlaneNum == -1 )
259 node->planenum = PLANENUM_LEAF;
260 node->has_structural_children = qfalse;
265 /* partition the list */
266 node->planenum = splitPlaneNum;
267 node->compileFlags = compileFlags;
268 node->has_structural_children = !(compileFlags & C_DETAIL) && !node->opaque;
269 plane = &mapplanes[ splitPlaneNum ];
270 childLists[0] = NULL;
271 childLists[1] = NULL;
273 for( split = list; split; split = next )
278 /* don't split by identical plane */
279 if( split->planenum == node->planenum )
281 FreeBspFace( split );
285 if(!(split->compileFlags & C_DETAIL))
288 /* determine which side the face falls on */
289 side = WindingOnPlaneSide( split->w, plane->normal, plane->dist );
292 if( side == SIDE_CROSS )
294 ClipWindingEpsilon( split->w, plane->normal, plane->dist, CLIP_EPSILON * 2,
295 &frontWinding, &backWinding );
297 newFace = AllocBspFace();
298 newFace->w = frontWinding;
299 newFace->next = childLists[0];
300 newFace->planenum = split->planenum;
301 newFace->priority = split->priority;
302 newFace->compileFlags = split->compileFlags;
303 childLists[0] = newFace;
306 newFace = AllocBspFace();
307 newFace->w = backWinding;
308 newFace->next = childLists[1];
309 newFace->planenum = split->planenum;
310 newFace->priority = split->priority;
311 newFace->compileFlags = split->compileFlags;
312 childLists[1] = newFace;
314 FreeBspFace( split );
315 } else if ( side == SIDE_FRONT ) {
316 split->next = childLists[0];
317 childLists[0] = split;
318 } else if ( side == SIDE_BACK ) {
319 split->next = childLists[1];
320 childLists[1] = split;
325 // recursively process children
326 for ( i = 0 ; i < 2 ; i++ ) {
327 node->children[i] = AllocNode();
328 node->children[i]->parent = node;
329 VectorCopy( node->mins, node->children[i]->mins );
330 VectorCopy( node->maxs, node->children[i]->maxs );
333 for ( i = 0 ; i < 3 ; i++ ) {
334 if ( plane->normal[i] == 1 ) {
335 node->children[0]->mins[i] = plane->dist;
336 node->children[1]->maxs[i] = plane->dist;
342 if((node->compileFlags & C_DETAIL) && isstruct)
343 Sys_FPrintf(SYS_ERR, "I am detail, my child is structural, this is a wtf1\n", node->has_structural_children);
346 for ( i = 0 ; i < 2 ; i++ ) {
347 BuildFaceTree_r ( node->children[i], childLists[i]);
348 node->has_structural_children |= node->children[i]->has_structural_children;
352 if((node->compileFlags & C_DETAIL) && !(node->children[0]->compileFlags & C_DETAIL) && node->children[0]->planenum != PLANENUM_LEAF)
353 Sys_FPrintf(SYS_ERR, "I am detail, my child is structural\n", node->has_structural_children);
354 if((node->compileFlags & C_DETAIL) && isstruct)
355 Sys_FPrintf(SYS_ERR, "I am detail, my child is structural, this is a wtf2\n", node->has_structural_children);
364 List will be freed before returning
367 tree_t *FaceBSP( face_t *list ) {
373 Sys_FPrintf (SYS_VRB, "--- FaceBSP ---\n" );
378 for( face = list; face != NULL; face = face->next )
381 for( i = 0; i < face->w->numpoints; i++ )
383 AddPointToBounds( face->w->p[ i ], tree->mins, tree->maxs );
386 Sys_FPrintf( SYS_VRB, "%9d faces\n", count );
388 for( i = 0; i < nummapplanes; i++)
390 mapplanes[ i ].counter=0;
393 tree->headnode = AllocNode();
394 VectorCopy( tree->mins, tree->headnode->mins );
395 VectorCopy( tree->maxs, tree->headnode->maxs );
398 BuildFaceTree_r ( tree->headnode, list );
400 Sys_FPrintf( SYS_VRB, "%9d leafs\n", c_faceLeafs );
408 MakeStructuralBSPFaceList()
409 get structural brush faces
412 face_t *MakeStructuralBSPFaceList( brush_t *list )
422 for( b = list; b != NULL; b = b->next )
424 if( !deepBSP && b->detail )
427 for( i = 0; i < b->numsides; i++ )
429 /* get side and winding */
435 /* ydnar: skip certain faces */
436 if( s->compileFlags & C_SKIP )
439 /* allocate a face */
441 f->w = CopyWinding( w );
442 f->planenum = s->planenum & ~1;
443 f->compileFlags = s->compileFlags; /* ydnar */
445 f->compileFlags |= C_DETAIL;
447 /* ydnar: set priority */
449 if( f->compileFlags & C_HINT )
450 f->priority += HINT_PRIORITY;
451 if( f->compileFlags & C_ANTIPORTAL )
452 f->priority += ANTIPORTAL_PRIORITY;
453 if( f->compileFlags & C_AREAPORTAL )
454 f->priority += AREAPORTAL_PRIORITY;
455 if( f->compileFlags & C_DETAIL )
456 f->priority += DETAIL_PRIORITY;
470 MakeVisibleBSPFaceList()
471 get visible brush faces
474 face_t *MakeVisibleBSPFaceList( brush_t *list )
484 for( b = list; b != NULL; b = b->next )
486 if( !deepBSP && b->detail )
489 for( i = 0; i < b->numsides; i++ )
491 /* get side and winding */
497 /* ydnar: skip certain faces */
498 if( s->compileFlags & C_SKIP )
501 /* allocate a face */
503 f->w = CopyWinding( w );
504 f->planenum = s->planenum & ~1;
505 f->compileFlags = s->compileFlags; /* ydnar */
507 f->compileFlags |= C_DETAIL;
509 /* ydnar: set priority */
511 if( f->compileFlags & C_HINT )
512 f->priority += HINT_PRIORITY;
513 if( f->compileFlags & C_ANTIPORTAL )
514 f->priority += ANTIPORTAL_PRIORITY;
515 if( f->compileFlags & C_AREAPORTAL )
516 f->priority += AREAPORTAL_PRIORITY;
517 if( f->compileFlags & C_DETAIL )
518 f->priority += DETAIL_PRIORITY;