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 ------------------------------------------------------------------------------- */
38 analyzes a Quake engine BSP file
41 typedef struct abspHeader_s
46 bspLump_t lumps[ 1 ]; /* unknown size */
50 typedef struct abspLumpTest_s
57 int AnalyzeBSP( int argc, char **argv ){
59 int size, i, version, offset, length, lumpInt, count;
63 char lumpString[ 1024 ], source[ 1024 ];
64 qboolean lumpSwap = qfalse;
65 abspLumpTest_t *lumpTest;
66 static abspLumpTest_t lumpTests[] =
68 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
69 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
70 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
71 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
72 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
73 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
74 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
75 { 104, 3, "IBSP LUMP_DRAWSURFS" },
76 { 44, 3, "IBSP LUMP_DRAWVERTS" },
77 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
78 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
79 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
80 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
87 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
91 /* process arguments */
92 for ( i = 1; i < ( argc - 1 ); i++ )
94 /* -format map|ase|... */
95 if ( !strcmp( argv[ i ], "-lumpswap" ) ) {
96 Sys_Printf( "Swapped lump structs enabled\n" );
101 /* clean up map name */
102 strcpy( source, ExpandArg( argv[ i ] ) );
103 Sys_Printf( "Loading %s\n", source );
106 size = LoadFile( source, (void**) &header );
107 if ( size == 0 || header == NULL ) {
108 Sys_Printf( "Unable to load %s.\n", source );
112 /* analyze ident/version */
113 memcpy( ident, header->ident, 4 );
115 version = LittleLong( header->version );
117 Sys_Printf( "Identity: %s\n", ident );
118 Sys_Printf( "Version: %d\n", version );
119 Sys_Printf( "---------------------------------------\n" );
121 /* analyze each lump */
122 for ( i = 0; i < 100; i++ )
124 /* call of duty swapped lump pairs */
126 offset = LittleLong( header->lumps[ i ].length );
127 length = LittleLong( header->lumps[ i ].offset );
130 /* standard lump pairs */
133 offset = LittleLong( header->lumps[ i ].offset );
134 length = LittleLong( header->lumps[ i ].length );
138 lump = (byte*) header + offset;
139 lumpInt = LittleLong( (int) *( (int*) lump ) );
140 lumpFloat = LittleFloat( (float) *( (float*) lump ) );
141 memcpy( lumpString, (char*) lump, ( (size_t)length < sizeof( lumpString ) ? (size_t)length : sizeof( lumpString ) - 1 ) );
142 lumpString[ sizeof( lumpString ) - 1 ] = '\0';
144 /* print basic lump info */
145 Sys_Printf( "Lump: %d\n", i );
146 Sys_Printf( "Offset: %d bytes\n", offset );
147 Sys_Printf( "Length: %d bytes\n", length );
149 /* only operate on valid lumps */
151 /* print data in 4 formats */
152 Sys_Printf( "As hex: %08X\n", lumpInt );
153 Sys_Printf( "As int: %d\n", lumpInt );
154 Sys_Printf( "As float: %f\n", lumpFloat );
155 Sys_Printf( "As string: %s\n", lumpString );
157 /* guess lump type */
158 if ( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' ) {
159 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
161 else if ( strstr( lumpString, "textures/" ) ) {
162 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
166 /* guess based on size/count */
167 for ( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
169 if ( ( length % lumpTest->radix ) != 0 ) {
172 count = length / lumpTest->radix;
173 if ( count < lumpTest->minCount ) {
176 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
181 Sys_Printf( "---------------------------------------\n" );
184 if ( offset + length >= size ) {
190 Sys_Printf( "Lump count: %d\n", i + 1 );
191 Sys_Printf( "File size: %d bytes\n", size );
193 /* return to caller */
201 emits statistics about the bsp file
204 int BSPInfo( int count, char **fileNames ){
206 char source[ 1024 ], ext[ 64 ];
213 Sys_Printf( "No files to dump info for.\n" );
217 /* enable info mode */
221 for ( i = 0; i < count; i++ )
223 Sys_Printf( "---------------------------------\n" );
225 /* mangle filename and get size */
226 strcpy( source, fileNames[ i ] );
227 ExtractFileExtension( source, ext );
228 if ( !Q_stricmp( ext, "map" ) ) {
229 StripExtension( source );
231 DefaultExtension( source, ".bsp" );
232 f = fopen( source, "rb" );
234 size = Q_filelength( f );
241 /* load the bsp file and print lump sizes */
242 Sys_Printf( "%s\n", source );
243 LoadBSPFile( source );
248 Sys_Printf( " total %9d\n", size );
249 Sys_Printf( " %9d KB\n", size / 1024 );
250 Sys_Printf( " %9d MB\n", size / ( 1024 * 1024 ) );
252 Sys_Printf( "---------------------------------\n" );
260 static void ExtrapolateTexcoords( const float *axyz, const float *ast, const float *bxyz, const float *bst, const float *cxyz, const float *cst, const float *axyz_new, float *ast_out, const float *bxyz_new, float *bst_out, const float *cxyz_new, float *cst_out ){
261 vec4_t scoeffs, tcoeffs;
267 VectorSubtract( bxyz, axyz, dab );
268 VectorSubtract( cxyz, axyz, dac );
269 CrossProduct( dab, dac, norm );
273 // s(v + norm) = s(v) when n ortho xyz
275 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
278 // scoeffs * (axyz, 1) == ast[0]
279 // scoeffs * (bxyz, 1) == bst[0]
280 // scoeffs * (cxyz, 1) == cst[0]
281 // scoeffs * (norm, 0) == 0
282 // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
283 solvematrix[0] = axyz[0];
284 solvematrix[4] = axyz[1];
285 solvematrix[8] = axyz[2];
287 solvematrix[1] = bxyz[0];
288 solvematrix[5] = bxyz[1];
289 solvematrix[9] = bxyz[2];
291 solvematrix[2] = cxyz[0];
292 solvematrix[6] = cxyz[1];
293 solvematrix[10] = cxyz[2];
295 solvematrix[3] = norm[0];
296 solvematrix[7] = norm[1];
297 solvematrix[11] = norm[2];
300 md = m4_det( solvematrix );
301 if ( md * md < 1e-10 ) {
302 Sys_Printf( "Cannot invert some matrix, some texcoords aren't extrapolated!" );
306 m4x4_invert( solvematrix );
312 m4x4_transform_vec4( solvematrix, scoeffs );
317 m4x4_transform_vec4( solvematrix, tcoeffs );
319 ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
320 ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
321 bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
322 bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
323 cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
324 cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
329 amaze and confuse your enemies with wierd scaled maps!
332 int ScaleBSPMain( int argc, char **argv ){
340 float *old_xyzst = NULL;
346 Sys_Printf( "Usage: q3map [-v] -scale [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
351 for ( i = 1; i < argc - 2; ++i )
353 if ( !strcmp( argv[i], "-tex" ) ) {
356 else if ( !strcmp( argv[i], "-spawn_ref" ) ) {
357 spawn_ref = atof( argv[i + 1] );
366 // if(argc-2 >= i) // always true
367 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
368 if ( argc - 3 >= i ) {
369 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
371 if ( argc - 4 >= i ) {
372 scale[0] = atof( argv[ argc - 4 ] );
375 uniform = ( ( scale[0] == scale[1] ) && ( scale[1] == scale[2] ) );
377 if ( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f ) {
378 Sys_Printf( "Usage: q3map [-v] -scale [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
379 Sys_Printf( "Non-zero scale value required.\n" );
383 /* do some path mangling */
384 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
385 StripExtension( source );
386 DefaultExtension( source, ".bsp" );
389 Sys_Printf( "Loading %s\n", source );
390 LoadBSPFile( source );
394 Sys_Printf( "--- ScaleBSP ---\n" );
395 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
397 /* scale entity keys */
398 for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
401 GetVectorForKey( &entities[ i ], "origin", vec );
402 if ( ( vec[ 0 ] || vec[ 1 ] || vec[ 2 ] ) ) {
403 if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
409 if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
412 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
413 SetKeyValue( &entities[ i ], "origin", str );
416 a = FloatForKey( &entities[ i ], "angle" );
417 if ( a == -1 || a == -2 ) { // z scale
420 else if ( fabs( sin( DEG2RAD( a ) ) ) < 0.707 ) {
428 f = FloatForKey( &entities[ i ], "lip" );
431 sprintf( str, "%f", f );
432 SetKeyValue( &entities[ i ], "lip", str );
435 /* scale plat height */
436 f = FloatForKey( &entities[ i ], "height" );
439 sprintf( str, "%f", f );
440 SetKeyValue( &entities[ i ], "height", str );
443 // TODO maybe allow a definition file for entities to specify which values are scaled how?
447 for ( i = 0; i < numBSPModels; i++ )
449 bspModels[ i ].mins[0] *= scale[0];
450 bspModels[ i ].mins[1] *= scale[1];
451 bspModels[ i ].mins[2] *= scale[2];
452 bspModels[ i ].maxs[0] *= scale[0];
453 bspModels[ i ].maxs[1] *= scale[1];
454 bspModels[ i ].maxs[2] *= scale[2];
458 for ( i = 0; i < numBSPNodes; i++ )
460 bspNodes[ i ].mins[0] *= scale[0];
461 bspNodes[ i ].mins[1] *= scale[1];
462 bspNodes[ i ].mins[2] *= scale[2];
463 bspNodes[ i ].maxs[0] *= scale[0];
464 bspNodes[ i ].maxs[1] *= scale[1];
465 bspNodes[ i ].maxs[2] *= scale[2];
469 for ( i = 0; i < numBSPLeafs; i++ )
471 bspLeafs[ i ].mins[0] *= scale[0];
472 bspLeafs[ i ].mins[1] *= scale[1];
473 bspLeafs[ i ].mins[2] *= scale[2];
474 bspLeafs[ i ].maxs[0] *= scale[0];
475 bspLeafs[ i ].maxs[1] *= scale[1];
476 bspLeafs[ i ].maxs[2] *= scale[2];
480 Sys_Printf( "Using texture unlocking (and probably breaking texture alignment a lot)\n" );
481 old_xyzst = safe_malloc( sizeof( *old_xyzst ) * numBSPDrawVerts * 5 );
482 for ( i = 0; i < numBSPDrawVerts; i++ )
484 old_xyzst[5 * i + 0] = bspDrawVerts[i].xyz[0];
485 old_xyzst[5 * i + 1] = bspDrawVerts[i].xyz[1];
486 old_xyzst[5 * i + 2] = bspDrawVerts[i].xyz[2];
487 old_xyzst[5 * i + 3] = bspDrawVerts[i].st[0];
488 old_xyzst[5 * i + 4] = bspDrawVerts[i].st[1];
492 /* scale drawverts */
493 for ( i = 0; i < numBSPDrawVerts; i++ )
495 bspDrawVerts[i].xyz[0] *= scale[0];
496 bspDrawVerts[i].xyz[1] *= scale[1];
497 bspDrawVerts[i].xyz[2] *= scale[2];
498 bspDrawVerts[i].normal[0] /= scale[0];
499 bspDrawVerts[i].normal[1] /= scale[1];
500 bspDrawVerts[i].normal[2] /= scale[2];
501 VectorNormalize( bspDrawVerts[i].normal, bspDrawVerts[i].normal );
505 for ( i = 0; i < numBSPDrawSurfaces; i++ )
507 switch ( bspDrawSurfaces[i].surfaceType )
511 if ( bspDrawSurfaces[i].numIndexes % 3 ) {
512 Error( "Not a triangulation!" );
514 for ( j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3 )
516 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j + 1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j + 2] + bspDrawSurfaces[i].firstVert;
517 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
518 float *oa = &old_xyzst[ia * 5], *ob = &old_xyzst[ib * 5], *oc = &old_xyzst[ic * 5];
523 ExtrapolateTexcoords(
538 for ( i = 0; i < numBSPPlanes; i++ )
540 bspPlanes[ i ].dist *= scale[0];
545 for ( i = 0; i < numBSPPlanes; i++ )
547 bspPlanes[ i ].normal[0] /= scale[0];
548 bspPlanes[ i ].normal[1] /= scale[1];
549 bspPlanes[ i ].normal[2] /= scale[2];
550 f = 1 / VectorLength( bspPlanes[i].normal );
551 VectorScale( bspPlanes[i].normal, f, bspPlanes[i].normal );
552 bspPlanes[ i ].dist *= f;
557 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
558 if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
559 VectorCopy( gridSize, vec );
564 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
565 SetKeyValue( &entities[ 0 ], "gridsize", str );
567 /* inject command line parameters */
568 InjectCommandLine( argv, 0, argc - 1 );
572 StripExtension( source );
573 DefaultExtension( source, "_s.bsp" );
574 Sys_Printf( "Writing %s\n", source );
575 WriteBSPFile( source );
577 /* return to sender */
584 a stripped down ProcessModels
586 void PseudoCompileBSP( qboolean need_tree ){
597 SetDrawSurfacesBuffer();
598 mapDrawSurfs = safe_malloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
599 memset( mapDrawSurfs, 0, sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
604 for ( mapEntityNum = 0; mapEntityNum < numEntities; mapEntityNum++ )
607 entity = &entities[ mapEntityNum ];
608 if ( entity->brushes == NULL && entity->patches == NULL ) {
612 if ( mapEntityNum != 0 ) {
613 sprintf( modelValue, "*%d", models++ );
614 SetKeyValue( entity, "model", modelValue );
617 /* process the model */
618 Sys_FPrintf( SYS_VRB, "############### model %i ###############\n", numBSPModels );
621 entity->firstDrawSurf = numMapDrawSurfs;
623 ClearMetaTriangles();
624 PatchMapDrawSurfs( entity );
626 if ( mapEntityNum == 0 && need_tree ) {
627 faces = MakeStructuralBSPFaceList( entities[0].brushes );
628 tree = FaceBSP( faces );
629 node = tree->headnode;
634 node->planenum = PLANENUM_LEAF;
636 tree->headnode = node;
639 /* a minimized ClipSidesIntoTree */
640 for ( brush = entity->brushes; brush; brush = brush->next )
642 /* walk the brush sides */
643 for ( i = 0; i < brush->numsides; i++ )
646 side = &brush->sides[ i ];
647 if ( side->winding == NULL ) {
651 if ( side->shaderInfo == NULL ) {
654 /* save this winding as a visible surface */
655 DrawSurfaceForSide( entity, brush, side, side->winding );
660 ClassifyEntitySurfaces( entity );
661 MakeEntityDecals( entity );
662 MakeEntityMetaTriangles( entity );
663 SmoothMetaTriangles();
664 MergeMetaTriangles();
666 FilterDrawsurfsIntoTree( entity, tree );
668 FilterStructuralBrushesIntoTree( entity, tree );
669 FilterDetailBrushesIntoTree( entity, tree );
671 EmitBrushes( entity->brushes, &entity->firstBrush, &entity->numBrushes );
672 EndModel( entity, node );
674 EndBSPFile( qfalse );
679 main argument processing function for bsp conversion
682 int ConvertBSPMain( int argc, char **argv ){
684 int ( *convertFunc )( char * );
687 qboolean map_allowed, force_bsp, force_map;
691 convertFunc = ConvertBSPToASE;
693 map_allowed = qfalse;
699 Sys_Printf( "Usage: q3map -convert [-format <ase|obj|map_bp|map>] [-shadersasbitmap|-lightmapsastexcoord|-deluxemapsastexcoord] [-readbsp|-readmap [-meta|-patchmeta]] [-v] <mapname>\n" );
703 /* process arguments */
704 for ( i = 1; i < ( argc - 1 ); i++ )
706 /* -format map|ase|... */
707 if ( !strcmp( argv[ i ], "-format" ) ) {
709 if ( !Q_stricmp( argv[ i ], "ase" ) ) {
710 convertFunc = ConvertBSPToASE;
711 map_allowed = qfalse;
713 else if ( !Q_stricmp( argv[ i ], "obj" ) ) {
714 convertFunc = ConvertBSPToOBJ;
715 map_allowed = qfalse;
717 else if ( !Q_stricmp( argv[ i ], "map_bp" ) ) {
718 convertFunc = ConvertBSPToMap_BP;
721 else if ( !Q_stricmp( argv[ i ], "map" ) ) {
722 convertFunc = ConvertBSPToMap;
727 convertGame = GetGame( argv[ i ] );
728 map_allowed = qfalse;
729 if ( convertGame == NULL ) {
730 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
734 else if ( !strcmp( argv[ i ], "-ne" ) ) {
735 normalEpsilon = atof( argv[ i + 1 ] );
737 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
739 else if ( !strcmp( argv[ i ], "-de" ) ) {
740 distanceEpsilon = atof( argv[ i + 1 ] );
742 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
744 else if ( !strcmp( argv[ i ], "-shaderasbitmap" ) || !strcmp( argv[ i ], "-shadersasbitmap" ) ) {
745 shadersAsBitmap = qtrue;
747 else if ( !strcmp( argv[ i ], "-lightmapastexcoord" ) || !strcmp( argv[ i ], "-lightmapsastexcoord" ) ) {
748 lightmapsAsTexcoord = qtrue;
750 else if ( !strcmp( argv[ i ], "-deluxemapastexcoord" ) || !strcmp( argv[ i ], "-deluxemapsastexcoord" ) ) {
751 lightmapsAsTexcoord = qtrue;
754 else if ( !strcmp( argv[ i ], "-readbsp" ) ) {
757 else if ( !strcmp( argv[ i ], "-readmap" ) ) {
760 else if ( !strcmp( argv[ i ], "-meta" ) ) {
763 else if ( !strcmp( argv[ i ], "-patchmeta" ) ) {
771 /* clean up map name */
772 strcpy( source, ExpandArg( argv[i] ) );
773 ExtractFileExtension( source, ext );
775 if ( !map_allowed && !force_map ) {
779 if ( force_map || ( !force_bsp && !Q_stricmp( ext, "map" ) && map_allowed ) ) {
780 if ( !map_allowed ) {
781 Sys_Printf( "WARNING: the requested conversion should not be done from .map files. Compile a .bsp first.\n" );
783 StripExtension( source );
784 DefaultExtension( source, ".map" );
785 Sys_Printf( "Loading %s\n", source );
786 LoadMapFile( source, qfalse, convertGame == NULL );
787 PseudoCompileBSP( convertGame != NULL );
791 StripExtension( source );
792 DefaultExtension( source, ".bsp" );
793 Sys_Printf( "Loading %s\n", source );
794 LoadBSPFile( source );
798 /* bsp format convert? */
799 if ( convertGame != NULL ) {
800 /* set global game */
804 StripExtension( source );
805 DefaultExtension( source, "_c.bsp" );
806 Sys_Printf( "Writing %s\n", source );
807 WriteBSPFile( source );
809 /* return to sender */
814 return convertFunc( source );