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
42 emits a bsp shader entry
\r
45 int EmitShader( const char *shader, int *contentFlags, int *surfaceFlags )
\r
51 /* handle special cases */
\r
52 if( shader == NULL )
\r
53 shader = "noshader";
\r
55 /* try to find an existing shader */
\r
56 for( i = 0; i < numBSPShaders; i++ )
\r
58 /* ydnar: handle custom surface/content flags */
\r
59 if( surfaceFlags != NULL && bspShaders[ i ].surfaceFlags != *surfaceFlags )
\r
61 if( contentFlags != NULL && bspShaders[ i ].contentFlags != *contentFlags )
\r
65 if( !Q_stricmp( shader, bspShaders[ i ].shader ) )
\r
69 /* get shaderinfo */
\r
70 si = ShaderInfoForShader( shader );
\r
72 /* emit a new shader */
\r
73 if( i == MAX_MAP_SHADERS )
\r
74 Error( "MAX_MAP_SHADERS" );
\r
76 strcpy( bspShaders[ i ].shader, shader );
\r
77 bspShaders[ i ].surfaceFlags = si->surfaceFlags;
\r
78 bspShaders[ i ].contentFlags = si->contentFlags;
\r
80 /* handle custom content/surface flags */
\r
81 if( surfaceFlags != NULL )
\r
82 bspShaders[ i ].surfaceFlags = *surfaceFlags;
\r
83 if( contentFlags != NULL )
\r
84 bspShaders[ i ].contentFlags = *contentFlags;
\r
86 /* recursively emit any damage shaders */
\r
87 if( si->damageShader[ 0 ] != '\0' )
\r
89 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
\r
90 EmitShader( si->damageShader, NULL, NULL );
\r
101 there is no oportunity to discard planes, because all of the original
\r
102 brushes will be saved in the map
\r
105 void EmitPlanes( void )
\r
112 /* walk plane list */
\r
114 for( i = 0; i < nummapplanes; i++, mp++ )
\r
116 bp = &bspPlanes[ numBSPPlanes ];
\r
117 VectorCopy( mp->normal, bp->normal );
\r
118 bp->dist = mp->dist;
\r
122 /* emit some statistics */
\r
123 Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
\r
130 emits a leafnode to the bsp file
\r
133 void EmitLeaf( node_t *node )
\r
137 drawSurfRef_t *dsr;
\r
142 if( numBSPLeafs >= MAX_MAP_LEAFS )
\r
143 Error( "MAX_MAP_LEAFS" );
\r
145 leaf_p = &bspLeafs[numBSPLeafs];
\r
148 leaf_p->cluster = node->cluster;
\r
149 leaf_p->area = node->area;
\r
151 /* emit bounding box */
\r
152 VectorCopy( node->mins, leaf_p->mins );
\r
153 VectorCopy( node->maxs, leaf_p->maxs );
\r
155 /* emit leaf brushes */
\r
156 leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
\r
157 for( b = node->brushlist; b; b = b->next )
\r
159 /* something is corrupting brushes */
\r
160 if( (int) b < 256 )
\r
162 Sys_Printf( "WARNING: Node brush list corrupted (0x%08X)\n", b );
\r
165 //% if( b->guard != 0xDEADBEEF )
\r
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 );
\r
168 if( numBSPLeafBrushes >= MAX_MAP_LEAFBRUSHES )
\r
169 Error( "MAX_MAP_LEAFBRUSHES" );
\r
170 bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum;
\r
171 numBSPLeafBrushes++;
\r
174 leaf_p->numBSPLeafBrushes = numBSPLeafBrushes - leaf_p->firstBSPLeafBrush;
\r
176 /* emit leaf surfaces */
\r
180 /* add the drawSurfRef_t drawsurfs */
\r
181 leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
\r
182 for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
\r
184 if( numBSPLeafSurfaces >= MAX_MAP_LEAFFACES )
\r
185 Error( "MAX_MAP_LEAFFACES" );
\r
186 bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum;
\r
187 numBSPLeafSurfaces++;
\r
190 leaf_p->numBSPLeafSurfaces = numBSPLeafSurfaces - leaf_p->firstBSPLeafSurface;
\r
196 recursively emit the bsp nodes
\r
199 int EmitDrawNode_r( node_t *node )
\r
205 /* check for leafnode */
\r
206 if( node->planenum == PLANENUM_LEAF )
\r
209 return -numBSPLeafs;
\r
213 if( numBSPNodes == MAX_MAP_NODES )
\r
214 Error( "MAX_MAP_NODES" );
\r
215 n = &bspNodes[ numBSPNodes ];
\r
218 VectorCopy (node->mins, n->mins);
\r
219 VectorCopy (node->maxs, n->maxs);
\r
221 if (node->planenum & 1)
\r
222 Error ("WriteDrawNodes_r: odd planenum");
\r
223 n->planeNum = node->planenum;
\r
226 // recursively output the other nodes
\r
228 for (i=0 ; i<2 ; i++)
\r
230 if (node->children[i]->planenum == PLANENUM_LEAF)
\r
232 n->children[i] = -(numBSPLeafs + 1);
\r
233 EmitLeaf (node->children[i]);
\r
237 n->children[i] = numBSPNodes;
\r
238 EmitDrawNode_r (node->children[i]);
\r
242 return n - bspNodes;
\r
252 void SetModelNumbers (void)
\r
259 for ( i=1 ; i<numEntities ; i++ ) {
\r
260 if ( entities[i].brushes || entities[i].patches ) {
\r
261 sprintf ( value, "*%i", models );
\r
263 SetKeyValue (&entities[i], "model", value);
\r
274 sets style keys for entity lights
\r
277 void SetLightStyles( void )
\r
279 int i, j, style, numStyles;
\r
280 qboolean keepLights;
\r
283 epair_t *ep, *next;
\r
285 char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
\r
286 int lightStyles[ MAX_SWITCHED_LIGHTS ];
\r
289 /* ydnar: determine if we keep lights in the bsp */
\r
290 t = ValueForKey( &entities[ 0 ], "_keepLights" );
\r
291 keepLights = (t[ 0 ] == '1') ? qtrue : qfalse;
\r
293 /* any light that is controlled (has a targetname) must have a unique style number generated for it */
\r
295 for( i = 1; i < numEntities; i++ )
\r
297 e = &entities[ i ];
\r
299 t = ValueForKey( e, "classname" );
\r
300 if( Q_strncasecmp( t, "light", 5 ) )
\r
302 t = ValueForKey( e, "targetname" );
\r
303 if( t[ 0 ] == '\0' )
\r
305 /* ydnar: strip the light from the BSP file */
\r
306 if( keepLights == qfalse )
\r
309 while( ep != NULL )
\r
318 numStrippedLights++;
\r
325 /* get existing style */
\r
326 style = IntForKey( e, "style" );
\r
327 if( style < LS_NORMAL || style > LS_NONE )
\r
328 Error( "Invalid lightstyle (%d) on entity %d", style, i );
\r
330 /* find this targetname */
\r
331 for( j = 0; j < numStyles; j++ )
\r
332 if( lightStyles[ j ] == style && !strcmp( lightTargets[ j ], t ) )
\r
335 /* add a new style */
\r
336 if( j >= numStyles )
\r
338 if( numStyles == MAX_SWITCHED_LIGHTS )
\r
339 Error( "MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS );
\r
340 strcpy( lightTargets[ j ], t );
\r
341 lightStyles[ j ] = style;
\r
345 /* set explicit style */
\r
346 sprintf( value, "%d", 32 + j );
\r
347 SetKeyValue( e, "style", value );
\r
349 /* set old style */
\r
350 if( style != LS_NORMAL )
\r
352 sprintf( value, "%d", style );
\r
353 SetKeyValue( e, "switch_style", value );
\r
357 /* emit some statistics */
\r
358 Sys_FPrintf( SYS_VRB, "%9d light entities stripped\n", numStrippedLights );
\r
365 starts a new bsp file
\r
368 void BeginBSPFile( void )
\r
370 /* these values may actually be initialized if the file existed when loaded, so clear them explicitly */
\r
373 numBSPBrushSides = 0;
\r
374 numBSPLeafSurfaces = 0;
\r
375 numBSPLeafBrushes = 0;
\r
377 /* leave leaf 0 as an error, because leafs are referenced as negative number nodes */
\r
381 /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
\r
382 numBSPDrawIndexes = 6;
\r
383 bspDrawIndexes[ 0 ] = 0;
\r
384 bspDrawIndexes[ 1 ] = 1;
\r
385 bspDrawIndexes[ 2 ] = 2;
\r
386 bspDrawIndexes[ 3 ] = 0;
\r
387 bspDrawIndexes[ 4 ] = 2;
\r
388 bspDrawIndexes[ 5 ] = 3;
\r
395 finishes a new bsp and writes to disk
\r
398 void EndBSPFile( void )
\r
405 numBSPEntities = numEntities;
\r
408 /* write the surface extra file */
\r
409 WriteSurfaceExtraFile( source );
\r
411 /* write the bsp */
\r
412 sprintf( path, "%s.bsp", source );
\r
413 Sys_Printf( "Writing %s\n", path );
\r
414 WriteBSPFile( path );
\r
421 writes the brush list to the bsp
\r
424 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes )
\r
429 bspBrushSide_t *cp;
\r
432 /* set initial brush */
\r
433 if( firstBrush != NULL )
\r
434 *firstBrush = numBSPBrushes;
\r
435 if( numBrushes != NULL )
\r
438 /* walk list of brushes */
\r
439 for( b = brushes; b != NULL; b = b->next )
\r
442 if( numBSPBrushes == MAX_MAP_BRUSHES )
\r
443 Error( "MAX_MAP_BRUSHES (%d)", numBSPBrushes );
\r
445 /* get bsp brush */
\r
446 b->outputNum = numBSPBrushes;
\r
447 db = &bspBrushes[ numBSPBrushes ];
\r
449 if( numBrushes != NULL )
\r
452 db->shaderNum = EmitShader( b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags );
\r
453 db->firstSide = numBSPBrushSides;
\r
457 for( j = 0; j < b->numsides; j++ )
\r
459 /* set output number to bogus initially */
\r
460 b->sides[ j ].outputNum = -1;
\r
462 /* don't emit generated backSide sides */
\r
463 if ( b->sides[ j ].backSide )
\r
467 if( numBSPBrushSides == MAX_MAP_BRUSHSIDES )
\r
468 Error( "MAX_MAP_BRUSHSIDES ");
\r
471 b->sides[ j ].outputNum = numBSPBrushSides;
\r
472 cp = &bspBrushSides[ numBSPBrushSides ];
\r
474 numBSPBrushSides++;
\r
475 cp->planeNum = b->sides[ j ].planenum;
\r
478 if( b->sides[ j ].shaderInfo )
\r
479 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
\r
481 cp->shaderNum = EmitShader( NULL, NULL, NULL );
\r
490 turns map fogs into bsp fogs
\r
493 void EmitFogs( void )
\r
499 numBSPFogs = numMapFogs;
\r
502 for( i = 0; i < numMapFogs; i++ )
\r
505 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
\r
507 /* global fog doesn't have an associated brush */
\r
508 if( mapFogs[ i ].brush == NULL )
\r
510 bspFogs[ i ].brushNum = -1;
\r
511 bspFogs[ i ].visibleSide = -1;
\r
516 bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
\r
518 /* try to use forced visible side */
\r
519 if( mapFogs[ i ].visibleSide >= 0 )
\r
521 bspFogs[ i ].visibleSide = mapFogs[ i ].visibleSide;
\r
525 /* find visible side */
\r
526 for( j = 0; j < 6; j++ )
\r
528 if( mapFogs[ i ].brush->sides[ j ].visibleHull != NULL )
\r
530 Sys_Printf( "Fog %d has visible side %d\n", i, j );
\r
531 bspFogs[ i ].visibleSide = j;
\r
543 sets up a new brush model
\r
546 void BeginModel( void )
\r
552 vec3_t lgMins, lgMaxs; /* ydnar: lightgrid mins/maxs */
\r
558 if( numBSPModels == MAX_MAP_MODELS )
\r
559 Error( "MAX_MAP_MODELS" );
\r
561 /* get model and entity */
\r
562 mod = &bspModels[ numBSPModels ];
\r
563 e = &entities[ mapEntityNum ];
\r
565 /* ydnar: lightgrid mins/maxs */
\r
566 ClearBounds( lgMins, lgMaxs );
\r
568 /* bound the brushes */
\r
569 ClearBounds( mins, maxs );
\r
570 for ( b = e->brushes; b; b = b->next )
\r
572 /* ignore non-real brushes (origin, etc) */
\r
573 if( b->numsides == 0 )
\r
575 AddPointToBounds( b->mins, mins, maxs );
\r
576 AddPointToBounds( b->maxs, mins, maxs );
\r
578 /* ydnar: lightgrid bounds */
\r
579 if( b->compileFlags & C_LIGHTGRID )
\r
581 AddPointToBounds( b->mins, lgMins, lgMaxs );
\r
582 AddPointToBounds( b->maxs, lgMins, lgMaxs );
\r
586 /* bound patches */
\r
587 for( p = e->patches; p; p = p->next )
\r
589 for( i = 0; i < (p->mesh.width * p->mesh.height); i++ )
\r
590 AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
\r
593 /* ydnar: lightgrid mins/maxs */
\r
594 if( lgMins[ 0 ] < 99999 )
\r
596 /* use lightgrid bounds */
\r
597 VectorCopy( lgMins, mod->mins );
\r
598 VectorCopy( lgMaxs, mod->maxs );
\r
602 /* use brush/patch bounds */
\r
603 VectorCopy( mins, mod->mins );
\r
604 VectorCopy( maxs, mod->maxs );
\r
608 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 ] );
\r
609 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 ] );
\r
612 mod->firstBSPSurface = numBSPDrawSurfaces;
\r
613 mod->firstBSPBrush = numBSPBrushes;
\r
621 finish a model's processing
\r
624 void EndModel( entity_t *e, node_t *headnode )
\r
630 Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
\r
633 mod = &bspModels[ numBSPModels ];
\r
634 EmitDrawNode_r( headnode );
\r
636 /* set surfaces and brushes */
\r
637 mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
\r
638 mod->firstBSPBrush = e->firstBrush;
\r
639 mod->numBSPBrushes = e->numBrushes;
\r
641 /* increment model count */
\r