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)
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 VectorSubtract(maxs, mins, extend);
319 VectorScale(extend, 1.0 / 64.0, extend);
321 VectorSubtract(mins, extend, mins);
322 VectorAdd(maxs, extend, maxs);
324 VectorCopy(mins, minimap.mins);
325 VectorSubtract(maxs, mins, minimap.size);
327 // line compatible to nexuiz mapinfo
328 Sys_Printf("size_texcoords %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
332 MiniMapSetupBrushes()
333 determines solid non-sky brushes in the world
336 void MiniMapSetupBrushes( void )
338 int i, j, b, compileFlags;
340 bspBrushSide_t *side;
346 Sys_FPrintf( SYS_VRB, "--- MiniMapSetupBrushes ---\n" );
349 if( opaqueBrushes == NULL )
350 opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 );
353 memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 );
354 numOpaqueBrushes = 0;
356 /* walk the list of worldspawn brushes */
357 for( i = 0; i < minimap.model->numBSPBrushes; i++ )
360 b = minimap.model->firstBSPBrush + i;
361 brush = &bspBrushes[ b ];
364 /* check all sides */
366 for( j = 0; j < brush->numSides; j++ )
368 /* do bsp shader calculations */
369 side = &bspBrushSides[ brush->firstSide + j ];
370 shader = &bspShaders[ side->shaderNum ];
372 /* get shader info */
373 si = ShaderInfoForShader( shader->shader );
377 /* or together compile flags */
378 compileFlags |= si->compileFlags;
381 shader = &bspShaders[ brush->shaderNum ];
382 si = ShaderInfoForShader( shader->shader );
386 compileFlags = si->compileFlags;
389 /* determine if this brush is solid */
390 if( (compileFlags & (C_SOLID | C_SKY)) == C_SOLID )
392 opaqueBrushes[ b >> 3 ] |= (1 << (b & 7));
398 /* emit some statistics */
399 Sys_FPrintf( SYS_VRB, "%9d solid brushes\n", numOpaqueBrushes );
402 qboolean MiniMapEvaluateSampleOffsets(int *bestj, int *bestk, float *bestval)
407 *bestj = *bestk = -1;
408 *bestval = 3; /* max possible val is 2 */
410 for(j = 0; j < minimap.samples; ++j)
411 for(k = j + 1; k < minimap.samples; ++k)
413 dx = minimap.sample_offsets[2*j+0] - minimap.sample_offsets[2*k+0];
414 dy = minimap.sample_offsets[2*j+1] - minimap.sample_offsets[2*k+1];
415 if(dx > +0.5) dx -= 1;
416 if(dx < -0.5) dx += 1;
417 if(dy > +0.5) dy -= 1;
418 if(dy < -0.5) dy += 1;
419 val = dx * dx + dy * dy;
431 void MiniMapMakeSampleOffsets()
434 float val, valj, valk, dx, dy, sx, sy, rx, ry;
436 Sys_Printf( "Generating good sample offsets (this may take a while)...\n" );
438 /* start with entirely random samples */
439 for(i = 0; i < minimap.samples; ++i)
441 minimap.sample_offsets[2*i+0] = Random();
442 minimap.sample_offsets[2*i+1] = Random();
445 for(i = 0; i < 1000; ++i)
447 if(MiniMapEvaluateSampleOffsets(&j, &k, &val))
449 sx = minimap.sample_offsets[2*j+0];
450 sy = minimap.sample_offsets[2*j+1];
451 minimap.sample_offsets[2*j+0] = rx = Random();
452 minimap.sample_offsets[2*j+1] = ry = Random();
453 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valj))
455 minimap.sample_offsets[2*j+0] = sx;
456 minimap.sample_offsets[2*j+1] = sy;
458 sx = minimap.sample_offsets[2*k+0];
459 sy = minimap.sample_offsets[2*k+1];
460 minimap.sample_offsets[2*k+0] = rx;
461 minimap.sample_offsets[2*k+1] = ry;
462 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valk))
464 minimap.sample_offsets[2*k+0] = sx;
465 minimap.sample_offsets[2*k+1] = sy;
471 /* valj is the greatest */
472 minimap.sample_offsets[2*j+0] = rx;
473 minimap.sample_offsets[2*j+1] = ry;
478 /* valj is the greater and it is useless - forget it */
485 /* valk is the greatest */
486 minimap.sample_offsets[2*k+0] = rx;
487 minimap.sample_offsets[2*k+1] = ry;
492 /* valk is the greater and it is useless - forget it */
501 int MiniMapBSPMain( int argc, char **argv )
503 char minimapFilename[1024];
506 char parentpath[1024];
507 float minimapSharpen;
517 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" );
521 /* load the BSP first */
522 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
523 StripExtension( source );
524 DefaultExtension( source, ".bsp" );
525 Sys_Printf( "Loading %s\n", source );
526 BeginMapShaderFile( source );
528 LoadBSPFile( source );
530 minimap.model = &bspModels[0];
531 VectorCopy(minimap.model->mins, mins);
532 VectorCopy(minimap.model->maxs, maxs);
533 *minimapFilename = 0;
535 minimap.width = minimap.height = 512;
537 minimap.sample_offsets = NULL;
539 /* process arguments */
540 for( i = 1; i < (argc - 1); i++ )
542 if( !strcmp( argv[ i ], "-size" ) )
544 minimap.width = minimap.height = atoi(argv[i + 1]);
546 Sys_Printf( "Image size set to %i\n", minimap.width );
548 else if( !strcmp( argv[ i ], "-sharpen" ) )
550 minimapSharpen = atof(argv[i + 1]);
552 Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
554 else if( !strcmp( argv[ i ], "-samples" ) )
556 minimap.samples = atoi(argv[i + 1]);
558 Sys_Printf( "Samples set to %i\n", minimap.samples );
559 if(minimap.sample_offsets)
560 free(minimap.sample_offsets);
561 minimap.sample_offsets = malloc(2 * sizeof(*minimap.sample_offsets) * minimap.samples);
562 MiniMapMakeSampleOffsets();
564 else if( !strcmp( argv[ i ], "-random" ) )
566 minimap.samples = atoi(argv[i + 1]);
568 Sys_Printf( "Random samples set to %i\n", minimap.samples );
569 if(minimap.sample_offsets)
570 free(minimap.sample_offsets);
571 minimap.sample_offsets = NULL;
573 else if( !strcmp( argv[ i ], "-o" ) )
575 strcpy(minimapFilename, argv[i + 1]);
577 Sys_Printf( "Output file name set to %s\n", minimapFilename );
579 else if( !strcmp( argv[ i ], "-minmax" ) && i < (argc - 7) )
581 mins[0] = atof(argv[i + 1]);
582 mins[1] = atof(argv[i + 2]);
583 mins[2] = atof(argv[i + 3]);
584 maxs[0] = atof(argv[i + 4]);
585 maxs[1] = atof(argv[i + 5]);
586 maxs[2] = atof(argv[i + 6]);
588 Sys_Printf( "Map mins/maxs overridden\n" );
592 MiniMapMakeMinsMaxs(mins, maxs);
594 if(!*minimapFilename)
596 ExtractFileBase(source, basename);
597 ExtractFilePath(source, path);
599 path[strlen(path)-1] = 0;
600 ExtractFilePath(path, parentpath);
601 sprintf(minimapFilename, "%sgfx", parentpath);
602 Q_mkdir(minimapFilename);
603 sprintf(minimapFilename, "%sgfx/%s_mini.tga", parentpath, basename);
604 Sys_Printf("Output file name automatically set to %s\n", minimapFilename);
607 if(minimapSharpen >= 0)
609 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
610 minimap.sharpen_boxmult = -minimapSharpen;
613 minimap.data1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
614 data3b = safe_malloc(minimap.width * minimap.height * 3);
615 if(minimapSharpen >= 0)
616 minimap.sharpendata1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
618 MiniMapSetupBrushes();
620 if(minimap.samples <= 1)
622 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
623 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapNoSupersampling);
627 if(minimap.sample_offsets)
629 Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
630 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSupersampled);
634 Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
635 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapRandomlySupersampled);
639 if(minimap.sharpendata1f)
641 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
642 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSharpen);
643 q = minimap.sharpendata1f;
650 Sys_Printf( "\nConverting...");
652 for(y = 0; y < minimap.height; ++y)
653 for(x = 0; x < minimap.width; ++x)
658 if(v > 255.0/256.0) v = 255.0/256.0;
665 Sys_Printf( " writing to %s...", minimapFilename );
666 WriteTGA24(minimapFilename, data3b, minimap.width, minimap.height, qfalse);
668 Sys_Printf( " done.\n" );
670 /* return to sender */
680 calculates an md4 checksum for a block of data
683 static int MD4BlockChecksum( void *buffer, int length )
685 return Com_BlockChecksum(buffer, length);
690 resets an aas checksum to match the given BSP
693 int FixAAS( int argc, char **argv )
695 int length, checksum;
698 char aas[ 1024 ], **ext;
711 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
715 /* do some path mangling */
716 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
717 StripExtension( source );
718 DefaultExtension( source, ".bsp" );
721 Sys_Printf( "--- FixAAS ---\n" );
724 Sys_Printf( "Loading %s\n", source );
725 length = LoadFile( source, &buffer );
727 /* create bsp checksum */
728 Sys_Printf( "Creating checksum...\n" );
729 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
731 /* write checksum to aas */
736 strcpy( aas, source );
737 StripExtension( aas );
739 Sys_Printf( "Trying %s\n", aas );
743 file = fopen( aas, "r+b" );
746 if( fwrite( &checksum, 4, 1, file ) != 1 )
747 Error( "Error writing checksum to %s", aas );
751 /* return to sender */
759 analyzes a Quake engine BSP file
762 typedef struct abspHeader_s
767 bspLump_t lumps[ 1 ]; /* unknown size */
771 typedef struct abspLumpTest_s
778 int AnalyzeBSP( int argc, char **argv )
780 abspHeader_t *header;
781 int size, i, version, offset, length, lumpInt, count;
785 char lumpString[ 1024 ], source[ 1024 ];
786 qboolean lumpSwap = qfalse;
787 abspLumpTest_t *lumpTest;
788 static abspLumpTest_t lumpTests[] =
790 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
791 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
792 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
793 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
794 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
795 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
796 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
797 { 104, 3, "IBSP LUMP_DRAWSURFS" },
798 { 44, 3, "IBSP LUMP_DRAWVERTS" },
799 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
800 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
801 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
802 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
810 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
814 /* process arguments */
815 for( i = 1; i < (argc - 1); i++ )
817 /* -format map|ase|... */
818 if( !strcmp( argv[ i ], "-lumpswap" ) )
820 Sys_Printf( "Swapped lump structs enabled\n" );
825 /* clean up map name */
826 strcpy( source, ExpandArg( argv[ i ] ) );
827 Sys_Printf( "Loading %s\n", source );
830 size = LoadFile( source, (void**) &header );
831 if( size == 0 || header == NULL )
833 Sys_Printf( "Unable to load %s.\n", source );
837 /* analyze ident/version */
838 memcpy( ident, header->ident, 4 );
840 version = LittleLong( header->version );
842 Sys_Printf( "Identity: %s\n", ident );
843 Sys_Printf( "Version: %d\n", version );
844 Sys_Printf( "---------------------------------------\n" );
846 /* analyze each lump */
847 for( i = 0; i < 100; i++ )
849 /* call of duty swapped lump pairs */
852 offset = LittleLong( header->lumps[ i ].length );
853 length = LittleLong( header->lumps[ i ].offset );
856 /* standard lump pairs */
859 offset = LittleLong( header->lumps[ i ].offset );
860 length = LittleLong( header->lumps[ i ].length );
864 lump = (byte*) header + offset;
865 lumpInt = LittleLong( (int) *((int*) lump) );
866 lumpFloat = LittleFloat( (float) *((float*) lump) );
867 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
868 lumpString[ 1024 ] = '\0';
870 /* print basic lump info */
871 Sys_Printf( "Lump: %d\n", i );
872 Sys_Printf( "Offset: %d bytes\n", offset );
873 Sys_Printf( "Length: %d bytes\n", length );
875 /* only operate on valid lumps */
878 /* print data in 4 formats */
879 Sys_Printf( "As hex: %08X\n", lumpInt );
880 Sys_Printf( "As int: %d\n", lumpInt );
881 Sys_Printf( "As float: %f\n", lumpFloat );
882 Sys_Printf( "As string: %s\n", lumpString );
884 /* guess lump type */
885 if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
886 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
887 else if( strstr( lumpString, "textures/" ) )
888 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
891 /* guess based on size/count */
892 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
894 if( (length % lumpTest->radix) != 0 )
896 count = length / lumpTest->radix;
897 if( count < lumpTest->minCount )
899 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
904 Sys_Printf( "---------------------------------------\n" );
907 if( offset + length >= size )
912 Sys_Printf( "Lump count: %d\n", i + 1 );
913 Sys_Printf( "File size: %d bytes\n", size );
915 /* return to caller */
923 emits statistics about the bsp file
926 int BSPInfo( int count, char **fileNames )
929 char source[ 1024 ], ext[ 64 ];
937 Sys_Printf( "No files to dump info for.\n");
941 /* enable info mode */
945 for( i = 0; i < count; i++ )
947 Sys_Printf( "---------------------------------\n" );
949 /* mangle filename and get size */
950 strcpy( source, fileNames[ i ] );
951 ExtractFileExtension( source, ext );
952 if( !Q_stricmp( ext, "map" ) )
953 StripExtension( source );
954 DefaultExtension( source, ".bsp" );
955 f = fopen( source, "rb" );
958 size = Q_filelength (f);
964 /* load the bsp file and print lump sizes */
965 Sys_Printf( "%s\n", source );
966 LoadBSPFile( source );
971 Sys_Printf( " total %9d\n", size );
972 Sys_Printf( " %9d KB\n", size / 1024 );
973 Sys_Printf( " %9d MB\n", size / (1024 * 1024) );
975 Sys_Printf( "---------------------------------\n" );
983 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)
985 vec4_t scoeffs, tcoeffs;
991 VectorSubtract(bxyz, axyz, dab);
992 VectorSubtract(cxyz, axyz, dac);
993 CrossProduct(dab, dac, norm);
997 // s(v + norm) = s(v) when n ortho xyz
999 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
1002 // scoeffs * (axyz, 1) == ast[0]
1003 // scoeffs * (bxyz, 1) == bst[0]
1004 // scoeffs * (cxyz, 1) == cst[0]
1005 // scoeffs * (norm, 0) == 0
1006 // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
1007 solvematrix[0] = axyz[0];
1008 solvematrix[4] = axyz[1];
1009 solvematrix[8] = axyz[2];
1010 solvematrix[12] = 1;
1011 solvematrix[1] = bxyz[0];
1012 solvematrix[5] = bxyz[1];
1013 solvematrix[9] = bxyz[2];
1014 solvematrix[13] = 1;
1015 solvematrix[2] = cxyz[0];
1016 solvematrix[6] = cxyz[1];
1017 solvematrix[10] = cxyz[2];
1018 solvematrix[14] = 1;
1019 solvematrix[3] = norm[0];
1020 solvematrix[7] = norm[1];
1021 solvematrix[11] = norm[2];
1022 solvematrix[15] = 0;
1024 md = m4_det(solvematrix);
1027 Sys_Printf("Cannot invert some matrix, some texcoords aren't extrapolated!");
1031 m4x4_invert(solvematrix);
1033 scoeffs[0] = ast[0];
1034 scoeffs[1] = bst[0];
1035 scoeffs[2] = cst[0];
1037 m4x4_transform_vec4(solvematrix, scoeffs);
1038 tcoeffs[0] = ast[1];
1039 tcoeffs[1] = bst[1];
1040 tcoeffs[2] = cst[1];
1042 m4x4_transform_vec4(solvematrix, tcoeffs);
1044 ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
1045 ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
1046 bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
1047 bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
1048 cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
1049 cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
1054 amaze and confuse your enemies with wierd scaled maps!
1057 int ScaleBSPMain( int argc, char **argv )
1066 float *old_xyzst = NULL;
1072 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1077 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
1079 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
1081 scale[0] = atof( argv[ argc - 4 ] );
1083 texscale = !strcmp(argv[1], "-tex");
1085 uniform = ((scale[0] == scale[1]) && (scale[1] == scale[2]));
1087 if( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f )
1089 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1090 Sys_Printf( "Non-zero scale value required.\n" );
1094 /* do some path mangling */
1095 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
1096 StripExtension( source );
1097 DefaultExtension( source, ".bsp" );
1100 Sys_Printf( "Loading %s\n", source );
1101 LoadBSPFile( source );
1105 Sys_Printf( "--- ScaleBSP ---\n" );
1106 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
1108 /* scale entity keys */
1109 for( i = 0; i < numBSPEntities && i < numEntities; i++ )
1112 GetVectorForKey( &entities[ i ], "origin", vec );
1113 if( (vec[ 0 ] || vec[ 1 ] || vec[ 2 ]) )
1118 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1119 SetKeyValue( &entities[ i ], "origin", str );
1122 a = FloatForKey( &entities[ i ], "angle" );
1123 if(a == -1 || a == -2) // z scale
1125 else if(fabs(sin(DEG2RAD(a))) < 0.707)
1130 /* scale door lip */
1131 f = FloatForKey( &entities[ i ], "lip" );
1135 sprintf( str, "%f", f );
1136 SetKeyValue( &entities[ i ], "lip", str );
1139 /* scale plat height */
1140 f = FloatForKey( &entities[ i ], "height" );
1144 sprintf( str, "%f", f );
1145 SetKeyValue( &entities[ i ], "height", str );
1148 // TODO maybe allow a definition file for entities to specify which values are scaled how?
1152 for( i = 0; i < numBSPModels; i++ )
1154 bspModels[ i ].mins[0] *= scale[0];
1155 bspModels[ i ].mins[1] *= scale[1];
1156 bspModels[ i ].mins[2] *= scale[2];
1157 bspModels[ i ].maxs[0] *= scale[0];
1158 bspModels[ i ].maxs[1] *= scale[1];
1159 bspModels[ i ].maxs[2] *= scale[2];
1163 for( i = 0; i < numBSPNodes; i++ )
1165 bspNodes[ i ].mins[0] *= scale[0];
1166 bspNodes[ i ].mins[1] *= scale[1];
1167 bspNodes[ i ].mins[2] *= scale[2];
1168 bspNodes[ i ].maxs[0] *= scale[0];
1169 bspNodes[ i ].maxs[1] *= scale[1];
1170 bspNodes[ i ].maxs[2] *= scale[2];
1174 for( i = 0; i < numBSPLeafs; i++ )
1176 bspLeafs[ i ].mins[0] *= scale[0];
1177 bspLeafs[ i ].mins[1] *= scale[1];
1178 bspLeafs[ i ].mins[2] *= scale[2];
1179 bspLeafs[ i ].maxs[0] *= scale[0];
1180 bspLeafs[ i ].maxs[1] *= scale[1];
1181 bspLeafs[ i ].maxs[2] *= scale[2];
1186 Sys_Printf("Using texture unlocking (and probably breaking texture alignment a lot)\n");
1187 old_xyzst = safe_malloc(sizeof(*old_xyzst) * numBSPDrawVerts * 5);
1188 for(i = 0; i < numBSPDrawVerts; i++)
1190 old_xyzst[5*i+0] = bspDrawVerts[i].xyz[0];
1191 old_xyzst[5*i+1] = bspDrawVerts[i].xyz[1];
1192 old_xyzst[5*i+2] = bspDrawVerts[i].xyz[2];
1193 old_xyzst[5*i+3] = bspDrawVerts[i].st[0];
1194 old_xyzst[5*i+4] = bspDrawVerts[i].st[1];
1198 /* scale drawverts */
1199 for( i = 0; i < numBSPDrawVerts; i++ )
1201 bspDrawVerts[i].xyz[0] *= scale[0];
1202 bspDrawVerts[i].xyz[1] *= scale[1];
1203 bspDrawVerts[i].xyz[2] *= scale[2];
1204 bspDrawVerts[i].normal[0] /= scale[0];
1205 bspDrawVerts[i].normal[1] /= scale[1];
1206 bspDrawVerts[i].normal[2] /= scale[2];
1207 VectorNormalize(bspDrawVerts[i].normal, bspDrawVerts[i].normal);
1212 for(i = 0; i < numBSPDrawSurfaces; i++)
1214 switch(bspDrawSurfaces[i].surfaceType)
1218 if(bspDrawSurfaces[i].numIndexes % 3)
1219 Error("Not a triangulation!");
1220 for(j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3)
1222 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j+1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j+2] + bspDrawSurfaces[i].firstVert;
1223 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
1224 float *oa = &old_xyzst[ia*5], *ob = &old_xyzst[ib*5], *oc = &old_xyzst[ic*5];
1229 ExtrapolateTexcoords(
1245 for( i = 0; i < numBSPPlanes; i++ )
1247 bspPlanes[ i ].dist *= scale[0];
1252 for( i = 0; i < numBSPPlanes; i++ )
1254 bspPlanes[ i ].normal[0] /= scale[0];
1255 bspPlanes[ i ].normal[1] /= scale[1];
1256 bspPlanes[ i ].normal[2] /= scale[2];
1257 f = 1/VectorLength(bspPlanes[i].normal);
1258 VectorScale(bspPlanes[i].normal, f, bspPlanes[i].normal);
1259 bspPlanes[ i ].dist *= f;
1263 /* scale gridsize */
1264 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1265 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
1266 VectorCopy( gridSize, vec );
1270 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1271 SetKeyValue( &entities[ 0 ], "gridsize", str );
1273 /* inject command line parameters */
1274 InjectCommandLine(argv, 0, argc - 1);
1278 StripExtension( source );
1279 DefaultExtension( source, "_s.bsp" );
1280 Sys_Printf( "Writing %s\n", source );
1281 WriteBSPFile( source );
1283 /* return to sender */
1291 main argument processing function for bsp conversion
1294 int ConvertBSPMain( int argc, char **argv )
1297 int (*convertFunc)( char * );
1298 game_t *convertGame;
1302 convertFunc = ConvertBSPToASE;
1308 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
1312 /* process arguments */
1313 for( i = 1; i < (argc - 1); i++ )
1315 /* -format map|ase|... */
1316 if( !strcmp( argv[ i ], "-format" ) )
1319 if( !Q_stricmp( argv[ i ], "ase" ) )
1320 convertFunc = ConvertBSPToASE;
1321 else if( !Q_stricmp( argv[ i ], "map" ) )
1322 convertFunc = ConvertBSPToMap;
1325 convertGame = GetGame( argv[ i ] );
1326 if( convertGame == NULL )
1327 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
1330 else if( !strcmp( argv[ i ], "-ne" ) )
1332 normalEpsilon = atof( argv[ i + 1 ] );
1334 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
1336 else if( !strcmp( argv[ i ], "-de" ) )
1338 distanceEpsilon = atof( argv[ i + 1 ] );
1340 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
1342 else if( !strcmp( argv[ i ], "-shadersasbitmap" ) )
1343 shadersAsBitmap = qtrue;
1346 /* clean up map name */
1347 strcpy( source, ExpandArg( argv[ i ] ) );
1348 StripExtension( source );
1349 DefaultExtension( source, ".bsp" );
1353 Sys_Printf( "Loading %s\n", source );
1355 /* ydnar: load surface file */
1356 //% LoadSurfaceExtraFile( source );
1358 LoadBSPFile( source );
1360 /* parse bsp entities */
1363 /* bsp format convert? */
1364 if( convertGame != NULL )
1366 /* set global game */
1370 StripExtension( source );
1371 DefaultExtension( source, "_c.bsp" );
1372 Sys_Printf( "Writing %s\n", source );
1373 WriteBSPFile( source );
1375 /* return to sender */
1379 /* normal convert */
1380 return convertFunc( source );
1390 int main( int argc, char **argv )
1396 /* we want consistent 'randomness' */
1400 start = I_FloatTime();
1402 /* this was changed to emit version number over the network */
1403 printf( Q3MAP_VERSION "\n" );
1406 atexit( ExitQ3Map );
1408 /* read general options first */
1409 for( i = 1; i < argc; i++ )
1412 if( !strcmp( argv[ i ], "-connect" ) )
1416 Broadcast_Setup( argv[ i ] );
1421 else if( !strcmp( argv[ i ], "-v" ) )
1431 else if( !strcmp( argv[ i ], "-force" ) )
1437 /* patch subdivisions */
1438 else if( !strcmp( argv[ i ], "-subdivisions" ) )
1442 patchSubdivisions = atoi( argv[ i ] );
1444 if( patchSubdivisions <= 0 )
1445 patchSubdivisions = 1;
1449 else if( !strcmp( argv[ i ], "-threads" ) )
1453 numthreads = atoi( argv[ i ] );
1458 /* init model library */
1460 PicoSetMallocFunc( safe_malloc );
1461 PicoSetFreeFunc( free );
1462 PicoSetPrintFunc( PicoPrintFunc );
1463 PicoSetLoadFileFunc( PicoLoadFileFunc );
1464 PicoSetFreeFileFunc( free );
1466 /* set number of threads */
1469 /* generate sinusoid jitter table */
1470 for( i = 0; i < MAX_JITTERS; i++ )
1472 jitters[ i ] = sin( i * 139.54152147 );
1473 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1476 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1477 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1479 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
1480 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1481 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1482 Sys_Printf( "%s\n", Q3MAP_MOTD );
1484 /* ydnar: new path initialization */
1485 InitPaths( &argc, argv );
1487 /* set game options */
1488 if (!patchSubdivisions)
1489 patchSubdivisions = game->patchSubdivisions;
1491 /* check if we have enough options left to attempt something */
1493 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1496 if( !strcmp( argv[ 1 ], "-fixaas" ) )
1497 r = FixAAS( argc - 1, argv + 1 );
1500 else if( !strcmp( argv[ 1 ], "-analyze" ) )
1501 r = AnalyzeBSP( argc - 1, argv + 1 );
1504 else if( !strcmp( argv[ 1 ], "-info" ) )
1505 r = BSPInfo( argc - 2, argv + 2 );
1508 else if( !strcmp( argv[ 1 ], "-vis" ) )
1509 r = VisMain( argc - 1, argv + 1 );
1512 else if( !strcmp( argv[ 1 ], "-light" ) )
1513 r = LightMain( argc - 1, argv + 1 );
1516 else if( !strcmp( argv[ 1 ], "-vlight" ) )
1518 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1519 argv[ 1 ] = "-fast"; /* eek a hack */
1520 r = LightMain( argc, argv );
1523 /* ydnar: lightmap export */
1524 else if( !strcmp( argv[ 1 ], "-export" ) )
1525 r = ExportLightmapsMain( argc - 1, argv + 1 );
1527 /* ydnar: lightmap import */
1528 else if( !strcmp( argv[ 1 ], "-import" ) )
1529 r = ImportLightmapsMain( argc - 1, argv + 1 );
1531 /* ydnar: bsp scaling */
1532 else if( !strcmp( argv[ 1 ], "-scale" ) )
1533 r = ScaleBSPMain( argc - 1, argv + 1 );
1535 /* ydnar: bsp conversion */
1536 else if( !strcmp( argv[ 1 ], "-convert" ) )
1537 r = ConvertBSPMain( argc - 1, argv + 1 );
1540 else if( !strcmp( argv[ 1 ], "-minimap" ) )
1541 r = MiniMapBSPMain(argc - 1, argv + 1);
1543 /* ydnar: otherwise create a bsp */
1545 r = BSPMain( argc, argv );
1548 end = I_FloatTime();
1549 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1551 /* shut down connection */
1552 Broadcast_Shutdown();
1554 /* return any error code */