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 returns a pseudorandom number between 0 and 1
47 return (vec_t) rand() / RAND_MAX;
51 char *Q_strncpyz( char *dst, const char *src, size_t len ) {
56 strncpy( dst, src, len );
57 dst[ len - 1 ] = '\0';
62 char *Q_strcat( char *dst, size_t dlen, const char *src ) {
63 size_t n = strlen( dst );
66 abort(); /* buffer overflow */
69 return Q_strncpyz( dst + n, src, dlen - n );
73 char *Q_strncat( char *dst, size_t dlen, const char *src, size_t slen ) {
74 size_t n = strlen( dst );
77 abort(); /* buffer overflow */
80 return Q_strncpyz( dst + n, src, MIN( slen, dlen - n ) );
88 static void ExitQ3Map( void ){
90 if ( mapDrawSurfs != NULL ) {
97 calculates an md4 checksum for a block of data
100 static int MD4BlockChecksum( void *buffer, int length ){
101 return Com_BlockChecksum( buffer, length );
106 resets an aas checksum to match the given BSP
109 int FixAAS( int argc, char **argv ){
110 int length, checksum;
113 char aas[ 1024 ], **ext;
125 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
129 /* do some path mangling */
130 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
131 StripExtension( source );
132 DefaultExtension( source, ".bsp" );
135 Sys_Printf( "--- FixAAS ---\n" );
138 Sys_Printf( "Loading %s\n", source );
139 length = LoadFile( source, &buffer );
141 /* create bsp checksum */
142 Sys_Printf( "Creating checksum...\n" );
143 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
145 /* write checksum to aas */
150 strcpy( aas, source );
151 StripExtension( aas );
153 Sys_Printf( "Trying %s\n", aas );
157 file = fopen( aas, "r+b" );
161 if ( fwrite( &checksum, 4, 1, file ) != 1 ) {
162 Error( "Error writing checksum to %s", aas );
167 /* return to sender */
175 analyzes a Quake engine BSP file
178 typedef struct abspHeader_s
183 bspLump_t lumps[ 1 ]; /* unknown size */
187 typedef struct abspLumpTest_s
194 int AnalyzeBSP( int argc, char **argv ){
195 abspHeader_t *header;
196 int size, i, version, offset, length, lumpInt, count;
200 char lumpString[ 1024 ], source[ 1024 ];
201 qboolean lumpSwap = qfalse;
202 abspLumpTest_t *lumpTest;
203 static abspLumpTest_t lumpTests[] =
205 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
206 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
207 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
208 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
209 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
210 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
211 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
212 { 104, 3, "IBSP LUMP_DRAWSURFS" },
213 { 44, 3, "IBSP LUMP_DRAWVERTS" },
214 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
215 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
216 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
217 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
224 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
228 /* process arguments */
229 for ( i = 1; i < ( argc - 1 ); i++ )
231 /* -format map|ase|... */
232 if ( !strcmp( argv[ i ], "-lumpswap" ) ) {
233 Sys_Printf( "Swapped lump structs enabled\n" );
238 /* clean up map name */
239 strcpy( source, ExpandArg( argv[ i ] ) );
240 Sys_Printf( "Loading %s\n", source );
243 size = LoadFile( source, (void**) &header );
244 if ( size == 0 || header == NULL ) {
245 Sys_Printf( "Unable to load %s.\n", source );
249 /* analyze ident/version */
250 memcpy( ident, header->ident, 4 );
252 version = LittleLong( header->version );
254 Sys_Printf( "Identity: %s\n", ident );
255 Sys_Printf( "Version: %d\n", version );
256 Sys_Printf( "---------------------------------------\n" );
258 /* analyze each lump */
259 for ( i = 0; i < 100; i++ )
261 /* call of duty swapped lump pairs */
263 offset = LittleLong( header->lumps[ i ].length );
264 length = LittleLong( header->lumps[ i ].offset );
267 /* standard lump pairs */
270 offset = LittleLong( header->lumps[ i ].offset );
271 length = LittleLong( header->lumps[ i ].length );
275 lump = (byte*) header + offset;
276 lumpInt = LittleLong( (int) *( (int*) lump ) );
277 lumpFloat = LittleFloat( (float) *( (float*) lump ) );
278 memcpy( lumpString, (char*) lump, ( (size_t)length < sizeof( lumpString ) ? (size_t)length : sizeof( lumpString ) - 1 ) );
279 lumpString[ sizeof( lumpString ) - 1 ] = '\0';
281 /* print basic lump info */
282 Sys_Printf( "Lump: %d\n", i );
283 Sys_Printf( "Offset: %d bytes\n", offset );
284 Sys_Printf( "Length: %d bytes\n", length );
286 /* only operate on valid lumps */
288 /* print data in 4 formats */
289 Sys_Printf( "As hex: %08X\n", lumpInt );
290 Sys_Printf( "As int: %d\n", lumpInt );
291 Sys_Printf( "As float: %f\n", lumpFloat );
292 Sys_Printf( "As string: %s\n", lumpString );
294 /* guess lump type */
295 if ( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' ) {
296 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
298 else if ( strstr( lumpString, "textures/" ) ) {
299 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
303 /* guess based on size/count */
304 for ( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
306 if ( ( length % lumpTest->radix ) != 0 ) {
309 count = length / lumpTest->radix;
310 if ( count < lumpTest->minCount ) {
313 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
318 Sys_Printf( "---------------------------------------\n" );
321 if ( offset + length >= size ) {
327 Sys_Printf( "Lump count: %d\n", i + 1 );
328 Sys_Printf( "File size: %d bytes\n", size );
330 /* return to caller */
338 emits statistics about the bsp file
341 int BSPInfo( int count, char **fileNames ){
343 char source[ 1024 ], ext[ 64 ];
350 Sys_Printf( "No files to dump info for.\n" );
354 /* enable info mode */
358 for ( i = 0; i < count; i++ )
360 Sys_Printf( "---------------------------------\n" );
362 /* mangle filename and get size */
363 strcpy( source, fileNames[ i ] );
364 ExtractFileExtension( source, ext );
365 if ( !Q_stricmp( ext, "map" ) ) {
366 StripExtension( source );
368 DefaultExtension( source, ".bsp" );
369 f = fopen( source, "rb" );
371 size = Q_filelength( f );
378 /* load the bsp file and print lump sizes */
379 Sys_Printf( "%s\n", source );
380 LoadBSPFile( source );
385 Sys_Printf( " total %9d\n", size );
386 Sys_Printf( " %9d KB\n", size / 1024 );
387 Sys_Printf( " %9d MB\n", size / ( 1024 * 1024 ) );
389 Sys_Printf( "---------------------------------\n" );
397 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 ){
398 vec4_t scoeffs, tcoeffs;
404 VectorSubtract( bxyz, axyz, dab );
405 VectorSubtract( cxyz, axyz, dac );
406 CrossProduct( dab, dac, norm );
410 // s(v + norm) = s(v) when n ortho xyz
412 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
415 // scoeffs * (axyz, 1) == ast[0]
416 // scoeffs * (bxyz, 1) == bst[0]
417 // scoeffs * (cxyz, 1) == cst[0]
418 // scoeffs * (norm, 0) == 0
419 // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
420 solvematrix[0] = axyz[0];
421 solvematrix[4] = axyz[1];
422 solvematrix[8] = axyz[2];
424 solvematrix[1] = bxyz[0];
425 solvematrix[5] = bxyz[1];
426 solvematrix[9] = bxyz[2];
428 solvematrix[2] = cxyz[0];
429 solvematrix[6] = cxyz[1];
430 solvematrix[10] = cxyz[2];
432 solvematrix[3] = norm[0];
433 solvematrix[7] = norm[1];
434 solvematrix[11] = norm[2];
437 md = m4_det( solvematrix );
438 if ( md * md < 1e-10 ) {
439 Sys_Printf( "Cannot invert some matrix, some texcoords aren't extrapolated!" );
443 m4x4_invert( solvematrix );
449 m4x4_transform_vec4( solvematrix, scoeffs );
454 m4x4_transform_vec4( solvematrix, tcoeffs );
456 ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
457 ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
458 bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
459 bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
460 cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
461 cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
466 amaze and confuse your enemies with wierd scaled maps!
469 int ScaleBSPMain( int argc, char **argv ){
477 float *old_xyzst = NULL;
483 Sys_Printf( "Usage: q3map [-v] -scale [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
488 for ( i = 1; i < argc - 2; ++i )
490 if ( !strcmp( argv[i], "-tex" ) ) {
493 else if ( !strcmp( argv[i], "-spawn_ref" ) ) {
494 spawn_ref = atof( argv[i + 1] );
503 // if(argc-2 >= i) // always true
504 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
505 if ( argc - 3 >= i ) {
506 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
508 if ( argc - 4 >= i ) {
509 scale[0] = atof( argv[ argc - 4 ] );
512 uniform = ( ( scale[0] == scale[1] ) && ( scale[1] == scale[2] ) );
514 if ( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f ) {
515 Sys_Printf( "Usage: q3map [-v] -scale [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
516 Sys_Printf( "Non-zero scale value required.\n" );
520 /* do some path mangling */
521 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
522 StripExtension( source );
523 DefaultExtension( source, ".bsp" );
526 Sys_Printf( "Loading %s\n", source );
527 LoadBSPFile( source );
531 Sys_Printf( "--- ScaleBSP ---\n" );
532 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
534 /* scale entity keys */
535 for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
538 GetVectorForKey( &entities[ i ], "origin", vec );
539 if ( ( vec[ 0 ] || vec[ 1 ] || vec[ 2 ] ) ) {
540 if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
546 if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
549 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
550 SetKeyValue( &entities[ i ], "origin", str );
553 a = FloatForKey( &entities[ i ], "angle" );
554 if ( a == -1 || a == -2 ) { // z scale
557 else if ( fabs( sin( DEG2RAD( a ) ) ) < 0.707 ) {
565 f = FloatForKey( &entities[ i ], "lip" );
568 sprintf( str, "%f", f );
569 SetKeyValue( &entities[ i ], "lip", str );
572 /* scale plat height */
573 f = FloatForKey( &entities[ i ], "height" );
576 sprintf( str, "%f", f );
577 SetKeyValue( &entities[ i ], "height", str );
580 // TODO maybe allow a definition file for entities to specify which values are scaled how?
584 for ( i = 0; i < numBSPModels; i++ )
586 bspModels[ i ].mins[0] *= scale[0];
587 bspModels[ i ].mins[1] *= scale[1];
588 bspModels[ i ].mins[2] *= scale[2];
589 bspModels[ i ].maxs[0] *= scale[0];
590 bspModels[ i ].maxs[1] *= scale[1];
591 bspModels[ i ].maxs[2] *= scale[2];
595 for ( i = 0; i < numBSPNodes; i++ )
597 bspNodes[ i ].mins[0] *= scale[0];
598 bspNodes[ i ].mins[1] *= scale[1];
599 bspNodes[ i ].mins[2] *= scale[2];
600 bspNodes[ i ].maxs[0] *= scale[0];
601 bspNodes[ i ].maxs[1] *= scale[1];
602 bspNodes[ i ].maxs[2] *= scale[2];
606 for ( i = 0; i < numBSPLeafs; i++ )
608 bspLeafs[ i ].mins[0] *= scale[0];
609 bspLeafs[ i ].mins[1] *= scale[1];
610 bspLeafs[ i ].mins[2] *= scale[2];
611 bspLeafs[ i ].maxs[0] *= scale[0];
612 bspLeafs[ i ].maxs[1] *= scale[1];
613 bspLeafs[ i ].maxs[2] *= scale[2];
617 Sys_Printf( "Using texture unlocking (and probably breaking texture alignment a lot)\n" );
618 old_xyzst = safe_malloc( sizeof( *old_xyzst ) * numBSPDrawVerts * 5 );
619 for ( i = 0; i < numBSPDrawVerts; i++ )
621 old_xyzst[5 * i + 0] = bspDrawVerts[i].xyz[0];
622 old_xyzst[5 * i + 1] = bspDrawVerts[i].xyz[1];
623 old_xyzst[5 * i + 2] = bspDrawVerts[i].xyz[2];
624 old_xyzst[5 * i + 3] = bspDrawVerts[i].st[0];
625 old_xyzst[5 * i + 4] = bspDrawVerts[i].st[1];
629 /* scale drawverts */
630 for ( i = 0; i < numBSPDrawVerts; i++ )
632 bspDrawVerts[i].xyz[0] *= scale[0];
633 bspDrawVerts[i].xyz[1] *= scale[1];
634 bspDrawVerts[i].xyz[2] *= scale[2];
635 bspDrawVerts[i].normal[0] /= scale[0];
636 bspDrawVerts[i].normal[1] /= scale[1];
637 bspDrawVerts[i].normal[2] /= scale[2];
638 VectorNormalize( bspDrawVerts[i].normal, bspDrawVerts[i].normal );
642 for ( i = 0; i < numBSPDrawSurfaces; i++ )
644 switch ( bspDrawSurfaces[i].surfaceType )
648 if ( bspDrawSurfaces[i].numIndexes % 3 ) {
649 Error( "Not a triangulation!" );
651 for ( j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3 )
653 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j + 1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j + 2] + bspDrawSurfaces[i].firstVert;
654 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
655 float *oa = &old_xyzst[ia * 5], *ob = &old_xyzst[ib * 5], *oc = &old_xyzst[ic * 5];
660 ExtrapolateTexcoords(
675 for ( i = 0; i < numBSPPlanes; i++ )
677 bspPlanes[ i ].dist *= scale[0];
682 for ( i = 0; i < numBSPPlanes; i++ )
684 bspPlanes[ i ].normal[0] /= scale[0];
685 bspPlanes[ i ].normal[1] /= scale[1];
686 bspPlanes[ i ].normal[2] /= scale[2];
687 f = 1 / VectorLength( bspPlanes[i].normal );
688 VectorScale( bspPlanes[i].normal, f, bspPlanes[i].normal );
689 bspPlanes[ i ].dist *= f;
694 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
695 if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
696 VectorCopy( gridSize, vec );
701 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
702 SetKeyValue( &entities[ 0 ], "gridsize", str );
704 /* inject command line parameters */
705 InjectCommandLine( argv, 0, argc - 1 );
709 StripExtension( source );
710 DefaultExtension( source, "_s.bsp" );
711 Sys_Printf( "Writing %s\n", source );
712 WriteBSPFile( source );
714 /* return to sender */
721 a stripped down ProcessModels
723 void PseudoCompileBSP( qboolean need_tree ){
734 SetDrawSurfacesBuffer();
735 mapDrawSurfs = safe_malloc( sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
736 memset( mapDrawSurfs, 0, sizeof( mapDrawSurface_t ) * MAX_MAP_DRAW_SURFS );
741 for ( mapEntityNum = 0; mapEntityNum < numEntities; mapEntityNum++ )
744 entity = &entities[ mapEntityNum ];
745 if ( entity->brushes == NULL && entity->patches == NULL ) {
749 if ( mapEntityNum != 0 ) {
750 sprintf( modelValue, "*%d", models++ );
751 SetKeyValue( entity, "model", modelValue );
754 /* process the model */
755 Sys_FPrintf( SYS_VRB, "############### model %i ###############\n", numBSPModels );
758 entity->firstDrawSurf = numMapDrawSurfs;
760 ClearMetaTriangles();
761 PatchMapDrawSurfs( entity );
763 if ( mapEntityNum == 0 && need_tree ) {
764 faces = MakeStructuralBSPFaceList( entities[0].brushes );
765 tree = FaceBSP( faces );
766 node = tree->headnode;
771 node->planenum = PLANENUM_LEAF;
773 tree->headnode = node;
776 /* a minimized ClipSidesIntoTree */
777 for ( brush = entity->brushes; brush; brush = brush->next )
779 /* walk the brush sides */
780 for ( i = 0; i < brush->numsides; i++ )
783 side = &brush->sides[ i ];
784 if ( side->winding == NULL ) {
788 if ( side->shaderInfo == NULL ) {
791 /* save this winding as a visible surface */
792 DrawSurfaceForSide( entity, brush, side, side->winding );
797 ClassifyEntitySurfaces( entity );
798 MakeEntityDecals( entity );
799 MakeEntityMetaTriangles( entity );
800 SmoothMetaTriangles();
801 MergeMetaTriangles();
803 FilterDrawsurfsIntoTree( entity, tree );
805 FilterStructuralBrushesIntoTree( entity, tree );
806 FilterDetailBrushesIntoTree( entity, tree );
808 EmitBrushes( entity->brushes, &entity->firstBrush, &entity->numBrushes );
809 EndModel( entity, node );
811 EndBSPFile( qfalse );
816 main argument processing function for bsp conversion
819 int ConvertBSPMain( int argc, char **argv ){
821 int ( *convertFunc )( char * );
824 qboolean map_allowed, force_bsp, force_map;
828 convertFunc = ConvertBSPToASE;
830 map_allowed = qfalse;
836 Sys_Printf( "Usage: q3map -convert [-format <ase|obj|map_bp|map>] [-shadersasbitmap|-lightmapsastexcoord|-deluxemapsastexcoord] [-readbsp|-readmap [-meta|-patchmeta]] [-v] <mapname>\n" );
840 /* process arguments */
841 for ( i = 1; i < ( argc - 1 ); i++ )
843 /* -format map|ase|... */
844 if ( !strcmp( argv[ i ], "-format" ) ) {
846 if ( !Q_stricmp( argv[ i ], "ase" ) ) {
847 convertFunc = ConvertBSPToASE;
848 map_allowed = qfalse;
850 else if ( !Q_stricmp( argv[ i ], "obj" ) ) {
851 convertFunc = ConvertBSPToOBJ;
852 map_allowed = qfalse;
854 else if ( !Q_stricmp( argv[ i ], "map_bp" ) ) {
855 convertFunc = ConvertBSPToMap_BP;
858 else if ( !Q_stricmp( argv[ i ], "map" ) ) {
859 convertFunc = ConvertBSPToMap;
864 convertGame = GetGame( argv[ i ] );
865 map_allowed = qfalse;
866 if ( convertGame == NULL ) {
867 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
871 else if ( !strcmp( argv[ i ], "-ne" ) ) {
872 normalEpsilon = atof( argv[ i + 1 ] );
874 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
876 else if ( !strcmp( argv[ i ], "-de" ) ) {
877 distanceEpsilon = atof( argv[ i + 1 ] );
879 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
881 else if ( !strcmp( argv[ i ], "-shaderasbitmap" ) || !strcmp( argv[ i ], "-shadersasbitmap" ) ) {
882 shadersAsBitmap = qtrue;
884 else if ( !strcmp( argv[ i ], "-lightmapastexcoord" ) || !strcmp( argv[ i ], "-lightmapsastexcoord" ) ) {
885 lightmapsAsTexcoord = qtrue;
887 else if ( !strcmp( argv[ i ], "-deluxemapastexcoord" ) || !strcmp( argv[ i ], "-deluxemapsastexcoord" ) ) {
888 lightmapsAsTexcoord = qtrue;
891 else if ( !strcmp( argv[ i ], "-readbsp" ) ) {
894 else if ( !strcmp( argv[ i ], "-readmap" ) ) {
897 else if ( !strcmp( argv[ i ], "-meta" ) ) {
900 else if ( !strcmp( argv[ i ], "-patchmeta" ) ) {
908 /* clean up map name */
909 strcpy( source, ExpandArg( argv[i] ) );
910 ExtractFileExtension( source, ext );
912 if ( !map_allowed && !force_map ) {
916 if ( force_map || ( !force_bsp && !Q_stricmp( ext, "map" ) && map_allowed ) ) {
917 if ( !map_allowed ) {
918 Sys_Printf( "WARNING: the requested conversion should not be done from .map files. Compile a .bsp first.\n" );
920 StripExtension( source );
921 DefaultExtension( source, ".map" );
922 Sys_Printf( "Loading %s\n", source );
923 LoadMapFile( source, qfalse, convertGame == NULL );
924 PseudoCompileBSP( convertGame != NULL );
928 StripExtension( source );
929 DefaultExtension( source, ".bsp" );
930 Sys_Printf( "Loading %s\n", source );
931 LoadBSPFile( source );
935 /* bsp format convert? */
936 if ( convertGame != NULL ) {
937 /* set global game */
941 StripExtension( source );
942 DefaultExtension( source, "_c.bsp" );
943 Sys_Printf( "Writing %s\n", source );
944 WriteBSPFile( source );
946 /* return to sender */
951 return convertFunc( source );
958 const char* description;
961 void HelpOptions(const char* group_name, int indentation, int width, struct HelpOption* options, int count)
964 char* indent = malloc(indentation+1);
965 memset(indent, ' ', indentation);
966 indent[indentation] = 0;
967 printf("%s%s:\n", indent, group_name);
969 indent = realloc(indent, indentation+1);
970 memset(indent, ' ', indentation);
971 indent[indentation] = 0;
974 for ( i = 0; i < count; i++ )
976 int printed = printf("%s%-24s ", indent, options[i].name);
977 int descsz = strlen(options[i].description);
979 while ( j < descsz && descsz-j > width - printed )
982 printf("%s%26c",indent,' ');
983 int fragment = width - printed;
984 while ( fragment > 0 && options[i].description[j+fragment-1] != ' ')
986 j += fwrite(options[i].description+j, sizeof(char), fragment, stdout);
988 printed = indentation+26;
992 printf("%s\n",options[i].description+j);
994 else if ( j < descsz )
996 printf("%s%26c%s\n",indent,' ',options[i].description+j);
1007 struct HelpOption bsp[] = {
1008 {"-bsp <filename.map>", "Switch that enters this stage"},
1009 {"-altsplit", "Alternate BSP tree splitting weights (should give more fps)"},
1010 {"-celshader <shadername>", "Sets a global cel shader name"},
1011 {"-custinfoparms", "Read scripts/custinfoparms.txt"},
1012 {"-debuginset", "Push all triangle vertexes towards the triangle center"},
1013 {"-debugportals", "Make BSP portals visible in the map"},
1014 {"-debugsurfaces", "Color the vertexes according to the index of the surface"},
1015 {"-deep", "Use detail brushes in the BSP tree, but at lowest priority (should give more fps)"},
1016 {"-de <F>", "Distance epsilon for plane snapping etc."},
1017 {"-fakemap", "Write fakemap.map containing all world brushes"},
1018 {"-flares", "Turn on support for flares (TEST?)"},
1019 {"-flat", "Enable flat shading (good for combining with -celshader)"},
1020 {"-fulldetail", "Treat detail brushes as structural ones"},
1021 {"-leaktest", "Abort if a leak was found"},
1022 {"-meta", "Combine adjacent triangles of the same texture to surfaces (ALWAYS USE THIS)"},
1023 {"-minsamplesize <N>", "Sets minimum lightmap resolution in luxels/qu"},
1024 {"-mi <N>", "Sets the maximum number of indexes per surface"},
1025 {"-mv <N>", "Sets the maximum number of vertices of a lightmapped surface"},
1026 {"-ne <F>", "Normal epsilon for plane snapping etc."},
1027 {"-nocurves", "Turn off support for patches"},
1028 {"-nodetail", "Leave out detail brushes"},
1029 {"-noflares", "Turn off support for flares"},
1030 {"-nofog", "Turn off support for fog volumes"},
1031 {"-nohint", "Turn off support for hint brushes"},
1032 {"-nosubdivide", "Turn off support for `q3map_tessSize` (breaks water vertex deforms)"},
1033 {"-notjunc", "Do not fix T-junctions (causes cracks between triangles, do not use)"},
1034 {"-nowater", "Turn off support for water, slime or lava (Stef, this is for you)"},
1035 {"-np <A>", "Force all surfaces to be nonplanar with a given shade angle"},
1036 {"-onlyents", "Only update entities in the BSP"},
1037 {"-patchmeta", "Turn patches into triangle meshes for display"},
1038 {"-rename", "Append “bspâ€
\9d suffix to miscmodel shaders (needed for SoF2)"},
1039 {"-samplesize <N>", "Sets default lightmap resolution in luxels/qu"},
1040 {"-skyfix", "Turn sky box into six surfaces to work around ATI problems"},
1041 {"-snap <N>", "Snap brush bevel planes to the given number of units"},
1042 {"-tempname <filename.map>", "Read the MAP file from the given file name"},
1043 {"-texrange <N>", "Limit per-surface texture range to the given number of units, and subdivide surfaces like with `q3map_tessSize` if this is not met"},
1044 {"-tmpout", "Write the BSP file to /tmp"},
1045 {"-verboseentities", "Enable `-v` only for map entities, not for the world"},
1047 HelpOptions("BSP Stage", 0, 80, bsp, sizeof(bsp)/sizeof(struct HelpOption));
1051 struct HelpOption vis[] = {
1052 {"-vis <filename.map>", "Switch that enters this stage"},
1053 {"-fast", "Very fast and crude vis calculation"},
1054 {"-mergeportals", "The less crude half of `-merge`, makes vis sometimes much faster but doesn't hurt fps usually"},
1055 {"-merge", "Faster but still okay vis calculation"},
1056 {"-nopassage", "Just use PortalFlow vis (usually less fps)"},
1057 {"-nosort", "Do not sort the portals before calculating vis (usually slower)"},
1058 {"-passageOnly", "Just use PassageFlow vis (usually less fps)"},
1059 {"-saveprt", "Keep the PRT file after running vis (so you can run vis again)"},
1060 {"-tmpin", "Use /tmp folder for input"},
1061 {"-tmpout", "Use /tmp folder for output"},
1063 HelpOptions("VIS Stage", 0, 80, vis, sizeof(vis)/sizeof(struct HelpOption));
1067 struct HelpOption light[] = {
1068 {"-light <filename.map>", "Switch that enters this stage"},
1069 {"-vlight <filename.map>", "Deprecated alias for `-light -fast` ... filename.map"},
1070 {"-approx <N>", "Vertex light approximation tolerance (never use in conjunction with deluxemapping)"},
1071 {"-areascale <F, `-area` F>", "Scaling factor for area lights (surfacelight)"},
1072 {"-border", "Add a red border to lightmaps for debugging"},
1073 {"-bouncegrid", "Also compute radiosity on the light grid"},
1074 {"-bounceonly", "Only compute radiosity"},
1075 {"-bouncescale <F>", "Scaling factor for radiosity"},
1076 {"-bounce <N>", "Number of bounces for radiosity"},
1077 {"-cheapgrid", "Use `-cheap` style lighting for radiosity"},
1078 {"-cheap", "Abort vertex light calculations when white is reached"},
1079 {"-compensate <F>", "Lightmap compensate (darkening factor applied after everything else)"},
1080 {"-cpma", "CPMA vertex lighting mode"},
1081 {"-custinfoparms", "Read scripts/custinfoparms.txt"},
1082 {"-dark", "Darken lightmap seams"},
1083 {"-debugaxis", "Color the lightmaps according to the lightmap axis"},
1084 {"-debugcluster", "Color the lightmaps according to the index of the cluster"},
1085 {"-debugdeluxe", "Show deluxemaps on the lightmap"},
1086 {"-debugnormals", "Color the lightmaps according to the direction of the surface normal"},
1087 {"-debugorigin", "Color the lightmaps according to the origin of the luxels"},
1088 {"-debugsurfaces, -debugsurface", "Color the lightmaps according to the index of the surface"},
1089 {"-debugunused", "This option does nothing"},
1090 {"-debug", "Mark the lightmaps according to the cluster: unmapped clusters get yellow, occluded ones get pink, flooded ones get blue overlay color, otherwise red"},
1091 {"-deluxemode 0", "Use modelspace deluxemaps (DarkPlaces)"},
1092 {"-deluxemode 1", "Use tangentspace deluxemaps"},
1093 {"-deluxe, -deluxemap", "Enable deluxemapping (light direction maps)"},
1094 {"-dirtdebug, -debugdirt", "Store the dirtmaps as lightmaps for debugging"},
1095 {"-dirtdepth", "Dirtmapping depth"},
1096 {"-dirtgain", "Dirtmapping exponent"},
1097 {"-dirtmode 0", "Ordered direction dirtmapping"},
1098 {"-dirtmode 1", "Randomized direction dirtmapping"},
1099 {"-dirtscale", "Dirtmapping scaling factor"},
1100 {"-dirty", "Enable dirtmapping"},
1101 {"-dump", "Dump radiosity from `-bounce` into numbered MAP file prefabs"},
1102 {"-export", "Export lightmaps when compile finished (like `-export` mode)"},
1103 {"-exposure <F>", "Lightmap exposure to better support overbright spots"},
1104 {"-external", "Force external lightmaps even if at size of internal lightmaps"},
1105 {"-extravisnudge", "Broken feature to nudge the luxel origin to a better vis cluster"},
1106 {"-extrawide", "Deprecated alias for `-super 2 -filter`"},
1107 {"-extra", "Deprecated alias for `-super 2`"},
1108 {"-fastbounce", "Use `-fast` style lighting for radiosity"},
1109 {"-faster", "Use a faster falloff curve for lighting; also implies `-fast`"},
1110 {"-fastgrid", "Use `-fast` style lighting for the light grid"},
1111 {"-fast", "Ignore tiny light contributions"},
1112 {"-filter", "Lightmap filtering"},
1113 {"-floodlight", "Enable floodlight (zero-effort somewhat decent lighting)"},
1114 {"-gamma <F>", "Lightmap gamma"},
1115 {"-gridambientscale <F>", "Scaling factor for the light grid ambient components only"},
1116 {"-gridscale <F>", "Scaling factor for the light grid only"},
1117 {"-keeplights", "Keep light entities in the BSP file after compile"},
1118 {"-lightmapdir <directory>", "Directory to store external lightmaps (default: same as map name without extension)"},
1119 {"-lightmapsize <N>", "Size of lightmaps to generate (must be a power of two)"},
1120 {"-lomem", "Low memory but slower lighting mode"},
1121 {"-lowquality", "Low quality floodlight (appears to currently break floodlight)"},
1122 {"-minsamplesize <N>", "Sets minimum lightmap resolution in luxels/qu"},
1123 {"-nocollapse", "Do not collapse identical lightmaps"},
1124 {"-nodeluxe, -nodeluxemap", "Disable deluxemapping"},
1125 {"-nogrid", "Disable grid light calculation (makes all entities fullbright)"},
1126 {"-nolightmapsearch", "Do not optimize lightmap packing for GPU memory usage (as doing so costs fps)"},
1127 {"-normalmap", "Color the lightmaps according to the direction of the surface normal (TODO is this identical to `-debugnormals`?)"},
1128 {"-nostyle, -nostyles", "Disable support for light styles"},
1129 {"-nosurf", "Disable tracing against surfaces (only uses BSP nodes then)"},
1130 {"-notrace", "Disable shadow occlusion"},
1131 {"-novertex", "Disable vertex lighting"},
1132 {"-patchshadows", "Cast shadows from patches"},
1133 {"-pointscale <F, `-point` F>", "Scaling factor for point lights (light entities)"},
1134 {"-q3", "Use nonlinear falloff curve by default (like Q3A)"},
1135 {"-samplescale <F>", "Scales all lightmap resolutions"},
1136 {"-samplesize <N>", "Sets default lightmap resolution in luxels/qu"},
1137 {"-samples <N>", "Adaptive supersampling quality"},
1138 {"-scale <F>", "Scaling factor for all light types"},
1139 {"-shadeangle <A>", "Angle for phong shading"},
1140 {"-shade", "Enable phong shading at default shade angle"},
1141 {"-skyscale <F, `-sky` F>", "Scaling factor for sky and sun light"},
1142 {"-smooth", "Deprecated alias for `-samples 2`"},
1143 {"-style, -styles", "Enable support for light styles"},
1144 {"-sunonly", "Only compute sun light"},
1145 {"-super <N, `-supersample` N>", "Ordered grid supersampling quality"},
1146 {"-thresh <F>", "Triangle subdivision threshold"},
1147 {"-trianglecheck", "Broken check that should ensure luxels apply to the right triangle"},
1148 {"-trisoup", "Convert brush faces to triangle soup"},
1149 {"-wolf", "Use linear falloff curve by default (like W:ET)"},
1152 HelpOptions("Light Stage", 0, 80, light, sizeof(light)/sizeof(struct HelpOption));
1157 struct HelpOption analize[] = {
1158 {"-analyze <filename.bsp>", "Switch that enters this mode"},
1159 {"-lumpswap", "Swap byte order in the lumps"},
1162 HelpOptions("Analyzing BSP-like file structure", 0, 80, analize, sizeof(analize)/sizeof(struct HelpOption));
1166 struct HelpOption scale[] = {
1167 {"-scale <S filename.bsp>", "Scale uniformly"},
1168 {"-scale <SX SY SZ filename.bsp>", "Scale non-uniformly"},
1169 {"-scale -tex <S filename.bsp>", "Scale uniformly without texture lock"},
1170 {"-scale -tex <SX SY SZ filename.bsp>", "Scale non-uniformly without texture lock"},
1172 HelpOptions("Scaling", 0, 80, scale, sizeof(scale)/sizeof(struct HelpOption));
1176 struct HelpOption convert[] = {
1177 {"-convert <filename.bsp>", "Switch that enters this mode"},
1178 {"-de <number>", "Distance epsilon for the conversion"},
1179 {"-format <converter>", "Select the converter (available: map, ase, or game names)"},
1180 {"-ne <F>", "Normal epsilon for the conversion"},
1181 {"-shadersasbitmap", "(only for ase) use the shader names as \\*BITMAP key so they work as prefabs"},
1184 HelpOptions("Converting & Decompiling", 0, 80, convert, sizeof(convert)/sizeof(struct HelpOption));
1189 struct HelpOption exportl[] = {
1190 {"-export <filename.bsp>", "Copies lightmaps from the BSP to `filename/lightmap_0000.tga` ff"}
1193 HelpOptions("Exporting lightmaps", 0, 80, exportl, sizeof(exportl)/sizeof(struct HelpOption));
1198 struct HelpOption fixaas[] = {
1199 {"-fixaas <filename.bsp>", "Switch that enters this mode"},
1202 HelpOptions("Fixing AAS checksum", 0, 80, fixaas, sizeof(fixaas)/sizeof(struct HelpOption));
1207 struct HelpOption info[] = {
1208 {"-info <filename.bsp>", "Switch that enters this mode"},
1211 HelpOptions("Get info about BSP file", 0, 80, info, sizeof(info)/sizeof(struct HelpOption));
1216 struct HelpOption import[] = {
1217 {"-import <filename.bsp>", "Copies lightmaps from `filename/lightmap_0000.tga` ff into the BSP"},
1220 HelpOptions("Importing lightmaps", 0, 80, import, sizeof(import)/sizeof(struct HelpOption));
1225 struct HelpOption minimap[] = {
1226 {"-minimap <filename.bsp>", "Creates a minimap of the BSP, by default writes to `../gfx/filename_mini.tga`"},
1227 {"-black", "Write the minimap as a black-on-transparency RGBA32 image"},
1228 {"-boost <F>", "Sets the contrast boost value (higher values make a brighter image); contrast boost is somewhat similar to gamma, but continuous even at zero"},
1229 {"-border <F>", "Sets the amount of border pixels relative to the total image size"},
1230 {"-gray", "Write the minimap as a white-on-black GRAY8 image"},
1231 {"-keepaspect", "Ensure the aspect ratio is kept (the minimap is then letterboxed to keep aspect)"},
1232 {"-minmax <xmin ymin zmin xmax ymax zmax>", "Forces specific map dimensions (note: the minimap actually uses these dimensions, scaled to the target size while keeping aspect with centering, and 1/64 of border appended to all sides)"},
1233 {"-nokeepaspect", "Do not ensure the aspect ratio is kept (makes it easier to use the image in your code, but looks bad together with sharpening)"},
1234 {"-o <filename.tga>", "Sets the output file name"},
1235 {"-random <N>", "Sets the randomized supersampling count (cannot be combined with `-samples`)"},
1236 {"-samples <N>", "Sets the ordered supersampling count (cannot be combined with `-random`)"},
1237 {"-sharpen <F>", "Sets the sharpening coefficient"},
1238 {"-size <N>", "Sets the width and height of the output image"},
1239 {"-white", "Write the minimap as a white-on-transparency RGBA32 image"},
1242 HelpOptions("MiniMap", 0, 80, minimap, sizeof(minimap)/sizeof(struct HelpOption));
1247 struct HelpOption common[] = {
1248 {"-connect <address>", "Talk to a NetRadiant instance using a specific XML based protocol"},
1249 {"-force", "Allow reading some broken/unsupported BSP files e.g. when decompiling, may also crash"},
1250 {"-fs_basepath <path>", "Sets the given path as main directory of the game (can be used more than once to look in multiple paths)"},
1251 {"-fs_game <gamename>", "Sets a different game directory name (default for Q3A: baseq3)"},
1252 {"-fs_homebase <dir>", "Specifies where the user home directory name is on Linux (default for Q3A: .q3a)"},
1253 {"-game <gamename>", "Load settings for the given game (default: quake3)"},
1254 {"-subdivisions <F>", "multiplier for patch subdivisions quality"},
1255 {"-threads <N>", "number of threads to use"},
1256 {"-v", "Verbose mode"}
1259 HelpOptions("Common Options", 0, 80, common, sizeof(common)/sizeof(struct HelpOption));
1263 void Help(const char* arg)
1265 printf("Usage: q3map2 [stage] [common options...] [stage options...] [stage source file]\n");
1266 printf(" q3map2 -help [stage]\n\n");
1270 struct HelpOption stages[] = {
1271 {"-bsp", "BSP Stage"},
1272 {"-vis", "VIS Stage"},
1273 {"-light", "Light Stage"},
1274 {"-analize", "Analyzing BSP-like file structure"},
1275 {"-scale", "Scaling"},
1276 {"-convert", "Converting & Decompiling"},
1277 {"-export", "Exporting lightmaps"},
1278 {"-fixaas", "Fixing AAS checksum"},
1279 {"-info", "Get info about BSP file"},
1280 {"-import", "Importing lightmaps"},
1281 {"-minimap", "MiniMap"},
1283 void(*help_funcs[])() = {
1297 if ( arg && strlen(arg) > 0 )
1299 if ( arg[0] == '-' )
1303 for ( i = 0; i < sizeof(stages)/sizeof(struct HelpOption); i++ )
1304 if ( strcmp(arg, stages[i].name+1) == 0 )
1311 HelpOptions("Stages", 0, 80, stages, sizeof(stages)/sizeof(struct HelpOption));
1319 int main( int argc, char **argv ){
1324 /* we want consistent 'randomness' */
1328 start = I_FloatTime();
1330 /* this was changed to emit version number over the network */
1331 printf( Q3MAP_VERSION "\n" );
1334 atexit( ExitQ3Map );
1336 /* read general options first */
1337 for ( i = 1; i < argc; i++ )
1340 if ( !strcmp( argv[ i ], "-h" ) || !strcmp( argv[ i ], "--help" )
1341 || !strcmp( argv[ i ], "-help" ) ) {
1347 if ( !strcmp( argv[ i ], "-connect" ) ) {
1350 Broadcast_Setup( argv[ i ] );
1355 else if ( !strcmp( argv[ i ], "-v" ) ) {
1363 else if ( !strcmp( argv[ i ], "-force" ) ) {
1368 /* patch subdivisions */
1369 else if ( !strcmp( argv[ i ], "-subdivisions" ) ) {
1372 patchSubdivisions = atoi( argv[ i ] );
1374 if ( patchSubdivisions <= 0 ) {
1375 patchSubdivisions = 1;
1380 else if ( !strcmp( argv[ i ], "-threads" ) ) {
1383 numthreads = atoi( argv[ i ] );
1388 /* init model library */
1390 PicoSetMallocFunc( safe_malloc );
1391 PicoSetFreeFunc( free );
1392 PicoSetPrintFunc( PicoPrintFunc );
1393 PicoSetLoadFileFunc( PicoLoadFileFunc );
1394 PicoSetFreeFileFunc( free );
1396 /* set number of threads */
1399 /* generate sinusoid jitter table */
1400 for ( i = 0; i < MAX_JITTERS; i++ )
1402 jitters[ i ] = sin( i * 139.54152147 );
1403 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1406 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1407 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1409 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
1410 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1411 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1412 Sys_Printf( "%s\n", Q3MAP_MOTD );
1414 /* ydnar: new path initialization */
1415 InitPaths( &argc, argv );
1417 /* set game options */
1418 if ( !patchSubdivisions ) {
1419 patchSubdivisions = game->patchSubdivisions;
1422 /* check if we have enough options left to attempt something */
1424 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1428 if ( !strcmp( argv[ 1 ], "-fixaas" ) ) {
1429 r = FixAAS( argc - 1, argv + 1 );
1433 else if ( !strcmp( argv[ 1 ], "-analyze" ) ) {
1434 r = AnalyzeBSP( argc - 1, argv + 1 );
1438 else if ( !strcmp( argv[ 1 ], "-info" ) ) {
1439 r = BSPInfo( argc - 2, argv + 2 );
1443 else if ( !strcmp( argv[ 1 ], "-vis" ) ) {
1444 r = VisMain( argc - 1, argv + 1 );
1448 else if ( !strcmp( argv[ 1 ], "-light" ) ) {
1449 r = LightMain( argc - 1, argv + 1 );
1453 else if ( !strcmp( argv[ 1 ], "-vlight" ) ) {
1454 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1455 argv[ 1 ] = "-fast"; /* eek a hack */
1456 r = LightMain( argc, argv );
1459 /* ydnar: lightmap export */
1460 else if ( !strcmp( argv[ 1 ], "-export" ) ) {
1461 r = ExportLightmapsMain( argc - 1, argv + 1 );
1464 /* ydnar: lightmap import */
1465 else if ( !strcmp( argv[ 1 ], "-import" ) ) {
1466 r = ImportLightmapsMain( argc - 1, argv + 1 );
1469 /* ydnar: bsp scaling */
1470 else if ( !strcmp( argv[ 1 ], "-scale" ) ) {
1471 r = ScaleBSPMain( argc - 1, argv + 1 );
1474 /* ydnar: bsp conversion */
1475 else if ( !strcmp( argv[ 1 ], "-convert" ) ) {
1476 r = ConvertBSPMain( argc - 1, argv + 1 );
1480 else if ( !strcmp( argv[ 1 ], "-minimap" ) ) {
1481 r = MiniMapBSPMain( argc - 1, argv + 1 );
1484 /* ydnar: otherwise create a bsp */
1486 r = BSPMain( argc, argv );
1490 end = I_FloatTime();
1491 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1493 /* shut down connection */
1494 Broadcast_Shutdown();
1496 /* return any error code */