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 ------------------------------------------------------------------------------- */
42 emits a bsp shader entry
45 int EmitShader( const char *shader, int *contentFlags, int *surfaceFlags )
51 /* handle special cases */
55 /* try to find an existing shader */
56 for( i = 0; i < numBSPShaders; i++ )
58 /* ydnar: handle custom surface/content flags */
59 if( surfaceFlags != NULL && bspShaders[ i ].surfaceFlags != *surfaceFlags )
61 if( contentFlags != NULL && bspShaders[ i ].contentFlags != *contentFlags )
65 if( !Q_stricmp( shader, bspShaders[ i ].shader ) )
70 si = ShaderInfoForShader( shader );
72 /* emit a new shader */
73 if( i == MAX_MAP_SHADERS )
74 Error( "MAX_MAP_SHADERS" );
76 strcpy( bspShaders[ i ].shader, shader );
77 bspShaders[ i ].surfaceFlags = si->surfaceFlags;
78 bspShaders[ i ].contentFlags = si->contentFlags;
80 /* handle custom content/surface flags */
81 if( surfaceFlags != NULL )
82 bspShaders[ i ].surfaceFlags = *surfaceFlags;
83 if( contentFlags != NULL )
84 bspShaders[ i ].contentFlags = *contentFlags;
86 /* recursively emit any damage shaders */
87 if( si->damageShader[ 0 ] != '\0' )
89 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
90 EmitShader( si->damageShader, NULL, NULL );
101 there is no oportunity to discard planes, because all of the original
102 brushes will be saved in the map
105 void EmitPlanes( void )
112 /* walk plane list */
114 for( i = 0; i < nummapplanes; i++, mp++ )
116 bp = &bspPlanes[ numBSPPlanes ];
117 VectorCopy( mp->normal, bp->normal );
122 /* emit some statistics */
123 Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
130 emits a leafnode to the bsp file
133 void EmitLeaf( node_t *node )
142 if( numBSPLeafs >= MAX_MAP_LEAFS )
143 Error( "MAX_MAP_LEAFS" );
145 leaf_p = &bspLeafs[numBSPLeafs];
148 leaf_p->cluster = node->cluster;
149 leaf_p->area = node->area;
151 /* emit bounding box */
152 VectorCopy( node->mins, leaf_p->mins );
153 VectorCopy( node->maxs, leaf_p->maxs );
155 /* emit leaf brushes */
156 leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
157 for( b = node->brushlist; b; b = b->next )
159 /* something is corrupting brushes */
162 Sys_Printf( "WARNING: Node brush list corrupted (0x%08X)\n", b );
165 //% if( b->guard != 0xDEADBEEF )
166 //% 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 );
168 if( numBSPLeafBrushes >= MAX_MAP_LEAFBRUSHES )
169 Error( "MAX_MAP_LEAFBRUSHES" );
170 bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum;
174 leaf_p->numBSPLeafBrushes = numBSPLeafBrushes - leaf_p->firstBSPLeafBrush;
176 /* emit leaf surfaces */
180 /* add the drawSurfRef_t drawsurfs */
181 leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
182 for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
184 if( numBSPLeafSurfaces >= MAX_MAP_LEAFFACES )
185 Error( "MAX_MAP_LEAFFACES" );
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 if( numBSPNodes == MAX_MAP_NODES )
214 Error( "MAX_MAP_NODES" );
215 n = &bspNodes[ numBSPNodes ];
218 VectorCopy (node->mins, n->mins);
219 VectorCopy (node->maxs, n->maxs);
221 if (node->planenum & 1)
222 Error ("WriteDrawNodes_r: odd planenum");
223 n->planeNum = node->planenum;
226 // recursively output the other nodes
228 for (i=0 ; i<2 ; i++)
230 if (node->children[i]->planenum == PLANENUM_LEAF)
232 n->children[i] = -(numBSPLeafs + 1);
233 EmitLeaf (node->children[i]);
237 n->children[i] = numBSPNodes;
238 EmitDrawNode_r (node->children[i]);
252 void SetModelNumbers (void)
259 for ( i=1 ; i<numEntities ; i++ ) {
260 if ( entities[i].brushes || entities[i].patches ) {
261 sprintf ( value, "*%i", models );
263 SetKeyValue (&entities[i], "model", value);
274 sets style keys for entity lights
277 void SetLightStyles( void )
279 int i, j, style, numStyles;
285 char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
286 int lightStyles[ MAX_SWITCHED_LIGHTS ];
289 /* ydnar: determine if we keep lights in the bsp */
290 t = ValueForKey( &entities[ 0 ], "_keepLights" );
291 keepLights = (t[ 0 ] == '1') ? qtrue : qfalse;
293 /* any light that is controlled (has a targetname) must have a unique style number generated for it */
295 for( i = 1; i < numEntities; i++ )
299 t = ValueForKey( e, "classname" );
300 if( Q_strncasecmp( t, "light", 5 ) )
302 t = ValueForKey( e, "targetname" );
305 /* ydnar: strip the light from the BSP file */
306 if( keepLights == qfalse )
325 /* get existing style */
326 style = IntForKey( e, "style" );
327 if( style < LS_NORMAL || style > LS_NONE )
328 Error( "Invalid lightstyle (%d) on entity %d", style, i );
330 /* find this targetname */
331 for( j = 0; j < numStyles; j++ )
332 if( lightStyles[ j ] == style && !strcmp( lightTargets[ j ], t ) )
335 /* add a new style */
338 if( numStyles == MAX_SWITCHED_LIGHTS )
339 Error( "MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS );
340 strcpy( lightTargets[ j ], t );
341 lightStyles[ j ] = style;
345 /* set explicit style */
346 sprintf( value, "%d", 32 + j );
347 SetKeyValue( e, "style", value );
350 if( style != LS_NORMAL )
352 sprintf( value, "%d", style );
353 SetKeyValue( e, "switch_style", value );
357 /* emit some statistics */
358 Sys_FPrintf( SYS_VRB, "%9d light entities stripped\n", numStrippedLights );
365 starts a new bsp file
368 void BeginBSPFile( void )
370 /* these values may actually be initialized if the file existed when loaded, so clear them explicitly */
373 numBSPBrushSides = 0;
374 numBSPLeafSurfaces = 0;
375 numBSPLeafBrushes = 0;
377 /* leave leaf 0 as an error, because leafs are referenced as negative number nodes */
381 /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
382 numBSPDrawIndexes = 6;
383 bspDrawIndexes[ 0 ] = 0;
384 bspDrawIndexes[ 1 ] = 1;
385 bspDrawIndexes[ 2 ] = 2;
386 bspDrawIndexes[ 3 ] = 0;
387 bspDrawIndexes[ 4 ] = 2;
388 bspDrawIndexes[ 5 ] = 3;
395 finishes a new bsp and writes to disk
398 void EndBSPFile( void )
403 Sys_FPrintf( SYS_VRB, "--- EndBSPFile ---\n" );
407 numBSPEntities = numEntities;
410 /* write the surface extra file */
411 WriteSurfaceExtraFile( source );
414 sprintf( path, "%s.bsp", source );
415 Sys_Printf( "Writing %s\n", path );
416 WriteBSPFile( path );
423 writes the brush list to the bsp
426 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes )
434 /* set initial brush */
435 if( firstBrush != NULL )
436 *firstBrush = numBSPBrushes;
437 if( numBrushes != NULL )
440 /* walk list of brushes */
441 for( b = brushes; b != NULL; b = b->next )
444 if( numBSPBrushes == MAX_MAP_BRUSHES )
445 Error( "MAX_MAP_BRUSHES (%d)", numBSPBrushes );
448 b->outputNum = numBSPBrushes;
449 db = &bspBrushes[ numBSPBrushes ];
451 if( numBrushes != NULL )
454 db->shaderNum = EmitShader( b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags );
455 db->firstSide = numBSPBrushSides;
459 for( j = 0; j < b->numsides; j++ )
461 /* set output number to bogus initially */
462 b->sides[ j ].outputNum = -1;
464 /* don't emit generated backSide sides */
465 if ( b->sides[ j ].backSide )
469 if( numBSPBrushSides == MAX_MAP_BRUSHSIDES )
470 Error( "MAX_MAP_BRUSHSIDES ");
473 b->sides[ j ].outputNum = numBSPBrushSides;
474 cp = &bspBrushSides[ numBSPBrushSides ];
477 cp->planeNum = b->sides[ j ].planenum;
480 if( b->sides[ j ].shaderInfo )
481 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
483 cp->shaderNum = EmitShader( NULL, NULL, NULL );
492 turns map fogs into bsp fogs
495 void EmitFogs( void )
501 numBSPFogs = numMapFogs;
504 for( i = 0; i < numMapFogs; i++ )
507 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
509 /* global fog doesn't have an associated brush */
510 if( mapFogs[ i ].brush == NULL )
512 bspFogs[ i ].brushNum = -1;
513 bspFogs[ i ].visibleSide = -1;
518 bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
520 /* try to use forced visible side */
521 if( mapFogs[ i ].visibleSide >= 0 )
523 bspFogs[ i ].visibleSide = mapFogs[ i ].visibleSide;
527 /* find visible side */
528 for( j = 0; j < 6; j++ )
530 if( mapFogs[ i ].brush->sides[ j ].visibleHull != NULL )
532 Sys_Printf( "Fog %d has visible side %d\n", i, j );
533 bspFogs[ i ].visibleSide = j;
545 sets up a new brush model
548 void BeginModel( void )
554 vec3_t lgMins, lgMaxs; /* ydnar: lightgrid mins/maxs */
560 if( numBSPModels == MAX_MAP_MODELS )
561 Error( "MAX_MAP_MODELS" );
563 /* get model and entity */
564 mod = &bspModels[ numBSPModels ];
565 e = &entities[ mapEntityNum ];
567 /* ydnar: lightgrid mins/maxs */
568 ClearBounds( lgMins, lgMaxs );
570 /* bound the brushes */
571 ClearBounds( mins, maxs );
572 for ( b = e->brushes; b; b = b->next )
574 /* ignore non-real brushes (origin, etc) */
575 if( b->numsides == 0 )
577 AddPointToBounds( b->mins, mins, maxs );
578 AddPointToBounds( b->maxs, mins, maxs );
580 /* ydnar: lightgrid bounds */
581 if( b->compileFlags & C_LIGHTGRID )
583 AddPointToBounds( b->mins, lgMins, lgMaxs );
584 AddPointToBounds( b->maxs, lgMins, lgMaxs );
589 for( p = e->patches; p; p = p->next )
591 for( i = 0; i < (p->mesh.width * p->mesh.height); i++ )
592 AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
595 /* ydnar: lightgrid mins/maxs */
596 if( lgMins[ 0 ] < 99999 )
598 /* use lightgrid bounds */
599 VectorCopy( lgMins, mod->mins );
600 VectorCopy( lgMaxs, mod->maxs );
604 /* use brush/patch bounds */
605 VectorCopy( mins, mod->mins );
606 VectorCopy( maxs, mod->maxs );
610 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 ] );
611 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 ] );
614 mod->firstBSPSurface = numBSPDrawSurfaces;
615 mod->firstBSPBrush = numBSPBrushes;
623 finish a model's processing
626 void EndModel( entity_t *e, node_t *headnode )
632 Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
635 mod = &bspModels[ numBSPModels ];
636 EmitDrawNode_r( headnode );
638 /* set surfaces and brushes */
639 mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
640 mod->firstBSPBrush = e->firstBrush;
641 mod->numBSPBrushes = e->numBrushes;
643 /* increment model count */