2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 ----------------------------------------------------------------------------------
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
26 ------------------------------------------------------------------------------- */
40 /* -------------------------------------------------------------------------------
44 ------------------------------------------------------------------------------- */
49 SetCloneModelNumbers() - ydnar
50 sets the model numbers for brush entities
53 static void SetCloneModelNumbers( void )
57 char modelValue[ 10 ];
58 const char *value, *value2, *value3;
61 /* start with 1 (worldspawn is model 0) */
63 for( i = 1; i < numEntities; i++ )
65 /* only entities with brushes or patches get a model number */
66 if( entities[ i ].brushes == NULL && entities[ i ].patches == NULL )
69 /* is this a clone? */
70 value = ValueForKey( &entities[ i ], "_clone" );
71 if( value[ 0 ] != '\0' )
74 /* add the model key */
75 sprintf( modelValue, "*%d", models );
76 SetKeyValue( &entities[ i ], "model", modelValue );
78 /* increment model count */
83 for( i = 1; i < numEntities; i++ )
85 /* only entities with brushes or patches get a model number */
86 if( entities[ i ].brushes == NULL && entities[ i ].patches == NULL )
89 /* is this a clone? */
90 value = ValueForKey( &entities[ i ], "_ins" );
91 if( value[ 0 ] == '\0' )
92 value = ValueForKey( &entities[ i ], "_instance" );
93 if( value[ 0 ] == '\0' )
94 value = ValueForKey( &entities[ i ], "_clone" );
95 if( value[ 0 ] == '\0' )
98 /* find an entity with matching clone name */
99 for( j = 0; j < numEntities; j++ )
101 /* is this a clone parent? */
102 value2 = ValueForKey( &entities[ j ], "_clonename" );
103 if( value2[ 0 ] == '\0' )
107 if( strcmp( value, value2 ) == 0 )
109 /* get the model num */
110 value3 = ValueForKey( &entities[ j ], "model" );
111 if( value3[ 0 ] == '\0' )
113 Sys_Printf( "WARNING: Cloned entity %s referenced entity without model\n", value2 );
116 models = atoi( &value2[ 1 ] );
118 /* add the model key */
119 sprintf( modelValue, "*%d", models );
120 SetKeyValue( &entities[ i ], "model", modelValue );
122 /* nuke the brushes/patches for this entity (fixme: leak!) */
123 entities[ i ].brushes = NULL;
124 entities[ i ].patches = NULL;
133 FixBrushSides() - ydnar
134 matches brushsides back to their appropriate drawsurface and shader
137 static void FixBrushSides( entity_t *e )
140 mapDrawSurface_t *ds;
142 bspBrushSide_t *side;
146 Sys_FPrintf( SYS_VRB, "--- FixBrushSides ---\n" );
148 /* walk list of drawsurfaces */
149 for( i = e->firstDrawSurf; i < numMapDrawSurfs; i++ )
151 /* get surface and try to early out */
152 ds = &mapDrawSurfs[ i ];
153 if( ds->outputNum < 0 )
156 /* walk sideref list */
157 for( sideRef = ds->sideRef; sideRef != NULL; sideRef = sideRef->next )
159 /* get bsp brush side */
160 if( sideRef->side == NULL || sideRef->side->outputNum < 0 )
162 side = &bspBrushSides[ sideRef->side->outputNum ];
164 /* set drawsurface */
165 side->surfaceNum = ds->outputNum;
166 //% Sys_FPrintf( SYS_VRB, "DS: %7d Side: %7d ", ds->outputNum, sideRef->side->outputNum );
169 if( strcmp( bspShaders[ side->shaderNum ].shader, ds->shaderInfo->shader ) )
171 //% Sys_FPrintf( SYS_VRB, "Remapping %s to %s\n", bspShaders[ side->shaderNum ].shader, ds->shaderInfo->shader );
172 side->shaderNum = EmitShader( ds->shaderInfo->shader, &ds->shaderInfo->contentFlags, &ds->shaderInfo->surfaceFlags );
182 creates a full bsp + surfaces for the worldspawn entity
185 void ProcessWorldModel( void )
191 qboolean ignoreLeaks, leaked;
192 xmlNodePtr polyline, leaknode;
193 char level[ 2 ], shader[ 1024 ];
197 /* sets integer blockSize from worldspawn "_blocksize" key if it exists */
198 value = ValueForKey( &entities[ 0 ], "_blocksize" );
199 if( value[ 0 ] == '\0' )
200 value = ValueForKey( &entities[ 0 ], "blocksize" );
201 if( value[ 0 ] == '\0' )
202 value = ValueForKey( &entities[ 0 ], "chopsize" ); /* sof2 */
203 if( value[ 0 ] != '\0' )
206 s = sscanf( value, "%d %d %d", &blockSize[ 0 ], &blockSize[ 1 ], &blockSize[ 2 ] );
208 /* handle legacy case */
211 blockSize[ 1 ] = blockSize[ 0 ];
212 blockSize[ 2 ] = blockSize[ 0 ];
215 Sys_Printf( "block size = { %d %d %d }\n", blockSize[ 0 ], blockSize[ 1 ], blockSize[ 2 ] );
217 /* sof2: ignore leaks? */
218 value = ValueForKey( &entities[ 0 ], "_ignoreleaks" ); /* ydnar */
219 if( value[ 0 ] == '\0' )
220 value = ValueForKey( &entities[ 0 ], "ignoreleaks" );
221 if( value[ 0 ] == '1' )
224 ignoreLeaks = qfalse;
226 /* begin worldspawn model */
229 e->firstDrawSurf = 0;
232 ClearMetaTriangles();
234 /* check for patches with adjacent edges that need to lod together */
235 PatchMapDrawSurfs( e );
237 /* build an initial bsp tree using all of the sides of all of the structural brushes */
238 faces = MakeStructuralBSPFaceList( entities[ 0 ].brushes );
239 tree = FaceBSP( faces );
240 MakeTreePortals( tree );
241 FilterStructuralBrushesIntoTree( e, tree );
243 /* see if the bsp is completely enclosed */
244 if( FloodEntities( tree ) || ignoreLeaks )
246 /* rebuild a better bsp tree using only the sides that are visible from the inside */
247 FillOutside( tree->headnode );
249 /* chop the sides to the convex hull of their visible fragments, giving us the smallest polygons */
250 ClipSidesIntoTree( e, tree );
252 /* build a visible face tree */
253 faces = MakeVisibleBSPFaceList( entities[ 0 ].brushes );
255 tree = FaceBSP( faces );
256 MakeTreePortals( tree );
257 FilterStructuralBrushesIntoTree( e, tree );
260 /* ydnar: flood again for skybox */
262 FloodEntities( tree );
266 Sys_FPrintf( SYS_NOXML, "**********************\n" );
267 Sys_FPrintf( SYS_NOXML, "******* leaked *******\n" );
268 Sys_FPrintf( SYS_NOXML, "**********************\n" );
269 polyline = LeakFile( tree );
270 leaknode = xmlNewNode( NULL, "message" );
271 xmlNodeSetContent( leaknode, "MAP LEAKED\n" );
272 xmlAddChild( leaknode, polyline );
273 level[0] = (int) '0' + SYS_ERR;
275 xmlSetProp( leaknode, "level", (char*) &level );
276 xml_SendNode( leaknode );
279 Sys_Printf ("--- MAP LEAKED, ABORTING LEAKTEST ---\n");
284 /* chop the sides to the convex hull of their visible fragments, giving us the smallest polygons */
285 ClipSidesIntoTree( e, tree );
288 /* save out information for visibility processing */
289 NumberClusters( tree );
291 WritePortalFile( tree );
293 /* flood from entities */
296 /* create drawsurfs for triangle models */
297 AddTriangleModels( e );
299 /* create drawsurfs for surface models */
300 AddEntitySurfaceModels( e );
302 /* generate bsp brushes from map brushes */
303 EmitBrushes( e->brushes, &e->firstBrush, &e->numBrushes );
305 /* add references to the detail brushes */
306 FilterDetailBrushesIntoTree( e, tree );
308 /* drawsurfs that cross fog boundaries will need to be split along the fog boundary */
310 FogDrawSurfaces( e );
312 /* subdivide each drawsurf as required by shader tesselation */
314 SubdivideFaceSurfaces( e, tree );
316 /* add in any vertexes required to fix t-junctions */
320 /* ydnar: classify the surfaces */
321 ClassifyEntitySurfaces( e );
323 /* ydnar: project decals */
324 MakeEntityDecals( e );
326 /* ydnar: meta surfaces */
327 MakeEntityMetaTriangles( e );
328 SmoothMetaTriangles();
330 MergeMetaTriangles();
332 /* ydnar: debug portals */
334 MakeDebugPortalSurfs( tree );
336 /* ydnar: fog hull */
337 value = ValueForKey( &entities[ 0 ], "_foghull" );
338 if( value[ 0 ] != '\0' )
340 sprintf( shader, "textures/%s", value );
341 MakeFogHullSurfs( e, tree, shader );
344 /* ydnar: bug 645: do flares for lights */
345 for( i = 0; i < numEntities && emitFlares; i++ )
347 entity_t *light, *target;
348 const char *value, *flareShader;
349 vec3_t origin, targetOrigin, normal, color;
354 light = &entities[ i ];
355 value = ValueForKey( light, "classname" );
356 if( !strcmp( value, "light" ) )
358 /* get flare shader */
359 flareShader = ValueForKey( light, "_flareshader" );
360 value = ValueForKey( light, "_flare" );
361 if( flareShader[ 0 ] != '\0' || value[ 0 ] != '\0' )
364 GetVectorForKey( light, "origin", origin );
365 GetVectorForKey( light, "_color", color );
366 lightStyle = IntForKey( light, "_style" );
367 if( lightStyle == 0 )
368 lightStyle = IntForKey( light, "style" );
370 /* handle directional spotlights */
371 value = ValueForKey( light, "target" );
372 if( value[ 0 ] != '\0' )
374 /* get target light */
375 target = FindTargetEntity( value );
378 GetVectorForKey( target, "origin", targetOrigin );
379 VectorSubtract( targetOrigin, origin, normal );
380 VectorNormalize( normal, normal );
384 //% VectorClear( normal );
385 VectorSet( normal, 0, 0, -1 );
387 /* create the flare surface (note shader defaults automatically) */
388 DrawSurfaceForFlare( mapEntityNum, origin, normal, color, (char*) flareShader, lightStyle );
393 /* add references to the final drawsurfs in the apropriate clusters */
394 FilterDrawsurfsIntoTree( e, tree );
396 /* match drawsurfaces back to original brushsides (sof2) */
400 EndModel( e, tree->headnode );
408 creates bsp + surfaces for other brush models
411 void ProcessSubModel( void )
419 /* start a brush model */
421 e = &entities[ mapEntityNum ];
422 e->firstDrawSurf = numMapDrawSurfs;
425 ClearMetaTriangles();
427 /* check for patches with adjacent edges that need to lod together */
428 PatchMapDrawSurfs( e );
430 /* allocate a tree */
432 node->planenum = PLANENUM_LEAF;
434 tree->headnode = node;
436 /* add the sides to the tree */
437 ClipSidesIntoTree( e, tree );
439 /* ydnar: create drawsurfs for triangle models */
440 AddTriangleModels( e );
442 /* create drawsurfs for surface models */
443 AddEntitySurfaceModels( e );
445 /* generate bsp brushes from map brushes */
446 EmitBrushes( e->brushes, &e->firstBrush, &e->numBrushes );
448 /* just put all the brushes in headnode */
449 for( b = e->brushes; b; b = b->next )
452 bc->next = node->brushlist;
453 node->brushlist = bc;
456 /* subdivide each drawsurf as required by shader tesselation */
458 SubdivideFaceSurfaces( e, tree );
460 /* add in any vertexes required to fix t-junctions */
464 /* ydnar: classify the surfaces and project lightmaps */
465 ClassifyEntitySurfaces( e );
467 /* ydnar: project decals */
468 MakeEntityDecals( e );
470 /* ydnar: meta surfaces */
471 MakeEntityMetaTriangles( e );
472 SmoothMetaTriangles();
474 MergeMetaTriangles();
476 /* add references to the final drawsurfs in the apropriate clusters */
477 FilterDrawsurfsIntoTree( e, tree );
479 /* match drawsurfaces back to original brushsides (sof2) */
491 process world + other models into the bsp
494 void ProcessModels( void )
500 /* preserve -v setting */
501 oldVerbose = verbose;
503 /* start a new bsp */
506 /* create map fogs */
509 /* walk entity list */
510 for( mapEntityNum = 0; mapEntityNum < numEntities; mapEntityNum++ )
513 entity = &entities[ mapEntityNum ];
514 if( entity->brushes == NULL && entity->patches == NULL )
517 /* process the model */
518 Sys_FPrintf( SYS_VRB, "############### model %i ###############\n", numBSPModels );
519 if( mapEntityNum == 0 )
524 /* potentially turn off the deluge of text */
525 verbose = verboseEntities;
528 /* restore -v setting */
529 verbose = oldVerbose;
539 this is probably broken unless teamed with a radiant version that preserves entity order
542 void OnlyEnts( void )
548 Sys_Printf( "--- OnlyEnts ---\n" );
550 sprintf( out, "%s.bsp", source );
555 LoadMapFile( name, qfalse );
559 numBSPEntities = numEntities;
569 handles creation of a bsp from a map file
572 int BSPMain( int argc, char **argv )
575 char path[ 1024 ], tempSource[ 1024 ];
576 qboolean onlyents = qfalse;
580 Sys_Printf( "--- BSP ---\n" );
582 SetDrawSurfacesBuffer();
583 mapDrawSurfs = safe_malloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
584 memset( mapDrawSurfs, 0, sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
587 tempSource[ 0 ] = '\0';
589 /* set flares flag */
590 emitFlares = game->emitFlares;
592 /* process arguments */
593 for( i = 1; i < (argc - 1); i++ )
595 if( !strcmp( argv[ i ], "-onlyents" ) )
597 Sys_Printf( "Running entity-only compile\n" );
600 else if( !strcmp( argv[ i ], "-tempname" ) )
601 strcpy( tempSource, argv[ ++i ] );
602 else if( !strcmp( argv[ i ], "-tmpout" ) )
603 strcpy( outbase, "/tmp" );
604 else if( !strcmp( argv[ i ], "-nowater" ) )
606 Sys_Printf( "Disabling water\n" );
609 else if( !strcmp( argv[ i ], "-nodetail" ) )
611 Sys_Printf( "Ignoring detail brushes\n") ;
614 else if( !strcmp( argv[ i ], "-fulldetail" ) )
616 Sys_Printf( "Turning detail brushes into structural brushes\n" );
619 else if( !strcmp( argv[ i ], "-nofog" ) )
621 Sys_Printf( "Fog volumes disabled\n" );
624 else if( !strcmp( argv[ i ], "-nosubdivide" ) )
626 Sys_Printf( "Disabling brush face subdivision\n" );
629 else if( !strcmp( argv[ i ], "-leaktest" ) )
631 Sys_Printf( "Leaktest enabled\n" );
634 else if( !strcmp( argv[ i ], "-verboseentities" ) )
636 Sys_Printf( "Verbose entities enabled\n" );
637 verboseEntities = qtrue;
639 else if( !strcmp( argv[ i ], "-nocurves" ) )
641 Sys_Printf( "Ignoring curved surfaces (patches)\n" );
642 noCurveBrushes = qtrue;
644 else if( !strcmp( argv[ i ], "-notjunc" ) )
646 Sys_Printf( "T-junction fixing disabled\n" );
649 else if( !strcmp( argv[ i ], "-fakemap" ) )
651 Sys_Printf( "Generating fakemap.map\n" );
654 else if( !strcmp( argv[ i ], "-samplesize" ) )
656 sampleSize = atoi( argv[ i + 1 ] );
660 Sys_Printf( "Lightmap sample size set to %dx%d units\n", sampleSize, sampleSize );
662 else if( !strcmp( argv[ i ], "-custinfoparms") )
664 Sys_Printf( "Custom info parms enabled\n" );
665 useCustomInfoParms = qtrue;
669 else if( !strcmp( argv[ i ], "-rename" ) )
671 Sys_Printf( "Appending _bsp suffix to misc_model shaders (SOF2)\n" );
672 renameModelShaders = qtrue;
676 else if( !strcmp( argv[ i ], "-ne" ) )
678 normalEpsilon = atof( argv[ i + 1 ] );
680 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
682 else if( !strcmp( argv[ i ], "-de" ) )
684 distanceEpsilon = atof( argv[ i + 1 ] );
686 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
688 else if( !strcmp( argv[ i ], "-mv" ) )
690 maxSurfaceVerts = atoi( argv[ i + 1 ] );
691 if( maxSurfaceVerts < 3 )
694 Sys_Printf( "Maximum per-surface vertex count set to %d\n", maxSurfaceVerts );
696 else if( !strcmp( argv[ i ], "-mi" ) )
698 maxSurfaceIndexes = atoi( argv[ i + 1 ] );
699 if( maxSurfaceIndexes < 3 )
700 maxSurfaceIndexes = 3;
702 Sys_Printf( "Maximum per-surface index count set to %d\n", maxSurfaceIndexes );
704 else if( !strcmp( argv[ i ], "-np" ) )
706 npDegrees = atof( argv[ i + 1 ] );
707 if( npDegrees < 0.0f )
708 shadeAngleDegrees = 0.0f;
709 else if( npDegrees > 0.0f )
710 Sys_Printf( "Forcing nonplanar surfaces with a breaking angle of %f degrees\n", npDegrees );
713 else if( !strcmp( argv[ i ], "-snap" ) )
715 bevelSnap = atoi( argv[ i + 1 ]);
720 Sys_Printf( "Snapping brush bevel planes to %d units\n", bevelSnap );
722 else if( !strcmp( argv[ i ], "-texrange" ) )
724 texRange = atoi( argv[ i + 1 ]);
728 Sys_Printf( "Limiting per-surface texture range to %d texels\n", texRange );
730 else if( !strcmp( argv[ i ], "-nohint" ) )
732 Sys_Printf( "Hint brushes disabled\n" );
735 else if( !strcmp( argv[ i ], "-flat" ) )
737 Sys_Printf( "Flatshading enabled\n" );
740 else if( !strcmp( argv[ i ], "-meta" ) )
742 Sys_Printf( "Creating meta surfaces from brush faces\n" );
745 else if( !strcmp( argv[ i ], "-patchmeta" ) )
747 Sys_Printf( "Creating meta surfaces from patches\n" );
750 else if( !strcmp( argv[ i ], "-flares" ) )
752 Sys_Printf( "Flare surfaces enabled\n" );
755 else if( !strcmp( argv[ i ], "-noflares" ) )
757 Sys_Printf( "Flare surfaces disabled\n" );
760 else if( !strcmp( argv[ i ], "-skyfix" ) )
762 Sys_Printf( "GL_CLAMP sky fix/hack/workaround enabled\n" );
765 else if( !strcmp( argv[ i ], "-debugsurfaces" ) )
767 Sys_Printf( "emitting debug surfaces\n" );
768 debugSurfaces = qtrue;
770 else if( !strcmp( argv[ i ], "-debuginset" ) )
772 Sys_Printf( "Debug surface triangle insetting enabled\n" );
775 else if( !strcmp( argv[ i ], "-debugportals" ) )
777 Sys_Printf( "Debug portal surfaces enabled\n" );
778 debugPortals = qtrue;
780 else if( !strcmp( argv[ i ], "-bsp" ) )
781 Sys_Printf( "-bsp argument unnecessary\n" );
783 Sys_Printf( "WARNING: Unknown option \"%s\"\n", argv[ i ] );
786 /* fixme: print more useful usage here */
787 if( i != (argc - 1) )
788 Error( "usage: q3map [options] mapfile" );
790 /* copy source name */
791 strcpy( source, ExpandArg( argv[ i ] ) );
792 StripExtension( source );
794 /* ydnar: set default sample size */
795 SetDefaultSampleSize( sampleSize );
797 /* delete portal, line and surface files */
798 sprintf( path, "%s.prt", source );
800 sprintf( path, "%s.lin", source );
802 //% sprintf( path, "%s.srf", source ); /* ydnar */
806 strcpy( name, ExpandArg( argv[ i ] ) );
807 if( strcmp( name + strlen( name ) - 4, ".reg" ) )
809 /* if we are doing a full map, delete the last saved region map */
810 sprintf( path, "%s.reg", source );
812 DefaultExtension( name, ".map" ); /* might be .reg */
815 /* if onlyents, just grab the entites and resave */
825 /* load original file from temp spot in case it was renamed by the editor on the way in */
826 if( strlen( tempSource ) > 0 )
827 LoadMapFile( tempSource, qfalse );
829 LoadMapFile( name, qfalse );
831 /* ydnar: decal setup */
834 /* ydnar: cloned brush model entities */
835 SetCloneModelNumbers();
837 /* process world and submodels */
840 /* set light styles from targetted light entities */
843 /* finish and write bsp */
846 /* remove temp map source file if appropriate */
847 if( strlen( tempSource ) > 0)
848 remove( tempSource );
850 /* return to sender */