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 MiniMapRandomlySubsampled(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 + Random() * dx,
201 val /= minimap.samples * minimap.size[2];
206 static void MiniMapSubsampled(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 MiniMapNoSubsampling(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()
284 vec3_t mins, maxs, extend;
285 VectorCopy(minimap.model->mins, mins);
286 VectorCopy(minimap.model->maxs, 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]);
317 int MiniMapBSPMain( int argc, char **argv )
319 char minimapFilename[1024];
320 float minimapSharpen;
329 Sys_Printf( "Usage: q3map [-v] -minimap [-size n] [-sharpen n] [-samples f] [-o filename.tga] [-minmax Xmin Ymin Zmin Xmax Ymax Zmax] <mapname>\n" );
333 /* load the BSP first */
334 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
335 StripExtension( source );
336 DefaultExtension( source, ".bsp" );
337 Sys_Printf( "Loading %s\n", source );
338 LoadBSPFile( source );
340 minimap.model = &bspModels[0];
341 MiniMapMakeMinsMaxs();
343 *minimapFilename = 0;
345 minimap.width = minimap.height = 512;
347 minimap.sample_offsets = NULL;
349 /* process arguments */
350 for( i = 1; i < (argc - 1); i++ )
352 if( !strcmp( argv[ i ], "-size" ) )
354 minimap.width = minimap.height = atoi(argv[i + 1]);
356 Sys_Printf( "Image size set to %i\n", minimap.width );
358 else if( !strcmp( argv[ i ], "-sharpen" ) )
360 minimapSharpen = atof(argv[i + 1]);
362 Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
364 else if( !strcmp( argv[ i ], "-samples" ) )
366 minimap.samples = atoi(argv[i + 1]);
368 Sys_Printf( "Samples set to %i\n", minimap.samples );
369 /* TODO generate a static subsampling pattern */
371 else if( !strcmp( argv[ i ], "-o" ) )
373 strcpy(minimapFilename, argv[i + 1]);
375 Sys_Printf( "Output file name set to %s\n", minimapFilename );
377 else if( !strcmp( argv[ i ], "-minmax" ) && i < (argc - 7) )
379 minimap.mins[0] = atof(argv[i + 1]);
380 minimap.mins[1] = atof(argv[i + 2]);
381 minimap.mins[2] = atof(argv[i + 3]);
382 minimap.size[0] = atof(argv[i + 4]) - minimap.mins[0];
383 minimap.size[1] = atof(argv[i + 5]) - minimap.mins[1];
384 minimap.size[2] = atof(argv[i + 6]) - minimap.mins[2];
386 Sys_Printf( "Map mins/maxs overridden\n" );
390 strcpy( minimapFilename, ExpandArg( argv[ argc - 1 ] ) );
391 StripExtension( minimapFilename );
392 DefaultExtension( minimapFilename, ".tga" );
394 if(minimapSharpen >= 0)
396 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
397 minimap.sharpen_boxmult = -minimapSharpen;
400 minimap.data1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
401 data3b = safe_malloc(minimap.width * minimap.height * 3);
402 if(minimapSharpen >= 0)
403 minimap.sharpendata1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
407 if(minimap.samples <= 1)
409 Sys_Printf( "\n--- MiniMapNoSubsampling (%d) ---\n", minimap.height );
410 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapNoSubsampling);
414 if(minimap.sample_offsets)
416 Sys_Printf( "\n--- MiniMapSubsampled (%d) ---\n", minimap.height );
417 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSubsampled);
421 Sys_Printf( "\n--- MiniMapRandomlySubsampled (%d) ---\n", minimap.height );
422 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapRandomlySubsampled);
426 if(minimap.sharpendata1f)
428 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
429 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSharpen);
430 q = minimap.sharpendata1f;
437 Sys_Printf( "\nConverting...");
439 for(y = 0; y < minimap.height; ++y)
440 for(x = 0; x < minimap.width; ++x)
445 if(v > 255.0/256.0) v = 255.0/256.0;
452 Sys_Printf( " writing to %s...", minimapFilename );
453 WriteTGA24(minimapFilename, data3b, minimap.width, minimap.height, qfalse);
455 Sys_Printf( " done.\n" );
457 /* return to sender */
467 calculates an md4 checksum for a block of data
470 static int MD4BlockChecksum( void *buffer, int length )
472 return Com_BlockChecksum(buffer, length);
477 resets an aas checksum to match the given BSP
480 int FixAAS( int argc, char **argv )
482 int length, checksum;
485 char aas[ 1024 ], **ext;
498 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
502 /* do some path mangling */
503 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
504 StripExtension( source );
505 DefaultExtension( source, ".bsp" );
508 Sys_Printf( "--- FixAAS ---\n" );
511 Sys_Printf( "Loading %s\n", source );
512 length = LoadFile( source, &buffer );
514 /* create bsp checksum */
515 Sys_Printf( "Creating checksum...\n" );
516 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
518 /* write checksum to aas */
523 strcpy( aas, source );
524 StripExtension( aas );
526 Sys_Printf( "Trying %s\n", aas );
530 file = fopen( aas, "r+b" );
533 if( fwrite( &checksum, 4, 1, file ) != 1 )
534 Error( "Error writing checksum to %s", aas );
538 /* return to sender */
546 analyzes a Quake engine BSP file
549 typedef struct abspHeader_s
554 bspLump_t lumps[ 1 ]; /* unknown size */
558 typedef struct abspLumpTest_s
565 int AnalyzeBSP( int argc, char **argv )
567 abspHeader_t *header;
568 int size, i, version, offset, length, lumpInt, count;
572 char lumpString[ 1024 ], source[ 1024 ];
573 qboolean lumpSwap = qfalse;
574 abspLumpTest_t *lumpTest;
575 static abspLumpTest_t lumpTests[] =
577 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
578 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
579 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
580 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
581 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
582 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
583 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
584 { 104, 3, "IBSP LUMP_DRAWSURFS" },
585 { 44, 3, "IBSP LUMP_DRAWVERTS" },
586 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
587 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
588 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
589 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
597 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
601 /* process arguments */
602 for( i = 1; i < (argc - 1); i++ )
604 /* -format map|ase|... */
605 if( !strcmp( argv[ i ], "-lumpswap" ) )
607 Sys_Printf( "Swapped lump structs enabled\n" );
612 /* clean up map name */
613 strcpy( source, ExpandArg( argv[ i ] ) );
614 Sys_Printf( "Loading %s\n", source );
617 size = LoadFile( source, (void**) &header );
618 if( size == 0 || header == NULL )
620 Sys_Printf( "Unable to load %s.\n", source );
624 /* analyze ident/version */
625 memcpy( ident, header->ident, 4 );
627 version = LittleLong( header->version );
629 Sys_Printf( "Identity: %s\n", ident );
630 Sys_Printf( "Version: %d\n", version );
631 Sys_Printf( "---------------------------------------\n" );
633 /* analyze each lump */
634 for( i = 0; i < 100; i++ )
636 /* call of duty swapped lump pairs */
639 offset = LittleLong( header->lumps[ i ].length );
640 length = LittleLong( header->lumps[ i ].offset );
643 /* standard lump pairs */
646 offset = LittleLong( header->lumps[ i ].offset );
647 length = LittleLong( header->lumps[ i ].length );
651 lump = (byte*) header + offset;
652 lumpInt = LittleLong( (int) *((int*) lump) );
653 lumpFloat = LittleFloat( (float) *((float*) lump) );
654 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
655 lumpString[ 1024 ] = '\0';
657 /* print basic lump info */
658 Sys_Printf( "Lump: %d\n", i );
659 Sys_Printf( "Offset: %d bytes\n", offset );
660 Sys_Printf( "Length: %d bytes\n", length );
662 /* only operate on valid lumps */
665 /* print data in 4 formats */
666 Sys_Printf( "As hex: %08X\n", lumpInt );
667 Sys_Printf( "As int: %d\n", lumpInt );
668 Sys_Printf( "As float: %f\n", lumpFloat );
669 Sys_Printf( "As string: %s\n", lumpString );
671 /* guess lump type */
672 if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
673 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
674 else if( strstr( lumpString, "textures/" ) )
675 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
678 /* guess based on size/count */
679 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
681 if( (length % lumpTest->radix) != 0 )
683 count = length / lumpTest->radix;
684 if( count < lumpTest->minCount )
686 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
691 Sys_Printf( "---------------------------------------\n" );
694 if( offset + length >= size )
699 Sys_Printf( "Lump count: %d\n", i + 1 );
700 Sys_Printf( "File size: %d bytes\n", size );
702 /* return to caller */
710 emits statistics about the bsp file
713 int BSPInfo( int count, char **fileNames )
716 char source[ 1024 ], ext[ 64 ];
724 Sys_Printf( "No files to dump info for.\n");
728 /* enable info mode */
732 for( i = 0; i < count; i++ )
734 Sys_Printf( "---------------------------------\n" );
736 /* mangle filename and get size */
737 strcpy( source, fileNames[ i ] );
738 ExtractFileExtension( source, ext );
739 if( !Q_stricmp( ext, "map" ) )
740 StripExtension( source );
741 DefaultExtension( source, ".bsp" );
742 f = fopen( source, "rb" );
745 size = Q_filelength (f);
751 /* load the bsp file and print lump sizes */
752 Sys_Printf( "%s\n", source );
753 LoadBSPFile( source );
758 Sys_Printf( " total %9d\n", size );
759 Sys_Printf( " %9d KB\n", size / 1024 );
760 Sys_Printf( " %9d MB\n", size / (1024 * 1024) );
762 Sys_Printf( "---------------------------------\n" );
770 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)
772 vec4_t scoeffs, tcoeffs;
778 VectorSubtract(bxyz, axyz, dab);
779 VectorSubtract(cxyz, axyz, dac);
780 CrossProduct(dab, dac, norm);
784 // s(v + norm) = s(v) when n ortho xyz
786 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
789 // scoeffs * (axyz, 1) == ast[0]
790 // scoeffs * (bxyz, 1) == bst[0]
791 // scoeffs * (cxyz, 1) == cst[0]
792 // scoeffs * (norm, 0) == 0
793 // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
794 solvematrix[0] = axyz[0];
795 solvematrix[4] = axyz[1];
796 solvematrix[8] = axyz[2];
798 solvematrix[1] = bxyz[0];
799 solvematrix[5] = bxyz[1];
800 solvematrix[9] = bxyz[2];
802 solvematrix[2] = cxyz[0];
803 solvematrix[6] = cxyz[1];
804 solvematrix[10] = cxyz[2];
806 solvematrix[3] = norm[0];
807 solvematrix[7] = norm[1];
808 solvematrix[11] = norm[2];
811 md = m4_det(solvematrix);
814 Sys_Printf("Cannot invert some matrix, some texcoords aren't extrapolated!");
818 m4x4_invert(solvematrix);
824 m4x4_transform_vec4(solvematrix, scoeffs);
829 m4x4_transform_vec4(solvematrix, tcoeffs);
831 ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
832 ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
833 bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
834 bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
835 cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
836 cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
841 amaze and confuse your enemies with wierd scaled maps!
844 int ScaleBSPMain( int argc, char **argv )
853 float *old_xyzst = NULL;
859 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
864 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
866 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
868 scale[0] = atof( argv[ argc - 4 ] );
870 texscale = !strcmp(argv[1], "-tex");
872 uniform = ((scale[0] == scale[1]) && (scale[1] == scale[2]));
874 if( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f )
876 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
877 Sys_Printf( "Non-zero scale value required.\n" );
881 /* do some path mangling */
882 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
883 StripExtension( source );
884 DefaultExtension( source, ".bsp" );
887 Sys_Printf( "Loading %s\n", source );
888 LoadBSPFile( source );
892 Sys_Printf( "--- ScaleBSP ---\n" );
893 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
895 /* scale entity keys */
896 for( i = 0; i < numBSPEntities && i < numEntities; i++ )
899 GetVectorForKey( &entities[ i ], "origin", vec );
900 if( (vec[ 0 ] || vec[ 1 ] || vec[ 2 ]) )
905 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
906 SetKeyValue( &entities[ i ], "origin", str );
909 a = FloatForKey( &entities[ i ], "angle" );
910 if(a == -1 || a == -2) // z scale
912 else if(fabs(sin(DEG2RAD(a))) < 0.707)
918 f = FloatForKey( &entities[ i ], "lip" );
922 sprintf( str, "%f", f );
923 SetKeyValue( &entities[ i ], "lip", str );
926 /* scale plat height */
927 f = FloatForKey( &entities[ i ], "height" );
931 sprintf( str, "%f", f );
932 SetKeyValue( &entities[ i ], "height", str );
935 // TODO maybe allow a definition file for entities to specify which values are scaled how?
939 for( i = 0; i < numBSPModels; i++ )
941 bspModels[ i ].mins[0] *= scale[0];
942 bspModels[ i ].mins[1] *= scale[1];
943 bspModels[ i ].mins[2] *= scale[2];
944 bspModels[ i ].maxs[0] *= scale[0];
945 bspModels[ i ].maxs[1] *= scale[1];
946 bspModels[ i ].maxs[2] *= scale[2];
950 for( i = 0; i < numBSPNodes; i++ )
952 bspNodes[ i ].mins[0] *= scale[0];
953 bspNodes[ i ].mins[1] *= scale[1];
954 bspNodes[ i ].mins[2] *= scale[2];
955 bspNodes[ i ].maxs[0] *= scale[0];
956 bspNodes[ i ].maxs[1] *= scale[1];
957 bspNodes[ i ].maxs[2] *= scale[2];
961 for( i = 0; i < numBSPLeafs; i++ )
963 bspLeafs[ i ].mins[0] *= scale[0];
964 bspLeafs[ i ].mins[1] *= scale[1];
965 bspLeafs[ i ].mins[2] *= scale[2];
966 bspLeafs[ i ].maxs[0] *= scale[0];
967 bspLeafs[ i ].maxs[1] *= scale[1];
968 bspLeafs[ i ].maxs[2] *= scale[2];
973 Sys_Printf("Using texture unlocking (and probably breaking texture alignment a lot)\n");
974 old_xyzst = safe_malloc(sizeof(*old_xyzst) * numBSPDrawVerts * 5);
975 for(i = 0; i < numBSPDrawVerts; i++)
977 old_xyzst[5*i+0] = bspDrawVerts[i].xyz[0];
978 old_xyzst[5*i+1] = bspDrawVerts[i].xyz[1];
979 old_xyzst[5*i+2] = bspDrawVerts[i].xyz[2];
980 old_xyzst[5*i+3] = bspDrawVerts[i].st[0];
981 old_xyzst[5*i+4] = bspDrawVerts[i].st[1];
985 /* scale drawverts */
986 for( i = 0; i < numBSPDrawVerts; i++ )
988 bspDrawVerts[i].xyz[0] *= scale[0];
989 bspDrawVerts[i].xyz[1] *= scale[1];
990 bspDrawVerts[i].xyz[2] *= scale[2];
991 bspDrawVerts[i].normal[0] /= scale[0];
992 bspDrawVerts[i].normal[1] /= scale[1];
993 bspDrawVerts[i].normal[2] /= scale[2];
994 VectorNormalize(bspDrawVerts[i].normal, bspDrawVerts[i].normal);
999 for(i = 0; i < numBSPDrawSurfaces; i++)
1001 switch(bspDrawSurfaces[i].surfaceType)
1005 if(bspDrawSurfaces[i].numIndexes % 3)
1006 Error("Not a triangulation!");
1007 for(j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3)
1009 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j+1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j+2] + bspDrawSurfaces[i].firstVert;
1010 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
1011 float *oa = &old_xyzst[ia*5], *ob = &old_xyzst[ib*5], *oc = &old_xyzst[ic*5];
1016 ExtrapolateTexcoords(
1032 for( i = 0; i < numBSPPlanes; i++ )
1034 bspPlanes[ i ].dist *= scale[0];
1039 for( i = 0; i < numBSPPlanes; i++ )
1041 bspPlanes[ i ].normal[0] /= scale[0];
1042 bspPlanes[ i ].normal[1] /= scale[1];
1043 bspPlanes[ i ].normal[2] /= scale[2];
1044 f = 1/VectorLength(bspPlanes[i].normal);
1045 VectorScale(bspPlanes[i].normal, f, bspPlanes[i].normal);
1046 bspPlanes[ i ].dist *= f;
1050 /* scale gridsize */
1051 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1052 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
1053 VectorCopy( gridSize, vec );
1057 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1058 SetKeyValue( &entities[ 0 ], "gridsize", str );
1060 /* inject command line parameters */
1061 InjectCommandLine(argv, 0, argc - 1);
1065 StripExtension( source );
1066 DefaultExtension( source, "_s.bsp" );
1067 Sys_Printf( "Writing %s\n", source );
1068 WriteBSPFile( source );
1070 /* return to sender */
1078 main argument processing function for bsp conversion
1081 int ConvertBSPMain( int argc, char **argv )
1084 int (*convertFunc)( char * );
1085 game_t *convertGame;
1089 convertFunc = ConvertBSPToASE;
1095 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
1099 /* process arguments */
1100 for( i = 1; i < (argc - 1); i++ )
1102 /* -format map|ase|... */
1103 if( !strcmp( argv[ i ], "-format" ) )
1106 if( !Q_stricmp( argv[ i ], "ase" ) )
1107 convertFunc = ConvertBSPToASE;
1108 else if( !Q_stricmp( argv[ i ], "map" ) )
1109 convertFunc = ConvertBSPToMap;
1112 convertGame = GetGame( argv[ i ] );
1113 if( convertGame == NULL )
1114 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
1117 else if( !strcmp( argv[ i ], "-ne" ) )
1119 normalEpsilon = atof( argv[ i + 1 ] );
1121 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
1123 else if( !strcmp( argv[ i ], "-de" ) )
1125 distanceEpsilon = atof( argv[ i + 1 ] );
1127 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
1129 else if( !strcmp( argv[ i ], "-shadersasbitmap" ) )
1130 shadersAsBitmap = qtrue;
1133 /* clean up map name */
1134 strcpy( source, ExpandArg( argv[ i ] ) );
1135 StripExtension( source );
1136 DefaultExtension( source, ".bsp" );
1140 Sys_Printf( "Loading %s\n", source );
1142 /* ydnar: load surface file */
1143 //% LoadSurfaceExtraFile( source );
1145 LoadBSPFile( source );
1147 /* parse bsp entities */
1150 /* bsp format convert? */
1151 if( convertGame != NULL )
1153 /* set global game */
1157 StripExtension( source );
1158 DefaultExtension( source, "_c.bsp" );
1159 Sys_Printf( "Writing %s\n", source );
1160 WriteBSPFile( source );
1162 /* return to sender */
1166 /* normal convert */
1167 return convertFunc( source );
1177 int main( int argc, char **argv )
1183 /* we want consistent 'randomness' */
1187 start = I_FloatTime();
1189 /* this was changed to emit version number over the network */
1190 printf( Q3MAP_VERSION "\n" );
1193 atexit( ExitQ3Map );
1195 /* read general options first */
1196 for( i = 1; i < argc; i++ )
1199 if( !strcmp( argv[ i ], "-connect" ) )
1203 Broadcast_Setup( argv[ i ] );
1208 else if( !strcmp( argv[ i ], "-v" ) )
1218 else if( !strcmp( argv[ i ], "-force" ) )
1224 /* patch subdivisions */
1225 else if( !strcmp( argv[ i ], "-subdivisions" ) )
1229 patchSubdivisions = atoi( argv[ i ] );
1231 if( patchSubdivisions <= 0 )
1232 patchSubdivisions = 1;
1236 else if( !strcmp( argv[ i ], "-threads" ) )
1240 numthreads = atoi( argv[ i ] );
1245 /* init model library */
1247 PicoSetMallocFunc( safe_malloc );
1248 PicoSetFreeFunc( free );
1249 PicoSetPrintFunc( PicoPrintFunc );
1250 PicoSetLoadFileFunc( PicoLoadFileFunc );
1251 PicoSetFreeFileFunc( free );
1253 /* set number of threads */
1256 /* generate sinusoid jitter table */
1257 for( i = 0; i < MAX_JITTERS; i++ )
1259 jitters[ i ] = sin( i * 139.54152147 );
1260 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1263 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1264 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1266 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
1267 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1268 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1269 Sys_Printf( "%s\n", Q3MAP_MOTD );
1271 /* ydnar: new path initialization */
1272 InitPaths( &argc, argv );
1274 /* set game options */
1275 if (!patchSubdivisions)
1276 patchSubdivisions = game->patchSubdivisions;
1278 /* check if we have enough options left to attempt something */
1280 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1283 if( !strcmp( argv[ 1 ], "-fixaas" ) )
1284 r = FixAAS( argc - 1, argv + 1 );
1287 else if( !strcmp( argv[ 1 ], "-analyze" ) )
1288 r = AnalyzeBSP( argc - 1, argv + 1 );
1291 else if( !strcmp( argv[ 1 ], "-info" ) )
1292 r = BSPInfo( argc - 2, argv + 2 );
1295 else if( !strcmp( argv[ 1 ], "-vis" ) )
1296 r = VisMain( argc - 1, argv + 1 );
1299 else if( !strcmp( argv[ 1 ], "-light" ) )
1300 r = LightMain( argc - 1, argv + 1 );
1303 else if( !strcmp( argv[ 1 ], "-vlight" ) )
1305 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1306 argv[ 1 ] = "-fast"; /* eek a hack */
1307 r = LightMain( argc, argv );
1310 /* ydnar: lightmap export */
1311 else if( !strcmp( argv[ 1 ], "-export" ) )
1312 r = ExportLightmapsMain( argc - 1, argv + 1 );
1314 /* ydnar: lightmap import */
1315 else if( !strcmp( argv[ 1 ], "-import" ) )
1316 r = ImportLightmapsMain( argc - 1, argv + 1 );
1318 /* ydnar: bsp scaling */
1319 else if( !strcmp( argv[ 1 ], "-scale" ) )
1320 r = ScaleBSPMain( argc - 1, argv + 1 );
1322 /* ydnar: bsp conversion */
1323 else if( !strcmp( argv[ 1 ], "-convert" ) )
1324 r = ConvertBSPMain( argc - 1, argv + 1 );
1327 else if( !strcmp( argv[ 1 ], "-minimap" ) )
1328 r = MiniMapBSPMain(argc - 1, argv + 1);
1330 /* ydnar: otherwise create a bsp */
1332 r = BSPMain( argc, argv );
1335 end = I_FloatTime();
1336 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1338 /* shut down connection */
1339 Broadcast_Shutdown();
1341 /* return any error code */