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)
151 for(i = 0; i < minimap.model->numBSPBrushes; ++i)
153 bi = minimap.model->firstBSPBrush + i;
154 if(opaqueBrushes[bi >> 3] & (1 << (bi & 7)))
158 // sort out mins/maxs of the brush
159 bspBrushSide_t *s = &bspBrushSides[b->firstSide];
160 if(x < -bspPlanes[s[0].planeNum].dist)
162 if(x > +bspPlanes[s[1].planeNum].dist)
164 if(y < -bspPlanes[s[2].planeNum].dist)
166 if(y > +bspPlanes[s[3].planeNum].dist)
169 if(BrushIntersectionWithLine(b, org, dir, &t0, &t1))
180 static void MiniMapRandomlySupersampled(int y)
183 float *p = &minimap.data1f[y * minimap.width];
184 float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
185 float dx = minimap.size[0] / (float) minimap.width;
186 float dy = minimap.size[1] / (float) minimap.height;
188 for(x = 0; x < minimap.width; ++x)
190 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
193 for(i = 0; i < minimap.samples; ++i)
195 float thisval = MiniMapSample(
196 xmin + (2 * Random() - 0.5) * dx, /* exaggerated random pattern for better results */
197 ymin + (2 * Random() - 0.5) * dy /* exaggerated random pattern for better results */
201 val /= minimap.samples * minimap.size[2];
206 static void MiniMapSupersampled(int y)
209 float *p = &minimap.data1f[y * minimap.width];
210 float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
211 float dx = minimap.size[0] / (float) minimap.width;
212 float dy = minimap.size[1] / (float) minimap.height;
214 for(x = 0; x < minimap.width; ++x)
216 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
219 for(i = 0; i < minimap.samples; ++i)
221 float thisval = MiniMapSample(
222 xmin + minimap.sample_offsets[2*i+0] * dx,
223 ymin + minimap.sample_offsets[2*i+1] * dy
227 val /= minimap.samples * minimap.size[2];
232 static void MiniMapNoSupersampling(int y)
235 float *p = &minimap.data1f[y * minimap.width];
236 float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
238 for(x = 0; x < minimap.width; ++x)
240 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
241 *p++ = MiniMapSample(xmin, ymin) / minimap.size[2];
245 static void MiniMapSharpen(int y)
248 qboolean up = (y > 0);
249 qboolean down = (y < minimap.height - 1);
250 float *p = &minimap.data1f[y * minimap.width];
251 float *q = &minimap.sharpendata1f[y * minimap.width];
253 for(x = 0; x < minimap.width; ++x)
255 qboolean left = (x > 0);
256 qboolean right = (x < minimap.width - 1);
257 float val = p[0] * minimap.sharpen_centermult;
260 val += p[-1 -minimap.width] * minimap.sharpen_boxmult;
262 val += p[-1 +minimap.width] * minimap.sharpen_boxmult;
264 val += p[+1 -minimap.width] * minimap.sharpen_boxmult;
266 val += p[+1 +minimap.width] * minimap.sharpen_boxmult;
269 val += p[-1] * minimap.sharpen_boxmult;
271 val += p[+1] * minimap.sharpen_boxmult;
273 val += p[-minimap.width] * minimap.sharpen_boxmult;
275 val += p[+minimap.width] * minimap.sharpen_boxmult;
282 void MiniMapMakeMinsMaxs(vec3_t mins_in, vec3_t maxs_in)
284 vec3_t mins, maxs, extend;
285 VectorCopy(mins_in, mins);
286 VectorCopy(maxs_in, maxs);
288 // line compatible to nexuiz mapinfo
289 Sys_Printf("size %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
291 VectorSubtract(maxs, mins, extend);
293 if(extend[1] > extend[0])
295 mins[0] -= (extend[1] - extend[0]) * 0.5;
296 maxs[0] += (extend[1] - extend[0]) * 0.5;
300 mins[1] -= (extend[0] - extend[1]) * 0.5;
301 maxs[1] += (extend[0] - extend[1]) * 0.5;
304 VectorSubtract(maxs, mins, extend);
305 VectorScale(extend, 1.0 / 64.0, extend);
307 VectorSubtract(mins, extend, mins);
308 VectorAdd(maxs, extend, maxs);
310 VectorCopy(mins, minimap.mins);
311 VectorSubtract(maxs, mins, minimap.size);
313 // line compatible to nexuiz mapinfo
314 Sys_Printf("size_texcoords %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
318 MiniMapSetupBrushes()
319 determines solid non-sky brushes in the world
322 void MiniMapSetupBrushes( void )
324 int i, j, b, compileFlags;
326 bspBrushSide_t *side;
332 Sys_FPrintf( SYS_VRB, "--- MiniMapSetupBrushes ---\n" );
335 if( opaqueBrushes == NULL )
336 opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 );
339 memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 );
340 numOpaqueBrushes = 0;
342 /* walk the list of worldspawn brushes */
343 for( i = 0; i < minimap.model->numBSPBrushes; i++ )
346 b = minimap.model->firstBSPBrush + i;
347 brush = &bspBrushes[ b ];
350 /* check all sides */
352 for( j = 0; j < brush->numSides; j++ )
354 /* do bsp shader calculations */
355 side = &bspBrushSides[ brush->firstSide + j ];
356 shader = &bspShaders[ side->shaderNum ];
358 /* get shader info */
359 si = ShaderInfoForShader( shader->shader );
363 /* or together compile flags */
364 compileFlags |= si->compileFlags;
367 shader = &bspShaders[ brush->shaderNum ];
368 si = ShaderInfoForShader( shader->shader );
372 compileFlags = si->compileFlags;
375 /* determine if this brush is solid */
376 if( (compileFlags & (C_SOLID | C_SKY)) == C_SOLID )
378 opaqueBrushes[ b >> 3 ] |= (1 << (b & 7));
384 /* emit some statistics */
385 Sys_FPrintf( SYS_VRB, "%9d solid brushes\n", numOpaqueBrushes );
388 int MiniMapBSPMain( int argc, char **argv )
390 char minimapFilename[1024];
393 char parentpath[1024];
394 float minimapSharpen;
404 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" );
408 /* load the BSP first */
409 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
410 StripExtension( source );
411 DefaultExtension( source, ".bsp" );
412 Sys_Printf( "Loading %s\n", source );
413 BeginMapShaderFile( source );
415 LoadBSPFile( source );
417 minimap.model = &bspModels[0];
418 VectorCopy(minimap.model->mins, mins);
419 VectorCopy(minimap.model->maxs, maxs);
420 *minimapFilename = 0;
422 minimap.width = minimap.height = 512;
424 minimap.sample_offsets = NULL;
426 /* process arguments */
427 for( i = 1; i < (argc - 1); i++ )
429 if( !strcmp( argv[ i ], "-size" ) )
431 minimap.width = minimap.height = atoi(argv[i + 1]);
433 Sys_Printf( "Image size set to %i\n", minimap.width );
435 else if( !strcmp( argv[ i ], "-sharpen" ) )
437 minimapSharpen = atof(argv[i + 1]);
439 Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
441 else if( !strcmp( argv[ i ], "-samples" ) )
443 minimap.samples = atoi(argv[i + 1]);
445 Sys_Printf( "Samples set to %i\n", minimap.samples );
446 if(minimap.sample_offsets)
447 free(minimap.sample_offsets);
448 minimap.sample_offsets = malloc(2 * sizeof(*minimap.sample_offsets) * minimap.samples);
449 /* TODO use a better pattern */
450 for(x = 0; x < 2 * minimap.samples; ++x)
451 minimap.sample_offsets[x] = Random();
453 else if( !strcmp( argv[ i ], "-random" ) )
455 minimap.samples = atoi(argv[i + 1]);
457 Sys_Printf( "Random samples set to %i\n", minimap.samples );
458 if(minimap.sample_offsets)
459 free(minimap.sample_offsets);
460 minimap.sample_offsets = NULL;
462 else if( !strcmp( argv[ i ], "-o" ) )
464 strcpy(minimapFilename, argv[i + 1]);
466 Sys_Printf( "Output file name set to %s\n", minimapFilename );
468 else if( !strcmp( argv[ i ], "-minmax" ) && i < (argc - 7) )
470 mins[0] = atof(argv[i + 1]);
471 mins[1] = atof(argv[i + 2]);
472 mins[2] = atof(argv[i + 3]);
473 maxs[0] = atof(argv[i + 4]);
474 maxs[1] = atof(argv[i + 5]);
475 maxs[2] = atof(argv[i + 6]);
477 Sys_Printf( "Map mins/maxs overridden\n" );
481 MiniMapMakeMinsMaxs(mins, maxs);
483 if(!*minimapFilename)
485 ExtractFileBase(source, basename);
486 ExtractFilePath(source, path);
488 path[strlen(path)-1] = 0;
489 ExtractFilePath(path, parentpath);
490 sprintf(minimapFilename, "%sgfx", parentpath);
491 Q_mkdir(minimapFilename);
492 sprintf(minimapFilename, "%sgfx/%s_mini.tga", parentpath, basename);
493 Sys_Printf("Output file name automatically set to %s\n", minimapFilename);
496 if(minimapSharpen >= 0)
498 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
499 minimap.sharpen_boxmult = -minimapSharpen;
502 minimap.data1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
503 data3b = safe_malloc(minimap.width * minimap.height * 3);
504 if(minimapSharpen >= 0)
505 minimap.sharpendata1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
507 MiniMapSetupBrushes();
509 if(minimap.samples <= 1)
511 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
512 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapNoSupersampling);
516 if(minimap.sample_offsets)
518 Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
519 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSupersampled);
523 Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
524 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapRandomlySupersampled);
528 if(minimap.sharpendata1f)
530 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
531 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSharpen);
532 q = minimap.sharpendata1f;
539 Sys_Printf( "\nConverting...");
541 for(y = 0; y < minimap.height; ++y)
542 for(x = 0; x < minimap.width; ++x)
547 if(v > 255.0/256.0) v = 255.0/256.0;
554 Sys_Printf( " writing to %s...", minimapFilename );
555 WriteTGA24(minimapFilename, data3b, minimap.width, minimap.height, qfalse);
557 Sys_Printf( " done.\n" );
559 /* return to sender */
569 calculates an md4 checksum for a block of data
572 static int MD4BlockChecksum( void *buffer, int length )
574 return Com_BlockChecksum(buffer, length);
579 resets an aas checksum to match the given BSP
582 int FixAAS( int argc, char **argv )
584 int length, checksum;
587 char aas[ 1024 ], **ext;
600 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
604 /* do some path mangling */
605 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
606 StripExtension( source );
607 DefaultExtension( source, ".bsp" );
610 Sys_Printf( "--- FixAAS ---\n" );
613 Sys_Printf( "Loading %s\n", source );
614 length = LoadFile( source, &buffer );
616 /* create bsp checksum */
617 Sys_Printf( "Creating checksum...\n" );
618 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
620 /* write checksum to aas */
625 strcpy( aas, source );
626 StripExtension( aas );
628 Sys_Printf( "Trying %s\n", aas );
632 file = fopen( aas, "r+b" );
635 if( fwrite( &checksum, 4, 1, file ) != 1 )
636 Error( "Error writing checksum to %s", aas );
640 /* return to sender */
648 analyzes a Quake engine BSP file
651 typedef struct abspHeader_s
656 bspLump_t lumps[ 1 ]; /* unknown size */
660 typedef struct abspLumpTest_s
667 int AnalyzeBSP( int argc, char **argv )
669 abspHeader_t *header;
670 int size, i, version, offset, length, lumpInt, count;
674 char lumpString[ 1024 ], source[ 1024 ];
675 qboolean lumpSwap = qfalse;
676 abspLumpTest_t *lumpTest;
677 static abspLumpTest_t lumpTests[] =
679 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
680 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
681 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
682 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
683 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
684 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
685 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
686 { 104, 3, "IBSP LUMP_DRAWSURFS" },
687 { 44, 3, "IBSP LUMP_DRAWVERTS" },
688 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
689 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
690 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
691 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
699 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
703 /* process arguments */
704 for( i = 1; i < (argc - 1); i++ )
706 /* -format map|ase|... */
707 if( !strcmp( argv[ i ], "-lumpswap" ) )
709 Sys_Printf( "Swapped lump structs enabled\n" );
714 /* clean up map name */
715 strcpy( source, ExpandArg( argv[ i ] ) );
716 Sys_Printf( "Loading %s\n", source );
719 size = LoadFile( source, (void**) &header );
720 if( size == 0 || header == NULL )
722 Sys_Printf( "Unable to load %s.\n", source );
726 /* analyze ident/version */
727 memcpy( ident, header->ident, 4 );
729 version = LittleLong( header->version );
731 Sys_Printf( "Identity: %s\n", ident );
732 Sys_Printf( "Version: %d\n", version );
733 Sys_Printf( "---------------------------------------\n" );
735 /* analyze each lump */
736 for( i = 0; i < 100; i++ )
738 /* call of duty swapped lump pairs */
741 offset = LittleLong( header->lumps[ i ].length );
742 length = LittleLong( header->lumps[ i ].offset );
745 /* standard lump pairs */
748 offset = LittleLong( header->lumps[ i ].offset );
749 length = LittleLong( header->lumps[ i ].length );
753 lump = (byte*) header + offset;
754 lumpInt = LittleLong( (int) *((int*) lump) );
755 lumpFloat = LittleFloat( (float) *((float*) lump) );
756 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
757 lumpString[ 1024 ] = '\0';
759 /* print basic lump info */
760 Sys_Printf( "Lump: %d\n", i );
761 Sys_Printf( "Offset: %d bytes\n", offset );
762 Sys_Printf( "Length: %d bytes\n", length );
764 /* only operate on valid lumps */
767 /* print data in 4 formats */
768 Sys_Printf( "As hex: %08X\n", lumpInt );
769 Sys_Printf( "As int: %d\n", lumpInt );
770 Sys_Printf( "As float: %f\n", lumpFloat );
771 Sys_Printf( "As string: %s\n", lumpString );
773 /* guess lump type */
774 if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
775 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
776 else if( strstr( lumpString, "textures/" ) )
777 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
780 /* guess based on size/count */
781 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
783 if( (length % lumpTest->radix) != 0 )
785 count = length / lumpTest->radix;
786 if( count < lumpTest->minCount )
788 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
793 Sys_Printf( "---------------------------------------\n" );
796 if( offset + length >= size )
801 Sys_Printf( "Lump count: %d\n", i + 1 );
802 Sys_Printf( "File size: %d bytes\n", size );
804 /* return to caller */
812 emits statistics about the bsp file
815 int BSPInfo( int count, char **fileNames )
818 char source[ 1024 ], ext[ 64 ];
826 Sys_Printf( "No files to dump info for.\n");
830 /* enable info mode */
834 for( i = 0; i < count; i++ )
836 Sys_Printf( "---------------------------------\n" );
838 /* mangle filename and get size */
839 strcpy( source, fileNames[ i ] );
840 ExtractFileExtension( source, ext );
841 if( !Q_stricmp( ext, "map" ) )
842 StripExtension( source );
843 DefaultExtension( source, ".bsp" );
844 f = fopen( source, "rb" );
847 size = Q_filelength (f);
853 /* load the bsp file and print lump sizes */
854 Sys_Printf( "%s\n", source );
855 LoadBSPFile( source );
860 Sys_Printf( " total %9d\n", size );
861 Sys_Printf( " %9d KB\n", size / 1024 );
862 Sys_Printf( " %9d MB\n", size / (1024 * 1024) );
864 Sys_Printf( "---------------------------------\n" );
872 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)
874 vec4_t scoeffs, tcoeffs;
880 VectorSubtract(bxyz, axyz, dab);
881 VectorSubtract(cxyz, axyz, dac);
882 CrossProduct(dab, dac, norm);
886 // s(v + norm) = s(v) when n ortho xyz
888 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
891 // scoeffs * (axyz, 1) == ast[0]
892 // scoeffs * (bxyz, 1) == bst[0]
893 // scoeffs * (cxyz, 1) == cst[0]
894 // scoeffs * (norm, 0) == 0
895 // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
896 solvematrix[0] = axyz[0];
897 solvematrix[4] = axyz[1];
898 solvematrix[8] = axyz[2];
900 solvematrix[1] = bxyz[0];
901 solvematrix[5] = bxyz[1];
902 solvematrix[9] = bxyz[2];
904 solvematrix[2] = cxyz[0];
905 solvematrix[6] = cxyz[1];
906 solvematrix[10] = cxyz[2];
908 solvematrix[3] = norm[0];
909 solvematrix[7] = norm[1];
910 solvematrix[11] = norm[2];
913 md = m4_det(solvematrix);
916 Sys_Printf("Cannot invert some matrix, some texcoords aren't extrapolated!");
920 m4x4_invert(solvematrix);
926 m4x4_transform_vec4(solvematrix, scoeffs);
931 m4x4_transform_vec4(solvematrix, tcoeffs);
933 ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
934 ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
935 bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
936 bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
937 cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
938 cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
943 amaze and confuse your enemies with wierd scaled maps!
946 int ScaleBSPMain( int argc, char **argv )
955 float *old_xyzst = NULL;
961 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
966 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
968 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
970 scale[0] = atof( argv[ argc - 4 ] );
972 texscale = !strcmp(argv[1], "-tex");
974 uniform = ((scale[0] == scale[1]) && (scale[1] == scale[2]));
976 if( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f )
978 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
979 Sys_Printf( "Non-zero scale value required.\n" );
983 /* do some path mangling */
984 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
985 StripExtension( source );
986 DefaultExtension( source, ".bsp" );
989 Sys_Printf( "Loading %s\n", source );
990 LoadBSPFile( source );
994 Sys_Printf( "--- ScaleBSP ---\n" );
995 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
997 /* scale entity keys */
998 for( i = 0; i < numBSPEntities && i < numEntities; i++ )
1001 GetVectorForKey( &entities[ i ], "origin", vec );
1002 if( (vec[ 0 ] || vec[ 1 ] || vec[ 2 ]) )
1007 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1008 SetKeyValue( &entities[ i ], "origin", str );
1011 a = FloatForKey( &entities[ i ], "angle" );
1012 if(a == -1 || a == -2) // z scale
1014 else if(fabs(sin(DEG2RAD(a))) < 0.707)
1019 /* scale door lip */
1020 f = FloatForKey( &entities[ i ], "lip" );
1024 sprintf( str, "%f", f );
1025 SetKeyValue( &entities[ i ], "lip", str );
1028 /* scale plat height */
1029 f = FloatForKey( &entities[ i ], "height" );
1033 sprintf( str, "%f", f );
1034 SetKeyValue( &entities[ i ], "height", str );
1037 // TODO maybe allow a definition file for entities to specify which values are scaled how?
1041 for( i = 0; i < numBSPModels; i++ )
1043 bspModels[ i ].mins[0] *= scale[0];
1044 bspModels[ i ].mins[1] *= scale[1];
1045 bspModels[ i ].mins[2] *= scale[2];
1046 bspModels[ i ].maxs[0] *= scale[0];
1047 bspModels[ i ].maxs[1] *= scale[1];
1048 bspModels[ i ].maxs[2] *= scale[2];
1052 for( i = 0; i < numBSPNodes; i++ )
1054 bspNodes[ i ].mins[0] *= scale[0];
1055 bspNodes[ i ].mins[1] *= scale[1];
1056 bspNodes[ i ].mins[2] *= scale[2];
1057 bspNodes[ i ].maxs[0] *= scale[0];
1058 bspNodes[ i ].maxs[1] *= scale[1];
1059 bspNodes[ i ].maxs[2] *= scale[2];
1063 for( i = 0; i < numBSPLeafs; i++ )
1065 bspLeafs[ i ].mins[0] *= scale[0];
1066 bspLeafs[ i ].mins[1] *= scale[1];
1067 bspLeafs[ i ].mins[2] *= scale[2];
1068 bspLeafs[ i ].maxs[0] *= scale[0];
1069 bspLeafs[ i ].maxs[1] *= scale[1];
1070 bspLeafs[ i ].maxs[2] *= scale[2];
1075 Sys_Printf("Using texture unlocking (and probably breaking texture alignment a lot)\n");
1076 old_xyzst = safe_malloc(sizeof(*old_xyzst) * numBSPDrawVerts * 5);
1077 for(i = 0; i < numBSPDrawVerts; i++)
1079 old_xyzst[5*i+0] = bspDrawVerts[i].xyz[0];
1080 old_xyzst[5*i+1] = bspDrawVerts[i].xyz[1];
1081 old_xyzst[5*i+2] = bspDrawVerts[i].xyz[2];
1082 old_xyzst[5*i+3] = bspDrawVerts[i].st[0];
1083 old_xyzst[5*i+4] = bspDrawVerts[i].st[1];
1087 /* scale drawverts */
1088 for( i = 0; i < numBSPDrawVerts; i++ )
1090 bspDrawVerts[i].xyz[0] *= scale[0];
1091 bspDrawVerts[i].xyz[1] *= scale[1];
1092 bspDrawVerts[i].xyz[2] *= scale[2];
1093 bspDrawVerts[i].normal[0] /= scale[0];
1094 bspDrawVerts[i].normal[1] /= scale[1];
1095 bspDrawVerts[i].normal[2] /= scale[2];
1096 VectorNormalize(bspDrawVerts[i].normal, bspDrawVerts[i].normal);
1101 for(i = 0; i < numBSPDrawSurfaces; i++)
1103 switch(bspDrawSurfaces[i].surfaceType)
1107 if(bspDrawSurfaces[i].numIndexes % 3)
1108 Error("Not a triangulation!");
1109 for(j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3)
1111 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j+1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j+2] + bspDrawSurfaces[i].firstVert;
1112 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
1113 float *oa = &old_xyzst[ia*5], *ob = &old_xyzst[ib*5], *oc = &old_xyzst[ic*5];
1118 ExtrapolateTexcoords(
1134 for( i = 0; i < numBSPPlanes; i++ )
1136 bspPlanes[ i ].dist *= scale[0];
1141 for( i = 0; i < numBSPPlanes; i++ )
1143 bspPlanes[ i ].normal[0] /= scale[0];
1144 bspPlanes[ i ].normal[1] /= scale[1];
1145 bspPlanes[ i ].normal[2] /= scale[2];
1146 f = 1/VectorLength(bspPlanes[i].normal);
1147 VectorScale(bspPlanes[i].normal, f, bspPlanes[i].normal);
1148 bspPlanes[ i ].dist *= f;
1152 /* scale gridsize */
1153 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1154 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
1155 VectorCopy( gridSize, vec );
1159 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1160 SetKeyValue( &entities[ 0 ], "gridsize", str );
1162 /* inject command line parameters */
1163 InjectCommandLine(argv, 0, argc - 1);
1167 StripExtension( source );
1168 DefaultExtension( source, "_s.bsp" );
1169 Sys_Printf( "Writing %s\n", source );
1170 WriteBSPFile( source );
1172 /* return to sender */
1180 main argument processing function for bsp conversion
1183 int ConvertBSPMain( int argc, char **argv )
1186 int (*convertFunc)( char * );
1187 game_t *convertGame;
1191 convertFunc = ConvertBSPToASE;
1197 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
1201 /* process arguments */
1202 for( i = 1; i < (argc - 1); i++ )
1204 /* -format map|ase|... */
1205 if( !strcmp( argv[ i ], "-format" ) )
1208 if( !Q_stricmp( argv[ i ], "ase" ) )
1209 convertFunc = ConvertBSPToASE;
1210 else if( !Q_stricmp( argv[ i ], "map" ) )
1211 convertFunc = ConvertBSPToMap;
1214 convertGame = GetGame( argv[ i ] );
1215 if( convertGame == NULL )
1216 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
1219 else if( !strcmp( argv[ i ], "-ne" ) )
1221 normalEpsilon = atof( argv[ i + 1 ] );
1223 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
1225 else if( !strcmp( argv[ i ], "-de" ) )
1227 distanceEpsilon = atof( argv[ i + 1 ] );
1229 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
1231 else if( !strcmp( argv[ i ], "-shadersasbitmap" ) )
1232 shadersAsBitmap = qtrue;
1235 /* clean up map name */
1236 strcpy( source, ExpandArg( argv[ i ] ) );
1237 StripExtension( source );
1238 DefaultExtension( source, ".bsp" );
1242 Sys_Printf( "Loading %s\n", source );
1244 /* ydnar: load surface file */
1245 //% LoadSurfaceExtraFile( source );
1247 LoadBSPFile( source );
1249 /* parse bsp entities */
1252 /* bsp format convert? */
1253 if( convertGame != NULL )
1255 /* set global game */
1259 StripExtension( source );
1260 DefaultExtension( source, "_c.bsp" );
1261 Sys_Printf( "Writing %s\n", source );
1262 WriteBSPFile( source );
1264 /* return to sender */
1268 /* normal convert */
1269 return convertFunc( source );
1279 int main( int argc, char **argv )
1285 /* we want consistent 'randomness' */
1289 start = I_FloatTime();
1291 /* this was changed to emit version number over the network */
1292 printf( Q3MAP_VERSION "\n" );
1295 atexit( ExitQ3Map );
1297 /* read general options first */
1298 for( i = 1; i < argc; i++ )
1301 if( !strcmp( argv[ i ], "-connect" ) )
1305 Broadcast_Setup( argv[ i ] );
1310 else if( !strcmp( argv[ i ], "-v" ) )
1320 else if( !strcmp( argv[ i ], "-force" ) )
1326 /* patch subdivisions */
1327 else if( !strcmp( argv[ i ], "-subdivisions" ) )
1331 patchSubdivisions = atoi( argv[ i ] );
1333 if( patchSubdivisions <= 0 )
1334 patchSubdivisions = 1;
1338 else if( !strcmp( argv[ i ], "-threads" ) )
1342 numthreads = atoi( argv[ i ] );
1347 /* init model library */
1349 PicoSetMallocFunc( safe_malloc );
1350 PicoSetFreeFunc( free );
1351 PicoSetPrintFunc( PicoPrintFunc );
1352 PicoSetLoadFileFunc( PicoLoadFileFunc );
1353 PicoSetFreeFileFunc( free );
1355 /* set number of threads */
1358 /* generate sinusoid jitter table */
1359 for( i = 0; i < MAX_JITTERS; i++ )
1361 jitters[ i ] = sin( i * 139.54152147 );
1362 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1365 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1366 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1368 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
1369 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1370 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1371 Sys_Printf( "%s\n", Q3MAP_MOTD );
1373 /* ydnar: new path initialization */
1374 InitPaths( &argc, argv );
1376 /* set game options */
1377 if (!patchSubdivisions)
1378 patchSubdivisions = game->patchSubdivisions;
1380 /* check if we have enough options left to attempt something */
1382 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1385 if( !strcmp( argv[ 1 ], "-fixaas" ) )
1386 r = FixAAS( argc - 1, argv + 1 );
1389 else if( !strcmp( argv[ 1 ], "-analyze" ) )
1390 r = AnalyzeBSP( argc - 1, argv + 1 );
1393 else if( !strcmp( argv[ 1 ], "-info" ) )
1394 r = BSPInfo( argc - 2, argv + 2 );
1397 else if( !strcmp( argv[ 1 ], "-vis" ) )
1398 r = VisMain( argc - 1, argv + 1 );
1401 else if( !strcmp( argv[ 1 ], "-light" ) )
1402 r = LightMain( argc - 1, argv + 1 );
1405 else if( !strcmp( argv[ 1 ], "-vlight" ) )
1407 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1408 argv[ 1 ] = "-fast"; /* eek a hack */
1409 r = LightMain( argc, argv );
1412 /* ydnar: lightmap export */
1413 else if( !strcmp( argv[ 1 ], "-export" ) )
1414 r = ExportLightmapsMain( argc - 1, argv + 1 );
1416 /* ydnar: lightmap import */
1417 else if( !strcmp( argv[ 1 ], "-import" ) )
1418 r = ImportLightmapsMain( argc - 1, argv + 1 );
1420 /* ydnar: bsp scaling */
1421 else if( !strcmp( argv[ 1 ], "-scale" ) )
1422 r = ScaleBSPMain( argc - 1, argv + 1 );
1424 /* ydnar: bsp conversion */
1425 else if( !strcmp( argv[ 1 ], "-convert" ) )
1426 r = ConvertBSPMain( argc - 1, argv + 1 );
1429 else if( !strcmp( argv[ 1 ], "-minimap" ) )
1430 r = MiniMapBSPMain(argc - 1, argv + 1);
1432 /* ydnar: otherwise create a bsp */
1434 r = BSPMain( argc, argv );
1437 end = I_FloatTime();
1438 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1440 /* shut down connection */
1441 Broadcast_Shutdown();
1443 /* return any error code */