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 ------------------------------------------------------------------------------- */
47 static prefixInfo_t prefixInfo[] = {
48 { "metal", TEX_SURF_METAL},
49 { "wood", TEX_SURF_WOOD},
50 { "cloth", TEX_SURF_CLOTH},
51 { "dirt", TEX_SURF_DIRT},
52 { "glass", TEX_SURF_GLASS},
53 { "plant", TEX_SURF_PLANT},
54 { "sand", TEX_SURF_SAND},
55 { "snow", TEX_SURF_SNOW},
56 { "stone", TEX_SURF_STONE},
57 { "water", TEX_SURF_WATER},
58 { "grass", TEX_SURF_GRASS},
61 #define NUM_PREFIXINFO 11 /* very important */
63 //Added by Spoon to recognize surfaceparms by shadernames
64 int GetSurfaceParm(const char *tex){
65 char surf[MAX_QPATH], tex2[MAX_QPATH];
71 for(i = 0; i < 64 && tex2[i] != '\0'; i++){
72 if(tex2[i] == '\\' || tex2[i] == '/')
84 /* Sys_Printf("%s\n", surf); */
86 for(i=0; i < NUM_PREFIXINFO; i++){
87 if(!Q_stricmp(surf, prefixInfo[i].name)){
88 return prefixInfo[i].surfaceFlags;
98 emits a bsp shader entry
101 int EmitShader( const char *shader, int *contentFlags, int *surfaceFlags ){
106 /* handle special cases */
107 if ( shader == NULL ) {
111 /* try to find an existing shader */
112 for ( i = 0; i < numBSPShaders; i++ )
114 /* if not Smokin'Guns like tex file */
115 if ( !game->texFile )
117 /* ydnar: handle custom surface/content flags */
118 if ( surfaceFlags != NULL && bspShaders[ i ].surfaceFlags != *surfaceFlags ) {
121 if ( contentFlags != NULL && bspShaders[ i ].contentFlags != *contentFlags ) {
126 si = ShaderInfoForShader( shader );
127 if ( si->remapShader && si->remapShader[ 0 ] ) {
128 shader = si->remapShader;
132 if ( !Q_stricmp( shader, bspShaders[ i ].shader ) ) {
138 si = ShaderInfoForShader( shader );
140 /* emit a new shader */
141 AUTOEXPAND_BY_REALLOC0_BSP( Shaders, 1024 );
144 strcpy( bspShaders[ i ].shader, shader );
145 bspShaders[ i ].surfaceFlags = si->surfaceFlags;
149 /* Smokin'Guns like tex file */
150 bspShaders[ i ].surfaceFlags |= GetSurfaceParm(si->shader);
153 bspShaders[ i ].contentFlags = si->contentFlags;
155 /* if not Smokin'Guns like tex file */
156 if ( !game->texFile )
158 /* handle custom content/surface flags */
159 if ( surfaceFlags != NULL ) {
160 bspShaders[ i ].surfaceFlags = *surfaceFlags;
162 if ( contentFlags != NULL ) {
163 bspShaders[ i ].contentFlags = *contentFlags;
167 /* recursively emit any damage shaders */
168 if ( si->damageShader != NULL && si->damageShader[ 0 ] != '\0' ) {
169 Sys_FPrintf( SYS_VRB, "Shader %s has damage shader %s\n", si->shader, si->damageShader );
170 EmitShader( si->damageShader, NULL, NULL );
181 there is no oportunity to discard planes, because all of the original
182 brushes will be saved in the map
185 void EmitPlanes( void ){
191 /* walk plane list */
193 for ( i = 0; i < nummapplanes; i++, mp++ )
195 AUTOEXPAND_BY_REALLOC_BSP( Planes, 1024 );
196 bp = &bspPlanes[ numBSPPlanes ];
197 VectorCopy( mp->normal, bp->normal );
202 /* emit some statistics */
203 Sys_FPrintf( SYS_VRB, "%9d BSP planes\n", numBSPPlanes );
210 emits a leafnode to the bsp file
213 void EmitLeaf( node_t *node ){
220 if ( numBSPLeafs >= MAX_MAP_LEAFS ) {
221 Error( "MAX_MAP_LEAFS" );
224 leaf_p = &bspLeafs[numBSPLeafs];
227 leaf_p->cluster = node->cluster;
228 leaf_p->area = node->area;
230 /* emit bounding box */
231 VectorCopy( node->mins, leaf_p->mins );
232 VectorCopy( node->maxs, leaf_p->maxs );
234 /* emit leaf brushes */
235 leaf_p->firstBSPLeafBrush = numBSPLeafBrushes;
236 for ( b = node->brushlist; b; b = b->next )
238 /* something is corrupting brushes */
239 if ( (size_t) b < 256 ) {
240 Sys_FPrintf( SYS_WRN, "WARNING: Node brush list corrupted (0x%08X)\n", b );
243 //% if( b->guard != 0xDEADBEEF )
244 //% 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 );
246 AUTOEXPAND_BY_REALLOC_BSP( LeafBrushes, 1024 );
247 bspLeafBrushes[ numBSPLeafBrushes ] = b->original->outputNum;
251 leaf_p->numBSPLeafBrushes = numBSPLeafBrushes - leaf_p->firstBSPLeafBrush;
253 /* emit leaf surfaces */
254 if ( node->opaque ) {
258 /* add the drawSurfRef_t drawsurfs */
259 leaf_p->firstBSPLeafSurface = numBSPLeafSurfaces;
260 for ( dsr = node->drawSurfReferences; dsr; dsr = dsr->nextRef )
262 AUTOEXPAND_BY_REALLOC_BSP( LeafSurfaces, 1024 );
263 bspLeafSurfaces[ numBSPLeafSurfaces ] = dsr->outputNum;
264 numBSPLeafSurfaces++;
267 leaf_p->numBSPLeafSurfaces = numBSPLeafSurfaces - leaf_p->firstBSPLeafSurface;
273 recursively emit the bsp nodes
276 int EmitDrawNode_r( node_t *node ){
281 /* check for leafnode */
282 if ( node->planenum == PLANENUM_LEAF ) {
288 AUTOEXPAND_BY_REALLOC_BSP( Nodes, 1024 );
293 VectorCopy( node->mins, n->mins );
294 VectorCopy( node->maxs, n->maxs );
296 if ( node->planenum & 1 ) {
297 Error( "WriteDrawNodes_r: odd planenum" );
299 n->planeNum = node->planenum;
302 // recursively output the other nodes
304 for ( i = 0 ; i < 2 ; i++ )
306 if ( node->children[i]->planenum == PLANENUM_LEAF ) {
307 n->children[i] = -( numBSPLeafs + 1 );
308 EmitLeaf( node->children[i] );
312 n->children[i] = numBSPNodes;
313 EmitDrawNode_r( node->children[i] );
314 // n may have become invalid here, so...
329 void SetModelNumbers( void ){
335 for ( i = 1 ; i < numEntities ; i++ ) {
336 if ( entities[i].brushes || entities[i].patches ) {
337 sprintf( value, "*%i", models );
339 SetKeyValue( &entities[i], "model", value );
350 sets style keys for entity lights
353 void SetLightStyles( void ){
354 int i, j, style, numStyles;
359 char lightTargets[ MAX_SWITCHED_LIGHTS ][ 64 ];
360 int lightStyles[ MAX_SWITCHED_LIGHTS ];
362 /* -keeplights option: force lights to be kept and ignore what the map file says */
364 SetKeyValue( &entities[0], "_keepLights", "1" );
367 /* ydnar: determine if we keep lights in the bsp */
368 if ( KeyExists( &entities[ 0 ], "_keepLights" ) == qtrue ) {
369 t = ValueForKey( &entities[ 0 ], "_keepLights" );
370 keepLights = ( t[ 0 ] == '1' ) ? qtrue : qfalse;
373 /* any light that is controlled (has a targetname) must have a unique style number generated for it */
375 for ( i = 1; i < numEntities; i++ )
379 t = ValueForKey( e, "classname" );
380 if ( Q_strncasecmp( t, "light", 5 ) ) {
383 t = ValueForKey( e, "targetname" );
384 if ( t[ 0 ] == '\0' ) {
385 /* ydnar: strip the light from the BSP file */
386 if ( keepLights == qfalse ) {
404 /* get existing style */
405 style = IntForKey( e, "style" );
406 if ( style < LS_NORMAL || style > LS_NONE ) {
407 Error( "Invalid lightstyle (%d) on entity %d", style, i );
410 /* find this targetname */
411 for ( j = 0; j < numStyles; j++ )
412 if ( lightStyles[ j ] == style && !strcmp( lightTargets[ j ], t ) ) {
416 /* add a new style */
417 if ( j >= numStyles ) {
418 if ( numStyles == MAX_SWITCHED_LIGHTS ) {
419 Error( "MAX_SWITCHED_LIGHTS (%d) exceeded, reduce the number of lights with targetnames", MAX_SWITCHED_LIGHTS );
421 strcpy( lightTargets[ j ], t );
422 lightStyles[ j ] = style;
426 /* set explicit style */
427 sprintf( value, "%d", 32 + j );
428 SetKeyValue( e, "style", value );
431 if ( style != LS_NORMAL ) {
432 sprintf( value, "%d", style );
433 SetKeyValue( e, "switch_style", value );
437 /* emit some statistics */
438 Sys_FPrintf( SYS_VRB, "%9d light entities stripped\n", numStrippedLights );
445 starts a new bsp file
448 void BeginBSPFile( void ){
449 /* these values may actually be initialized if the file existed when loaded, so clear them explicitly */
452 numBSPBrushSides = 0;
453 numBSPLeafSurfaces = 0;
454 numBSPLeafBrushes = 0;
456 /* leave leaf 0 as an error, because leafs are referenced as negative number nodes */
460 /* ydnar: gs mods: set the first 6 drawindexes to 0 1 2 2 1 3 for triangles and quads */
461 numBSPDrawIndexes = 6;
462 AUTOEXPAND_BY_REALLOC_BSP( DrawIndexes, 1024 );
463 bspDrawIndexes[ 0 ] = 0;
464 bspDrawIndexes[ 1 ] = 1;
465 bspDrawIndexes[ 2 ] = 2;
466 bspDrawIndexes[ 3 ] = 0;
467 bspDrawIndexes[ 4 ] = 2;
468 bspDrawIndexes[ 5 ] = 3;
474 RestoreSurfaceFlags()
475 read Smokin'Guns like tex file
476 added by spoon to get back the changed surfaceflags
479 void RestoreSurfaceFlags( char *filename ) {
482 int surfaceFlags[ MAX_MAP_DRAW_SURFS ];
485 /* first parse the tex file */
486 texfile = fopen( filename, "r" );
489 fscanf( texfile, "TEXFILE\n%i\n", &numTexInfos );
491 /* Sys_Printf( "%i\n", numTexInfos ); */
493 for ( i = 0; i < numTexInfos; i++ ) {
496 fscanf( texfile, "%i %f %f %f\n", &surfaceFlags[ i ],
497 &color[ 0 ], &color[ 1 ], &color[ 2 ]);
499 bspShaders[ i ].surfaceFlags = surfaceFlags[ i ];
501 /* Sys_Printf( "%i\n", surfaceFlags[ i ] ); */
504 Sys_Printf("couldn't find %s not tex-file is now writed without surfaceFlags!\n", filename);
512 write Smokin'Guns like tex file
516 void WriteTexFile( char* filename ) {
520 if ( !compile_map ) {
521 RestoreSurfaceFlags( filename );
524 Sys_Printf( "Writing %s ...\n", filename );
526 texfile = fopen ( filename, "w" );
528 fprintf( texfile, "TEXFILE\n" );
530 fprintf( texfile, "%i\n", numBSPShaders );
532 for ( i = 0 ; i < numBSPShaders ; i++ ) {
533 shaderInfo_t *se = ShaderInfoForShader( bspShaders[ i ].shader );
535 fprintf( texfile, "\n%i %f %f %f", bspShaders[ i ].surfaceFlags,
536 se->color[ 0 ], se->color[ 1 ], se->color[ 2 ] );
538 bspShaders[ i ].surfaceFlags = i;
548 finishes a new bsp and writes to disk
551 void EndBSPFile( qboolean do_write, const char *BSPFilePath, const char *surfaceFilePath ){
553 Sys_FPrintf( SYS_VRB, "--- EndBSPFile ---\n" );
557 numBSPEntities = numEntities;
561 /* write the surface extra file */
562 WriteSurfaceExtraFile( surfaceFilePath );
566 char basename[ 1024 ];
567 char filename[ 1024 ];
568 strncpy( basename, BSPFilePath, sizeof(basename) );
569 StripExtension( basename );
570 sprintf( filename, "%s.tex", basename );
572 /* only create tex file if it is the first compile */
573 WriteTexFile( filename );
577 Sys_Printf( "Writing %s\n", BSPFilePath );
578 WriteBSPFile( BSPFilePath );
586 writes the brush list to the bsp
589 void EmitBrushes( brush_t *brushes, int *firstBrush, int *numBrushes ){
596 /* set initial brush */
597 if ( firstBrush != NULL ) {
598 *firstBrush = numBSPBrushes;
600 if ( numBrushes != NULL ) {
604 /* walk list of brushes */
605 for ( b = brushes; b != NULL; b = b->next )
608 AUTOEXPAND_BY_REALLOC_BSP( Brushes, 1024 );
611 b->outputNum = numBSPBrushes;
612 db = &bspBrushes[ numBSPBrushes ];
614 if ( numBrushes != NULL ) {
618 db->shaderNum = EmitShader( b->contentShader->shader, &b->contentShader->contentFlags, &b->contentShader->surfaceFlags );
619 db->firstSide = numBSPBrushSides;
623 for ( j = 0; j < b->numsides; j++ )
625 /* set output number to bogus initially */
626 b->sides[ j ].outputNum = -1;
629 AUTOEXPAND_BY_REALLOC_BSP( BrushSides, 1024 );
632 b->sides[ j ].outputNum = numBSPBrushSides;
633 cp = &bspBrushSides[ numBSPBrushSides ];
636 cp->planeNum = b->sides[ j ].planenum;
639 if ( b->sides[ j ].shaderInfo ) {
640 cp->shaderNum = EmitShader( b->sides[ j ].shaderInfo->shader, &b->sides[ j ].shaderInfo->contentFlags, &b->sides[ j ].shaderInfo->surfaceFlags );
643 cp->shaderNum = EmitShader( NULL, NULL, NULL );
653 turns map fogs into bsp fogs
656 void EmitFogs( void ){
661 numBSPFogs = numMapFogs;
664 for ( i = 0; i < numMapFogs; i++ )
667 strcpy( bspFogs[ i ].shader, mapFogs[ i ].si->shader );
669 /* global fog doesn't have an associated brush */
670 if ( mapFogs[ i ].brush == NULL ) {
671 bspFogs[ i ].brushNum = -1;
672 bspFogs[ i ].visibleSide = -1;
677 bspFogs[ i ].brushNum = mapFogs[ i ].brush->outputNum;
679 /* try to use forced visible side */
680 if ( mapFogs[ i ].visibleSide >= 0 ) {
681 bspFogs[ i ].visibleSide = mapFogs[ i ].visibleSide;
685 /* find visible side */
686 for ( j = 0; j < 6; j++ )
688 if ( mapFogs[ i ].brush->sides[ j ].visibleHull != NULL ) {
689 Sys_Printf( "Fog %d has visible side %d\n", i, j );
690 bspFogs[ i ].visibleSide = j;
702 sets up a new brush model
705 void BeginModel( void ){
710 vec3_t lgMins, lgMaxs; /* ydnar: lightgrid mins/maxs */
716 AUTOEXPAND_BY_REALLOC_BSP( Models, 256 );
718 /* get model and entity */
719 mod = &bspModels[ numBSPModels ];
720 e = &entities[ mapEntityNum ];
722 /* ydnar: lightgrid mins/maxs */
723 ClearBounds( lgMins, lgMaxs );
725 /* bound the brushes */
726 ClearBounds( mins, maxs );
727 for ( b = e->brushes; b; b = b->next )
729 /* ignore non-real brushes (origin, etc) */
730 if ( b->numsides == 0 ) {
733 AddPointToBounds( b->mins, mins, maxs );
734 AddPointToBounds( b->maxs, mins, maxs );
736 /* ydnar: lightgrid bounds */
737 if ( b->compileFlags & C_LIGHTGRID ) {
738 AddPointToBounds( b->mins, lgMins, lgMaxs );
739 AddPointToBounds( b->maxs, lgMins, lgMaxs );
744 for ( p = e->patches; p; p = p->next )
746 for ( i = 0; i < ( p->mesh.width * p->mesh.height ); i++ )
747 AddPointToBounds( p->mesh.verts[i].xyz, mins, maxs );
750 /* ydnar: lightgrid mins/maxs */
751 if ( lgMins[ 0 ] < 99999 ) {
752 /* use lightgrid bounds */
753 VectorCopy( lgMins, mod->mins );
754 VectorCopy( lgMaxs, mod->maxs );
758 /* use brush/patch bounds */
759 VectorCopy( mins, mod->mins );
760 VectorCopy( maxs, mod->maxs );
764 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 ] );
765 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 ] );
768 mod->firstBSPSurface = numBSPDrawSurfaces;
769 mod->firstBSPBrush = numBSPBrushes;
777 finish a model's processing
780 void EndModel( entity_t *e, node_t *headnode ){
785 Sys_FPrintf( SYS_VRB, "--- EndModel ---\n" );
788 mod = &bspModels[ numBSPModels ];
789 EmitDrawNode_r( headnode );
791 /* set surfaces and brushes */
792 mod->numBSPSurfaces = numBSPDrawSurfaces - mod->firstBSPSurface;
793 mod->firstBSPBrush = e->firstBrush;
794 mod->numBSPBrushes = e->numBrushes;
796 /* increment model count */