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 ) )
71 si = ShaderInfoForShader( shader );
73 /* emit a new shader */
74 if( i == MAX_MAP_SHADERS )
75 Error( "MAX_MAP_SHADERS" );
77 strcpy( bspShaders[ i ].shader, shader );
78 bspShaders[ i ].surfaceFlags = si->surfaceFlags;
79 bspShaders[ i ].contentFlags = si->contentFlags;
81 /* handle custom content/surface flags */
82 if( surfaceFlags != NULL )
83 bspShaders[ i ].surfaceFlags = *surfaceFlags;
84 if( contentFlags != NULL )
85 bspShaders[ i ].contentFlags = *contentFlags;
87 /* recursively emit any damage shaders */
88 if( si->damageShader != NULL && si->damageShader[ 0 ] != '\0' )
90 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
91 EmitShader( si->damageShader, NULL, NULL );
102 there is no oportunity to discard planes, because all of the original
103 brushes will be saved in the map
106 void EmitPlanes( void )
113 /* walk plane list */
115 for( i = 0; i < nummapplanes; i++, mp++ )
117 bp = &bspPlanes[ numBSPPlanes ];
118 VectorCopy( mp->normal, bp->normal );
123 /* emit some statistics */
124 Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
131 emits a leafnode to the bsp file
134 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 */
160 if( (size_t) b < 256 )
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 )
405 numBSPEntities = numEntities;
408 /* write the surface extra file */
409 WriteSurfaceExtraFile( source );
412 sprintf( path, "%s.bsp", source );
413 Sys_Printf( "Writing %s\n", path );
414 WriteBSPFile( path );
421 writes the brush list to the bsp
424 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes )
432 /* set initial brush */
433 if( firstBrush != NULL )
434 *firstBrush = numBSPBrushes;
435 if( numBrushes != NULL )
438 /* walk list of brushes */
439 for( b = brushes; b != NULL; b = b->next )
442 if( numBSPBrushes == MAX_MAP_BRUSHES )
443 Error( "MAX_MAP_BRUSHES (%d)", numBSPBrushes );
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 if( numBSPBrushSides == MAX_MAP_BRUSHSIDES )
464 Error( "MAX_MAP_BRUSHSIDES ");
467 b->sides[ j ].outputNum = numBSPBrushSides;
468 cp = &bspBrushSides[ numBSPBrushSides ];
471 cp->planeNum = b->sides[ j ].planenum;
474 if( b->sides[ j ].shaderInfo )
475 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
477 cp->shaderNum = EmitShader( NULL, NULL, NULL );
486 turns map fogs into bsp fogs
489 void EmitFogs( void )
495 numBSPFogs = numMapFogs;
498 for( i = 0; i < numMapFogs; i++ )
501 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
503 /* global fog doesn't have an associated brush */
504 if( mapFogs[ i ].brush == NULL )
506 bspFogs[ i ].brushNum = -1;
507 bspFogs[ i ].visibleSide = -1;
512 bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
514 /* try to use forced visible side */
515 if( mapFogs[ i ].visibleSide >= 0 )
517 bspFogs[ i ].visibleSide = mapFogs[ i ].visibleSide;
521 /* find visible side */
522 for( j = 0; j < 6; j++ )
524 if( mapFogs[ i ].brush->sides[ j ].visibleHull != NULL )
526 Sys_Printf( "Fog %d has visible side %d\n", i, j );
527 bspFogs[ i ].visibleSide = j;
539 sets up a new brush model
542 void BeginModel( void )
548 vec3_t lgMins, lgMaxs; /* ydnar: lightgrid mins/maxs */
554 if( numBSPModels == MAX_MAP_MODELS )
555 Error( "MAX_MAP_MODELS" );
557 /* get model and entity */
558 mod = &bspModels[ numBSPModels ];
559 e = &entities[ mapEntityNum ];
561 /* ydnar: lightgrid mins/maxs */
562 ClearBounds( lgMins, lgMaxs );
564 /* bound the brushes */
565 ClearBounds( mins, maxs );
566 for ( b = e->brushes; b; b = b->next )
568 /* ignore non-real brushes (origin, etc) */
569 if( b->numsides == 0 )
571 AddPointToBounds( b->mins, mins, maxs );
572 AddPointToBounds( b->maxs, mins, maxs );
574 /* ydnar: lightgrid bounds */
575 if( b->compileFlags & C_LIGHTGRID )
577 AddPointToBounds( b->mins, lgMins, lgMaxs );
578 AddPointToBounds( b->maxs, lgMins, lgMaxs );
583 for( p = e->patches; p; p = p->next )
585 for( i = 0; i < (p->mesh.width * p->mesh.height); i++ )
586 AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
589 /* ydnar: lightgrid mins/maxs */
590 if( lgMins[ 0 ] < 99999 )
592 /* use lightgrid bounds */
593 VectorCopy( lgMins, mod->mins );
594 VectorCopy( lgMaxs, mod->maxs );
598 /* use brush/patch bounds */
599 VectorCopy( mins, mod->mins );
600 VectorCopy( maxs, mod->maxs );
604 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 ] );
605 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 ] );
608 mod->firstBSPSurface = numBSPDrawSurfaces;
609 mod->firstBSPBrush = numBSPBrushes;
617 finish a model's processing
620 void EndModel( entity_t *e, node_t *headnode )
626 Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
629 mod = &bspModels[ numBSPModels ];
630 EmitDrawNode_r( headnode );
632 /* set surfaces and brushes */
633 mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
634 mod->firstBSPBrush = e->firstBrush;
635 mod->numBSPBrushes = e->numBrushes;
637 /* increment model count */