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 ) {
68 if ( !Q_stricmp( shader, bspShaders[ i ].shader ) ) {
74 si = ShaderInfoForShader( shader );
76 /* emit a new shader */
77 if ( i == MAX_MAP_SHADERS ) {
78 Error( "MAX_MAP_SHADERS" );
81 strcpy( bspShaders[ i ].shader, shader );
82 bspShaders[ i ].surfaceFlags = si->surfaceFlags;
83 bspShaders[ i ].contentFlags = si->contentFlags;
85 /* handle custom content/surface flags */
86 if ( surfaceFlags != NULL ) {
87 bspShaders[ i ].surfaceFlags = *surfaceFlags;
89 if ( contentFlags != NULL ) {
90 bspShaders[ i ].contentFlags = *contentFlags;
93 /* recursively emit any damage shaders */
94 if ( si->damageShader != NULL && si->damageShader[ 0 ] != '\0' ) {
95 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
96 EmitShader( si->damageShader, NULL, NULL );
107 there is no oportunity to discard planes, because all of the original
108 brushes will be saved in the map
111 void EmitPlanes( void ){
117 /* walk plane list */
119 for ( i = 0; i < nummapplanes; i++, mp++ )
121 bp = &bspPlanes[ numBSPPlanes ];
122 VectorCopy( mp->normal, bp->normal );
127 /* emit some statistics */
128 Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
135 emits a leafnode to the bsp file
138 void EmitLeaf( node_t *node ){
145 if ( numBSPLeafs >= MAX_MAP_LEAFS ) {
146 Error( "MAX_MAP_LEAFS" );
149 leaf_p = &bspLeafs[numBSPLeafs];
152 leaf_p->cluster = node->cluster;
153 leaf_p->area = node->area;
155 /* emit bounding box */
156 VectorCopy( node->mins, leaf_p->mins );
157 VectorCopy( node->maxs, leaf_p->maxs );
159 /* emit leaf brushes */
160 leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
161 for ( b = node->brushlist; b; b = b->next )
163 /* something is corrupting brushes */
164 if ( (size_t) b < 256 ) {
165 Sys_Printf( "WARNING: Node brush list corrupted (0x%08X)\n", b );
168 //% if( b->guard != 0xDEADBEEF )
169 //% 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 );
171 if ( numBSPLeafBrushes >= MAX_MAP_LEAFBRUSHES ) {
172 Error( "MAX_MAP_LEAFBRUSHES" );
174 bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum;
178 leaf_p->numBSPLeafBrushes = numBSPLeafBrushes - leaf_p->firstBSPLeafBrush;
180 /* emit leaf surfaces */
181 if ( node->opaque ) {
185 /* add the drawSurfRef_t drawsurfs */
186 leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
187 for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
189 if ( numBSPLeafSurfaces >= MAX_MAP_LEAFFACES ) {
190 Error( "MAX_MAP_LEAFFACES" );
192 bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum;
193 numBSPLeafSurfaces++;
196 leaf_p->numBSPLeafSurfaces = numBSPLeafSurfaces - leaf_p->firstBSPLeafSurface;
202 recursively emit the bsp nodes
205 int EmitDrawNode_r( node_t *node ){
210 /* check for leafnode */
211 if ( node->planenum == PLANENUM_LEAF ) {
217 if ( numBSPNodes == MAX_MAP_NODES ) {
218 Error( "MAX_MAP_NODES" );
220 n = &bspNodes[ numBSPNodes ];
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] );
257 void SetModelNumbers( void ){
263 for ( i = 1 ; i < numEntities ; i++ ) {
264 if ( entities[i].brushes || entities[i].patches ) {
265 sprintf( value, "*%i", models );
267 SetKeyValue( &entities[i], "model", value );
278 sets style keys for entity lights
281 void SetLightStyles( void ){
282 int i, j, style, numStyles;
288 char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
289 int lightStyles[ MAX_SWITCHED_LIGHTS ];
292 /* ydnar: determine if we keep lights in the bsp */
293 t = ValueForKey( &entities[ 0 ], "_keepLights" );
294 keepLights = ( t[ 0 ] == '1' ) ? qtrue : qfalse;
296 /* any light that is controlled (has a targetname) must have a unique style number generated for it */
298 for ( i = 1; i < numEntities; i++ )
302 t = ValueForKey( e, "classname" );
303 if ( Q_strncasecmp( t, "light", 5 ) ) {
306 t = ValueForKey( e, "targetname" );
307 if ( t[ 0 ] == '\0' ) {
308 /* ydnar: strip the light from the BSP file */
309 if ( keepLights == qfalse ) {
327 /* get existing style */
328 style = IntForKey( e, "style" );
329 if ( style < LS_NORMAL || style > LS_NONE ) {
330 Error( "Invalid lightstyle (%d) on entity %d", style, i );
333 /* find this targetname */
334 for ( j = 0; j < numStyles; j++ )
335 if ( lightStyles[ j ] == style && !strcmp( lightTargets[ j ], t ) ) {
339 /* add a new style */
340 if ( j >= numStyles ) {
341 if ( numStyles == MAX_SWITCHED_LIGHTS ) {
342 Error( "MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS );
344 strcpy( lightTargets[ j ], t );
345 lightStyles[ j ] = style;
349 /* set explicit style */
350 sprintf( value, "%d", 32 + j );
351 SetKeyValue( e, "style", value );
354 if ( style != LS_NORMAL ) {
355 sprintf( value, "%d", style );
356 SetKeyValue( e, "switch_style", value );
360 /* emit some statistics */
361 Sys_FPrintf( SYS_VRB, "%9d light entities stripped\n", numStrippedLights );
368 starts a new bsp file
371 void BeginBSPFile( void ){
372 /* these values may actually be initialized if the file existed when loaded, so clear them explicitly */
375 numBSPBrushSides = 0;
376 numBSPLeafSurfaces = 0;
377 numBSPLeafBrushes = 0;
379 /* leave leaf 0 as an error, because leafs are referenced as negative number nodes */
383 /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
384 numBSPDrawIndexes = 6;
385 bspDrawIndexes[ 0 ] = 0;
386 bspDrawIndexes[ 1 ] = 1;
387 bspDrawIndexes[ 2 ] = 2;
388 bspDrawIndexes[ 3 ] = 0;
389 bspDrawIndexes[ 4 ] = 2;
390 bspDrawIndexes[ 5 ] = 3;
397 finishes a new bsp and writes to disk
400 void EndBSPFile( void ){
404 Sys_FPrintf( SYS_VRB, "--- EndBSPFile ---\n" );
408 numBSPEntities = numEntities;
411 /* write the surface extra file */
412 WriteSurfaceExtraFile( source );
415 sprintf( path, "%s.bsp", source );
416 Sys_Printf( "Writing %s\n", path );
417 WriteBSPFile( path );
424 writes the brush list to the bsp
427 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes ){
434 /* set initial brush */
435 if ( firstBrush != NULL ) {
436 *firstBrush = numBSPBrushes;
438 if ( numBrushes != NULL ) {
442 /* walk list of brushes */
443 for ( b = brushes; b != NULL; b = b->next )
446 if ( numBSPBrushes == MAX_MAP_BRUSHES ) {
447 Error( "MAX_MAP_BRUSHES (%d)", numBSPBrushes );
451 b->outputNum = numBSPBrushes;
452 db = &bspBrushes[ numBSPBrushes ];
454 if ( numBrushes != NULL ) {
458 db->shaderNum = EmitShader( b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags );
459 db->firstSide = numBSPBrushSides;
463 for ( j = 0; j < b->numsides; j++ )
465 /* set output number to bogus initially */
466 b->sides[ j ].outputNum = -1;
469 if ( numBSPBrushSides == MAX_MAP_BRUSHSIDES ) {
470 Error( "MAX_MAP_BRUSHSIDES " );
474 b->sides[ j ].outputNum = numBSPBrushSides;
475 cp = &bspBrushSides[ numBSPBrushSides ];
478 cp->planeNum = b->sides[ j ].planenum;
481 if ( b->sides[ j ].shaderInfo ) {
482 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
485 cp->shaderNum = EmitShader( NULL, NULL, NULL );
495 turns map fogs into bsp fogs
498 void EmitFogs( void ){
503 numBSPFogs = numMapFogs;
506 for ( i = 0; i < numMapFogs; i++ )
509 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
511 /* global fog doesn't have an associated brush */
512 if ( mapFogs[ i ].brush == NULL ) {
513 bspFogs[ i ].brushNum = -1;
514 bspFogs[ i ].visibleSide = -1;
519 bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
521 /* try to use forced visible side */
522 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 ) {
531 Sys_Printf( "Fog %d has visible side %d\n", i, j );
532 bspFogs[ i ].visibleSide = j;
544 sets up a new brush model
547 void BeginModel( void ){
552 vec3_t lgMins, lgMaxs; /* ydnar: lightgrid mins/maxs */
558 if ( numBSPModels == MAX_MAP_MODELS ) {
559 Error( "MAX_MAP_MODELS" );
562 /* get model and entity */
563 mod = &bspModels[ numBSPModels ];
564 e = &entities[ mapEntityNum ];
566 /* ydnar: lightgrid mins/maxs */
567 ClearBounds( lgMins, lgMaxs );
569 /* bound the brushes */
570 ClearBounds( mins, maxs );
571 for ( b = e->brushes; b; b = b->next )
573 /* ignore non-real brushes (origin, etc) */
574 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 ) {
582 AddPointToBounds( b->mins, lgMins, lgMaxs );
583 AddPointToBounds( b->maxs, lgMins, lgMaxs );
588 for ( p = e->patches; p; p = p->next )
590 for ( i = 0; i < ( p->mesh.width * p->mesh.height ); i++ )
591 AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
594 /* ydnar: lightgrid mins/maxs */
595 if ( lgMins[ 0 ] < 99999 ) {
596 /* use lightgrid bounds */
597 VectorCopy( lgMins, mod->mins );
598 VectorCopy( lgMaxs, mod->maxs );
602 /* use brush/patch bounds */
603 VectorCopy( mins, mod->mins );
604 VectorCopy( maxs, mod->maxs );
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 ] );
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 ] );
612 mod->firstBSPSurface = numBSPDrawSurfaces;
613 mod->firstBSPBrush = numBSPBrushes;
621 finish a model's processing
624 void EndModel( entity_t *e, node_t *headnode ){
629 Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
632 mod = &bspModels[ numBSPModels ];
633 EmitDrawNode_r( headnode );
635 /* set surfaces and brushes */
636 mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
637 mod->firstBSPBrush = e->firstBrush;
638 mod->numBSPBrushes = e->numBrushes;
640 /* increment model count */