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 for( split = list; split; split = split->next )
126 split->checked = qfalse;
128 for( split = list; split; split = split->next )
130 if ( split->checked )
133 plane = &mapplanes[ split->planenum ];
138 for ( check = list ; check ; check = check->next ) {
139 if ( check->planenum == split->planenum ) {
141 check->checked = qtrue; // won't need to test this plane again
144 side = WindingOnPlaneSide( check->w, plane->normal, plane->dist );
145 if ( side == SIDE_CROSS ) {
147 } else if ( side == SIDE_FRONT ) {
149 } else if ( side == SIDE_BACK ) {
154 if(bspAlternateSplitWeights)
159 sizeBias=WindingArea(split->w);
161 //Base score = 20000 perfectly balanced
162 value = 20000-(abs(front-back));
163 value -= plane->counter;// If we've already used this plane sometime in the past try not to use it again
164 value -= facing ; // if we're going to have alot of other surfs use this plane, we want to get it in quickly.
165 value -= splits*5; //more splits = bad
166 value += sizeBias*10; //We want a huge score bias based on plane size
170 value = 5*facing - 5*splits; // - abs(front-back);
171 if ( plane->type < 3 ) {
172 value+=5; // axial is better
176 value += split->priority; // prioritize hints higher
178 if ( value > bestValue ) {
188 /* nothing, we have a leaf */
189 if( bestValue == -99999 )
192 //Sys_FPrintf (SYS_VRB, "F: %d B:%d S:%d FA:%ds\n",frontC,backC,splitsC,facingC );
194 /* set best split data */
195 *splitPlaneNum = bestSplit->planenum;
196 *compileFlags = bestSplit->compileFlags;
198 if (*splitPlaneNum>-1) mapplanes[ *splitPlaneNum ].counter++;
205 counts bsp faces in the linked list
208 int CountFaceList( face_t *list )
214 for( ; list != NULL; list = list->next )
223 recursively builds the bsp, splitting on face planes
226 void BuildFaceTree_r( node_t *node, face_t *list )
233 face_t *childLists[2];
234 winding_t *frontWinding, *backWinding;
236 int splitPlaneNum, compileFlags;
239 /* count faces left */
240 i = CountFaceList( list );
242 /* select the best split plane */
243 SelectSplitPlaneNum( node, list, &splitPlaneNum, &compileFlags );
245 /* if we don't have any more faces, this is a node */
246 if ( splitPlaneNum == -1 )
248 node->planenum = PLANENUM_LEAF;
253 /* partition the list */
254 node->planenum = splitPlaneNum;
255 node->compileFlags = compileFlags;
256 plane = &mapplanes[ splitPlaneNum ];
257 childLists[0] = NULL;
258 childLists[1] = NULL;
259 for( split = list; split; split = next )
264 /* don't split by identical plane */
265 if( split->planenum == node->planenum )
267 FreeBspFace( split );
271 /* determine which side the face falls on */
272 side = WindingOnPlaneSide( split->w, plane->normal, plane->dist );
275 if( side == SIDE_CROSS )
277 ClipWindingEpsilon( split->w, plane->normal, plane->dist, CLIP_EPSILON * 2,
278 &frontWinding, &backWinding );
280 newFace = AllocBspFace();
281 newFace->w = frontWinding;
282 newFace->next = childLists[0];
283 newFace->planenum = split->planenum;
284 newFace->priority = split->priority;
285 newFace->compileFlags = split->compileFlags;
286 childLists[0] = newFace;
289 newFace = AllocBspFace();
290 newFace->w = backWinding;
291 newFace->next = childLists[1];
292 newFace->planenum = split->planenum;
293 newFace->priority = split->priority;
294 newFace->compileFlags = split->compileFlags;
295 childLists[1] = newFace;
297 FreeBspFace( split );
298 } else if ( side == SIDE_FRONT ) {
299 split->next = childLists[0];
300 childLists[0] = split;
301 } else if ( side == SIDE_BACK ) {
302 split->next = childLists[1];
303 childLists[1] = split;
308 // recursively process children
309 for ( i = 0 ; i < 2 ; i++ ) {
310 node->children[i] = AllocNode();
311 node->children[i]->parent = node;
312 VectorCopy( node->mins, node->children[i]->mins );
313 VectorCopy( node->maxs, node->children[i]->maxs );
316 for ( i = 0 ; i < 3 ; i++ ) {
317 if ( plane->normal[i] == 1 ) {
318 node->children[0]->mins[i] = plane->dist;
319 node->children[1]->maxs[i] = plane->dist;
324 for ( i = 0 ; i < 2 ; i++ ) {
325 BuildFaceTree_r ( node->children[i], childLists[i]);
334 List will be freed before returning
337 tree_t *FaceBSP( face_t *list ) {
343 Sys_FPrintf (SYS_VRB, "--- FaceBSP ---\n" );
348 for( face = list; face != NULL; face = face->next )
351 for( i = 0; i < face->w->numpoints; i++ )
353 AddPointToBounds( face->w->p[ i ], tree->mins, tree->maxs );
356 Sys_FPrintf( SYS_VRB, "%9d faces\n", count );
358 for( i = 0; i < nummapplanes; i++)
360 mapplanes[ i ].counter=0;
363 tree->headnode = AllocNode();
364 VectorCopy( tree->mins, tree->headnode->mins );
365 VectorCopy( tree->maxs, tree->headnode->maxs );
368 BuildFaceTree_r ( tree->headnode, list );
370 Sys_FPrintf( SYS_VRB, "%9d leafs\n", c_faceLeafs );
378 MakeStructuralBSPFaceList()
379 get structural brush faces
382 face_t *MakeStructuralBSPFaceList( brush_t *list )
392 for( b = list; b != NULL; b = b->next )
397 for( i = 0; i < b->numsides; i++ )
399 /* get side and winding */
405 /* ydnar: skip certain faces */
406 if( s->compileFlags & C_SKIP )
409 /* allocate a face */
411 f->w = CopyWinding( w );
412 f->planenum = s->planenum & ~1;
413 f->compileFlags = s->compileFlags; /* ydnar */
415 /* ydnar: set priority */
417 if( f->compileFlags & C_HINT )
418 f->priority += HINT_PRIORITY;
419 if( f->compileFlags & C_ANTIPORTAL )
420 f->priority += ANTIPORTAL_PRIORITY;
421 if( f->compileFlags & C_AREAPORTAL )
422 f->priority += AREAPORTAL_PRIORITY;
436 MakeVisibleBSPFaceList()
437 get visible brush faces
440 face_t *MakeVisibleBSPFaceList( brush_t *list )
450 for( b = list; b != NULL; b = b->next )
455 for( i = 0; i < b->numsides; i++ )
457 /* get side and winding */
463 /* ydnar: skip certain faces */
464 if( s->compileFlags & C_SKIP )
467 /* allocate a face */
469 f->w = CopyWinding( w );
470 f->planenum = s->planenum & ~1;
471 f->compileFlags = s->compileFlags; /* ydnar */
473 /* ydnar: set priority */
475 if( f->compileFlags & C_HINT )
476 f->priority += HINT_PRIORITY;
477 if( f->compileFlags & C_ANTIPORTAL )
478 f->priority += ANTIPORTAL_PRIORITY;
479 if( f->compileFlags & C_AREAPORTAL )
480 f->priority += AREAPORTAL_PRIORITY;