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