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_malloc0( sizeof( *f ) );
64 void FreeBspFace( face_t *f ) {
75 finds the best split plane for this node
78 static void SelectSplitPlaneNum( node_t *node, face_t *list, int *splitPlaneNum, int *compileFlags ){
82 int splits, facing, front, back;
92 /* ydnar: set some defaults */
93 *splitPlaneNum = -1; /* leaf */
96 /* ydnar 2002-06-24: changed this to split on z-axis as well */
97 /* ydnar 2002-09-21: changed blocksize to be a vector, so mappers can specify a 3 element value */
99 /* if it is crossing a block boundary, force a split */
100 for ( i = 0; i < 3; i++ )
102 if ( blockSize[ i ] <= 0 ) {
105 dist = blockSize[ i ] * ( floor( node->mins[ i ] / blockSize[ i ] ) + 1 );
106 if ( node->maxs[ i ] > dist ) {
107 VectorClear( normal );
109 planenum = FindFloatPlane( normal, dist, 0, NULL );
110 *splitPlaneNum = planenum;
115 /* pick one of the face planes */
120 // div0: this check causes detail/structural mixes
121 //for( split = list; split; split = split->next )
122 // split->checked = qfalse;
124 for ( split = list; split; split = split->next )
126 //if ( split->checked )
129 plane = &mapplanes[ split->planenum ];
134 for ( check = list ; check ; check = check->next ) {
135 if ( check->planenum == split->planenum ) {
137 //check->checked = qtrue; // won't need to test this plane again
140 side = WindingOnPlaneSide( check->w, plane->normal, plane->dist );
141 if ( side == SIDE_CROSS ) {
144 else if ( side == SIDE_FRONT ) {
147 else if ( side == SIDE_BACK ) {
152 if ( bspAlternateSplitWeights ) {
156 sizeBias = WindingArea( split->w );
158 //Base score = 20000 perfectly balanced
159 value = 20000 - ( abs( front - back ) );
160 value -= plane->counter; // If we've already used this plane sometime in the past try not to use it again
161 value -= facing ; // if we're going to have alot of other surfs use this plane, we want to get it in quickly.
162 value -= splits * 5; //more splits = bad
163 value += sizeBias * 10; //We want a huge score bias based on plane size
167 value = 5 * facing - 5 * splits; // - abs(front-back);
168 if ( plane->type < 3 ) {
169 value += 5; // axial is better
173 value += split->priority; // prioritize hints higher
175 if ( value > bestValue ) {
181 /* nothing, we have a leaf */
182 if ( bestValue == -99999 ) {
186 /* set best split data */
187 *splitPlaneNum = bestSplit->planenum;
188 *compileFlags = bestSplit->compileFlags;
190 if ( *splitPlaneNum > -1 ) {
191 mapplanes[ *splitPlaneNum ].counter++;
199 counts bsp faces in the linked list
202 int CountFaceList( face_t *list ){
207 for ( ; list != NULL; list = list->next )
216 recursively builds the bsp, splitting on face planes
219 void BuildFaceTree_r( node_t *node, face_t *list ){
225 face_t *childLists[2];
226 winding_t *frontWinding, *backWinding;
228 int splitPlaneNum, compileFlags;
231 /* count faces left */
232 i = CountFaceList( list );
234 /* select the best split plane */
235 SelectSplitPlaneNum( node, list, &splitPlaneNum, &compileFlags );
237 /* if we don't have any more faces, this is a node */
238 if ( splitPlaneNum == -1 ) {
239 node->planenum = PLANENUM_LEAF;
240 node->has_structural_children = qfalse;
245 /* partition the list */
246 node->planenum = splitPlaneNum;
247 node->compileFlags = compileFlags;
248 node->has_structural_children = !( compileFlags & C_DETAIL ) && !node->opaque;
249 plane = &mapplanes[ splitPlaneNum ];
250 childLists[0] = NULL;
251 childLists[1] = NULL;
253 for ( split = list; split; split = next )
258 /* don't split by identical plane */
259 if ( split->planenum == node->planenum ) {
260 FreeBspFace( split );
264 /* determine which side the face falls on */
265 side = WindingOnPlaneSide( split->w, plane->normal, plane->dist );
268 if ( side == SIDE_CROSS ) {
269 /* strict; if no winding is left, we have a "virtually identical" plane and don't want to split by it */
270 ClipWindingEpsilonStrict( split->w, plane->normal, plane->dist, CLIP_EPSILON * 2,
271 &frontWinding, &backWinding );
272 if ( frontWinding ) {
273 newFace = AllocBspFace();
274 newFace->w = frontWinding;
275 newFace->next = childLists[0];
276 newFace->planenum = split->planenum;
277 newFace->priority = split->priority;
278 newFace->compileFlags = split->compileFlags;
279 childLists[0] = newFace;
282 newFace = AllocBspFace();
283 newFace->w = backWinding;
284 newFace->next = childLists[1];
285 newFace->planenum = split->planenum;
286 newFace->priority = split->priority;
287 newFace->compileFlags = split->compileFlags;
288 childLists[1] = newFace;
290 FreeBspFace( split );
292 else if ( side == SIDE_FRONT ) {
293 split->next = childLists[0];
294 childLists[0] = split;
296 else if ( side == SIDE_BACK ) {
297 split->next = childLists[1];
298 childLists[1] = split;
303 // recursively process children
304 for ( i = 0 ; i < 2 ; i++ ) {
305 node->children[i] = AllocNode();
306 node->children[i]->parent = node;
307 VectorCopy( node->mins, node->children[i]->mins );
308 VectorCopy( node->maxs, node->children[i]->maxs );
311 for ( i = 0 ; i < 3 ; i++ ) {
312 if ( plane->normal[i] == 1 ) {
313 node->children[0]->mins[i] = plane->dist;
314 node->children[1]->maxs[i] = plane->dist;
317 if ( plane->normal[i] == -1 ) {
318 node->children[0]->maxs[i] = -plane->dist;
319 node->children[1]->mins[i] = -plane->dist;
324 for ( i = 0 ; i < 2 ; i++ ) {
325 BuildFaceTree_r( node->children[i], childLists[i] );
326 node->has_structural_children |= node->children[i]->has_structural_children;
335 List will be freed before returning
338 tree_t *FaceBSP( face_t *list ) {
344 Sys_FPrintf( SYS_VRB, "--- FaceBSP ---\n" );
349 for ( face = list; face != NULL; face = face->next )
352 for ( i = 0; i < face->w->numpoints; i++ )
354 AddPointToBounds( face->w->p[ i ], tree->mins, tree->maxs );
357 Sys_FPrintf( SYS_VRB, "%9d faces\n", count );
359 for ( i = 0; i < nummapplanes; i++ )
361 mapplanes[ i ].counter = 0;
364 tree->headnode = AllocNode();
365 VectorCopy( tree->mins, tree->headnode->mins );
366 VectorCopy( tree->maxs, tree->headnode->maxs );
369 BuildFaceTree_r( tree->headnode, list );
371 Sys_FPrintf( SYS_VRB, "%9d leafs\n", c_faceLeafs );
379 MakeStructuralBSPFaceList()
380 get structural brush faces
383 face_t *MakeStructuralBSPFaceList( brush_t *list ){
392 for ( b = list; b != NULL; b = b->next )
394 if ( !deepBSP && b->detail ) {
398 for ( i = 0; i < b->numsides; i++ )
400 /* get side and winding */
407 /* ydnar: skip certain faces */
408 if ( s->compileFlags & C_SKIP ) {
412 /* allocate a face */
414 f->w = CopyWinding( w );
415 f->planenum = s->planenum & ~1;
416 f->compileFlags = s->compileFlags; /* ydnar */
418 f->compileFlags |= C_DETAIL;
421 /* ydnar: set priority */
423 if ( f->compileFlags & C_HINT ) {
424 f->priority += HINT_PRIORITY;
426 if ( f->compileFlags & C_ANTIPORTAL ) {
427 f->priority += ANTIPORTAL_PRIORITY;
429 if ( f->compileFlags & C_AREAPORTAL ) {
430 f->priority += AREAPORTAL_PRIORITY;
432 if ( f->compileFlags & C_DETAIL ) {
433 f->priority += DETAIL_PRIORITY;
448 MakeVisibleBSPFaceList()
449 get visible brush faces
452 face_t *MakeVisibleBSPFaceList( brush_t *list ){
461 for ( b = list; b != NULL; b = b->next )
463 if ( !deepBSP && b->detail ) {
467 for ( i = 0; i < b->numsides; i++ )
469 /* get side and winding */
476 /* ydnar: skip certain faces */
477 if ( s->compileFlags & C_SKIP ) {
481 /* allocate a face */
483 f->w = CopyWinding( w );
484 f->planenum = s->planenum & ~1;
485 f->compileFlags = s->compileFlags; /* ydnar */
487 f->compileFlags |= C_DETAIL;
490 /* ydnar: set priority */
492 if ( f->compileFlags & C_HINT ) {
493 f->priority += HINT_PRIORITY;
495 if ( f->compileFlags & C_ANTIPORTAL ) {
496 f->priority += ANTIPORTAL_PRIORITY;
498 if ( f->compileFlags & C_AREAPORTAL ) {
499 f->priority += AREAPORTAL_PRIORITY;
501 if ( f->compileFlags & C_DETAIL ) {
502 f->priority += DETAIL_PRIORITY;