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 ){
51 /* handle special cases */
52 if ( shader == NULL ) {
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 ) {
63 if ( contentFlags != NULL && bspShaders[ i ].contentFlags != *contentFlags ) {
67 si = ShaderInfoForShader( shader );
68 if ( si->remapShader && si->remapShader[ 0 ] ) {
69 shader = si->remapShader;
73 if ( !Q_stricmp( shader, bspShaders[ i ].shader ) ) {
79 si = ShaderInfoForShader( shader );
81 /* emit a new shader */
82 AUTOEXPAND_BY_REALLOC_BSP( Shaders, 1024 );
85 strcpy( bspShaders[ i ].shader, shader );
86 bspShaders[ i ].surfaceFlags = si->surfaceFlags;
87 bspShaders[ i ].contentFlags = si->contentFlags;
89 /* handle custom content/surface flags */
90 if ( surfaceFlags != NULL ) {
91 bspShaders[ i ].surfaceFlags = *surfaceFlags;
93 if ( contentFlags != NULL ) {
94 bspShaders[ i ].contentFlags = *contentFlags;
97 /* recursively emit any damage shaders */
98 if ( si->damageShader != NULL && si->damageShader[ 0 ] != '\0' ) {
99 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
100 EmitShader( si->damageShader, NULL, NULL );
111 there is no oportunity to discard planes, because all of the original
112 brushes will be saved in the map
115 void EmitPlanes( void ){
121 /* walk plane list */
123 for ( i = 0; i < nummapplanes; i++, mp++ )
125 AUTOEXPAND_BY_REALLOC_BSP( Planes, 1024 );
126 bp = &bspPlanes[ numBSPPlanes ];
127 VectorCopy( mp->normal, bp->normal );
132 /* emit some statistics */
133 Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
140 emits a leafnode to the bsp file
143 void EmitLeaf( node_t *node ){
150 if ( numBSPLeafs >= MAX_MAP_LEAFS ) {
151 Error( "MAX_MAP_LEAFS" );
154 leaf_p = &bspLeafs[numBSPLeafs];
157 leaf_p->cluster = node->cluster;
158 leaf_p->area = node->area;
160 /* emit bounding box */
161 VectorCopy( node->mins, leaf_p->mins );
162 VectorCopy( node->maxs, leaf_p->maxs );
164 /* emit leaf brushes */
165 leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
166 for ( b = node->brushlist; b; b = b->next )
168 /* something is corrupting brushes */
169 if ( (size_t) b < 256 ) {
170 Sys_FPrintf( SYS_WRN, "WARNING: Node brush list corrupted (0x%08X)\n", b );
173 //% if( b->guard != 0xDEADBEEF )
174 //% 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 );
176 AUTOEXPAND_BY_REALLOC_BSP( LeafBrushes, 1024 );
177 bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum;
181 leaf_p->numBSPLeafBrushes = numBSPLeafBrushes - leaf_p->firstBSPLeafBrush;
183 /* emit leaf surfaces */
184 if ( node->opaque ) {
188 /* add the drawSurfRef_t drawsurfs */
189 leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
190 for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
192 AUTOEXPAND_BY_REALLOC_BSP( LeafSurfaces, 1024 );
193 bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum;
194 numBSPLeafSurfaces++;
197 leaf_p->numBSPLeafSurfaces = numBSPLeafSurfaces - leaf_p->firstBSPLeafSurface;
203 recursively emit the bsp nodes
206 int EmitDrawNode_r( node_t *node ){
211 /* check for leafnode */
212 if ( node->planenum == PLANENUM_LEAF ) {
218 AUTOEXPAND_BY_REALLOC_BSP( Nodes, 1024 );
223 VectorCopy( node->mins, n->mins );
224 VectorCopy( node->maxs, n->maxs );
226 if ( node->planenum & 1 ) {
227 Error( "WriteDrawNodes_r: odd planenum" );
229 n->planeNum = node->planenum;
232 // recursively output the other nodes
234 for ( i = 0 ; i < 2 ; i++ )
236 if ( node->children[i]->planenum == PLANENUM_LEAF ) {
237 n->children[i] = -( numBSPLeafs + 1 );
238 EmitLeaf( node->children[i] );
242 n->children[i] = numBSPNodes;
243 EmitDrawNode_r( node->children[i] );
244 // n may have become invalid here, so...
259 void SetModelNumbers( void ){
265 for ( i = 1 ; i < numEntities ; i++ ) {
266 if ( entities[i].brushes || entities[i].patches ) {
267 sprintf( value, "*%i", models );
269 SetKeyValue( &entities[i], "model", value );
280 sets style keys for entity lights
283 void SetLightStyles( void ){
284 int i, j, style, numStyles;
289 char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
290 int lightStyles[ MAX_SWITCHED_LIGHTS ];
292 /* -keeplights option: force lights to be kept and ignore what the map file says */
294 SetKeyValue( &entities[0], "_keepLights", "1" );
297 /* ydnar: determine if we keep lights in the bsp */
298 if ( KeyExists( &entities[ 0 ], "_keepLights" ) == qtrue ) {
299 t = ValueForKey( &entities[ 0 ], "_keepLights" );
300 keepLights = ( t[ 0 ] == '1' ) ? qtrue : qfalse;
303 /* any light that is controlled (has a targetname) must have a unique style number generated for it */
305 for ( i = 1; i < numEntities; i++ )
309 t = ValueForKey( e, "classname" );
310 if ( Q_strncasecmp( t, "light", 5 ) ) {
313 t = ValueForKey( e, "targetname" );
314 if ( t[ 0 ] == '\0' ) {
315 /* ydnar: strip the light from the BSP file */
316 if ( keepLights == qfalse ) {
334 /* get existing style */
335 style = IntForKey( e, "style" );
336 if ( style < LS_NORMAL || style > LS_NONE ) {
337 Error( "Invalid lightstyle (%d) on entity %d", style, i );
340 /* find this targetname */
341 for ( j = 0; j < numStyles; j++ )
342 if ( lightStyles[ j ] == style && !strcmp( lightTargets[ j ], t ) ) {
346 /* add a new style */
347 if ( j >= numStyles ) {
348 if ( numStyles == MAX_SWITCHED_LIGHTS ) {
349 Error( "MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS );
351 strcpy( lightTargets[ j ], t );
352 lightStyles[ j ] = style;
356 /* set explicit style */
357 sprintf( value, "%d", 32 + j );
358 SetKeyValue( e, "style", value );
361 if ( style != LS_NORMAL ) {
362 sprintf( value, "%d", style );
363 SetKeyValue( e, "switch_style", value );
367 /* emit some statistics */
368 Sys_FPrintf( SYS_VRB, "%9d light entities stripped\n", numStrippedLights );
375 starts a new bsp file
378 void BeginBSPFile( void ){
379 /* these values may actually be initialized if the file existed when loaded, so clear them explicitly */
382 numBSPBrushSides = 0;
383 numBSPLeafSurfaces = 0;
384 numBSPLeafBrushes = 0;
386 /* leave leaf 0 as an error, because leafs are referenced as negative number nodes */
390 /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
391 numBSPDrawIndexes = 6;
392 AUTOEXPAND_BY_REALLOC_BSP( DrawIndexes, 1024 );
393 bspDrawIndexes[ 0 ] = 0;
394 bspDrawIndexes[ 1 ] = 1;
395 bspDrawIndexes[ 2 ] = 2;
396 bspDrawIndexes[ 3 ] = 0;
397 bspDrawIndexes[ 4 ] = 2;
398 bspDrawIndexes[ 5 ] = 3;
405 finishes a new bsp and writes to disk
408 void EndBSPFile( qboolean do_write, const char *BSPFilePath, const char *surfaceFilePath ){
410 Sys_FPrintf( SYS_VRB, "--- EndBSPFile ---\n" );
414 numBSPEntities = numEntities;
418 /* write the surface extra file */
419 WriteSurfaceExtraFile( surfaceFilePath );
422 Sys_Printf( "Writing %s\n", BSPFilePath );
423 WriteBSPFile( BSPFilePath );
431 writes the brush list to the bsp
434 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes ){
441 /* set initial brush */
442 if ( firstBrush != NULL ) {
443 *firstBrush = numBSPBrushes;
445 if ( numBrushes != NULL ) {
449 /* walk list of brushes */
450 for ( b = brushes; b != NULL; b = b->next )
453 AUTOEXPAND_BY_REALLOC_BSP( Brushes, 1024 );
456 b->outputNum = numBSPBrushes;
457 db = &bspBrushes[ numBSPBrushes ];
459 if ( numBrushes != NULL ) {
463 db->shaderNum = EmitShader( b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags );
464 db->firstSide = numBSPBrushSides;
468 for ( j = 0; j < b->numsides; j++ )
470 /* set output number to bogus initially */
471 b->sides[ j ].outputNum = -1;
474 AUTOEXPAND_BY_REALLOC_BSP( BrushSides, 1024 );
477 b->sides[ j ].outputNum = numBSPBrushSides;
478 cp = &bspBrushSides[ numBSPBrushSides ];
481 cp->planeNum = b->sides[ j ].planenum;
484 if ( b->sides[ j ].shaderInfo ) {
485 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
488 cp->shaderNum = EmitShader( NULL, NULL, NULL );
498 turns map fogs into bsp fogs
501 void EmitFogs( void ){
506 numBSPFogs = numMapFogs;
509 for ( i = 0; i < numMapFogs; i++ )
512 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
514 /* global fog doesn't have an associated brush */
515 if ( mapFogs[ i ].brush == NULL ) {
516 bspFogs[ i ].brushNum = -1;
517 bspFogs[ i ].visibleSide = -1;
522 bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
524 /* try to use forced visible side */
525 if ( mapFogs[ i ].visibleSide >= 0 ) {
526 bspFogs[ i ].visibleSide = mapFogs[ i ].visibleSide;
530 /* find visible side */
531 for ( j = 0; j < 6; j++ )
533 if ( mapFogs[ i ].brush->sides[ j ].visibleHull != NULL ) {
534 Sys_Printf( "Fog %d has visible side %d\n", i, j );
535 bspFogs[ i ].visibleSide = j;
547 sets up a new brush model
550 void BeginModel( void ){
555 vec3_t lgMins, lgMaxs; /* ydnar: lightgrid mins/maxs */
561 AUTOEXPAND_BY_REALLOC_BSP( Models, 256 );
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 ) {
578 AddPointToBounds( b->mins, mins, maxs );
579 AddPointToBounds( b->maxs, mins, maxs );
581 /* ydnar: lightgrid bounds */
582 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 ) {
597 /* use lightgrid bounds */
598 VectorCopy( lgMins, mod->mins );
599 VectorCopy( lgMaxs, mod->maxs );
603 /* use brush/patch bounds */
604 VectorCopy( mins, mod->mins );
605 VectorCopy( maxs, mod->maxs );
609 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 ] );
610 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 ] );
613 mod->firstBSPSurface = numBSPDrawSurfaces;
614 mod->firstBSPBrush = numBSPBrushes;
622 finish a model's processing
625 void EndModel( entity_t *e, node_t *headnode ){
630 Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
633 mod = &bspModels[ numBSPModels ];
634 EmitDrawNode_r( headnode );
636 /* set surfaces and brushes */
637 mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
638 mod->firstBSPBrush = e->firstBrush;
639 mod->numBSPBrushes = e->numBrushes;
641 /* increment model count */