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, j, b, compileFlags;
343 bspBrushSide_t *side;
349 Sys_FPrintf( SYS_VRB, "--- MiniMapSetupBrushes ---\n" );
352 if( opaqueBrushes == NULL )
353 opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 );
356 memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 );
357 numOpaqueBrushes = 0;
359 /* walk the list of worldspawn brushes */
360 for( i = 0; i < minimap.model->numBSPBrushes; i++ )
363 b = minimap.model->firstBSPBrush + i;
364 brush = &bspBrushes[ b ];
367 /* check all sides */
369 for( j = 0; j < brush->numSides; j++ )
371 /* do bsp shader calculations */
372 side = &bspBrushSides[ brush->firstSide + j ];
373 shader = &bspShaders[ side->shaderNum ];
375 /* get shader info */
376 si = ShaderInfoForShader( shader->shader );
380 /* or together compile flags */
381 compileFlags |= si->compileFlags;
384 shader = &bspShaders[ brush->shaderNum ];
385 si = ShaderInfoForShader( shader->shader );
389 compileFlags = si->compileFlags;
392 /* determine if this brush is solid */
393 if( (compileFlags & (C_SOLID | C_SKY)) == C_SOLID )
395 opaqueBrushes[ b >> 3 ] |= (1 << (b & 7));
401 /* emit some statistics */
402 Sys_FPrintf( SYS_VRB, "%9d solid brushes\n", numOpaqueBrushes );
405 qboolean MiniMapEvaluateSampleOffsets(int *bestj, int *bestk, float *bestval)
410 *bestj = *bestk = -1;
411 *bestval = 3; /* max possible val is 2 */
413 for(j = 0; j < minimap.samples; ++j)
414 for(k = j + 1; k < minimap.samples; ++k)
416 dx = minimap.sample_offsets[2*j+0] - minimap.sample_offsets[2*k+0];
417 dy = minimap.sample_offsets[2*j+1] - minimap.sample_offsets[2*k+1];
418 if(dx > +0.5) dx -= 1;
419 if(dx < -0.5) dx += 1;
420 if(dy > +0.5) dy -= 1;
421 if(dy < -0.5) dy += 1;
422 val = dx * dx + dy * dy;
434 void MiniMapMakeSampleOffsets()
437 float val, valj, valk, dx, dy, sx, sy, rx, ry;
439 Sys_Printf( "Generating good sample offsets (this may take a while)...\n" );
441 /* start with entirely random samples */
442 for(i = 0; i < minimap.samples; ++i)
444 minimap.sample_offsets[2*i+0] = Random();
445 minimap.sample_offsets[2*i+1] = Random();
448 for(i = 0; i < 1000; ++i)
450 if(MiniMapEvaluateSampleOffsets(&j, &k, &val))
452 sx = minimap.sample_offsets[2*j+0];
453 sy = minimap.sample_offsets[2*j+1];
454 minimap.sample_offsets[2*j+0] = rx = Random();
455 minimap.sample_offsets[2*j+1] = ry = Random();
456 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valj))
458 minimap.sample_offsets[2*j+0] = sx;
459 minimap.sample_offsets[2*j+1] = sy;
461 sx = minimap.sample_offsets[2*k+0];
462 sy = minimap.sample_offsets[2*k+1];
463 minimap.sample_offsets[2*k+0] = rx;
464 minimap.sample_offsets[2*k+1] = ry;
465 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valk))
467 minimap.sample_offsets[2*k+0] = sx;
468 minimap.sample_offsets[2*k+1] = sy;
474 /* valj is the greatest */
475 minimap.sample_offsets[2*j+0] = rx;
476 minimap.sample_offsets[2*j+1] = ry;
481 /* valj is the greater and it is useless - forget it */
488 /* valk is the greatest */
489 minimap.sample_offsets[2*k+0] = rx;
490 minimap.sample_offsets[2*k+1] = ry;
495 /* valk is the greater and it is useless - forget it */
504 int MiniMapBSPMain( int argc, char **argv )
506 char minimapFilename[1024];
509 char parentpath[1024];
510 float minimapSharpen;
521 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" );
525 /* load the BSP first */
526 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
527 StripExtension( source );
528 DefaultExtension( source, ".bsp" );
529 Sys_Printf( "Loading %s\n", source );
530 BeginMapShaderFile( source );
532 LoadBSPFile( source );
534 minimap.model = &bspModels[0];
535 VectorCopy(minimap.model->mins, mins);
536 VectorCopy(minimap.model->maxs, maxs);
537 *minimapFilename = 0;
539 minimap.width = minimap.height = 512;
541 minimap.sample_offsets = NULL;
542 border = 1/66.0; /* good default for Nexuiz */
544 /* process arguments */
545 for( i = 1; i < (argc - 1); i++ )
547 if( !strcmp( argv[ i ], "-size" ) )
549 minimap.width = minimap.height = atoi(argv[i + 1]);
551 Sys_Printf( "Image size set to %i\n", minimap.width );
553 else if( !strcmp( argv[ i ], "-sharpen" ) )
555 minimapSharpen = atof(argv[i + 1]);
557 Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
559 else if( !strcmp( argv[ i ], "-samples" ) )
561 minimap.samples = atoi(argv[i + 1]);
563 Sys_Printf( "Samples set to %i\n", minimap.samples );
564 if(minimap.sample_offsets)
565 free(minimap.sample_offsets);
566 minimap.sample_offsets = malloc(2 * sizeof(*minimap.sample_offsets) * minimap.samples);
567 MiniMapMakeSampleOffsets();
569 else if( !strcmp( argv[ i ], "-random" ) )
571 minimap.samples = atoi(argv[i + 1]);
573 Sys_Printf( "Random samples set to %i\n", minimap.samples );
574 if(minimap.sample_offsets)
575 free(minimap.sample_offsets);
576 minimap.sample_offsets = NULL;
578 else if( !strcmp( argv[ i ], "-border" ) )
580 border = atof(argv[i + 1]);
582 Sys_Printf( "Border set to %f\n", border );
584 else if( !strcmp( argv[ i ], "-o" ) )
586 strcpy(minimapFilename, argv[i + 1]);
588 Sys_Printf( "Output file name set to %s\n", minimapFilename );
590 else if( !strcmp( argv[ i ], "-minmax" ) && i < (argc - 7) )
592 mins[0] = atof(argv[i + 1]);
593 mins[1] = atof(argv[i + 2]);
594 mins[2] = atof(argv[i + 3]);
595 maxs[0] = atof(argv[i + 4]);
596 maxs[1] = atof(argv[i + 5]);
597 maxs[2] = atof(argv[i + 6]);
599 Sys_Printf( "Map mins/maxs overridden\n" );
603 MiniMapMakeMinsMaxs(mins, maxs, border);
605 if(!*minimapFilename)
607 ExtractFileBase(source, basename);
608 ExtractFilePath(source, path);
610 path[strlen(path)-1] = 0;
611 ExtractFilePath(path, parentpath);
612 sprintf(minimapFilename, "%sgfx", parentpath);
613 Q_mkdir(minimapFilename);
614 sprintf(minimapFilename, "%sgfx/%s_mini.tga", parentpath, basename);
615 Sys_Printf("Output file name automatically set to %s\n", minimapFilename);
618 if(minimapSharpen >= 0)
620 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
621 minimap.sharpen_boxmult = -minimapSharpen;
624 minimap.data1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
625 data3b = safe_malloc(minimap.width * minimap.height * 3);
626 if(minimapSharpen >= 0)
627 minimap.sharpendata1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
629 MiniMapSetupBrushes();
631 if(minimap.samples <= 1)
633 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
634 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapNoSupersampling);
638 if(minimap.sample_offsets)
640 Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
641 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSupersampled);
645 Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
646 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapRandomlySupersampled);
650 if(minimap.sharpendata1f)
652 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
653 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSharpen);
654 q = minimap.sharpendata1f;
661 Sys_Printf( "\nConverting...");
663 for(y = 0; y < minimap.height; ++y)
664 for(x = 0; x < minimap.width; ++x)
669 if(v > 255.0/256.0) v = 255.0/256.0;
676 Sys_Printf( " writing to %s...", minimapFilename );
677 WriteTGA24(minimapFilename, data3b, minimap.width, minimap.height, qfalse);
679 Sys_Printf( " done.\n" );
681 /* return to sender */
691 calculates an md4 checksum for a block of data
694 static int MD4BlockChecksum( void *buffer, int length )
696 return Com_BlockChecksum(buffer, length);
701 resets an aas checksum to match the given BSP
704 int FixAAS( int argc, char **argv )
706 int length, checksum;
709 char aas[ 1024 ], **ext;
722 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
726 /* do some path mangling */
727 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
728 StripExtension( source );
729 DefaultExtension( source, ".bsp" );
732 Sys_Printf( "--- FixAAS ---\n" );
735 Sys_Printf( "Loading %s\n", source );
736 length = LoadFile( source, &buffer );
738 /* create bsp checksum */
739 Sys_Printf( "Creating checksum...\n" );
740 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
742 /* write checksum to aas */
747 strcpy( aas, source );
748 StripExtension( aas );
750 Sys_Printf( "Trying %s\n", aas );
754 file = fopen( aas, "r+b" );
757 if( fwrite( &checksum, 4, 1, file ) != 1 )
758 Error( "Error writing checksum to %s", aas );
762 /* return to sender */
770 analyzes a Quake engine BSP file
773 typedef struct abspHeader_s
778 bspLump_t lumps[ 1 ]; /* unknown size */
782 typedef struct abspLumpTest_s
789 int AnalyzeBSP( int argc, char **argv )
791 abspHeader_t *header;
792 int size, i, version, offset, length, lumpInt, count;
796 char lumpString[ 1024 ], source[ 1024 ];
797 qboolean lumpSwap = qfalse;
798 abspLumpTest_t *lumpTest;
799 static abspLumpTest_t lumpTests[] =
801 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
802 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
803 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
804 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
805 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
806 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
807 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
808 { 104, 3, "IBSP LUMP_DRAWSURFS" },
809 { 44, 3, "IBSP LUMP_DRAWVERTS" },
810 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
811 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
812 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
813 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
821 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
825 /* process arguments */
826 for( i = 1; i < (argc - 1); i++ )
828 /* -format map|ase|... */
829 if( !strcmp( argv[ i ], "-lumpswap" ) )
831 Sys_Printf( "Swapped lump structs enabled\n" );
836 /* clean up map name */
837 strcpy( source, ExpandArg( argv[ i ] ) );
838 Sys_Printf( "Loading %s\n", source );
841 size = LoadFile( source, (void**) &header );
842 if( size == 0 || header == NULL )
844 Sys_Printf( "Unable to load %s.\n", source );
848 /* analyze ident/version */
849 memcpy( ident, header->ident, 4 );
851 version = LittleLong( header->version );
853 Sys_Printf( "Identity: %s\n", ident );
854 Sys_Printf( "Version: %d\n", version );
855 Sys_Printf( "---------------------------------------\n" );
857 /* analyze each lump */
858 for( i = 0; i < 100; i++ )
860 /* call of duty swapped lump pairs */
863 offset = LittleLong( header->lumps[ i ].length );
864 length = LittleLong( header->lumps[ i ].offset );
867 /* standard lump pairs */
870 offset = LittleLong( header->lumps[ i ].offset );
871 length = LittleLong( header->lumps[ i ].length );
875 lump = (byte*) header + offset;
876 lumpInt = LittleLong( (int) *((int*) lump) );
877 lumpFloat = LittleFloat( (float) *((float*) lump) );
878 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
879 lumpString[ 1024 ] = '\0';
881 /* print basic lump info */
882 Sys_Printf( "Lump: %d\n", i );
883 Sys_Printf( "Offset: %d bytes\n", offset );
884 Sys_Printf( "Length: %d bytes\n", length );
886 /* only operate on valid lumps */
889 /* print data in 4 formats */
890 Sys_Printf( "As hex: %08X\n", lumpInt );
891 Sys_Printf( "As int: %d\n", lumpInt );
892 Sys_Printf( "As float: %f\n", lumpFloat );
893 Sys_Printf( "As string: %s\n", lumpString );
895 /* guess lump type */
896 if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
897 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
898 else if( strstr( lumpString, "textures/" ) )
899 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
902 /* guess based on size/count */
903 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
905 if( (length % lumpTest->radix) != 0 )
907 count = length / lumpTest->radix;
908 if( count < lumpTest->minCount )
910 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
915 Sys_Printf( "---------------------------------------\n" );
918 if( offset + length >= size )
923 Sys_Printf( "Lump count: %d\n", i + 1 );
924 Sys_Printf( "File size: %d bytes\n", size );
926 /* return to caller */
934 emits statistics about the bsp file
937 int BSPInfo( int count, char **fileNames )
940 char source[ 1024 ], ext[ 64 ];
948 Sys_Printf( "No files to dump info for.\n");
952 /* enable info mode */
956 for( i = 0; i < count; i++ )
958 Sys_Printf( "---------------------------------\n" );
960 /* mangle filename and get size */
961 strcpy( source, fileNames[ i ] );
962 ExtractFileExtension( source, ext );
963 if( !Q_stricmp( ext, "map" ) )
964 StripExtension( source );
965 DefaultExtension( source, ".bsp" );
966 f = fopen( source, "rb" );
969 size = Q_filelength (f);
975 /* load the bsp file and print lump sizes */
976 Sys_Printf( "%s\n", source );
977 LoadBSPFile( source );
982 Sys_Printf( " total %9d\n", size );
983 Sys_Printf( " %9d KB\n", size / 1024 );
984 Sys_Printf( " %9d MB\n", size / (1024 * 1024) );
986 Sys_Printf( "---------------------------------\n" );
994 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)
996 vec4_t scoeffs, tcoeffs;
1002 VectorSubtract(bxyz, axyz, dab);
1003 VectorSubtract(cxyz, axyz, dac);
1004 CrossProduct(dab, dac, norm);
1008 // s(v + norm) = s(v) when n ortho xyz
1010 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
1013 // scoeffs * (axyz, 1) == ast[0]
1014 // scoeffs * (bxyz, 1) == bst[0]
1015 // scoeffs * (cxyz, 1) == cst[0]
1016 // scoeffs * (norm, 0) == 0
1017 // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
1018 solvematrix[0] = axyz[0];
1019 solvematrix[4] = axyz[1];
1020 solvematrix[8] = axyz[2];
1021 solvematrix[12] = 1;
1022 solvematrix[1] = bxyz[0];
1023 solvematrix[5] = bxyz[1];
1024 solvematrix[9] = bxyz[2];
1025 solvematrix[13] = 1;
1026 solvematrix[2] = cxyz[0];
1027 solvematrix[6] = cxyz[1];
1028 solvematrix[10] = cxyz[2];
1029 solvematrix[14] = 1;
1030 solvematrix[3] = norm[0];
1031 solvematrix[7] = norm[1];
1032 solvematrix[11] = norm[2];
1033 solvematrix[15] = 0;
1035 md = m4_det(solvematrix);
1038 Sys_Printf("Cannot invert some matrix, some texcoords aren't extrapolated!");
1042 m4x4_invert(solvematrix);
1044 scoeffs[0] = ast[0];
1045 scoeffs[1] = bst[0];
1046 scoeffs[2] = cst[0];
1048 m4x4_transform_vec4(solvematrix, scoeffs);
1049 tcoeffs[0] = ast[1];
1050 tcoeffs[1] = bst[1];
1051 tcoeffs[2] = cst[1];
1053 m4x4_transform_vec4(solvematrix, tcoeffs);
1055 ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
1056 ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
1057 bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
1058 bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
1059 cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
1060 cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
1065 amaze and confuse your enemies with wierd scaled maps!
1068 int ScaleBSPMain( int argc, char **argv )
1077 float *old_xyzst = NULL;
1083 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1088 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
1090 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
1092 scale[0] = atof( argv[ argc - 4 ] );
1094 texscale = !strcmp(argv[1], "-tex");
1096 uniform = ((scale[0] == scale[1]) && (scale[1] == scale[2]));
1098 if( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f )
1100 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1101 Sys_Printf( "Non-zero scale value required.\n" );
1105 /* do some path mangling */
1106 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
1107 StripExtension( source );
1108 DefaultExtension( source, ".bsp" );
1111 Sys_Printf( "Loading %s\n", source );
1112 LoadBSPFile( source );
1116 Sys_Printf( "--- ScaleBSP ---\n" );
1117 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
1119 /* scale entity keys */
1120 for( i = 0; i < numBSPEntities && i < numEntities; i++ )
1123 GetVectorForKey( &entities[ i ], "origin", vec );
1124 if( (vec[ 0 ] || vec[ 1 ] || vec[ 2 ]) )
1129 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1130 SetKeyValue( &entities[ i ], "origin", str );
1133 a = FloatForKey( &entities[ i ], "angle" );
1134 if(a == -1 || a == -2) // z scale
1136 else if(fabs(sin(DEG2RAD(a))) < 0.707)
1141 /* scale door lip */
1142 f = FloatForKey( &entities[ i ], "lip" );
1146 sprintf( str, "%f", f );
1147 SetKeyValue( &entities[ i ], "lip", str );
1150 /* scale plat height */
1151 f = FloatForKey( &entities[ i ], "height" );
1155 sprintf( str, "%f", f );
1156 SetKeyValue( &entities[ i ], "height", str );
1159 // TODO maybe allow a definition file for entities to specify which values are scaled how?
1163 for( i = 0; i < numBSPModels; i++ )
1165 bspModels[ i ].mins[0] *= scale[0];
1166 bspModels[ i ].mins[1] *= scale[1];
1167 bspModels[ i ].mins[2] *= scale[2];
1168 bspModels[ i ].maxs[0] *= scale[0];
1169 bspModels[ i ].maxs[1] *= scale[1];
1170 bspModels[ i ].maxs[2] *= scale[2];
1174 for( i = 0; i < numBSPNodes; i++ )
1176 bspNodes[ i ].mins[0] *= scale[0];
1177 bspNodes[ i ].mins[1] *= scale[1];
1178 bspNodes[ i ].mins[2] *= scale[2];
1179 bspNodes[ i ].maxs[0] *= scale[0];
1180 bspNodes[ i ].maxs[1] *= scale[1];
1181 bspNodes[ i ].maxs[2] *= scale[2];
1185 for( i = 0; i < numBSPLeafs; i++ )
1187 bspLeafs[ i ].mins[0] *= scale[0];
1188 bspLeafs[ i ].mins[1] *= scale[1];
1189 bspLeafs[ i ].mins[2] *= scale[2];
1190 bspLeafs[ i ].maxs[0] *= scale[0];
1191 bspLeafs[ i ].maxs[1] *= scale[1];
1192 bspLeafs[ i ].maxs[2] *= scale[2];
1197 Sys_Printf("Using texture unlocking (and probably breaking texture alignment a lot)\n");
1198 old_xyzst = safe_malloc(sizeof(*old_xyzst) * numBSPDrawVerts * 5);
1199 for(i = 0; i < numBSPDrawVerts; i++)
1201 old_xyzst[5*i+0] = bspDrawVerts[i].xyz[0];
1202 old_xyzst[5*i+1] = bspDrawVerts[i].xyz[1];
1203 old_xyzst[5*i+2] = bspDrawVerts[i].xyz[2];
1204 old_xyzst[5*i+3] = bspDrawVerts[i].st[0];
1205 old_xyzst[5*i+4] = bspDrawVerts[i].st[1];
1209 /* scale drawverts */
1210 for( i = 0; i < numBSPDrawVerts; i++ )
1212 bspDrawVerts[i].xyz[0] *= scale[0];
1213 bspDrawVerts[i].xyz[1] *= scale[1];
1214 bspDrawVerts[i].xyz[2] *= scale[2];
1215 bspDrawVerts[i].normal[0] /= scale[0];
1216 bspDrawVerts[i].normal[1] /= scale[1];
1217 bspDrawVerts[i].normal[2] /= scale[2];
1218 VectorNormalize(bspDrawVerts[i].normal, bspDrawVerts[i].normal);
1223 for(i = 0; i < numBSPDrawSurfaces; i++)
1225 switch(bspDrawSurfaces[i].surfaceType)
1229 if(bspDrawSurfaces[i].numIndexes % 3)
1230 Error("Not a triangulation!");
1231 for(j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3)
1233 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j+1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j+2] + bspDrawSurfaces[i].firstVert;
1234 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
1235 float *oa = &old_xyzst[ia*5], *ob = &old_xyzst[ib*5], *oc = &old_xyzst[ic*5];
1240 ExtrapolateTexcoords(
1256 for( i = 0; i < numBSPPlanes; i++ )
1258 bspPlanes[ i ].dist *= scale[0];
1263 for( i = 0; i < numBSPPlanes; i++ )
1265 bspPlanes[ i ].normal[0] /= scale[0];
1266 bspPlanes[ i ].normal[1] /= scale[1];
1267 bspPlanes[ i ].normal[2] /= scale[2];
1268 f = 1/VectorLength(bspPlanes[i].normal);
1269 VectorScale(bspPlanes[i].normal, f, bspPlanes[i].normal);
1270 bspPlanes[ i ].dist *= f;
1274 /* scale gridsize */
1275 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1276 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
1277 VectorCopy( gridSize, vec );
1281 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1282 SetKeyValue( &entities[ 0 ], "gridsize", str );
1284 /* inject command line parameters */
1285 InjectCommandLine(argv, 0, argc - 1);
1289 StripExtension( source );
1290 DefaultExtension( source, "_s.bsp" );
1291 Sys_Printf( "Writing %s\n", source );
1292 WriteBSPFile( source );
1294 /* return to sender */
1302 main argument processing function for bsp conversion
1305 int ConvertBSPMain( int argc, char **argv )
1308 int (*convertFunc)( char * );
1309 game_t *convertGame;
1313 convertFunc = ConvertBSPToASE;
1319 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
1323 /* process arguments */
1324 for( i = 1; i < (argc - 1); i++ )
1326 /* -format map|ase|... */
1327 if( !strcmp( argv[ i ], "-format" ) )
1330 if( !Q_stricmp( argv[ i ], "ase" ) )
1331 convertFunc = ConvertBSPToASE;
1332 else if( !Q_stricmp( argv[ i ], "map" ) )
1333 convertFunc = ConvertBSPToMap;
1336 convertGame = GetGame( argv[ i ] );
1337 if( convertGame == NULL )
1338 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
1341 else if( !strcmp( argv[ i ], "-ne" ) )
1343 normalEpsilon = atof( argv[ i + 1 ] );
1345 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
1347 else if( !strcmp( argv[ i ], "-de" ) )
1349 distanceEpsilon = atof( argv[ i + 1 ] );
1351 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
1353 else if( !strcmp( argv[ i ], "-shadersasbitmap" ) )
1354 shadersAsBitmap = qtrue;
1357 /* clean up map name */
1358 strcpy( source, ExpandArg( argv[ i ] ) );
1359 StripExtension( source );
1360 DefaultExtension( source, ".bsp" );
1364 Sys_Printf( "Loading %s\n", source );
1366 /* ydnar: load surface file */
1367 //% LoadSurfaceExtraFile( source );
1369 LoadBSPFile( source );
1371 /* parse bsp entities */
1374 /* bsp format convert? */
1375 if( convertGame != NULL )
1377 /* set global game */
1381 StripExtension( source );
1382 DefaultExtension( source, "_c.bsp" );
1383 Sys_Printf( "Writing %s\n", source );
1384 WriteBSPFile( source );
1386 /* return to sender */
1390 /* normal convert */
1391 return convertFunc( source );
1401 int main( int argc, char **argv )
1407 /* we want consistent 'randomness' */
1411 start = I_FloatTime();
1413 /* this was changed to emit version number over the network */
1414 printf( Q3MAP_VERSION "\n" );
1417 atexit( ExitQ3Map );
1419 /* read general options first */
1420 for( i = 1; i < argc; i++ )
1423 if( !strcmp( argv[ i ], "-connect" ) )
1427 Broadcast_Setup( argv[ i ] );
1432 else if( !strcmp( argv[ i ], "-v" ) )
1442 else if( !strcmp( argv[ i ], "-force" ) )
1448 /* patch subdivisions */
1449 else if( !strcmp( argv[ i ], "-subdivisions" ) )
1453 patchSubdivisions = atoi( argv[ i ] );
1455 if( patchSubdivisions <= 0 )
1456 patchSubdivisions = 1;
1460 else if( !strcmp( argv[ i ], "-threads" ) )
1464 numthreads = atoi( argv[ i ] );
1469 /* init model library */
1471 PicoSetMallocFunc( safe_malloc );
1472 PicoSetFreeFunc( free );
1473 PicoSetPrintFunc( PicoPrintFunc );
1474 PicoSetLoadFileFunc( PicoLoadFileFunc );
1475 PicoSetFreeFileFunc( free );
1477 /* set number of threads */
1480 /* generate sinusoid jitter table */
1481 for( i = 0; i < MAX_JITTERS; i++ )
1483 jitters[ i ] = sin( i * 139.54152147 );
1484 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1487 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1488 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1490 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
1491 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1492 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1493 Sys_Printf( "%s\n", Q3MAP_MOTD );
1495 /* ydnar: new path initialization */
1496 InitPaths( &argc, argv );
1498 /* set game options */
1499 if (!patchSubdivisions)
1500 patchSubdivisions = game->patchSubdivisions;
1502 /* check if we have enough options left to attempt something */
1504 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1507 if( !strcmp( argv[ 1 ], "-fixaas" ) )
1508 r = FixAAS( argc - 1, argv + 1 );
1511 else if( !strcmp( argv[ 1 ], "-analyze" ) )
1512 r = AnalyzeBSP( argc - 1, argv + 1 );
1515 else if( !strcmp( argv[ 1 ], "-info" ) )
1516 r = BSPInfo( argc - 2, argv + 2 );
1519 else if( !strcmp( argv[ 1 ], "-vis" ) )
1520 r = VisMain( argc - 1, argv + 1 );
1523 else if( !strcmp( argv[ 1 ], "-light" ) )
1524 r = LightMain( argc - 1, argv + 1 );
1527 else if( !strcmp( argv[ 1 ], "-vlight" ) )
1529 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1530 argv[ 1 ] = "-fast"; /* eek a hack */
1531 r = LightMain( argc, argv );
1534 /* ydnar: lightmap export */
1535 else if( !strcmp( argv[ 1 ], "-export" ) )
1536 r = ExportLightmapsMain( argc - 1, argv + 1 );
1538 /* ydnar: lightmap import */
1539 else if( !strcmp( argv[ 1 ], "-import" ) )
1540 r = ImportLightmapsMain( argc - 1, argv + 1 );
1542 /* ydnar: bsp scaling */
1543 else if( !strcmp( argv[ 1 ], "-scale" ) )
1544 r = ScaleBSPMain( argc - 1, argv + 1 );
1546 /* ydnar: bsp conversion */
1547 else if( !strcmp( argv[ 1 ], "-convert" ) )
1548 r = ConvertBSPMain( argc - 1, argv + 1 );
1551 else if( !strcmp( argv[ 1 ], "-minimap" ) )
1552 r = MiniMapBSPMain(argc - 1, argv + 1);
1554 /* ydnar: otherwise create a bsp */
1556 r = BSPMain( argc, argv );
1559 end = I_FloatTime();
1560 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1562 /* shut down connection */
1563 Broadcast_Shutdown();
1565 /* return any error code */