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)
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]);
305 VectorSubtract(maxs, mins, extend);
307 if(extend[1] > extend[0])
309 mins[0] -= (extend[1] - extend[0]) * 0.5;
310 maxs[0] += (extend[1] - extend[0]) * 0.5;
314 mins[1] -= (extend[0] - extend[1]) * 0.5;
315 maxs[1] += (extend[0] - extend[1]) * 0.5;
318 /* border: amount of black area around the image */
319 /* input: border, 1-2*border, border but we need border/(1-2*border) */
321 VectorSubtract(maxs, mins, extend);
322 VectorScale(extend, border / (1 - 2 * border), extend);
324 VectorSubtract(mins, extend, mins);
325 VectorAdd(maxs, extend, maxs);
327 VectorCopy(mins, minimap.mins);
328 VectorSubtract(maxs, mins, minimap.size);
330 // line compatible to nexuiz mapinfo
331 Sys_Printf("size_texcoords %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
335 MiniMapSetupBrushes()
336 determines solid non-sky brushes in the world
339 void MiniMapSetupBrushes( void )
341 int i, b, compileFlags;
348 Sys_FPrintf( SYS_VRB, "--- MiniMapSetupBrushes ---\n" );
351 if( opaqueBrushes == NULL )
352 opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 );
355 memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 );
356 numOpaqueBrushes = 0;
358 /* walk the list of worldspawn brushes */
359 for( i = 0; i < minimap.model->numBSPBrushes; i++ )
362 b = minimap.model->firstBSPBrush + i;
363 brush = &bspBrushes[ b ];
366 /* check all sides */
368 for( j = 0; j < brush->numSides; j++ )
370 /* do bsp shader calculations */
371 side = &bspBrushSides[ brush->firstSide + j ];
372 shader = &bspShaders[ side->shaderNum ];
374 /* get shader info */
375 si = ShaderInfoForShader( shader->shader );
379 /* or together compile flags */
380 compileFlags |= si->compileFlags;
383 shader = &bspShaders[ brush->shaderNum ];
384 si = ShaderInfoForShader( shader->shader );
388 compileFlags = si->compileFlags;
391 /* determine if this brush is solid */
392 if( (compileFlags & (C_SOLID | C_SKY)) == C_SOLID )
394 opaqueBrushes[ b >> 3 ] |= (1 << (b & 7));
400 /* emit some statistics */
401 Sys_FPrintf( SYS_VRB, "%9d solid brushes\n", numOpaqueBrushes );
404 qboolean MiniMapEvaluateSampleOffsets(int *bestj, int *bestk, float *bestval)
409 *bestj = *bestk = -1;
410 *bestval = 3; /* max possible val is 2 */
412 for(j = 0; j < minimap.samples; ++j)
413 for(k = j + 1; k < minimap.samples; ++k)
415 dx = minimap.sample_offsets[2*j+0] - minimap.sample_offsets[2*k+0];
416 dy = minimap.sample_offsets[2*j+1] - minimap.sample_offsets[2*k+1];
417 if(dx > +0.5) dx -= 1;
418 if(dx < -0.5) dx += 1;
419 if(dy > +0.5) dy -= 1;
420 if(dy < -0.5) dy += 1;
421 val = dx * dx + dy * dy;
433 void MiniMapMakeSampleOffsets()
436 float val, valj, valk, sx, sy, rx, ry;
438 Sys_Printf( "Generating good sample offsets (this may take a while)...\n" );
440 /* start with entirely random samples */
441 for(i = 0; i < minimap.samples; ++i)
443 minimap.sample_offsets[2*i+0] = Random();
444 minimap.sample_offsets[2*i+1] = Random();
447 for(i = 0; i < 1000; ++i)
449 if(MiniMapEvaluateSampleOffsets(&j, &k, &val))
451 sx = minimap.sample_offsets[2*j+0];
452 sy = minimap.sample_offsets[2*j+1];
453 minimap.sample_offsets[2*j+0] = rx = Random();
454 minimap.sample_offsets[2*j+1] = ry = Random();
455 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valj))
457 minimap.sample_offsets[2*j+0] = sx;
458 minimap.sample_offsets[2*j+1] = sy;
460 sx = minimap.sample_offsets[2*k+0];
461 sy = minimap.sample_offsets[2*k+1];
462 minimap.sample_offsets[2*k+0] = rx;
463 minimap.sample_offsets[2*k+1] = ry;
464 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valk))
466 minimap.sample_offsets[2*k+0] = sx;
467 minimap.sample_offsets[2*k+1] = sy;
473 /* valj is the greatest */
474 minimap.sample_offsets[2*j+0] = rx;
475 minimap.sample_offsets[2*j+1] = ry;
480 /* valj is the greater and it is useless - forget it */
487 /* valk is the greatest */
488 minimap.sample_offsets[2*k+0] = rx;
489 minimap.sample_offsets[2*k+1] = ry;
494 /* valk is the greater and it is useless - forget it */
503 void MergeRelativePath(char *out, const char *absolute, const char *relative)
505 const char *endpos = absolute + strlen(absolute);
506 while(endpos != absolute && (endpos[-1] == '/' || endpos[-1] == '\\'))
508 while(relative[0] == '.' && relative[1] == '.' && (relative[2] == '/' || relative[2] == '\\'))
511 while(endpos != absolute)
514 if(*endpos == '/' || *endpos == '\\')
517 while(endpos != absolute && (endpos[-1] == '/' || endpos[-1] == '\\'))
520 memcpy(out, absolute, endpos - absolute);
521 out[endpos - absolute] = '/';
522 strcpy(out + (endpos - absolute + 1), relative);
525 int MiniMapBSPMain( int argc, char **argv )
527 char minimapFilename[1024];
530 char relativeMinimapFilename[1024];
531 float minimapSharpen;
542 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" );
546 /* load the BSP first */
547 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
548 StripExtension( source );
549 DefaultExtension( source, ".bsp" );
550 Sys_Printf( "Loading %s\n", source );
551 BeginMapShaderFile( source );
553 LoadBSPFile( source );
555 minimap.model = &bspModels[0];
556 VectorCopy(minimap.model->mins, mins);
557 VectorCopy(minimap.model->maxs, maxs);
559 *minimapFilename = 0;
560 minimapSharpen = game->miniMapSharpen;
561 minimap.width = minimap.height = game->miniMapSize;
562 border = game->miniMapBorder;
565 minimap.sample_offsets = NULL;
567 /* process arguments */
568 for( i = 1; i < (argc - 1); i++ )
570 if( !strcmp( argv[ i ], "-size" ) )
572 minimap.width = minimap.height = atoi(argv[i + 1]);
574 Sys_Printf( "Image size set to %i\n", minimap.width );
576 else if( !strcmp( argv[ i ], "-sharpen" ) )
578 minimapSharpen = atof(argv[i + 1]);
580 Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
582 else if( !strcmp( argv[ i ], "-samples" ) )
584 minimap.samples = atoi(argv[i + 1]);
586 Sys_Printf( "Samples set to %i\n", minimap.samples );
587 if(minimap.sample_offsets)
588 free(minimap.sample_offsets);
589 minimap.sample_offsets = malloc(2 * sizeof(*minimap.sample_offsets) * minimap.samples);
590 MiniMapMakeSampleOffsets();
592 else if( !strcmp( argv[ i ], "-random" ) )
594 minimap.samples = atoi(argv[i + 1]);
596 Sys_Printf( "Random samples set to %i\n", minimap.samples );
597 if(minimap.sample_offsets)
598 free(minimap.sample_offsets);
599 minimap.sample_offsets = NULL;
601 else if( !strcmp( argv[ i ], "-border" ) )
603 border = atof(argv[i + 1]);
605 Sys_Printf( "Border set to %f\n", border );
607 else if( !strcmp( argv[ i ], "-o" ) )
609 strcpy(minimapFilename, argv[i + 1]);
611 Sys_Printf( "Output file name set to %s\n", minimapFilename );
613 else if( !strcmp( argv[ i ], "-minmax" ) && i < (argc - 7) )
615 mins[0] = atof(argv[i + 1]);
616 mins[1] = atof(argv[i + 2]);
617 mins[2] = atof(argv[i + 3]);
618 maxs[0] = atof(argv[i + 4]);
619 maxs[1] = atof(argv[i + 5]);
620 maxs[2] = atof(argv[i + 6]);
622 Sys_Printf( "Map mins/maxs overridden\n" );
626 MiniMapMakeMinsMaxs(mins, maxs, border);
628 if(!*minimapFilename)
630 ExtractFileBase(source, basename);
631 ExtractFilePath(source, path);
632 sprintf(relativeMinimapFilename, game->miniMapNameFormat, basename);
633 MergeRelativePath(minimapFilename, path, relativeMinimapFilename);
634 Sys_Printf("Output file name automatically set to %s\n", minimapFilename);
637 if(minimapSharpen >= 0)
639 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
640 minimap.sharpen_boxmult = -minimapSharpen;
643 minimap.data1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
644 data3b = safe_malloc(minimap.width * minimap.height * 3);
645 if(minimapSharpen >= 0)
646 minimap.sharpendata1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
648 MiniMapSetupBrushes();
650 if(minimap.samples <= 1)
652 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
653 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapNoSupersampling);
657 if(minimap.sample_offsets)
659 Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
660 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSupersampled);
664 Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
665 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapRandomlySupersampled);
669 if(minimap.sharpendata1f)
671 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
672 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSharpen);
673 q = minimap.sharpendata1f;
680 Sys_Printf( "\nConverting...");
682 for(y = 0; y < minimap.height; ++y)
683 for(x = 0; x < minimap.width; ++x)
688 if(v > 255.0/256.0) v = 255.0/256.0;
695 Sys_Printf( " writing to %s...", minimapFilename );
696 WriteTGA24(minimapFilename, data3b, minimap.width, minimap.height, qfalse);
698 Sys_Printf( " done.\n" );
700 /* return to sender */
710 calculates an md4 checksum for a block of data
713 static int MD4BlockChecksum( void *buffer, int length )
715 return Com_BlockChecksum(buffer, length);
720 resets an aas checksum to match the given BSP
723 int FixAAS( int argc, char **argv )
725 int length, checksum;
728 char aas[ 1024 ], **ext;
741 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
745 /* do some path mangling */
746 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
747 StripExtension( source );
748 DefaultExtension( source, ".bsp" );
751 Sys_Printf( "--- FixAAS ---\n" );
754 Sys_Printf( "Loading %s\n", source );
755 length = LoadFile( source, &buffer );
757 /* create bsp checksum */
758 Sys_Printf( "Creating checksum...\n" );
759 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
761 /* write checksum to aas */
766 strcpy( aas, source );
767 StripExtension( aas );
769 Sys_Printf( "Trying %s\n", aas );
773 file = fopen( aas, "r+b" );
776 if( fwrite( &checksum, 4, 1, file ) != 1 )
777 Error( "Error writing checksum to %s", aas );
781 /* return to sender */
789 analyzes a Quake engine BSP file
792 typedef struct abspHeader_s
797 bspLump_t lumps[ 1 ]; /* unknown size */
801 typedef struct abspLumpTest_s
808 int AnalyzeBSP( int argc, char **argv )
810 abspHeader_t *header;
811 int size, i, version, offset, length, lumpInt, count;
815 char lumpString[ 1024 ], source[ 1024 ];
816 qboolean lumpSwap = qfalse;
817 abspLumpTest_t *lumpTest;
818 static abspLumpTest_t lumpTests[] =
820 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
821 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
822 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
823 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
824 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
825 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
826 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
827 { 104, 3, "IBSP LUMP_DRAWSURFS" },
828 { 44, 3, "IBSP LUMP_DRAWVERTS" },
829 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
830 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
831 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
832 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
840 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
844 /* process arguments */
845 for( i = 1; i < (argc - 1); i++ )
847 /* -format map|ase|... */
848 if( !strcmp( argv[ i ], "-lumpswap" ) )
850 Sys_Printf( "Swapped lump structs enabled\n" );
855 /* clean up map name */
856 strcpy( source, ExpandArg( argv[ i ] ) );
857 Sys_Printf( "Loading %s\n", source );
860 size = LoadFile( source, (void**) &header );
861 if( size == 0 || header == NULL )
863 Sys_Printf( "Unable to load %s.\n", source );
867 /* analyze ident/version */
868 memcpy( ident, header->ident, 4 );
870 version = LittleLong( header->version );
872 Sys_Printf( "Identity: %s\n", ident );
873 Sys_Printf( "Version: %d\n", version );
874 Sys_Printf( "---------------------------------------\n" );
876 /* analyze each lump */
877 for( i = 0; i < 100; i++ )
879 /* call of duty swapped lump pairs */
882 offset = LittleLong( header->lumps[ i ].length );
883 length = LittleLong( header->lumps[ i ].offset );
886 /* standard lump pairs */
889 offset = LittleLong( header->lumps[ i ].offset );
890 length = LittleLong( header->lumps[ i ].length );
894 lump = (byte*) header + offset;
895 lumpInt = LittleLong( (int) *((int*) lump) );
896 lumpFloat = LittleFloat( (float) *((float*) lump) );
897 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
898 lumpString[ 1024 ] = '\0';
900 /* print basic lump info */
901 Sys_Printf( "Lump: %d\n", i );
902 Sys_Printf( "Offset: %d bytes\n", offset );
903 Sys_Printf( "Length: %d bytes\n", length );
905 /* only operate on valid lumps */
908 /* print data in 4 formats */
909 Sys_Printf( "As hex: %08X\n", lumpInt );
910 Sys_Printf( "As int: %d\n", lumpInt );
911 Sys_Printf( "As float: %f\n", lumpFloat );
912 Sys_Printf( "As string: %s\n", lumpString );
914 /* guess lump type */
915 if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
916 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
917 else if( strstr( lumpString, "textures/" ) )
918 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
921 /* guess based on size/count */
922 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
924 if( (length % lumpTest->radix) != 0 )
926 count = length / lumpTest->radix;
927 if( count < lumpTest->minCount )
929 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
934 Sys_Printf( "---------------------------------------\n" );
937 if( offset + length >= size )
942 Sys_Printf( "Lump count: %d\n", i + 1 );
943 Sys_Printf( "File size: %d bytes\n", size );
945 /* return to caller */
953 emits statistics about the bsp file
956 int BSPInfo( int count, char **fileNames )
959 char source[ 1024 ], ext[ 64 ];
967 Sys_Printf( "No files to dump info for.\n");
971 /* enable info mode */
975 for( i = 0; i < count; i++ )
977 Sys_Printf( "---------------------------------\n" );
979 /* mangle filename and get size */
980 strcpy( source, fileNames[ i ] );
981 ExtractFileExtension( source, ext );
982 if( !Q_stricmp( ext, "map" ) )
983 StripExtension( source );
984 DefaultExtension( source, ".bsp" );
985 f = fopen( source, "rb" );
988 size = Q_filelength (f);
994 /* load the bsp file and print lump sizes */
995 Sys_Printf( "%s\n", source );
996 LoadBSPFile( source );
1001 Sys_Printf( " total %9d\n", size );
1002 Sys_Printf( " %9d KB\n", size / 1024 );
1003 Sys_Printf( " %9d MB\n", size / (1024 * 1024) );
1005 Sys_Printf( "---------------------------------\n" );
1013 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)
1015 vec4_t scoeffs, tcoeffs;
1021 VectorSubtract(bxyz, axyz, dab);
1022 VectorSubtract(cxyz, axyz, dac);
1023 CrossProduct(dab, dac, norm);
1027 // s(v + norm) = s(v) when n ortho xyz
1029 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
1032 // scoeffs * (axyz, 1) == ast[0]
1033 // scoeffs * (bxyz, 1) == bst[0]
1034 // scoeffs * (cxyz, 1) == cst[0]
1035 // scoeffs * (norm, 0) == 0
1036 // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
1037 solvematrix[0] = axyz[0];
1038 solvematrix[4] = axyz[1];
1039 solvematrix[8] = axyz[2];
1040 solvematrix[12] = 1;
1041 solvematrix[1] = bxyz[0];
1042 solvematrix[5] = bxyz[1];
1043 solvematrix[9] = bxyz[2];
1044 solvematrix[13] = 1;
1045 solvematrix[2] = cxyz[0];
1046 solvematrix[6] = cxyz[1];
1047 solvematrix[10] = cxyz[2];
1048 solvematrix[14] = 1;
1049 solvematrix[3] = norm[0];
1050 solvematrix[7] = norm[1];
1051 solvematrix[11] = norm[2];
1052 solvematrix[15] = 0;
1054 md = m4_det(solvematrix);
1057 Sys_Printf("Cannot invert some matrix, some texcoords aren't extrapolated!");
1061 m4x4_invert(solvematrix);
1063 scoeffs[0] = ast[0];
1064 scoeffs[1] = bst[0];
1065 scoeffs[2] = cst[0];
1067 m4x4_transform_vec4(solvematrix, scoeffs);
1068 tcoeffs[0] = ast[1];
1069 tcoeffs[1] = bst[1];
1070 tcoeffs[2] = cst[1];
1072 m4x4_transform_vec4(solvematrix, tcoeffs);
1074 ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
1075 ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
1076 bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
1077 bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
1078 cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
1079 cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
1084 amaze and confuse your enemies with wierd scaled maps!
1087 int ScaleBSPMain( int argc, char **argv )
1096 float *old_xyzst = NULL;
1102 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1107 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
1109 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
1111 scale[0] = atof( argv[ argc - 4 ] );
1113 texscale = !strcmp(argv[1], "-tex");
1115 uniform = ((scale[0] == scale[1]) && (scale[1] == scale[2]));
1117 if( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f )
1119 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1120 Sys_Printf( "Non-zero scale value required.\n" );
1124 /* do some path mangling */
1125 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
1126 StripExtension( source );
1127 DefaultExtension( source, ".bsp" );
1130 Sys_Printf( "Loading %s\n", source );
1131 LoadBSPFile( source );
1135 Sys_Printf( "--- ScaleBSP ---\n" );
1136 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
1138 /* scale entity keys */
1139 for( i = 0; i < numBSPEntities && i < numEntities; i++ )
1142 GetVectorForKey( &entities[ i ], "origin", vec );
1143 if( (vec[ 0 ] || vec[ 1 ] || vec[ 2 ]) )
1148 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1149 SetKeyValue( &entities[ i ], "origin", str );
1152 a = FloatForKey( &entities[ i ], "angle" );
1153 if(a == -1 || a == -2) // z scale
1155 else if(fabs(sin(DEG2RAD(a))) < 0.707)
1160 /* scale door lip */
1161 f = FloatForKey( &entities[ i ], "lip" );
1165 sprintf( str, "%f", f );
1166 SetKeyValue( &entities[ i ], "lip", str );
1169 /* scale plat height */
1170 f = FloatForKey( &entities[ i ], "height" );
1174 sprintf( str, "%f", f );
1175 SetKeyValue( &entities[ i ], "height", str );
1178 // TODO maybe allow a definition file for entities to specify which values are scaled how?
1182 for( i = 0; i < numBSPModels; i++ )
1184 bspModels[ i ].mins[0] *= scale[0];
1185 bspModels[ i ].mins[1] *= scale[1];
1186 bspModels[ i ].mins[2] *= scale[2];
1187 bspModels[ i ].maxs[0] *= scale[0];
1188 bspModels[ i ].maxs[1] *= scale[1];
1189 bspModels[ i ].maxs[2] *= scale[2];
1193 for( i = 0; i < numBSPNodes; i++ )
1195 bspNodes[ i ].mins[0] *= scale[0];
1196 bspNodes[ i ].mins[1] *= scale[1];
1197 bspNodes[ i ].mins[2] *= scale[2];
1198 bspNodes[ i ].maxs[0] *= scale[0];
1199 bspNodes[ i ].maxs[1] *= scale[1];
1200 bspNodes[ i ].maxs[2] *= scale[2];
1204 for( i = 0; i < numBSPLeafs; i++ )
1206 bspLeafs[ i ].mins[0] *= scale[0];
1207 bspLeafs[ i ].mins[1] *= scale[1];
1208 bspLeafs[ i ].mins[2] *= scale[2];
1209 bspLeafs[ i ].maxs[0] *= scale[0];
1210 bspLeafs[ i ].maxs[1] *= scale[1];
1211 bspLeafs[ i ].maxs[2] *= scale[2];
1216 Sys_Printf("Using texture unlocking (and probably breaking texture alignment a lot)\n");
1217 old_xyzst = safe_malloc(sizeof(*old_xyzst) * numBSPDrawVerts * 5);
1218 for(i = 0; i < numBSPDrawVerts; i++)
1220 old_xyzst[5*i+0] = bspDrawVerts[i].xyz[0];
1221 old_xyzst[5*i+1] = bspDrawVerts[i].xyz[1];
1222 old_xyzst[5*i+2] = bspDrawVerts[i].xyz[2];
1223 old_xyzst[5*i+3] = bspDrawVerts[i].st[0];
1224 old_xyzst[5*i+4] = bspDrawVerts[i].st[1];
1228 /* scale drawverts */
1229 for( i = 0; i < numBSPDrawVerts; i++ )
1231 bspDrawVerts[i].xyz[0] *= scale[0];
1232 bspDrawVerts[i].xyz[1] *= scale[1];
1233 bspDrawVerts[i].xyz[2] *= scale[2];
1234 bspDrawVerts[i].normal[0] /= scale[0];
1235 bspDrawVerts[i].normal[1] /= scale[1];
1236 bspDrawVerts[i].normal[2] /= scale[2];
1237 VectorNormalize(bspDrawVerts[i].normal, bspDrawVerts[i].normal);
1242 for(i = 0; i < numBSPDrawSurfaces; i++)
1244 switch(bspDrawSurfaces[i].surfaceType)
1248 if(bspDrawSurfaces[i].numIndexes % 3)
1249 Error("Not a triangulation!");
1250 for(j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3)
1252 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j+1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j+2] + bspDrawSurfaces[i].firstVert;
1253 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
1254 float *oa = &old_xyzst[ia*5], *ob = &old_xyzst[ib*5], *oc = &old_xyzst[ic*5];
1259 ExtrapolateTexcoords(
1275 for( i = 0; i < numBSPPlanes; i++ )
1277 bspPlanes[ i ].dist *= scale[0];
1282 for( i = 0; i < numBSPPlanes; i++ )
1284 bspPlanes[ i ].normal[0] /= scale[0];
1285 bspPlanes[ i ].normal[1] /= scale[1];
1286 bspPlanes[ i ].normal[2] /= scale[2];
1287 f = 1/VectorLength(bspPlanes[i].normal);
1288 VectorScale(bspPlanes[i].normal, f, bspPlanes[i].normal);
1289 bspPlanes[ i ].dist *= f;
1293 /* scale gridsize */
1294 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1295 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
1296 VectorCopy( gridSize, vec );
1300 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1301 SetKeyValue( &entities[ 0 ], "gridsize", str );
1303 /* inject command line parameters */
1304 InjectCommandLine(argv, 0, argc - 1);
1308 StripExtension( source );
1309 DefaultExtension( source, "_s.bsp" );
1310 Sys_Printf( "Writing %s\n", source );
1311 WriteBSPFile( source );
1313 /* return to sender */
1321 main argument processing function for bsp conversion
1324 int ConvertBSPMain( int argc, char **argv )
1327 int (*convertFunc)( char * );
1328 game_t *convertGame;
1332 convertFunc = ConvertBSPToASE;
1338 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
1342 /* process arguments */
1343 for( i = 1; i < (argc - 1); i++ )
1345 /* -format map|ase|... */
1346 if( !strcmp( argv[ i ], "-format" ) )
1349 if( !Q_stricmp( argv[ i ], "ase" ) )
1350 convertFunc = ConvertBSPToASE;
1351 else if( !Q_stricmp( argv[ i ], "map" ) )
1352 convertFunc = ConvertBSPToMap;
1355 convertGame = GetGame( argv[ i ] );
1356 if( convertGame == NULL )
1357 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
1360 else if( !strcmp( argv[ i ], "-ne" ) )
1362 normalEpsilon = atof( argv[ i + 1 ] );
1364 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
1366 else if( !strcmp( argv[ i ], "-de" ) )
1368 distanceEpsilon = atof( argv[ i + 1 ] );
1370 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
1372 else if( !strcmp( argv[ i ], "-shadersasbitmap" ) )
1373 shadersAsBitmap = qtrue;
1376 /* clean up map name */
1377 strcpy( source, ExpandArg( argv[ i ] ) );
1378 StripExtension( source );
1379 DefaultExtension( source, ".bsp" );
1383 Sys_Printf( "Loading %s\n", source );
1385 /* ydnar: load surface file */
1386 //% LoadSurfaceExtraFile( source );
1388 LoadBSPFile( source );
1390 /* parse bsp entities */
1393 /* bsp format convert? */
1394 if( convertGame != NULL )
1396 /* set global game */
1400 StripExtension( source );
1401 DefaultExtension( source, "_c.bsp" );
1402 Sys_Printf( "Writing %s\n", source );
1403 WriteBSPFile( source );
1405 /* return to sender */
1409 /* normal convert */
1410 return convertFunc( source );
1420 int main( int argc, char **argv )
1426 /* we want consistent 'randomness' */
1430 start = I_FloatTime();
1432 /* this was changed to emit version number over the network */
1433 printf( Q3MAP_VERSION "\n" );
1436 atexit( ExitQ3Map );
1438 /* read general options first */
1439 for( i = 1; i < argc; i++ )
1442 if( !strcmp( argv[ i ], "-connect" ) )
1446 Broadcast_Setup( argv[ i ] );
1451 else if( !strcmp( argv[ i ], "-v" ) )
1461 else if( !strcmp( argv[ i ], "-force" ) )
1467 /* patch subdivisions */
1468 else if( !strcmp( argv[ i ], "-subdivisions" ) )
1472 patchSubdivisions = atoi( argv[ i ] );
1474 if( patchSubdivisions <= 0 )
1475 patchSubdivisions = 1;
1479 else if( !strcmp( argv[ i ], "-threads" ) )
1483 numthreads = atoi( argv[ i ] );
1488 /* init model library */
1490 PicoSetMallocFunc( safe_malloc );
1491 PicoSetFreeFunc( free );
1492 PicoSetPrintFunc( PicoPrintFunc );
1493 PicoSetLoadFileFunc( PicoLoadFileFunc );
1494 PicoSetFreeFileFunc( free );
1496 /* set number of threads */
1499 /* generate sinusoid jitter table */
1500 for( i = 0; i < MAX_JITTERS; i++ )
1502 jitters[ i ] = sin( i * 139.54152147 );
1503 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1506 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1507 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1509 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
1510 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1511 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1512 Sys_Printf( "%s\n", Q3MAP_MOTD );
1514 /* ydnar: new path initialization */
1515 InitPaths( &argc, argv );
1517 /* set game options */
1518 if (!patchSubdivisions)
1519 patchSubdivisions = game->patchSubdivisions;
1521 /* check if we have enough options left to attempt something */
1523 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1526 if( !strcmp( argv[ 1 ], "-fixaas" ) )
1527 r = FixAAS( argc - 1, argv + 1 );
1530 else if( !strcmp( argv[ 1 ], "-analyze" ) )
1531 r = AnalyzeBSP( argc - 1, argv + 1 );
1534 else if( !strcmp( argv[ 1 ], "-info" ) )
1535 r = BSPInfo( argc - 2, argv + 2 );
1538 else if( !strcmp( argv[ 1 ], "-vis" ) )
1539 r = VisMain( argc - 1, argv + 1 );
1542 else if( !strcmp( argv[ 1 ], "-light" ) )
1543 r = LightMain( argc - 1, argv + 1 );
1546 else if( !strcmp( argv[ 1 ], "-vlight" ) )
1548 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1549 argv[ 1 ] = "-fast"; /* eek a hack */
1550 r = LightMain( argc, argv );
1553 /* ydnar: lightmap export */
1554 else if( !strcmp( argv[ 1 ], "-export" ) )
1555 r = ExportLightmapsMain( argc - 1, argv + 1 );
1557 /* ydnar: lightmap import */
1558 else if( !strcmp( argv[ 1 ], "-import" ) )
1559 r = ImportLightmapsMain( argc - 1, argv + 1 );
1561 /* ydnar: bsp scaling */
1562 else if( !strcmp( argv[ 1 ], "-scale" ) )
1563 r = ScaleBSPMain( argc - 1, argv + 1 );
1565 /* ydnar: bsp conversion */
1566 else if( !strcmp( argv[ 1 ], "-convert" ) )
1567 r = ConvertBSPMain( argc - 1, argv + 1 );
1570 else if( !strcmp( argv[ 1 ], "-minimap" ) )
1571 r = MiniMapBSPMain(argc - 1, argv + 1);
1573 /* ydnar: otherwise create a bsp */
1575 r = BSPMain( argc, argv );
1578 end = I_FloatTime();
1579 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1581 /* shut down connection */
1582 Broadcast_Shutdown();
1584 /* return any error code */