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 ------------------------------------------------------------------------------- */
43 emits a bsp shader entry
46 int EmitShader( const char *shader, int *contentFlags, int *surfaceFlags )
52 /* handle special cases */
56 /* try to find an existing shader */
57 for( i = 0; i < numBSPShaders; i++ )
59 /* ydnar: handle custom surface/content flags */
60 if( surfaceFlags != NULL && bspShaders[ i ].surfaceFlags != *surfaceFlags )
62 if( contentFlags != NULL && bspShaders[ i ].contentFlags != *contentFlags )
66 if( !Q_stricmp( shader, bspShaders[ i ].shader ) )
73 si = ShaderInfoForShader( shader );
75 /* emit a new shader */
76 AUTOEXPAND_BY_REALLOC_BSP(Shaders, 1024);
79 strcpy( bspShaders[ i ].shader, shader );
80 bspShaders[ i ].surfaceFlags = si->surfaceFlags;
81 bspShaders[ i ].contentFlags = si->contentFlags;
83 /* handle custom content/surface flags */
84 if( surfaceFlags != NULL )
85 bspShaders[ i ].surfaceFlags = *surfaceFlags;
86 if( contentFlags != NULL )
87 bspShaders[ i ].contentFlags = *contentFlags;
89 /* recursively emit any damage shaders */
90 if( si->damageShader != NULL && si->damageShader[ 0 ] != '\0' )
92 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
93 EmitShader( si->damageShader, NULL, NULL );
104 there is no oportunity to discard planes, because all of the original
105 brushes will be saved in the map
108 void EmitPlanes( void )
115 /* walk plane list */
117 for( i = 0; i < nummapplanes; i++, mp++ )
119 bp = &bspPlanes[ numBSPPlanes ];
120 VectorCopy( mp->normal, bp->normal );
125 /* emit some statistics */
126 Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
133 emits a leafnode to the bsp file
136 void EmitLeaf( node_t *node )
144 if( numBSPLeafs >= MAX_MAP_LEAFS )
145 Error( "MAX_MAP_LEAFS" );
147 leaf_p = &bspLeafs[numBSPLeafs];
150 leaf_p->cluster = node->cluster;
151 leaf_p->area = node->area;
153 /* emit bounding box */
154 VectorCopy( node->mins, leaf_p->mins );
155 VectorCopy( node->maxs, leaf_p->maxs );
157 /* emit leaf brushes */
158 leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
159 for( b = node->brushlist; b; b = b->next )
161 /* something is corrupting brushes */
162 if( (size_t) b < 256 )
164 Sys_Printf( "WARNING: Node brush list corrupted (0x%08X)\n", b );
167 //% if( b->guard != 0xDEADBEEF )
168 //% Sys_Printf( "Brush %6d: 0x%08X Guard: 0x%08X Next: 0x%08X Original: 0x%08X Sides: %d\n", b->brushNum, b, b, b->next, b->original, b->numsides );
170 AUTOEXPAND_BY_REALLOC_BSP(LeafBrushes, 1024);
171 bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum;
175 leaf_p->numBSPLeafBrushes = numBSPLeafBrushes - leaf_p->firstBSPLeafBrush;
177 /* emit leaf surfaces */
181 /* add the drawSurfRef_t drawsurfs */
182 leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
183 for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
185 AUTOEXPAND_BY_REALLOC_BSP(LeafSurfaces, 1024);
186 bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum;
187 numBSPLeafSurfaces++;
190 leaf_p->numBSPLeafSurfaces = numBSPLeafSurfaces - leaf_p->firstBSPLeafSurface;
196 recursively emit the bsp nodes
199 int EmitDrawNode_r( node_t *node )
205 /* check for leafnode */
206 if( node->planenum == PLANENUM_LEAF )
213 AUTOEXPAND_BY_REALLOC_BSP(Nodes, 1024);
214 n = &bspNodes[ numBSPNodes ];
217 VectorCopy (node->mins, n->mins);
218 VectorCopy (node->maxs, n->maxs);
220 if (node->planenum & 1)
221 Error ("WriteDrawNodes_r: odd planenum");
222 n->planeNum = node->planenum;
225 // recursively output the other nodes
227 for (i=0 ; i<2 ; i++)
229 if (node->children[i]->planenum == PLANENUM_LEAF)
231 n->children[i] = -(numBSPLeafs + 1);
232 EmitLeaf (node->children[i]);
236 n->children[i] = numBSPNodes;
237 EmitDrawNode_r (node->children[i]);
251 void SetModelNumbers (void)
258 for ( i=1 ; i<numEntities ; i++ ) {
259 if ( entities[i].brushes || entities[i].patches ) {
260 sprintf ( value, "*%i", models );
262 SetKeyValue (&entities[i], "model", value);
273 sets style keys for entity lights
276 void SetLightStyles( void )
278 int i, j, style, numStyles;
284 char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
285 int lightStyles[ MAX_SWITCHED_LIGHTS ];
288 /* ydnar: determine if we keep lights in the bsp */
289 t = ValueForKey( &entities[ 0 ], "_keepLights" );
290 keepLights = (t[ 0 ] == '1') ? qtrue : qfalse;
292 /* any light that is controlled (has a targetname) must have a unique style number generated for it */
294 for( i = 1; i < numEntities; i++ )
298 t = ValueForKey( e, "classname" );
299 if( Q_strncasecmp( t, "light", 5 ) )
301 t = ValueForKey( e, "targetname" );
304 /* ydnar: strip the light from the BSP file */
305 if( keepLights == qfalse )
324 /* get existing style */
325 style = IntForKey( e, "style" );
326 if( style < LS_NORMAL || style > LS_NONE )
327 Error( "Invalid lightstyle (%d) on entity %d", style, i );
329 /* find this targetname */
330 for( j = 0; j < numStyles; j++ )
331 if( lightStyles[ j ] == style && !strcmp( lightTargets[ j ], t ) )
334 /* add a new style */
337 if( numStyles == MAX_SWITCHED_LIGHTS )
338 Error( "MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS );
339 strcpy( lightTargets[ j ], t );
340 lightStyles[ j ] = style;
344 /* set explicit style */
345 sprintf( value, "%d", 32 + j );
346 SetKeyValue( e, "style", value );
349 if( style != LS_NORMAL )
351 sprintf( value, "%d", style );
352 SetKeyValue( e, "switch_style", value );
356 /* emit some statistics */
357 Sys_FPrintf( SYS_VRB, "%9d light entities stripped\n", numStrippedLights );
364 starts a new bsp file
367 void BeginBSPFile( void )
369 /* these values may actually be initialized if the file existed when loaded, so clear them explicitly */
372 numBSPBrushSides = 0;
373 numBSPLeafSurfaces = 0;
374 numBSPLeafBrushes = 0;
376 /* leave leaf 0 as an error, because leafs are referenced as negative number nodes */
380 /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
381 numBSPDrawIndexes = 6;
382 bspDrawIndexes[ 0 ] = 0;
383 bspDrawIndexes[ 1 ] = 1;
384 bspDrawIndexes[ 2 ] = 2;
385 bspDrawIndexes[ 3 ] = 0;
386 bspDrawIndexes[ 4 ] = 2;
387 bspDrawIndexes[ 5 ] = 3;
394 finishes a new bsp and writes to disk
397 void EndBSPFile( void )
402 Sys_FPrintf( SYS_VRB, "--- EndBSPFile ---\n" );
406 numBSPEntities = numEntities;
409 /* write the surface extra file */
410 WriteSurfaceExtraFile( source );
413 sprintf( path, "%s.bsp", source );
414 Sys_Printf( "Writing %s\n", path );
415 WriteBSPFile( path );
422 writes the brush list to the bsp
425 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes )
433 /* set initial brush */
434 if( firstBrush != NULL )
435 *firstBrush = numBSPBrushes;
436 if( numBrushes != NULL )
439 /* walk list of brushes */
440 for( b = brushes; b != NULL; b = b->next )
443 AUTOEXPAND_BY_REALLOC_BSP(Brushes, 1024);
446 b->outputNum = numBSPBrushes;
447 db = &bspBrushes[ numBSPBrushes ];
449 if( numBrushes != NULL )
452 db->shaderNum = EmitShader( b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags );
453 db->firstSide = numBSPBrushSides;
457 for( j = 0; j < b->numsides; j++ )
459 /* set output number to bogus initially */
460 b->sides[ j ].outputNum = -1;
463 AUTOEXPAND_BY_REALLOC_BSP(BrushSides, 1024);
466 b->sides[ j ].outputNum = numBSPBrushSides;
467 cp = &bspBrushSides[ numBSPBrushSides ];
470 cp->planeNum = b->sides[ j ].planenum;
473 if( b->sides[ j ].shaderInfo )
474 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
476 cp->shaderNum = EmitShader( NULL, NULL, NULL );
485 turns map fogs into bsp fogs
488 void EmitFogs( void )
494 numBSPFogs = numMapFogs;
497 for( i = 0; i < numMapFogs; i++ )
500 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
502 /* global fog doesn't have an associated brush */
503 if( mapFogs[ i ].brush == NULL )
505 bspFogs[ i ].brushNum = -1;
506 bspFogs[ i ].visibleSide = -1;
511 bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
513 /* try to use forced visible side */
514 if( mapFogs[ i ].visibleSide >= 0 )
516 bspFogs[ i ].visibleSide = mapFogs[ i ].visibleSide;
520 /* find visible side */
521 for( j = 0; j < 6; j++ )
523 if( mapFogs[ i ].brush->sides[ j ].visibleHull != NULL )
525 Sys_Printf( "Fog %d has visible side %d\n", i, j );
526 bspFogs[ i ].visibleSide = j;
538 sets up a new brush model
541 void BeginModel( void )
547 vec3_t lgMins, lgMaxs; /* ydnar: lightgrid mins/maxs */
553 AUTOEXPAND_BY_REALLOC_BSP(Models, 256);
555 /* get model and entity */
556 mod = &bspModels[ numBSPModels ];
557 e = &entities[ mapEntityNum ];
559 /* ydnar: lightgrid mins/maxs */
560 ClearBounds( lgMins, lgMaxs );
562 /* bound the brushes */
563 ClearBounds( mins, maxs );
564 for ( b = e->brushes; b; b = b->next )
566 /* ignore non-real brushes (origin, etc) */
567 if( b->numsides == 0 )
569 AddPointToBounds( b->mins, mins, maxs );
570 AddPointToBounds( b->maxs, mins, maxs );
572 /* ydnar: lightgrid bounds */
573 if( b->compileFlags & C_LIGHTGRID )
575 AddPointToBounds( b->mins, lgMins, lgMaxs );
576 AddPointToBounds( b->maxs, lgMins, lgMaxs );
581 for( p = e->patches; p; p = p->next )
583 for( i = 0; i < (p->mesh.width * p->mesh.height); i++ )
584 AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
587 /* ydnar: lightgrid mins/maxs */
588 if( lgMins[ 0 ] < 99999 )
590 /* use lightgrid bounds */
591 VectorCopy( lgMins, mod->mins );
592 VectorCopy( lgMaxs, mod->maxs );
596 /* use brush/patch bounds */
597 VectorCopy( mins, mod->mins );
598 VectorCopy( maxs, mod->maxs );
602 Sys_FPrintf( SYS_VRB, "BSP bounds: { %f %f %f } { %f %f %f }\n", mins[ 0 ], mins[ 1 ], mins[ 2 ], maxs[ 0 ], maxs[ 1 ], maxs[ 2 ] );
603 Sys_FPrintf( SYS_VRB, "Lightgrid bounds: { %f %f %f } { %f %f %f }\n", lgMins[ 0 ], lgMins[ 1 ], lgMins[ 2 ], lgMaxs[ 0 ], lgMaxs[ 1 ], lgMaxs[ 2 ] );
606 mod->firstBSPSurface = numBSPDrawSurfaces;
607 mod->firstBSPBrush = numBSPBrushes;
615 finish a model's processing
618 void EndModel( entity_t *e, node_t *headnode )
624 Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
627 mod = &bspModels[ numBSPModels ];
628 EmitDrawNode_r( headnode );
630 /* set surfaces and brushes */
631 mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
632 mod->firstBSPBrush = e->firstBrush;
633 mod->numBSPBrushes = e->numBrushes;
635 /* increment model count */