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 void RandomVector2f(float v[2])
184 v[0] = 2 * Random() - 1;
185 v[1] = 2 * Random() - 1;
187 while(v[0] * v[0] + v[1] * v[1] > 1);
190 static void MiniMapRandomlySupersampled(int y)
193 float *p = &minimap.data1f[y * minimap.width];
194 float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
195 float dx = minimap.size[0] / (float) minimap.width;
196 float dy = minimap.size[1] / (float) minimap.height;
199 for(x = 0; x < minimap.width; ++x)
201 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
204 for(i = 0; i < minimap.samples; ++i)
207 float thisval = MiniMapSample(
208 xmin + (uv[0] + 0.5) * dx, /* exaggerated random pattern for better results */
209 ymin + (uv[1] + 0.5) * dy /* exaggerated random pattern for better results */
213 val /= minimap.samples * minimap.size[2];
218 static void MiniMapSupersampled(int y)
221 float *p = &minimap.data1f[y * minimap.width];
222 float ymin = minimap.mins[1] + minimap.size[1] * (y / (float) minimap.height);
223 float dx = minimap.size[0] / (float) minimap.width;
224 float dy = minimap.size[1] / (float) minimap.height;
226 for(x = 0; x < minimap.width; ++x)
228 float xmin = minimap.mins[0] + minimap.size[0] * (x / (float) minimap.width);
231 for(i = 0; i < minimap.samples; ++i)
233 float thisval = MiniMapSample(
234 xmin + minimap.sample_offsets[2*i+0] * dx,
235 ymin + minimap.sample_offsets[2*i+1] * dy
239 val /= minimap.samples * minimap.size[2];
244 static void MiniMapNoSupersampling(int y)
247 float *p = &minimap.data1f[y * minimap.width];
248 float ymin = minimap.mins[1] + minimap.size[1] * ((y + 0.5) / (float) minimap.height);
250 for(x = 0; x < minimap.width; ++x)
252 float xmin = minimap.mins[0] + minimap.size[0] * ((x + 0.5) / (float) minimap.width);
253 *p++ = MiniMapSample(xmin, ymin) / minimap.size[2];
257 static void MiniMapSharpen(int y)
260 qboolean up = (y > 0);
261 qboolean down = (y < minimap.height - 1);
262 float *p = &minimap.data1f[y * minimap.width];
263 float *q = &minimap.sharpendata1f[y * minimap.width];
265 for(x = 0; x < minimap.width; ++x)
267 qboolean left = (x > 0);
268 qboolean right = (x < minimap.width - 1);
269 float val = p[0] * minimap.sharpen_centermult;
272 val += p[-1 -minimap.width] * minimap.sharpen_boxmult;
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;
281 val += p[-1] * minimap.sharpen_boxmult;
283 val += p[+1] * minimap.sharpen_boxmult;
285 val += p[-minimap.width] * minimap.sharpen_boxmult;
287 val += p[+minimap.width] * minimap.sharpen_boxmult;
294 void MiniMapMakeMinsMaxs(vec3_t mins_in, vec3_t maxs_in)
296 vec3_t mins, maxs, extend;
297 VectorCopy(mins_in, mins);
298 VectorCopy(maxs_in, maxs);
300 // line compatible to nexuiz mapinfo
301 Sys_Printf("size %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
303 VectorSubtract(maxs, mins, extend);
305 if(extend[1] > extend[0])
307 mins[0] -= (extend[1] - extend[0]) * 0.5;
308 maxs[0] += (extend[1] - extend[0]) * 0.5;
312 mins[1] -= (extend[0] - extend[1]) * 0.5;
313 maxs[1] += (extend[0] - extend[1]) * 0.5;
316 VectorSubtract(maxs, mins, extend);
317 VectorScale(extend, 1.0 / 64.0, extend);
319 VectorSubtract(mins, extend, mins);
320 VectorAdd(maxs, extend, maxs);
322 VectorCopy(mins, minimap.mins);
323 VectorSubtract(maxs, mins, minimap.size);
325 // line compatible to nexuiz mapinfo
326 Sys_Printf("size_texcoords %f %f %f %f %f %f\n", mins[0], mins[1], mins[2], maxs[0], maxs[1], maxs[2]);
330 MiniMapSetupBrushes()
331 determines solid non-sky brushes in the world
334 void MiniMapSetupBrushes( void )
336 int i, j, b, compileFlags;
338 bspBrushSide_t *side;
344 Sys_FPrintf( SYS_VRB, "--- MiniMapSetupBrushes ---\n" );
347 if( opaqueBrushes == NULL )
348 opaqueBrushes = safe_malloc( numBSPBrushes / 8 + 1 );
351 memset( opaqueBrushes, 0, numBSPBrushes / 8 + 1 );
352 numOpaqueBrushes = 0;
354 /* walk the list of worldspawn brushes */
355 for( i = 0; i < minimap.model->numBSPBrushes; i++ )
358 b = minimap.model->firstBSPBrush + i;
359 brush = &bspBrushes[ b ];
362 /* check all sides */
364 for( j = 0; j < brush->numSides; j++ )
366 /* do bsp shader calculations */
367 side = &bspBrushSides[ brush->firstSide + j ];
368 shader = &bspShaders[ side->shaderNum ];
370 /* get shader info */
371 si = ShaderInfoForShader( shader->shader );
375 /* or together compile flags */
376 compileFlags |= si->compileFlags;
379 shader = &bspShaders[ brush->shaderNum ];
380 si = ShaderInfoForShader( shader->shader );
384 compileFlags = si->compileFlags;
387 /* determine if this brush is solid */
388 if( (compileFlags & (C_SOLID | C_SKY)) == C_SOLID )
390 opaqueBrushes[ b >> 3 ] |= (1 << (b & 7));
396 /* emit some statistics */
397 Sys_FPrintf( SYS_VRB, "%9d solid brushes\n", numOpaqueBrushes );
400 qboolean MiniMapEvaluateSampleOffsets(int *bestj, int *bestk, float *bestval)
405 *bestj = *bestk = -1;
406 *bestval = 3; /* max possible val is 2 */
408 for(j = 0; j < minimap.samples; ++j)
409 for(k = j + 1; k < minimap.samples; ++k)
411 dx = minimap.sample_offsets[2*j+0] - minimap.sample_offsets[2*k+0];
412 dy = minimap.sample_offsets[2*j+1] - minimap.sample_offsets[2*k+1];
413 if(dx > +0.5) dx -= 1;
414 if(dx < -0.5) dx += 1;
415 if(dy > +0.5) dy -= 1;
416 if(dy < -0.5) dy += 1;
417 val = dx * dx + dy * dy;
429 void MiniMapMakeSampleOffsets()
432 float val, valj, valk, dx, dy, sx, sy, rx, ry;
434 Sys_Printf( "Generating good sample offsets (this may take a while)...\n" );
436 /* start with entirely random samples */
437 for(i = 0; i < minimap.samples; ++i)
439 minimap.sample_offsets[2*i+0] = Random();
440 minimap.sample_offsets[2*i+1] = Random();
443 for(i = 0; i < 1000; ++i)
445 if(MiniMapEvaluateSampleOffsets(&j, &k, &val))
447 sx = minimap.sample_offsets[2*j+0];
448 sy = minimap.sample_offsets[2*j+1];
449 minimap.sample_offsets[2*j+0] = rx = Random();
450 minimap.sample_offsets[2*j+1] = ry = Random();
451 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valj))
453 minimap.sample_offsets[2*j+0] = sx;
454 minimap.sample_offsets[2*j+1] = sy;
456 sx = minimap.sample_offsets[2*k+0];
457 sy = minimap.sample_offsets[2*k+1];
458 minimap.sample_offsets[2*k+0] = rx;
459 minimap.sample_offsets[2*k+1] = ry;
460 if(!MiniMapEvaluateSampleOffsets(&jj, &kk, &valk))
462 minimap.sample_offsets[2*k+0] = sx;
463 minimap.sample_offsets[2*k+1] = sy;
469 /* valj is the greatest */
470 minimap.sample_offsets[2*j+0] = rx;
471 minimap.sample_offsets[2*j+1] = ry;
476 /* valj is the greater and it is useless - forget it */
483 /* valk is the greatest */
484 minimap.sample_offsets[2*k+0] = rx;
485 minimap.sample_offsets[2*k+1] = ry;
490 /* valk is the greater and it is useless - forget it */
499 int MiniMapBSPMain( int argc, char **argv )
501 char minimapFilename[1024];
504 char parentpath[1024];
505 float minimapSharpen;
515 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" );
519 /* load the BSP first */
520 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
521 StripExtension( source );
522 DefaultExtension( source, ".bsp" );
523 Sys_Printf( "Loading %s\n", source );
524 BeginMapShaderFile( source );
526 LoadBSPFile( source );
528 minimap.model = &bspModels[0];
529 VectorCopy(minimap.model->mins, mins);
530 VectorCopy(minimap.model->maxs, maxs);
531 *minimapFilename = 0;
533 minimap.width = minimap.height = 512;
535 minimap.sample_offsets = NULL;
537 /* process arguments */
538 for( i = 1; i < (argc - 1); i++ )
540 if( !strcmp( argv[ i ], "-size" ) )
542 minimap.width = minimap.height = atoi(argv[i + 1]);
544 Sys_Printf( "Image size set to %i\n", minimap.width );
546 else if( !strcmp( argv[ i ], "-sharpen" ) )
548 minimapSharpen = atof(argv[i + 1]);
550 Sys_Printf( "Sharpening coefficient set to %f\n", minimapSharpen );
552 else if( !strcmp( argv[ i ], "-samples" ) )
554 minimap.samples = atoi(argv[i + 1]);
556 Sys_Printf( "Samples set to %i\n", minimap.samples );
557 if(minimap.sample_offsets)
558 free(minimap.sample_offsets);
559 minimap.sample_offsets = malloc(2 * sizeof(*minimap.sample_offsets) * minimap.samples);
560 MiniMapMakeSampleOffsets();
562 else if( !strcmp( argv[ i ], "-random" ) )
564 minimap.samples = atoi(argv[i + 1]);
566 Sys_Printf( "Random samples set to %i\n", minimap.samples );
567 if(minimap.sample_offsets)
568 free(minimap.sample_offsets);
569 minimap.sample_offsets = NULL;
571 else if( !strcmp( argv[ i ], "-o" ) )
573 strcpy(minimapFilename, argv[i + 1]);
575 Sys_Printf( "Output file name set to %s\n", minimapFilename );
577 else if( !strcmp( argv[ i ], "-minmax" ) && i < (argc - 7) )
579 mins[0] = atof(argv[i + 1]);
580 mins[1] = atof(argv[i + 2]);
581 mins[2] = atof(argv[i + 3]);
582 maxs[0] = atof(argv[i + 4]);
583 maxs[1] = atof(argv[i + 5]);
584 maxs[2] = atof(argv[i + 6]);
586 Sys_Printf( "Map mins/maxs overridden\n" );
590 MiniMapMakeMinsMaxs(mins, maxs);
592 if(!*minimapFilename)
594 ExtractFileBase(source, basename);
595 ExtractFilePath(source, path);
597 path[strlen(path)-1] = 0;
598 ExtractFilePath(path, parentpath);
599 sprintf(minimapFilename, "%sgfx", parentpath);
600 Q_mkdir(minimapFilename);
601 sprintf(minimapFilename, "%sgfx/%s_mini.tga", parentpath, basename);
602 Sys_Printf("Output file name automatically set to %s\n", minimapFilename);
605 if(minimapSharpen >= 0)
607 minimap.sharpen_centermult = 8 * minimapSharpen + 1;
608 minimap.sharpen_boxmult = -minimapSharpen;
611 minimap.data1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
612 data3b = safe_malloc(minimap.width * minimap.height * 3);
613 if(minimapSharpen >= 0)
614 minimap.sharpendata1f = safe_malloc(minimap.width * minimap.height * sizeof(*minimap.data1f));
616 MiniMapSetupBrushes();
618 if(minimap.samples <= 1)
620 Sys_Printf( "\n--- MiniMapNoSupersampling (%d) ---\n", minimap.height );
621 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapNoSupersampling);
625 if(minimap.sample_offsets)
627 Sys_Printf( "\n--- MiniMapSupersampled (%d) ---\n", minimap.height );
628 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSupersampled);
632 Sys_Printf( "\n--- MiniMapRandomlySupersampled (%d) ---\n", minimap.height );
633 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapRandomlySupersampled);
637 if(minimap.sharpendata1f)
639 Sys_Printf( "\n--- MiniMapSharpen (%d) ---\n", minimap.height );
640 RunThreadsOnIndividual(minimap.height, qtrue, MiniMapSharpen);
641 q = minimap.sharpendata1f;
648 Sys_Printf( "\nConverting...");
650 for(y = 0; y < minimap.height; ++y)
651 for(x = 0; x < minimap.width; ++x)
656 if(v > 255.0/256.0) v = 255.0/256.0;
663 Sys_Printf( " writing to %s...", minimapFilename );
664 WriteTGA24(minimapFilename, data3b, minimap.width, minimap.height, qfalse);
666 Sys_Printf( " done.\n" );
668 /* return to sender */
678 calculates an md4 checksum for a block of data
681 static int MD4BlockChecksum( void *buffer, int length )
683 return Com_BlockChecksum(buffer, length);
688 resets an aas checksum to match the given BSP
691 int FixAAS( int argc, char **argv )
693 int length, checksum;
696 char aas[ 1024 ], **ext;
709 Sys_Printf( "Usage: q3map -fixaas [-v] <mapname>\n" );
713 /* do some path mangling */
714 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
715 StripExtension( source );
716 DefaultExtension( source, ".bsp" );
719 Sys_Printf( "--- FixAAS ---\n" );
722 Sys_Printf( "Loading %s\n", source );
723 length = LoadFile( source, &buffer );
725 /* create bsp checksum */
726 Sys_Printf( "Creating checksum...\n" );
727 checksum = LittleLong( MD4BlockChecksum( buffer, length ) );
729 /* write checksum to aas */
734 strcpy( aas, source );
735 StripExtension( aas );
737 Sys_Printf( "Trying %s\n", aas );
741 file = fopen( aas, "r+b" );
744 if( fwrite( &checksum, 4, 1, file ) != 1 )
745 Error( "Error writing checksum to %s", aas );
749 /* return to sender */
757 analyzes a Quake engine BSP file
760 typedef struct abspHeader_s
765 bspLump_t lumps[ 1 ]; /* unknown size */
769 typedef struct abspLumpTest_s
776 int AnalyzeBSP( int argc, char **argv )
778 abspHeader_t *header;
779 int size, i, version, offset, length, lumpInt, count;
783 char lumpString[ 1024 ], source[ 1024 ];
784 qboolean lumpSwap = qfalse;
785 abspLumpTest_t *lumpTest;
786 static abspLumpTest_t lumpTests[] =
788 { sizeof( bspPlane_t ), 6, "IBSP LUMP_PLANES" },
789 { sizeof( bspBrush_t ), 1, "IBSP LUMP_BRUSHES" },
790 { 8, 6, "IBSP LUMP_BRUSHSIDES" },
791 { sizeof( bspBrushSide_t ), 6, "RBSP LUMP_BRUSHSIDES" },
792 { sizeof( bspModel_t ), 1, "IBSP LUMP_MODELS" },
793 { sizeof( bspNode_t ), 2, "IBSP LUMP_NODES" },
794 { sizeof( bspLeaf_t ), 1, "IBSP LUMP_LEAFS" },
795 { 104, 3, "IBSP LUMP_DRAWSURFS" },
796 { 44, 3, "IBSP LUMP_DRAWVERTS" },
797 { 4, 6, "IBSP LUMP_DRAWINDEXES" },
798 { 128 * 128 * 3, 1, "IBSP LUMP_LIGHTMAPS" },
799 { 256 * 256 * 3, 1, "IBSP LUMP_LIGHTMAPS (256 x 256)" },
800 { 512 * 512 * 3, 1, "IBSP LUMP_LIGHTMAPS (512 x 512)" },
808 Sys_Printf( "Usage: q3map -analyze [-lumpswap] [-v] <mapname>\n" );
812 /* process arguments */
813 for( i = 1; i < (argc - 1); i++ )
815 /* -format map|ase|... */
816 if( !strcmp( argv[ i ], "-lumpswap" ) )
818 Sys_Printf( "Swapped lump structs enabled\n" );
823 /* clean up map name */
824 strcpy( source, ExpandArg( argv[ i ] ) );
825 Sys_Printf( "Loading %s\n", source );
828 size = LoadFile( source, (void**) &header );
829 if( size == 0 || header == NULL )
831 Sys_Printf( "Unable to load %s.\n", source );
835 /* analyze ident/version */
836 memcpy( ident, header->ident, 4 );
838 version = LittleLong( header->version );
840 Sys_Printf( "Identity: %s\n", ident );
841 Sys_Printf( "Version: %d\n", version );
842 Sys_Printf( "---------------------------------------\n" );
844 /* analyze each lump */
845 for( i = 0; i < 100; i++ )
847 /* call of duty swapped lump pairs */
850 offset = LittleLong( header->lumps[ i ].length );
851 length = LittleLong( header->lumps[ i ].offset );
854 /* standard lump pairs */
857 offset = LittleLong( header->lumps[ i ].offset );
858 length = LittleLong( header->lumps[ i ].length );
862 lump = (byte*) header + offset;
863 lumpInt = LittleLong( (int) *((int*) lump) );
864 lumpFloat = LittleFloat( (float) *((float*) lump) );
865 memcpy( lumpString, (char*) lump, (length < 1024 ? length : 1024) );
866 lumpString[ 1024 ] = '\0';
868 /* print basic lump info */
869 Sys_Printf( "Lump: %d\n", i );
870 Sys_Printf( "Offset: %d bytes\n", offset );
871 Sys_Printf( "Length: %d bytes\n", length );
873 /* only operate on valid lumps */
876 /* print data in 4 formats */
877 Sys_Printf( "As hex: %08X\n", lumpInt );
878 Sys_Printf( "As int: %d\n", lumpInt );
879 Sys_Printf( "As float: %f\n", lumpFloat );
880 Sys_Printf( "As string: %s\n", lumpString );
882 /* guess lump type */
883 if( lumpString[ 0 ] == '{' && lumpString[ 2 ] == '"' )
884 Sys_Printf( "Type guess: IBSP LUMP_ENTITIES\n" );
885 else if( strstr( lumpString, "textures/" ) )
886 Sys_Printf( "Type guess: IBSP LUMP_SHADERS\n" );
889 /* guess based on size/count */
890 for( lumpTest = lumpTests; lumpTest->radix > 0; lumpTest++ )
892 if( (length % lumpTest->radix) != 0 )
894 count = length / lumpTest->radix;
895 if( count < lumpTest->minCount )
897 Sys_Printf( "Type guess: %s (%d x %d)\n", lumpTest->name, count, lumpTest->radix );
902 Sys_Printf( "---------------------------------------\n" );
905 if( offset + length >= size )
910 Sys_Printf( "Lump count: %d\n", i + 1 );
911 Sys_Printf( "File size: %d bytes\n", size );
913 /* return to caller */
921 emits statistics about the bsp file
924 int BSPInfo( int count, char **fileNames )
927 char source[ 1024 ], ext[ 64 ];
935 Sys_Printf( "No files to dump info for.\n");
939 /* enable info mode */
943 for( i = 0; i < count; i++ )
945 Sys_Printf( "---------------------------------\n" );
947 /* mangle filename and get size */
948 strcpy( source, fileNames[ i ] );
949 ExtractFileExtension( source, ext );
950 if( !Q_stricmp( ext, "map" ) )
951 StripExtension( source );
952 DefaultExtension( source, ".bsp" );
953 f = fopen( source, "rb" );
956 size = Q_filelength (f);
962 /* load the bsp file and print lump sizes */
963 Sys_Printf( "%s\n", source );
964 LoadBSPFile( source );
969 Sys_Printf( " total %9d\n", size );
970 Sys_Printf( " %9d KB\n", size / 1024 );
971 Sys_Printf( " %9d MB\n", size / (1024 * 1024) );
973 Sys_Printf( "---------------------------------\n" );
981 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)
983 vec4_t scoeffs, tcoeffs;
989 VectorSubtract(bxyz, axyz, dab);
990 VectorSubtract(cxyz, axyz, dac);
991 CrossProduct(dab, dac, norm);
995 // s(v + norm) = s(v) when n ortho xyz
997 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
1000 // scoeffs * (axyz, 1) == ast[0]
1001 // scoeffs * (bxyz, 1) == bst[0]
1002 // scoeffs * (cxyz, 1) == cst[0]
1003 // scoeffs * (norm, 0) == 0
1004 // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
1005 solvematrix[0] = axyz[0];
1006 solvematrix[4] = axyz[1];
1007 solvematrix[8] = axyz[2];
1008 solvematrix[12] = 1;
1009 solvematrix[1] = bxyz[0];
1010 solvematrix[5] = bxyz[1];
1011 solvematrix[9] = bxyz[2];
1012 solvematrix[13] = 1;
1013 solvematrix[2] = cxyz[0];
1014 solvematrix[6] = cxyz[1];
1015 solvematrix[10] = cxyz[2];
1016 solvematrix[14] = 1;
1017 solvematrix[3] = norm[0];
1018 solvematrix[7] = norm[1];
1019 solvematrix[11] = norm[2];
1020 solvematrix[15] = 0;
1022 md = m4_det(solvematrix);
1025 Sys_Printf("Cannot invert some matrix, some texcoords aren't extrapolated!");
1029 m4x4_invert(solvematrix);
1031 scoeffs[0] = ast[0];
1032 scoeffs[1] = bst[0];
1033 scoeffs[2] = cst[0];
1035 m4x4_transform_vec4(solvematrix, scoeffs);
1036 tcoeffs[0] = ast[1];
1037 tcoeffs[1] = bst[1];
1038 tcoeffs[2] = cst[1];
1040 m4x4_transform_vec4(solvematrix, tcoeffs);
1042 ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
1043 ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
1044 bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
1045 bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
1046 cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
1047 cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
1052 amaze and confuse your enemies with wierd scaled maps!
1055 int ScaleBSPMain( int argc, char **argv )
1064 float *old_xyzst = NULL;
1070 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1075 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
1077 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
1079 scale[0] = atof( argv[ argc - 4 ] );
1081 texscale = !strcmp(argv[1], "-tex");
1083 uniform = ((scale[0] == scale[1]) && (scale[1] == scale[2]));
1085 if( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f )
1087 Sys_Printf( "Usage: q3map [-v] -scale [-tex] <value> <mapname>\n" );
1088 Sys_Printf( "Non-zero scale value required.\n" );
1092 /* do some path mangling */
1093 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
1094 StripExtension( source );
1095 DefaultExtension( source, ".bsp" );
1098 Sys_Printf( "Loading %s\n", source );
1099 LoadBSPFile( source );
1103 Sys_Printf( "--- ScaleBSP ---\n" );
1104 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
1106 /* scale entity keys */
1107 for( i = 0; i < numBSPEntities && i < numEntities; i++ )
1110 GetVectorForKey( &entities[ i ], "origin", vec );
1111 if( (vec[ 0 ] || vec[ 1 ] || vec[ 2 ]) )
1116 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1117 SetKeyValue( &entities[ i ], "origin", str );
1120 a = FloatForKey( &entities[ i ], "angle" );
1121 if(a == -1 || a == -2) // z scale
1123 else if(fabs(sin(DEG2RAD(a))) < 0.707)
1128 /* scale door lip */
1129 f = FloatForKey( &entities[ i ], "lip" );
1133 sprintf( str, "%f", f );
1134 SetKeyValue( &entities[ i ], "lip", str );
1137 /* scale plat height */
1138 f = FloatForKey( &entities[ i ], "height" );
1142 sprintf( str, "%f", f );
1143 SetKeyValue( &entities[ i ], "height", str );
1146 // TODO maybe allow a definition file for entities to specify which values are scaled how?
1150 for( i = 0; i < numBSPModels; i++ )
1152 bspModels[ i ].mins[0] *= scale[0];
1153 bspModels[ i ].mins[1] *= scale[1];
1154 bspModels[ i ].mins[2] *= scale[2];
1155 bspModels[ i ].maxs[0] *= scale[0];
1156 bspModels[ i ].maxs[1] *= scale[1];
1157 bspModels[ i ].maxs[2] *= scale[2];
1161 for( i = 0; i < numBSPNodes; i++ )
1163 bspNodes[ i ].mins[0] *= scale[0];
1164 bspNodes[ i ].mins[1] *= scale[1];
1165 bspNodes[ i ].mins[2] *= scale[2];
1166 bspNodes[ i ].maxs[0] *= scale[0];
1167 bspNodes[ i ].maxs[1] *= scale[1];
1168 bspNodes[ i ].maxs[2] *= scale[2];
1172 for( i = 0; i < numBSPLeafs; i++ )
1174 bspLeafs[ i ].mins[0] *= scale[0];
1175 bspLeafs[ i ].mins[1] *= scale[1];
1176 bspLeafs[ i ].mins[2] *= scale[2];
1177 bspLeafs[ i ].maxs[0] *= scale[0];
1178 bspLeafs[ i ].maxs[1] *= scale[1];
1179 bspLeafs[ i ].maxs[2] *= scale[2];
1184 Sys_Printf("Using texture unlocking (and probably breaking texture alignment a lot)\n");
1185 old_xyzst = safe_malloc(sizeof(*old_xyzst) * numBSPDrawVerts * 5);
1186 for(i = 0; i < numBSPDrawVerts; i++)
1188 old_xyzst[5*i+0] = bspDrawVerts[i].xyz[0];
1189 old_xyzst[5*i+1] = bspDrawVerts[i].xyz[1];
1190 old_xyzst[5*i+2] = bspDrawVerts[i].xyz[2];
1191 old_xyzst[5*i+3] = bspDrawVerts[i].st[0];
1192 old_xyzst[5*i+4] = bspDrawVerts[i].st[1];
1196 /* scale drawverts */
1197 for( i = 0; i < numBSPDrawVerts; i++ )
1199 bspDrawVerts[i].xyz[0] *= scale[0];
1200 bspDrawVerts[i].xyz[1] *= scale[1];
1201 bspDrawVerts[i].xyz[2] *= scale[2];
1202 bspDrawVerts[i].normal[0] /= scale[0];
1203 bspDrawVerts[i].normal[1] /= scale[1];
1204 bspDrawVerts[i].normal[2] /= scale[2];
1205 VectorNormalize(bspDrawVerts[i].normal, bspDrawVerts[i].normal);
1210 for(i = 0; i < numBSPDrawSurfaces; i++)
1212 switch(bspDrawSurfaces[i].surfaceType)
1216 if(bspDrawSurfaces[i].numIndexes % 3)
1217 Error("Not a triangulation!");
1218 for(j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3)
1220 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j+1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j+2] + bspDrawSurfaces[i].firstVert;
1221 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
1222 float *oa = &old_xyzst[ia*5], *ob = &old_xyzst[ib*5], *oc = &old_xyzst[ic*5];
1227 ExtrapolateTexcoords(
1243 for( i = 0; i < numBSPPlanes; i++ )
1245 bspPlanes[ i ].dist *= scale[0];
1250 for( i = 0; i < numBSPPlanes; i++ )
1252 bspPlanes[ i ].normal[0] /= scale[0];
1253 bspPlanes[ i ].normal[1] /= scale[1];
1254 bspPlanes[ i ].normal[2] /= scale[2];
1255 f = 1/VectorLength(bspPlanes[i].normal);
1256 VectorScale(bspPlanes[i].normal, f, bspPlanes[i].normal);
1257 bspPlanes[ i ].dist *= f;
1261 /* scale gridsize */
1262 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
1263 if( (vec[ 0 ] + vec[ 1 ] + vec[ 2 ]) == 0.0f )
1264 VectorCopy( gridSize, vec );
1268 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
1269 SetKeyValue( &entities[ 0 ], "gridsize", str );
1271 /* inject command line parameters */
1272 InjectCommandLine(argv, 0, argc - 1);
1276 StripExtension( source );
1277 DefaultExtension( source, "_s.bsp" );
1278 Sys_Printf( "Writing %s\n", source );
1279 WriteBSPFile( source );
1281 /* return to sender */
1289 main argument processing function for bsp conversion
1292 int ConvertBSPMain( int argc, char **argv )
1295 int (*convertFunc)( char * );
1296 game_t *convertGame;
1300 convertFunc = ConvertBSPToASE;
1306 Sys_Printf( "Usage: q3map -scale <value> [-v] <mapname>\n" );
1310 /* process arguments */
1311 for( i = 1; i < (argc - 1); i++ )
1313 /* -format map|ase|... */
1314 if( !strcmp( argv[ i ], "-format" ) )
1317 if( !Q_stricmp( argv[ i ], "ase" ) )
1318 convertFunc = ConvertBSPToASE;
1319 else if( !Q_stricmp( argv[ i ], "map" ) )
1320 convertFunc = ConvertBSPToMap;
1323 convertGame = GetGame( argv[ i ] );
1324 if( convertGame == NULL )
1325 Sys_Printf( "Unknown conversion format \"%s\". Defaulting to ASE.\n", argv[ i ] );
1328 else if( !strcmp( argv[ i ], "-ne" ) )
1330 normalEpsilon = atof( argv[ i + 1 ] );
1332 Sys_Printf( "Normal epsilon set to %f\n", normalEpsilon );
1334 else if( !strcmp( argv[ i ], "-de" ) )
1336 distanceEpsilon = atof( argv[ i + 1 ] );
1338 Sys_Printf( "Distance epsilon set to %f\n", distanceEpsilon );
1340 else if( !strcmp( argv[ i ], "-shadersasbitmap" ) )
1341 shadersAsBitmap = qtrue;
1344 /* clean up map name */
1345 strcpy( source, ExpandArg( argv[ i ] ) );
1346 StripExtension( source );
1347 DefaultExtension( source, ".bsp" );
1351 Sys_Printf( "Loading %s\n", source );
1353 /* ydnar: load surface file */
1354 //% LoadSurfaceExtraFile( source );
1356 LoadBSPFile( source );
1358 /* parse bsp entities */
1361 /* bsp format convert? */
1362 if( convertGame != NULL )
1364 /* set global game */
1368 StripExtension( source );
1369 DefaultExtension( source, "_c.bsp" );
1370 Sys_Printf( "Writing %s\n", source );
1371 WriteBSPFile( source );
1373 /* return to sender */
1377 /* normal convert */
1378 return convertFunc( source );
1388 int main( int argc, char **argv )
1394 /* we want consistent 'randomness' */
1398 start = I_FloatTime();
1400 /* this was changed to emit version number over the network */
1401 printf( Q3MAP_VERSION "\n" );
1404 atexit( ExitQ3Map );
1406 /* read general options first */
1407 for( i = 1; i < argc; i++ )
1410 if( !strcmp( argv[ i ], "-connect" ) )
1414 Broadcast_Setup( argv[ i ] );
1419 else if( !strcmp( argv[ i ], "-v" ) )
1429 else if( !strcmp( argv[ i ], "-force" ) )
1435 /* patch subdivisions */
1436 else if( !strcmp( argv[ i ], "-subdivisions" ) )
1440 patchSubdivisions = atoi( argv[ i ] );
1442 if( patchSubdivisions <= 0 )
1443 patchSubdivisions = 1;
1447 else if( !strcmp( argv[ i ], "-threads" ) )
1451 numthreads = atoi( argv[ i ] );
1456 /* init model library */
1458 PicoSetMallocFunc( safe_malloc );
1459 PicoSetFreeFunc( free );
1460 PicoSetPrintFunc( PicoPrintFunc );
1461 PicoSetLoadFileFunc( PicoLoadFileFunc );
1462 PicoSetFreeFileFunc( free );
1464 /* set number of threads */
1467 /* generate sinusoid jitter table */
1468 for( i = 0; i < MAX_JITTERS; i++ )
1470 jitters[ i ] = sin( i * 139.54152147 );
1471 //% Sys_Printf( "Jitter %4d: %f\n", i, jitters[ i ] );
1474 /* we print out two versions, q3map's main version (since it evolves a bit out of GtkRadiant)
1475 and we put the GtkRadiant version to make it easy to track with what version of Radiant it was built with */
1477 Sys_Printf( "Q3Map - v1.0r (c) 1999 Id Software Inc.\n" );
1478 Sys_Printf( "Q3Map (ydnar) - v" Q3MAP_VERSION "\n" );
1479 Sys_Printf( "NetRadiant - v" RADIANT_VERSION " " __DATE__ " " __TIME__ "\n" );
1480 Sys_Printf( "%s\n", Q3MAP_MOTD );
1482 /* ydnar: new path initialization */
1483 InitPaths( &argc, argv );
1485 /* set game options */
1486 if (!patchSubdivisions)
1487 patchSubdivisions = game->patchSubdivisions;
1489 /* check if we have enough options left to attempt something */
1491 Error( "Usage: %s [general options] [options] mapfile", argv[ 0 ] );
1494 if( !strcmp( argv[ 1 ], "-fixaas" ) )
1495 r = FixAAS( argc - 1, argv + 1 );
1498 else if( !strcmp( argv[ 1 ], "-analyze" ) )
1499 r = AnalyzeBSP( argc - 1, argv + 1 );
1502 else if( !strcmp( argv[ 1 ], "-info" ) )
1503 r = BSPInfo( argc - 2, argv + 2 );
1506 else if( !strcmp( argv[ 1 ], "-vis" ) )
1507 r = VisMain( argc - 1, argv + 1 );
1510 else if( !strcmp( argv[ 1 ], "-light" ) )
1511 r = LightMain( argc - 1, argv + 1 );
1514 else if( !strcmp( argv[ 1 ], "-vlight" ) )
1516 Sys_Printf( "WARNING: VLight is no longer supported, defaulting to -light -fast instead\n\n" );
1517 argv[ 1 ] = "-fast"; /* eek a hack */
1518 r = LightMain( argc, argv );
1521 /* ydnar: lightmap export */
1522 else if( !strcmp( argv[ 1 ], "-export" ) )
1523 r = ExportLightmapsMain( argc - 1, argv + 1 );
1525 /* ydnar: lightmap import */
1526 else if( !strcmp( argv[ 1 ], "-import" ) )
1527 r = ImportLightmapsMain( argc - 1, argv + 1 );
1529 /* ydnar: bsp scaling */
1530 else if( !strcmp( argv[ 1 ], "-scale" ) )
1531 r = ScaleBSPMain( argc - 1, argv + 1 );
1533 /* ydnar: bsp conversion */
1534 else if( !strcmp( argv[ 1 ], "-convert" ) )
1535 r = ConvertBSPMain( argc - 1, argv + 1 );
1538 else if( !strcmp( argv[ 1 ], "-minimap" ) )
1539 r = MiniMapBSPMain(argc - 1, argv + 1);
1541 /* ydnar: otherwise create a bsp */
1543 r = BSPMain( argc, argv );
1546 end = I_FloatTime();
1547 Sys_Printf( "%9.0f seconds elapsed\n", end - start );
1549 /* shut down connection */
1550 Broadcast_Shutdown();
1552 /* return any error code */