2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ----------------------------------------------------------------------------------
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26 ------------------------------------------------------------------------------- */
48 face_t *AllocBspFace( void ) {
51 f = safe_malloc(sizeof(*f));
52 memset( f, 0, 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 )
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 )
105 dist = blockSize[ i ] * (floor( node->mins[ i ] / blockSize[ i ] ) + 1);
106 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 */
120 for( split = list; split; split = split->next )
121 split->checked = qfalse;
123 for( split = list; split; split = split->next )
125 if ( split->checked )
128 plane = &mapplanes[ split->planenum ];
133 for ( check = list ; check ; check = check->next ) {
134 if ( check->planenum == split->planenum ) {
136 check->checked = qtrue; // won't need to test this plane again
139 side = WindingOnPlaneSide( check->w, plane->normal, plane->dist );
140 if ( side == SIDE_CROSS ) {
142 } else if ( side == SIDE_FRONT ) {
144 } else if ( side == SIDE_BACK ) {
148 value = 5*facing - 5*splits; // - abs(front-back);
149 if ( plane->type < 3 ) {
150 value+=5; // axial is better
152 value += split->priority; // prioritize hints higher
154 if ( value > bestValue ) {
160 /* nothing, we have a leaf */
161 if( bestValue == -99999 )
164 /* set best split data */
165 *splitPlaneNum = bestSplit->planenum;
166 *compileFlags = bestSplit->compileFlags;
173 counts bsp faces in the linked list
176 int CountFaceList( face_t *list )
182 for( list; list != NULL; list = list->next )
191 recursively builds the bsp, splitting on face planes
194 void BuildFaceTree_r( node_t *node, face_t *list )
201 face_t *childLists[2];
202 winding_t *frontWinding, *backWinding;
204 int splitPlaneNum, compileFlags;
207 /* count faces left */
208 i = CountFaceList( list );
210 /* select the best split plane */
211 SelectSplitPlaneNum( node, list, &splitPlaneNum, &compileFlags );
213 /* if we don't have any more faces, this is a node */
214 if ( splitPlaneNum == -1 )
216 node->planenum = PLANENUM_LEAF;
221 /* partition the list */
222 node->planenum = splitPlaneNum;
223 node->compileFlags = compileFlags;
224 plane = &mapplanes[ splitPlaneNum ];
225 childLists[0] = NULL;
226 childLists[1] = NULL;
227 for( split = list; split; split = next )
232 /* don't split by identical plane */
233 if( split->planenum == node->planenum )
235 FreeBspFace( split );
239 /* determine which side the face falls on */
240 side = WindingOnPlaneSide( split->w, plane->normal, plane->dist );
243 if( side == SIDE_CROSS )
245 ClipWindingEpsilon( split->w, plane->normal, plane->dist, CLIP_EPSILON * 2,
246 &frontWinding, &backWinding );
248 newFace = AllocBspFace();
249 newFace->w = frontWinding;
250 newFace->next = childLists[0];
251 newFace->planenum = split->planenum;
252 newFace->priority = split->priority;
253 newFace->compileFlags = split->compileFlags;
254 childLists[0] = newFace;
257 newFace = AllocBspFace();
258 newFace->w = backWinding;
259 newFace->next = childLists[1];
260 newFace->planenum = split->planenum;
261 newFace->priority = split->priority;
262 newFace->compileFlags = split->compileFlags;
263 childLists[1] = newFace;
265 FreeBspFace( split );
266 } else if ( side == SIDE_FRONT ) {
267 split->next = childLists[0];
268 childLists[0] = split;
269 } else if ( side == SIDE_BACK ) {
270 split->next = childLists[1];
271 childLists[1] = split;
276 // recursively process children
277 for ( i = 0 ; i < 2 ; i++ ) {
278 node->children[i] = AllocNode();
279 node->children[i]->parent = node;
280 VectorCopy( node->mins, node->children[i]->mins );
281 VectorCopy( node->maxs, node->children[i]->maxs );
284 for ( i = 0 ; i < 3 ; i++ ) {
285 if ( plane->normal[i] == 1 ) {
286 node->children[0]->mins[i] = plane->dist;
287 node->children[1]->maxs[i] = plane->dist;
292 for ( i = 0 ; i < 2 ; i++ ) {
293 BuildFaceTree_r ( node->children[i], childLists[i]);
302 List will be freed before returning
305 tree_t *FaceBSP( face_t *list ) {
311 Sys_FPrintf (SYS_VRB, "--- FaceBSP ---\n" );
316 for( face = list; face != NULL; face = face->next )
319 for( i = 0; i < face->w->numpoints; i++ )
321 AddPointToBounds( face->w->p[ i ], tree->mins, tree->maxs );
324 Sys_FPrintf( SYS_VRB, "%9d faces\n", count );
326 tree->headnode = AllocNode();
327 VectorCopy( tree->mins, tree->headnode->mins );
328 VectorCopy( tree->maxs, tree->headnode->maxs );
331 BuildFaceTree_r ( tree->headnode, list );
333 Sys_FPrintf( SYS_VRB, "%9d leafs\n", c_faceLeafs );
341 MakeStructuralBSPFaceList()
342 get structural brush faces
345 face_t *MakeStructuralBSPFaceList( brush_t *list )
355 for( b = list; b != NULL; b = b->next )
360 for( i = 0; i < b->numsides; i++ )
362 /* get side and winding */
368 /* ydnar: skip certain faces */
369 if( s->compileFlags & C_SKIP )
372 /* allocate a face */
374 f->w = CopyWinding( w );
375 f->planenum = s->planenum & ~1;
376 f->compileFlags = s->compileFlags; /* ydnar */
378 /* ydnar: set priority */
380 if( f->compileFlags & C_HINT )
381 f->priority += HINT_PRIORITY;
382 if( f->compileFlags & C_ANTIPORTAL )
383 f->priority += ANTIPORTAL_PRIORITY;
384 if( f->compileFlags & C_AREAPORTAL )
385 f->priority += AREAPORTAL_PRIORITY;
399 MakeVisibleBSPFaceList()
400 get visible brush faces
403 face_t *MakeVisibleBSPFaceList( brush_t *list )
413 for( b = list; b != NULL; b = b->next )
418 for( i = 0; i < b->numsides; i++ )
420 /* get side and winding */
426 /* ydnar: skip certain faces */
427 if( s->compileFlags & C_SKIP )
430 /* allocate a face */
432 f->w = CopyWinding( w );
433 f->planenum = s->planenum & ~1;
434 f->compileFlags = s->compileFlags; /* ydnar */
436 /* ydnar: set priority */
438 if( f->compileFlags & C_HINT )
439 f->priority += HINT_PRIORITY;
440 if( f->compileFlags & C_ANTIPORTAL )
441 f->priority += ANTIPORTAL_PRIORITY;
442 if( f->compileFlags & C_AREAPORTAL )
443 f->priority += AREAPORTAL_PRIORITY;