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
48 return (vec_t) rand() / RAND_MAX;
58 static void ExitQ3Map( void )
61 if( mapDrawSurfs != NULL )
69 /* borrowed from light.c */
70 void WriteTGA24( char *filename, byte *data, int width, int height, qboolean flip );
71 typedef struct minimap_s
77 float *sample_offsets;
78 float sharpen_boxmult;
79 float sharpen_centermult;
85 static minimap_t minimap;
87 qboolean BrushIntersectionWithLine(bspBrush_t *brush, vec3_t start, vec3_t dir, float *t_in, float *t_out)
90 qboolean in = qfalse, out = qfalse;
91 bspBrushSide_t *sides = &bspBrushSides[brush->firstSide];
93 for(i = 0; i < brush->numSides; ++i)
95 bspPlane_t *p = &bspPlanes[sides[i].planeNum];
96 float sn = DotProduct(start, p->normal);
97 float dn = DotProduct(dir, p->normal);
101 return qfalse; // outside!
105 float t = (p->dist - sn) / dn;
112 // as t_in can only increase, and t_out can only decrease, early out
113 if(out && *t_in >= *t_out)
119 if(!out || t < *t_out)
123 // as t_in can only increase, and t_out can only decrease, early out
124 if(in && *t_in >= *t_out)
133 static float MiniMapSample(float x, float y)
152 for(i = 0; i < minimap.model->numBSPBrushes; ++i)
154 bi = minimap.model->firstBSPBrush + i;
155 if(opaqueBrushes[bi >> 3] & (1 << (bi & 7)))
159 // sort out mins/maxs of the brush
160 s = &bspBrushSides[b->firstSide];
161 if(x < -bspPlanes[s[0].planeNum].dist)
163 if(x > +bspPlanes[s[1].planeNum].dist)
165 if(y < -bspPlanes[s[2].planeNum].dist)
167 if(y > +bspPlanes[s[3].planeNum].dist)
170 if(BrushIntersectionWithLine(b, org, dir, &t0, &t1))
181 void RandomVector2f(float v[2])
185 v[0] = 2 * Random() - 1;
186 v[1] = 2 * Random() - 1;
188 while(v[0] * v[0] + v[1] * v[1] > 1);
191 static void MiniMapRandomlySupersampled(int y)
194 float *p = &minimap.data1f[y * minimap.width];
195 float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
196 float dx = minimap.size[0] / (float) minimap.width;
197 float dy = minimap.size[1] / (float) minimap.height;
201 for(x = 0; x < minimap.width; ++x)
203 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
206 for(i = 0; i < minimap.samples; ++i)
209 thisval = MiniMapSample(
210 xmin + (uv[0] + 0.5) * dx, /* exaggerated random pattern for better results */
211 ymin + (uv[1] + 0.5) * dy /* exaggerated random pattern for better results */
215 val /= minimap.samples * minimap.size[2];
220 static void MiniMapSupersampled(int y)
223 float *p = &minimap.data1f[y * minimap.width];
224 float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
225 float dx = minimap.size[0] / (float) minimap.width;
226 float dy = minimap.size[1] / (float) minimap.height;
228 for(x = 0; x < minimap.width; ++x)
230 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
233 for(i = 0; i < minimap.samples; ++i)
235 float thisval = MiniMapSample(
236 xmin + minimap.sample_offsets[2*i+0] * dx,
237 ymin + minimap.sample_offsets[2*i+1] * dy
241 val /= minimap.samples * minimap.size[2];
246 static void MiniMapNoSupersampling(int y)
249 float *p = &minimap.data1f[y * minimap.width];
250 float ymin = minimap.mins[1] + minimap.size[1] * ((y + 0.5) / (float) minimap.height);
252 for(x = 0; x < minimap.width; ++x)
254 float xmin = minimap.mins[0] + minimap.size[0] * ((x + 0.5) / (float) minimap.width);
255 *p++ = MiniMapSample(xmin, ymin) / minimap.size[2];
259 static void MiniMapSharpen(int y)
262 qboolean up = (y > 0);
263 qboolean down = (y < minimap.height - 1);
264 float *p = &minimap.data1f[y * minimap.width];
265 float *q = &minimap.sharpendata1f[y * minimap.width];
267 for(x = 0; x < minimap.width; ++x)
269 qboolean left = (x > 0);
270 qboolean right = (x < minimap.width - 1);
271 float val = p[0] * minimap.sharpen_centermult;
274 val += p[-1 -minimap.width] * minimap.sharpen_boxmult;
276 val += p[-1 +minimap.width] * minimap.sharpen_boxmult;
278 val += p[+1 -minimap.width] * minimap.sharpen_boxmult;
280 val += p[+1 +minimap.width] * minimap.sharpen_boxmult;
283 val += p[-1] * minimap.sharpen_boxmult;
285 val += p[+1] * minimap.sharpen_boxmult;
287 val += p[-minimap.width] * minimap.sharpen_boxmult;
289 val += p[+minimap.width] * minimap.sharpen_boxmult;
296 void MiniMapMakeMinsMaxs(vec3_t mins_in, vec3_t maxs_in, float border, qboolean keepaspect)
298 vec3_t mins, maxs, extend;
299 VectorCopy(mins_in, mins);
300 VectorCopy(maxs_in, maxs);
302 // line compatible to nexuiz mapinfo
303 Sys_Printf("size %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
307 VectorSubtract(maxs, mins, extend);
308 if(extend[1] > extend[0])
310 mins[0] -= (extend[1] - extend[0]) * 0.5;
311 maxs[0] += (extend[1] - extend[0]) * 0.5;
315 mins[1] -= (extend[0] - extend[1]) * 0.5;
316 maxs[1] += (extend[0] - extend[1]) * 0.5;
320 /* border: amount of black area around the image */
321 /* input: border, 1-2*border, border but we need border/(1-2*border) */
323 VectorSubtract(maxs, mins, extend);
324 VectorScale(extend, border / (1 - 2 * border), extend);
326 VectorSubtract(mins, extend, mins);
327 VectorAdd(maxs, extend, maxs);
329 VectorCopy(mins, minimap.mins);
330 VectorSubtract(maxs, mins, minimap.size);
332 // line compatible to nexuiz mapinfo
333 Sys_Printf("size_texcoords %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
337 MiniMapSetupBrushes()
338 determines solid non-sky brushes in the world
341 void MiniMapSetupBrushes( void )
343 int i, b, compileFlags;
350 Sys_FPrintf( SYS_VRB, "--- MiniMapSetupBrushes ---\n" );
353 if( opaqueBrushes == NULL )
354 opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 );
357 memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 );
358 numOpaqueBrushes = 0;
360 /* walk the list of worldspawn brushes */
361 for( i = 0; i < minimap.model->numBSPBrushes; i++ )
364 b = minimap.model->firstBSPBrush + i;
365 brush = &bspBrushes[ b ];
368 /* check all sides */
370 for( j = 0; j < brush->numSides; j++ )
372 /* do bsp shader calculations */
373 side = &bspBrushSides[ brush->firstSide + j ];
374 shader = &bspShaders[ side->shaderNum ];
376 /* get shader info */
377 si = ShaderInfoForShader( shader->shader );
381 /* or together compile flags */
382 compileFlags |= si->compileFlags;
385 shader = &bspShaders[ brush->shaderNum ];
386 si = ShaderInfoForShader( shader->shader );
390 compileFlags = si->compileFlags;
393 /* determine if this brush is solid */
394 if( (compileFlags & (C_SOLID | C_SKY)) == C_SOLID )
396 opaqueBrushes[ b >> 3 ] |= (1 << (b & 7));
402 /* emit some statistics */
403 Sys_FPrintf( SYS_VRB, "%9d solid brushes\n", numOpaqueBrushes );
406 qboolean MiniMapEvaluateSampleOffsets(int *bestj, int *bestk, float *bestval)
411 *bestj = *bestk = -1;
412 *bestval = 3; /* max possible val is 2 */
414 for(j = 0; j < minimap.samples; ++j)
415 for(k = j + 1; k < minimap.samples; ++k)
417 dx = minimap.sample_offsets[2*j+0] - minimap.sample_offsets[2*k+0];
418 dy = minimap.sample_offsets[2*j+1] - minimap.sample_offsets[2*k+1];
419 if(dx > +0.5) dx -= 1;
420 if(dx < -0.5) dx += 1;
421 if(dy > +0.5) dy -= 1;
422 if(dy < -0.5) dy += 1;
423 val = dx * dx + dy * dy;
435 void MiniMapMakeSampleOffsets()
438 float val, valj, valk, sx, sy, rx, ry;
440 Sys_Printf( "Generating good sample offsets (this may take a while)...\n" );
442 /* start with entirely random samples */
443 for(i = 0; i < minimap.samples; ++i)
445 minimap.sample_offsets[2*i+0] = Random();
446 minimap.sample_offsets[2*i+1] = Random();
449 for(i = 0; i < 1000; ++i)
451 if(MiniMapEvaluateSampleOffsets(&j, &k, &val))
453 sx = minimap.sample_offsets[2*j+0];
454 sy = minimap.sample_offsets[2*j+1];
455 minimap.sample_offsets[2*j+0] = rx = Random();
456 minimap.sample_offsets[2*j+1] = ry = Random();
457 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valj))
459 minimap.sample_offsets[2*j+0] = sx;
460 minimap.sample_offsets[2*j+1] = sy;
462 sx = minimap.sample_offsets[2*k+0];
463 sy = minimap.sample_offsets[2*k+1];
464 minimap.sample_offsets[2*k+0] = rx;
465 minimap.sample_offsets[2*k+1] = ry;
466 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valk))
468 minimap.sample_offsets[2*k+0] = sx;
469 minimap.sample_offsets[2*k+1] = sy;
475 /* valj is the greatest */
476 minimap.sample_offsets[2*j+0] = rx;
477 minimap.sample_offsets[2*j+1] = ry;
482 /* valj is the greater and it is useless - forget it */
489 /* valk is the greatest */
490 minimap.sample_offsets[2*k+0] = rx;
491 minimap.sample_offsets[2*k+1] = ry;
496 /* valk is the greater and it is useless - forget it */
505 void MergeRelativePath(char *out, const char *absolute, const char *relative)
507 const char *endpos = absolute + strlen(absolute);
508 while(endpos != absolute && (endpos[-1] == '/' || endpos[-1] == '\\'))
510 while(relative[0] == '.' && relative[1] == '.' && (relative[2] == '/' || relative[2] == '\\'))
513 while(endpos != absolute)
516 if(*endpos == '/' || *endpos == '\\')
519 while(endpos != absolute && (endpos[-1] == '/' || endpos[-1] == '\\'))
522 memcpy(out, absolute, endpos - absolute);
523 out[endpos - absolute] = '/';
524 strcpy(out + (endpos - absolute + 1), relative);
527 int MiniMapBSPMain( int argc, char **argv )
529 char minimapFilename[1024];
532 char relativeMinimapFilename[1024];
533 float minimapSharpen;
545 Sys_Printf( "Usage: q3map [-v] -minimap [-size n] [-sharpen f] [-samples n | -random n] [-o filename.tga] [-minmax Xmin Ymin Zmin Xmax Ymax Zmax] <mapname>\n" );
549 /* load the BSP first */
550 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
551 StripExtension( source );
552 DefaultExtension( source, ".bsp" );
553 Sys_Printf( "Loading %s\n", source );
554 BeginMapShaderFile( source );
556 LoadBSPFile( source );
558 minimap.model = &bspModels[0];
559 VectorCopy(minimap.model->mins, mins);
560 VectorCopy(minimap.model->maxs, maxs);
562 *minimapFilename = 0;
563 minimapSharpen = game->miniMapSharpen;
564 minimap.width = minimap.height = game->miniMapSize;
565 border = game->miniMapBorder;
566 keepaspect = game->miniMapKeepAspect;
569 minimap.sample_offsets = NULL;
571 /* process arguments */
572 for( i = 1; i < (argc - 1); i++ )
574 if( !strcmp( argv[ i ], "-size" ) )
576 minimap.width = minimap.height = atoi(argv[i + 1]);
578 Sys_Printf( "Image size set to %i\n", minimap.width );
580 else if( !strcmp( argv[ i ], "-sharpen" ) )
582 minimapSharpen = atof(argv[i + 1]);
584 Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
586 else if( !strcmp( argv[ i ], "-samples" ) )
588 minimap.samples = atoi(argv[i + 1]);
590 Sys_Printf( "Samples set to %i\n", minimap.samples );
591 if(minimap.sample_offsets)
592 free(minimap.sample_offsets);
593 minimap.sample_offsets = malloc(2 * sizeof(*minimap.sample_offsets) * minimap.samples);
594 MiniMapMakeSampleOffsets();
596 else if( !strcmp( argv[ i ], "-random" ) )
598 minimap.samples = atoi(argv[i + 1]);
600 Sys_Printf( "Random samples set to %i\n", minimap.samples );
601 if(minimap.sample_offsets)
602 free(minimap.sample_offsets);
603 minimap.sample_offsets = NULL;
605 else if( !strcmp( argv[ i ], "-border" ) )
607 border = atof(argv[i + 1]);
609 Sys_Printf( "Border set to %f\n", border );
611 else if( !strcmp( argv[ i ], "-keepaspect" ) )
614 Sys_Printf( "Keeping aspect ratio by letterboxing\n", border );
616 else if( !strcmp( argv[ i ], "-nokeepaspect" ) )
619 Sys_Printf( "Not keeping aspect ratio\n", border );
621 else if( !strcmp( argv[ i ], "-o" ) )
623 strcpy(minimapFilename, argv[i + 1]);
625 Sys_Printf( "Output file name set to %s\n", minimapFilename );
627 else if( !strcmp( argv[ i ], "-minmax" ) && i < (argc - 7) )
629 mins[0] = atof(argv[i + 1]);
630 mins[1] = atof(argv[i + 2]);
631 mins[2] = atof(argv[i + 3]);
632 maxs[0] = atof(argv[i + 4]);
633 maxs[1] = atof(argv[i + 5]);
634 maxs[2] = atof(argv[i + 6]);
636 Sys_Printf( "Map mins/maxs overridden\n" );
640 MiniMapMakeMinsMaxs(mins, maxs, border, keepaspect);
642 if(!*minimapFilename)
644 ExtractFileBase(source, basename);
645 ExtractFilePath(source, path);
646 sprintf(relativeMinimapFilename, game->miniMapNameFormat, basename);
647 MergeRelativePath(minimapFilename, path, relativeMinimapFilename);
648 Sys_Printf("Output file name automatically set to %s\n", minimapFilename);
651 if(minimapSharpen >= 0)
653 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
654 minimap.sharpen_boxmult = -minimapSharpen;
657 minimap.data1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
658 data3b = safe_malloc(minimap.width * minimap.height * 3);
659 if(minimapSharpen >= 0)
660 minimap.sharpendata1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
662 MiniMapSetupBrushes();
664 if(minimap.samples <= 1)
666 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
667 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapNoSupersampling);
671 if(minimap.sample_offsets)
673 Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
674 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSupersampled);
678 Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
679 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapRandomlySupersampled);
683 if(minimap.sharpendata1f)
685 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
686 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSharpen);
687 q = minimap.sharpendata1f;
694 Sys_Printf( "\nConverting...");
696 for(y = 0; y < minimap.height; ++y)
697 for(x = 0; x < minimap.width; ++x)
702 if(v > 255.0/256.0) v = 255.0/256.0;
709 Sys_Printf( " writing to %s...", minimapFilename );
710 ExtractFilePath(minimapFilename, path);
712 WriteTGA24(minimapFilename, data3b, minimap.width, minimap.height, qfalse);
714 Sys_Printf( " done.\n" );
716 /* return to sender */
726 calculates an md4 checksum for a block of data
729 static int MD4BlockChecksum( void *buffer, int length )
731 return Com_BlockChecksum(buffer, length);
736 resets an aas checksum to match the given BSP
739 int FixAAS( int argc, char **argv )
741 int length, checksum;
744 char aas[ 1024 ], **ext;
757 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
761 /* do some path mangling */
762 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
763 StripExtension( source );
764 DefaultExtension( source, ".bsp" );
767 Sys_Printf( "--- FixAAS ---\n" );
770 Sys_Printf( "Loading %s\n", source );
771 length = LoadFile( source, &buffer );
773 /* create bsp checksum */
774 Sys_Printf( "Creating checksum...\n" );
775 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
777 /* write checksum to aas */
782 strcpy( aas, source );
783 StripExtension( aas );
785 Sys_Printf( "Trying %s\n", aas );
789 file = fopen( aas, "r+b" );
792 if( fwrite( &checksum, 4, 1, file ) != 1 )
793 Error( "Error writing checksum to %s", aas );
797 /* return to sender */
805 analyzes a Quake engine BSP file
808 typedef struct abspHeader_s
813 bspLump_t lumps[ 1 ]; /* unknown size */
817 typedef struct abspLumpTest_s
824 int AnalyzeBSP( int argc, char **argv )
826 abspHeader_t *header;
827 int size, i, version, offset, length, lumpInt, count;
831 char lumpString[ 1024 ], source[ 1024 ];
832 qboolean lumpSwap = qfalse;
833 abspLumpTest_t *lumpTest;
834 static abspLumpTest_t lumpTests[] =
836 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
837 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
838 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
839 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
840 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
841 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
842 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
843 { 104, 3, "IBSP LUMP_DRAWSURFS" },
844 { 44, 3, "IBSP LUMP_DRAWVERTS" },
845 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
846 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
847 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
848 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
856 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
860 /* process arguments */
861 for( i = 1; i < (argc - 1); i++ )
863 /* -format map|ase|... */
864 if( !strcmp( argv[ i ], "-lumpswap" ) )
866 Sys_Printf( "Swapped lump structs enabled\n" );
871 /* clean up map name */
872 strcpy( source, ExpandArg( argv[ i ] ) );
873 Sys_Printf( "Loading %s\n", source );
876 size = LoadFile( source, (void**) &header );
877 if( size == 0 || header == NULL )
879 Sys_Printf( "Unable to load %s.\n", source );
883 /* analyze ident/version */
884 memcpy( ident, header->ident, 4 );
886 version = LittleLong( header->version );
888 Sys_Printf( "Identity: %s\n", ident );
889 Sys_Printf( "Version: %d\n", version );
890 Sys_Printf( "---------------------------------------\n" );
892 /* analyze each lump */
893 for( i = 0; i < 100; i++ )
895 /* call of duty swapped lump pairs */
898 offset = LittleLong( header->lumps[ i ].length );
899 length = LittleLong( header->lumps[ i ].offset );
902 /* standard lump pairs */
905 offset = LittleLong( header->lumps[ i ].offset );
906 length = LittleLong( header->lumps[ i ].length );
910 lump = (byte*) header + offset;
911 lumpInt = LittleLong( (int) *((int*) lump) );
912 lumpFloat = LittleFloat( (float) *((float*) lump) );
913 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
914 lumpString[ 1024 ] = '\0';
916 /* print basic lump info */
917 Sys_Printf( "Lump: %d\n", i );
918 Sys_Printf( "Offset: %d bytes\n", offset );
919 Sys_Printf( "Length: %d bytes\n", length );
921 /* only operate on valid lumps */
924 /* print data in 4 formats */
925 Sys_Printf( "As hex: %08X\n", lumpInt );
926 Sys_Printf( "As int: %d\n", lumpInt );
927 Sys_Printf( "As float: %f\n", lumpFloat );
928 Sys_Printf( "As string: %s\n", lumpString );
930 /* guess lump type */
931 if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
932 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
933 else if( strstr( lumpString, "textures/" ) )
934 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
937 /* guess based on size/count */
938 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
940 if( (length % lumpTest->radix) != 0 )
942 count = length / lumpTest->radix;
943 if( count < lumpTest->minCount )
945 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
950 Sys_Printf( "---------------------------------------\n" );
953 if( offset + length >= size )
958 Sys_Printf( "Lump count: %d\n", i + 1 );
959 Sys_Printf( "File size: %d bytes\n", size );
961 /* return to caller */
969 emits statistics about the bsp file
972 int BSPInfo( int count, char **fileNames )
975 char source[ 1024 ], ext[ 64 ];
983 Sys_Printf( "No files to dump info for.\n");
987 /* enable info mode */
991 for( i = 0; i < count; i++ )
993 Sys_Printf( "---------------------------------\n" );
995 /* mangle filename and get size */
996 strcpy( source, fileNames[ i ] );
997 ExtractFileExtension( source, ext );
998 if( !Q_stricmp( ext, "map" ) )
999 StripExtension( source );
1000 DefaultExtension( source, ".bsp" );
1001 f = fopen( source, "rb" );
1004 size = Q_filelength (f);
1010 /* load the bsp file and print lump sizes */
1011 Sys_Printf( "%s\n", source );
1012 LoadBSPFile( source );
1013 PrintBSPFileSizes();
1017 Sys_Printf( " total %9d\n", size );
1018 Sys_Printf( " %9d KB\n", size / 1024 );
1019 Sys_Printf( " %9d MB\n", size / (1024 * 1024) );
1021 Sys_Printf( "---------------------------------\n" );
1029 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)
1031 vec4_t scoeffs, tcoeffs;
1037 VectorSubtract(bxyz, axyz, dab);
1038 VectorSubtract(cxyz, axyz, dac);
1039 CrossProduct(dab, dac, norm);
1043 // s(v + norm) = s(v) when n ortho xyz
1045 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
1048 // scoeffs * (axyz, 1) == ast[0]
1049 // scoeffs * (bxyz, 1) == bst[0]
1050 // scoeffs * (cxyz, 1) == cst[0]
1051 // scoeffs * (norm, 0) == 0
1052 // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
1053 solvematrix[0] = axyz[0];
1054 solvematrix[4] = axyz[1];
1055 solvematrix[8] = axyz[2];
1056 solvematrix[12] = 1;
1057 solvematrix[1] = bxyz[0];
1058 solvematrix[5] = bxyz[1];
1059 solvematrix[9] = bxyz[2];
1060 solvematrix[13] = 1;
1061 solvematrix[2] = cxyz[0];
1062 solvematrix[6] = cxyz[1];
1063 solvematrix[10] = cxyz[2];
1064 solvematrix[14] = 1;
1065 solvematrix[3] = norm[0];
1066 solvematrix[7] = norm[1];
1067 solvematrix[11] = norm[2];
1068 solvematrix[15] = 0;
1070 md = m4_det(solvematrix);
1073 Sys_Printf("Cannot invert some matrix, some texcoords aren't extrapolated!");
1077 m4x4_invert(solvematrix);
1079 scoeffs[0] = ast[0];
1080 scoeffs[1] = bst[0];
1081 scoeffs[2] = cst[0];
1083 m4x4_transform_vec4(solvematrix, scoeffs);
1084 tcoeffs[0] = ast[1];
1085 tcoeffs[1] = bst[1];
1086 tcoeffs[2] = cst[1];
1088 m4x4_transform_vec4(solvematrix, tcoeffs);
1090 ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
1091 ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
1092 bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
1093 bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
1094 cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
1095 cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
1100 amaze and confuse your enemies with wierd scaled maps!
1103 int ScaleBSPMain( int argc, char **argv )
1112 float *old_xyzst = NULL;
1118 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1123 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
1125 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
1127 scale[0] = atof( argv[ argc - 4 ] );
1129 texscale = !strcmp(argv[1], "-tex");
1131 uniform = ((scale[0] == scale[1]) && (scale[1] == scale[2]));
1133 if( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f )
1135 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1136 Sys_Printf( "Non-zero scale value required.\n" );
1140 /* do some path mangling */
1141 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
1142 StripExtension( source );
1143 DefaultExtension( source, ".bsp" );
1146 Sys_Printf( "Loading %s\n", source );
1147 LoadBSPFile( source );
1151 Sys_Printf( "--- ScaleBSP ---\n" );
1152 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
1154 /* scale entity keys */
1155 for( i = 0; i < numBSPEntities && i < numEntities; i++ )
1158 GetVectorForKey( &entities[ i ], "origin", vec );
1159 if( (vec[ 0 ] || vec[ 1 ] || vec[ 2 ]) )
1164 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1165 SetKeyValue( &entities[ i ], "origin", str );
1168 a = FloatForKey( &entities[ i ], "angle" );
1169 if(a == -1 || a == -2) // z scale
1171 else if(fabs(sin(DEG2RAD(a))) < 0.707)
1176 /* scale door lip */
1177 f = FloatForKey( &entities[ i ], "lip" );
1181 sprintf( str, "%f", f );
1182 SetKeyValue( &entities[ i ], "lip", str );
1185 /* scale plat height */
1186 f = FloatForKey( &entities[ i ], "height" );
1190 sprintf( str, "%f", f );
1191 SetKeyValue( &entities[ i ], "height", str );
1194 // TODO maybe allow a definition file for entities to specify which values are scaled how?
1198 for( i = 0; i < numBSPModels; i++ )
1200 bspModels[ i ].mins[0] *= scale[0];
1201 bspModels[ i ].mins[1] *= scale[1];
1202 bspModels[ i ].mins[2] *= scale[2];
1203 bspModels[ i ].maxs[0] *= scale[0];
1204 bspModels[ i ].maxs[1] *= scale[1];
1205 bspModels[ i ].maxs[2] *= scale[2];
1209 for( i = 0; i < numBSPNodes; i++ )
1211 bspNodes[ i ].mins[0] *= scale[0];
1212 bspNodes[ i ].mins[1] *= scale[1];
1213 bspNodes[ i ].mins[2] *= scale[2];
1214 bspNodes[ i ].maxs[0] *= scale[0];
1215 bspNodes[ i ].maxs[1] *= scale[1];
1216 bspNodes[ i ].maxs[2] *= scale[2];
1220 for( i = 0; i < numBSPLeafs; i++ )
1222 bspLeafs[ i ].mins[0] *= scale[0];
1223 bspLeafs[ i ].mins[1] *= scale[1];
1224 bspLeafs[ i ].mins[2] *= scale[2];
1225 bspLeafs[ i ].maxs[0] *= scale[0];
1226 bspLeafs[ i ].maxs[1] *= scale[1];
1227 bspLeafs[ i ].maxs[2] *= scale[2];
1232 Sys_Printf("Using texture unlocking (and probably breaking texture alignment a lot)\n");
1233 old_xyzst = safe_malloc(sizeof(*old_xyzst) * numBSPDrawVerts * 5);
1234 for(i = 0; i < numBSPDrawVerts; i++)
1236 old_xyzst[5*i+0] = bspDrawVerts[i].xyz[0];
1237 old_xyzst[5*i+1] = bspDrawVerts[i].xyz[1];
1238 old_xyzst[5*i+2] = bspDrawVerts[i].xyz[2];
1239 old_xyzst[5*i+3] = bspDrawVerts[i].st[0];
1240 old_xyzst[5*i+4] = bspDrawVerts[i].st[1];
1244 /* scale drawverts */
1245 for( i = 0; i < numBSPDrawVerts; i++ )
1247 bspDrawVerts[i].xyz[0] *= scale[0];
1248 bspDrawVerts[i].xyz[1] *= scale[1];
1249 bspDrawVerts[i].xyz[2] *= scale[2];
1250 bspDrawVerts[i].normal[0] /= scale[0];
1251 bspDrawVerts[i].normal[1] /= scale[1];
1252 bspDrawVerts[i].normal[2] /= scale[2];
1253 VectorNormalize(bspDrawVerts[i].normal, bspDrawVerts[i].normal);
1258 for(i = 0; i < numBSPDrawSurfaces; i++)
1260 switch(bspDrawSurfaces[i].surfaceType)
1264 if(bspDrawSurfaces[i].numIndexes % 3)
1265 Error("Not a triangulation!");
1266 for(j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3)
1268 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j+1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j+2] + bspDrawSurfaces[i].firstVert;
1269 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
1270 float *oa = &old_xyzst[ia*5], *ob = &old_xyzst[ib*5], *oc = &old_xyzst[ic*5];
1275 ExtrapolateTexcoords(
1291 for( i = 0; i < numBSPPlanes; i++ )
1293 bspPlanes[ i ].dist *= scale[0];
1298 for( i = 0; i < numBSPPlanes; i++ )
1300 bspPlanes[ i ].normal[0] /= scale[0];
1301 bspPlanes[ i ].normal[1] /= scale[1];
1302 bspPlanes[ i ].normal[2] /= scale[2];
1303 f = 1/VectorLength(bspPlanes[i].normal);
1304 VectorScale(bspPlanes[i].normal, f, bspPlanes[i].normal);
1305 bspPlanes[ i ].dist *= f;
1309 /* scale gridsize */
1310 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1311 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
1312 VectorCopy( gridSize, vec );
1316 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1317 SetKeyValue( &entities[ 0 ], "gridsize", str );
1319 /* inject command line parameters */
1320 InjectCommandLine(argv, 0, argc - 1);
1324 StripExtension( source );
1325 DefaultExtension( source, "_s.bsp" );
1326 Sys_Printf( "Writing %s\n", source );
1327 WriteBSPFile( source );
1329 /* return to sender */
1337 main argument processing function for bsp conversion
1340 int ConvertBSPMain( int argc, char **argv )
1343 int (*convertFunc)( char * );
1344 game_t *convertGame;
1348 convertFunc = ConvertBSPToASE;
1354 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
1358 /* process arguments */
1359 for( i = 1; i < (argc - 1); i++ )
1361 /* -format map|ase|... */
1362 if( !strcmp( argv[ i ], "-format" ) )
1365 if( !Q_stricmp( argv[ i ], "ase" ) )
1366 convertFunc = ConvertBSPToASE;
1367 else if( !Q_stricmp( argv[ i ], "map" ) )
1368 convertFunc = ConvertBSPToMap;
1371 convertGame = GetGame( argv[ i ] );
1372 if( convertGame == NULL )
1373 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
1376 else if( !strcmp( argv[ i ], "-ne" ) )
1378 normalEpsilon = atof( argv[ i + 1 ] );
1380 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
1382 else if( !strcmp( argv[ i ], "-de" ) )
1384 distanceEpsilon = atof( argv[ i + 1 ] );
1386 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
1388 else if( !strcmp( argv[ i ], "-shadersasbitmap" ) )
1389 shadersAsBitmap = qtrue;
1392 /* clean up map name */
1393 strcpy( source, ExpandArg( argv[ i ] ) );
1394 StripExtension( source );
1395 DefaultExtension( source, ".bsp" );
1399 Sys_Printf( "Loading %s\n", source );
1401 /* ydnar: load surface file */
1402 //% LoadSurfaceExtraFile( source );
1404 LoadBSPFile( source );
1406 /* parse bsp entities */
1409 /* bsp format convert? */
1410 if( convertGame != NULL )
1412 /* set global game */
1416 StripExtension( source );
1417 DefaultExtension( source, "_c.bsp" );
1418 Sys_Printf( "Writing %s\n", source );
1419 WriteBSPFile( source );
1421 /* return to sender */
1425 /* normal convert */
1426 return convertFunc( source );
1436 int main( int argc, char **argv )
1442 /* we want consistent 'randomness' */
1446 start = I_FloatTime();
1448 /* this was changed to emit version number over the network */
1449 printf( Q3MAP_VERSION "\n" );
1452 atexit( ExitQ3Map );
1454 /* read general options first */
1455 for( i = 1; i < argc; i++ )
1458 if( !strcmp( argv[ i ], "-connect" ) )
1462 Broadcast_Setup( argv[ i ] );
1467 else if( !strcmp( argv[ i ], "-v" ) )
1477 else if( !strcmp( argv[ i ], "-force" ) )
1483 /* patch subdivisions */
1484 else if( !strcmp( argv[ i ], "-subdivisions" ) )
1488 patchSubdivisions = atoi( argv[ i ] );
1490 if( patchSubdivisions <= 0 )
1491 patchSubdivisions = 1;
1495 else if( !strcmp( argv[ i ], "-threads" ) )
1499 numthreads = atoi( argv[ i ] );
1504 /* init model library */
1506 PicoSetMallocFunc( safe_malloc );
1507 PicoSetFreeFunc( free );
1508 PicoSetPrintFunc( PicoPrintFunc );
1509 PicoSetLoadFileFunc( PicoLoadFileFunc );
1510 PicoSetFreeFileFunc( free );
1512 /* set number of threads */
1515 /* generate sinusoid jitter table */
1516 for( i = 0; i < MAX_JITTERS; i++ )
1518 jitters[ i ] = sin( i * 139.54152147 );
1519 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1522 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1523 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1525 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
1526 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1527 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1528 Sys_Printf( "%s\n", Q3MAP_MOTD );
1530 /* ydnar: new path initialization */
1531 InitPaths( &argc, argv );
1533 /* set game options */
1534 if (!patchSubdivisions)
1535 patchSubdivisions = game->patchSubdivisions;
1537 /* check if we have enough options left to attempt something */
1539 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1542 if( !strcmp( argv[ 1 ], "-fixaas" ) )
1543 r = FixAAS( argc - 1, argv + 1 );
1546 else if( !strcmp( argv[ 1 ], "-analyze" ) )
1547 r = AnalyzeBSP( argc - 1, argv + 1 );
1550 else if( !strcmp( argv[ 1 ], "-info" ) )
1551 r = BSPInfo( argc - 2, argv + 2 );
1554 else if( !strcmp( argv[ 1 ], "-vis" ) )
1555 r = VisMain( argc - 1, argv + 1 );
1558 else if( !strcmp( argv[ 1 ], "-light" ) )
1559 r = LightMain( argc - 1, argv + 1 );
1562 else if( !strcmp( argv[ 1 ], "-vlight" ) )
1564 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1565 argv[ 1 ] = "-fast"; /* eek a hack */
1566 r = LightMain( argc, argv );
1569 /* ydnar: lightmap export */
1570 else if( !strcmp( argv[ 1 ], "-export" ) )
1571 r = ExportLightmapsMain( argc - 1, argv + 1 );
1573 /* ydnar: lightmap import */
1574 else if( !strcmp( argv[ 1 ], "-import" ) )
1575 r = ImportLightmapsMain( argc - 1, argv + 1 );
1577 /* ydnar: bsp scaling */
1578 else if( !strcmp( argv[ 1 ], "-scale" ) )
1579 r = ScaleBSPMain( argc - 1, argv + 1 );
1581 /* ydnar: bsp conversion */
1582 else if( !strcmp( argv[ 1 ], "-convert" ) )
1583 r = ConvertBSPMain( argc - 1, argv + 1 );
1586 else if( !strcmp( argv[ 1 ], "-minimap" ) )
1587 r = MiniMapBSPMain(argc - 1, argv + 1);
1589 /* ydnar: otherwise create a bsp */
1591 r = BSPMain( argc, argv );
1594 end = I_FloatTime();
1595 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1597 /* shut down connection */
1598 Broadcast_Shutdown();
1600 /* return any error code */