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;
94 /* ydnar: set some defaults */
95 *splitPlaneNum = -1; /* leaf */
98 /* ydnar 2002-06-24: changed this to split on z-axis as well */
99 /* ydnar 2002-09-21: changed blocksize to be a vector, so mappers can specify a 3 element value */
101 /* if it is crossing a block boundary, force a split */
102 for ( i = 0; i < 3; i++ )
104 if ( blockSize[ i ] <= 0 ) {
107 dist = blockSize[ i ] * ( floor( node->mins[ i ] / blockSize[ i ] ) + 1 );
108 if ( node->maxs[ i ] > dist ) {
109 VectorClear( normal );
111 planenum = FindFloatPlane( normal, dist, 0, NULL );
112 *splitPlaneNum = planenum;
117 /* pick one of the face planes */
122 // div0: this check causes detail/structural mixes
123 //for( split = list; split; split = split->next )
124 // split->checked = qfalse;
126 for ( split = list; split; split = split->next )
128 //if ( split->checked )
131 plane = &mapplanes[ split->planenum ];
136 for ( check = list ; check ; check = check->next ) {
137 if ( check->planenum == split->planenum ) {
139 //check->checked = qtrue; // won't need to test this plane again
142 side = WindingOnPlaneSide( check->w, plane->normal, plane->dist );
143 if ( side == SIDE_CROSS ) {
146 else if ( side == SIDE_FRONT ) {
149 else if ( side == SIDE_BACK ) {
154 if ( bspAlternateSplitWeights ) {
158 sizeBias = WindingArea( split->w );
160 //Base score = 20000 perfectly balanced
161 value = 20000 - ( abs( front - back ) );
162 value -= plane->counter; // If we've already used this plane sometime in the past try not to use it again
163 value -= facing ; // if we're going to have alot of other surfs use this plane, we want to get it in quickly.
164 value -= splits * 5; //more splits = bad
165 value += sizeBias * 10; //We want a huge score bias based on plane size
169 value = 5 * facing - 5 * splits; // - abs(front-back);
170 if ( plane->type < 3 ) {
171 value += 5; // axial is better
175 value += split->priority; // prioritize hints higher
177 if ( value > bestValue ) {
183 /* nothing, we have a leaf */
184 if ( bestValue == -99999 ) {
188 /* set best split data */
189 *splitPlaneNum = bestSplit->planenum;
190 *compileFlags = bestSplit->compileFlags;
192 if ( *splitPlaneNum > -1 ) {
193 mapplanes[ *splitPlaneNum ].counter++;
201 counts bsp faces in the linked list
204 int CountFaceList( face_t *list ){
209 for ( ; list != NULL; list = list->next )
218 recursively builds the bsp, splitting on face planes
221 void BuildFaceTree_r( node_t *node, face_t *list ){
227 face_t *childLists[2];
228 winding_t *frontWinding, *backWinding;
230 int splitPlaneNum, compileFlags;
233 /* count faces left */
234 i = CountFaceList( list );
236 /* select the best split plane */
237 SelectSplitPlaneNum( node, list, &splitPlaneNum, &compileFlags );
239 /* if we don't have any more faces, this is a node */
240 if ( splitPlaneNum == -1 ) {
241 node->planenum = PLANENUM_LEAF;
242 node->has_structural_children = qfalse;
247 /* partition the list */
248 node->planenum = splitPlaneNum;
249 node->compileFlags = compileFlags;
250 node->has_structural_children = !( compileFlags & C_DETAIL ) && !node->opaque;
251 plane = &mapplanes[ splitPlaneNum ];
252 childLists[0] = NULL;
253 childLists[1] = NULL;
255 for ( split = list; split; split = next )
260 /* don't split by identical plane */
261 if ( split->planenum == node->planenum ) {
262 FreeBspFace( split );
266 /* determine which side the face falls on */
267 side = WindingOnPlaneSide( split->w, plane->normal, plane->dist );
270 if ( side == SIDE_CROSS ) {
271 /* strict; if no winding is left, we have a "virtually identical" plane and don't want to split by it */
272 ClipWindingEpsilonStrict( split->w, plane->normal, plane->dist, CLIP_EPSILON * 2,
273 &frontWinding, &backWinding );
274 if ( frontWinding ) {
275 newFace = AllocBspFace();
276 newFace->w = frontWinding;
277 newFace->next = childLists[0];
278 newFace->planenum = split->planenum;
279 newFace->priority = split->priority;
280 newFace->compileFlags = split->compileFlags;
281 childLists[0] = newFace;
284 newFace = AllocBspFace();
285 newFace->w = backWinding;
286 newFace->next = childLists[1];
287 newFace->planenum = split->planenum;
288 newFace->priority = split->priority;
289 newFace->compileFlags = split->compileFlags;
290 childLists[1] = newFace;
292 FreeBspFace( split );
294 else if ( side == SIDE_FRONT ) {
295 split->next = childLists[0];
296 childLists[0] = split;
298 else if ( side == SIDE_BACK ) {
299 split->next = childLists[1];
300 childLists[1] = split;
305 // recursively process children
306 for ( i = 0 ; i < 2 ; i++ ) {
307 node->children[i] = AllocNode();
308 node->children[i]->parent = node;
309 VectorCopy( node->mins, node->children[i]->mins );
310 VectorCopy( node->maxs, node->children[i]->maxs );
313 for ( i = 0 ; i < 3 ; i++ ) {
314 if ( plane->normal[i] == 1 ) {
315 node->children[0]->mins[i] = plane->dist;
316 node->children[1]->maxs[i] = plane->dist;
319 if ( plane->normal[i] == -1 ) {
320 node->children[0]->maxs[i] = -plane->dist;
321 node->children[1]->mins[i] = -plane->dist;
326 for ( i = 0 ; i < 2 ; i++ ) {
327 BuildFaceTree_r( node->children[i], childLists[i] );
328 node->has_structural_children |= node->children[i]->has_structural_children;
337 List will be freed before returning
340 tree_t *FaceBSP( face_t *list ) {
346 Sys_FPrintf( SYS_VRB, "--- FaceBSP ---\n" );
351 for ( face = list; face != NULL; face = face->next )
354 for ( i = 0; i < face->w->numpoints; i++ )
356 AddPointToBounds( face->w->p[ i ], tree->mins, tree->maxs );
359 Sys_FPrintf( SYS_VRB, "%9d faces\n", count );
361 for ( i = 0; i < nummapplanes; i++ )
363 mapplanes[ i ].counter = 0;
366 tree->headnode = AllocNode();
367 VectorCopy( tree->mins, tree->headnode->mins );
368 VectorCopy( tree->maxs, tree->headnode->maxs );
371 BuildFaceTree_r( tree->headnode, list );
373 Sys_FPrintf( SYS_VRB, "%9d leafs\n", c_faceLeafs );
381 MakeStructuralBSPFaceList()
382 get structural brush faces
385 face_t *MakeStructuralBSPFaceList( brush_t *list ){
394 for ( b = list; b != NULL; b = b->next )
396 if ( !deepBSP && b->detail ) {
400 for ( i = 0; i < b->numsides; i++ )
402 /* get side and winding */
409 /* ydnar: skip certain faces */
410 if ( s->compileFlags & C_SKIP ) {
414 /* allocate a face */
416 f->w = CopyWinding( w );
417 f->planenum = s->planenum & ~1;
418 f->compileFlags = s->compileFlags; /* ydnar */
420 f->compileFlags |= C_DETAIL;
423 /* ydnar: set priority */
425 if ( f->compileFlags & C_HINT ) {
426 f->priority += HINT_PRIORITY;
428 if ( f->compileFlags & C_ANTIPORTAL ) {
429 f->priority += ANTIPORTAL_PRIORITY;
431 if ( f->compileFlags & C_AREAPORTAL ) {
432 f->priority += AREAPORTAL_PRIORITY;
434 if ( f->compileFlags & C_DETAIL ) {
435 f->priority += DETAIL_PRIORITY;
450 MakeVisibleBSPFaceList()
451 get visible brush faces
454 face_t *MakeVisibleBSPFaceList( brush_t *list ){
463 for ( b = list; b != NULL; b = b->next )
465 if ( !deepBSP && b->detail ) {
469 for ( i = 0; i < b->numsides; i++ )
471 /* get side and winding */
478 /* ydnar: skip certain faces */
479 if ( s->compileFlags & C_SKIP ) {
483 /* allocate a face */
485 f->w = CopyWinding( w );
486 f->planenum = s->planenum & ~1;
487 f->compileFlags = s->compileFlags; /* ydnar */
489 f->compileFlags |= C_DETAIL;
492 /* ydnar: set priority */
494 if ( f->compileFlags & C_HINT ) {
495 f->priority += HINT_PRIORITY;
497 if ( f->compileFlags & C_ANTIPORTAL ) {
498 f->priority += ANTIPORTAL_PRIORITY;
500 if ( f->compileFlags & C_AREAPORTAL ) {
501 f->priority += AREAPORTAL_PRIORITY;
503 if ( f->compileFlags & C_DETAIL ) {
504 f->priority += DETAIL_PRIORITY;