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 ------------------------------------------------------------------------------- */
36 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 ){
37 vec4_t scoeffs, tcoeffs;
43 VectorSubtract( bxyz, axyz, dab );
44 VectorSubtract( cxyz, axyz, dac );
45 CrossProduct( dab, dac, norm );
49 // s(v + norm) = s(v) when n ortho xyz
51 // s(v) = DotProduct(v, scoeffs) + scoeffs[3]
54 // scoeffs * (axyz, 1) == ast[0]
55 // scoeffs * (bxyz, 1) == bst[0]
56 // scoeffs * (cxyz, 1) == cst[0]
57 // scoeffs * (norm, 0) == 0
58 // scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
59 solvematrix[0] = axyz[0];
60 solvematrix[4] = axyz[1];
61 solvematrix[8] = axyz[2];
63 solvematrix[1] = bxyz[0];
64 solvematrix[5] = bxyz[1];
65 solvematrix[9] = bxyz[2];
67 solvematrix[2] = cxyz[0];
68 solvematrix[6] = cxyz[1];
69 solvematrix[10] = cxyz[2];
71 solvematrix[3] = norm[0];
72 solvematrix[7] = norm[1];
73 solvematrix[11] = norm[2];
76 md = m4_det( solvematrix );
77 if ( md * md < 1e-10 ) {
78 Sys_Printf( "Cannot invert some matrix, some texcoords aren't extrapolated!" );
82 m4x4_invert( solvematrix );
88 m4x4_transform_vec4( solvematrix, scoeffs );
93 m4x4_transform_vec4( solvematrix, tcoeffs );
95 ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
96 ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
97 bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
98 bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
99 cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
100 cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
105 amaze and confuse your enemies with wierd scaled maps!
108 int ScaleBSPMain( int argc, char **argv ){
116 float *old_xyzst = NULL;
122 Sys_Printf( "Usage: q3map [-v] -scale [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
127 for ( i = 1; i < argc - 2; ++i )
129 if ( !strcmp( argv[i], "-tex" ) ) {
132 else if ( !strcmp( argv[i], "-spawn_ref" ) ) {
133 spawn_ref = atof( argv[i + 1] );
142 // if(argc-2 >= i) // always true
143 scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
144 if ( argc - 3 >= i ) {
145 scale[1] = scale[0] = atof( argv[ argc - 3 ] );
147 if ( argc - 4 >= i ) {
148 scale[0] = atof( argv[ argc - 4 ] );
151 uniform = ( ( scale[0] == scale[1] ) && ( scale[1] == scale[2] ) );
153 if ( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f ) {
154 Sys_Printf( "Usage: q3map [-v] -scale [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
155 Sys_Printf( "Non-zero scale value required.\n" );
159 /* do some path mangling */
160 strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
161 StripExtension( source );
162 DefaultExtension( source, ".bsp" );
165 Sys_Printf( "Loading %s\n", source );
166 LoadBSPFile( source );
170 Sys_Printf( "--- ScaleBSP ---\n" );
171 Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
173 /* scale entity keys */
174 for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
177 GetVectorForKey( &entities[ i ], "origin", vec );
178 if ( ( vec[ 0 ] || vec[ 1 ] || vec[ 2 ] ) ) {
179 if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
185 if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
188 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
189 SetKeyValue( &entities[ i ], "origin", str );
192 a = FloatForKey( &entities[ i ], "angle" );
193 if ( a == -1 || a == -2 ) { // z scale
196 else if ( fabs( sin( DEG2RAD( a ) ) ) < 0.707 ) {
204 f = FloatForKey( &entities[ i ], "lip" );
207 sprintf( str, "%f", f );
208 SetKeyValue( &entities[ i ], "lip", str );
211 /* scale plat height */
212 f = FloatForKey( &entities[ i ], "height" );
215 sprintf( str, "%f", f );
216 SetKeyValue( &entities[ i ], "height", str );
219 // TODO maybe allow a definition file for entities to specify which values are scaled how?
223 for ( i = 0; i < numBSPModels; i++ )
225 bspModels[ i ].mins[0] *= scale[0];
226 bspModels[ i ].mins[1] *= scale[1];
227 bspModels[ i ].mins[2] *= scale[2];
228 bspModels[ i ].maxs[0] *= scale[0];
229 bspModels[ i ].maxs[1] *= scale[1];
230 bspModels[ i ].maxs[2] *= scale[2];
234 for ( i = 0; i < numBSPNodes; i++ )
236 bspNodes[ i ].mins[0] *= scale[0];
237 bspNodes[ i ].mins[1] *= scale[1];
238 bspNodes[ i ].mins[2] *= scale[2];
239 bspNodes[ i ].maxs[0] *= scale[0];
240 bspNodes[ i ].maxs[1] *= scale[1];
241 bspNodes[ i ].maxs[2] *= scale[2];
245 for ( i = 0; i < numBSPLeafs; i++ )
247 bspLeafs[ i ].mins[0] *= scale[0];
248 bspLeafs[ i ].mins[1] *= scale[1];
249 bspLeafs[ i ].mins[2] *= scale[2];
250 bspLeafs[ i ].maxs[0] *= scale[0];
251 bspLeafs[ i ].maxs[1] *= scale[1];
252 bspLeafs[ i ].maxs[2] *= scale[2];
256 Sys_Printf( "Using texture unlocking (and probably breaking texture alignment a lot)\n" );
257 old_xyzst = safe_malloc( sizeof( *old_xyzst ) * numBSPDrawVerts * 5 );
258 for ( i = 0; i < numBSPDrawVerts; i++ )
260 old_xyzst[5 * i + 0] = bspDrawVerts[i].xyz[0];
261 old_xyzst[5 * i + 1] = bspDrawVerts[i].xyz[1];
262 old_xyzst[5 * i + 2] = bspDrawVerts[i].xyz[2];
263 old_xyzst[5 * i + 3] = bspDrawVerts[i].st[0];
264 old_xyzst[5 * i + 4] = bspDrawVerts[i].st[1];
268 /* scale drawverts */
269 for ( i = 0; i < numBSPDrawVerts; i++ )
271 bspDrawVerts[i].xyz[0] *= scale[0];
272 bspDrawVerts[i].xyz[1] *= scale[1];
273 bspDrawVerts[i].xyz[2] *= scale[2];
274 bspDrawVerts[i].normal[0] /= scale[0];
275 bspDrawVerts[i].normal[1] /= scale[1];
276 bspDrawVerts[i].normal[2] /= scale[2];
277 VectorNormalize( bspDrawVerts[i].normal, bspDrawVerts[i].normal );
281 for ( i = 0; i < numBSPDrawSurfaces; i++ )
283 switch ( bspDrawSurfaces[i].surfaceType )
287 if ( bspDrawSurfaces[i].numIndexes % 3 ) {
288 Error( "Not a triangulation!" );
290 for ( j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3 )
292 int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j + 1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j + 2] + bspDrawSurfaces[i].firstVert;
293 bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
294 float *oa = &old_xyzst[ia * 5], *ob = &old_xyzst[ib * 5], *oc = &old_xyzst[ic * 5];
299 ExtrapolateTexcoords(
314 for ( i = 0; i < numBSPPlanes; i++ )
316 bspPlanes[ i ].dist *= scale[0];
321 for ( i = 0; i < numBSPPlanes; i++ )
323 bspPlanes[ i ].normal[0] /= scale[0];
324 bspPlanes[ i ].normal[1] /= scale[1];
325 bspPlanes[ i ].normal[2] /= scale[2];
326 f = 1 / VectorLength( bspPlanes[i].normal );
327 VectorScale( bspPlanes[i].normal, f, bspPlanes[i].normal );
328 bspPlanes[ i ].dist *= f;
333 GetVectorForKey( &entities[ 0 ], "gridsize", vec );
334 if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
335 VectorCopy( gridSize, vec );
340 sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
341 SetKeyValue( &entities[ 0 ], "gridsize", str );
343 /* inject command line parameters */
344 InjectCommandLine( argv, 0, argc - 1 );
348 StripExtension( source );
349 DefaultExtension( source, "_s.bsp" );
350 Sys_Printf( "Writing %s\n", source );
351 WriteBSPFile( source );
353 /* return to sender */