forked from id-tech-3-tools/map-compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbsp_scale.cpp
More file actions
349 lines (301 loc) · 10 KB
/
Copy pathbsp_scale.cpp
File metadata and controls
349 lines (301 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* -------------------------------------------------------------------------------
Copyright (C) 1999-2007 id Software, Inc. and contributors.
For a list of contributors, see the accompanying CONTRIBUTORS file.
This file is part of GtkRadiant.
GtkRadiant is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
GtkRadiant is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GtkRadiant; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
-------------------------------------------------------------------------------
This code has been altered significantly from its original form, to support
several games based on the Quake III Arena engine, in the form of "Q3Map2."
------------------------------------------------------------------------------- */
/* dependencies */
#include "q3map2.h"
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 ){
vec4_t scoeffs, tcoeffs;
float md;
m4x4_t solvematrix;
vec3_t norm;
vec3_t dab, dac;
VectorSubtract( bxyz, axyz, dab );
VectorSubtract( cxyz, axyz, dac );
CrossProduct( dab, dac, norm );
// assume:
// s = f(x, y, z)
// s(v + norm) = s(v) when n ortho xyz
// s(v) = DotProduct(v, scoeffs) + scoeffs[3]
// solve:
// scoeffs * (axyz, 1) == ast[0]
// scoeffs * (bxyz, 1) == bst[0]
// scoeffs * (cxyz, 1) == cst[0]
// scoeffs * (norm, 0) == 0
// scoeffs * [axyz, 1 | bxyz, 1 | cxyz, 1 | norm, 0] = [ast[0], bst[0], cst[0], 0]
solvematrix[0] = axyz[0];
solvematrix[4] = axyz[1];
solvematrix[8] = axyz[2];
solvematrix[12] = 1;
solvematrix[1] = bxyz[0];
solvematrix[5] = bxyz[1];
solvematrix[9] = bxyz[2];
solvematrix[13] = 1;
solvematrix[2] = cxyz[0];
solvematrix[6] = cxyz[1];
solvematrix[10] = cxyz[2];
solvematrix[14] = 1;
solvematrix[3] = norm[0];
solvematrix[7] = norm[1];
solvematrix[11] = norm[2];
solvematrix[15] = 0;
md = m4_det( solvematrix );
if ( md * md < 1e-10 ) {
Sys_Printf( "Cannot invert some matrix, some texcoords aren't extrapolated!" );
return;
}
m4x4_invert( solvematrix );
scoeffs[0] = ast[0];
scoeffs[1] = bst[0];
scoeffs[2] = cst[0];
scoeffs[3] = 0;
m4x4_transform_vec4( solvematrix, scoeffs );
tcoeffs[0] = ast[1];
tcoeffs[1] = bst[1];
tcoeffs[2] = cst[1];
tcoeffs[3] = 0;
m4x4_transform_vec4( solvematrix, tcoeffs );
ast_out[0] = scoeffs[0] * axyz_new[0] + scoeffs[1] * axyz_new[1] + scoeffs[2] * axyz_new[2] + scoeffs[3];
ast_out[1] = tcoeffs[0] * axyz_new[0] + tcoeffs[1] * axyz_new[1] + tcoeffs[2] * axyz_new[2] + tcoeffs[3];
bst_out[0] = scoeffs[0] * bxyz_new[0] + scoeffs[1] * bxyz_new[1] + scoeffs[2] * bxyz_new[2] + scoeffs[3];
bst_out[1] = tcoeffs[0] * bxyz_new[0] + tcoeffs[1] * bxyz_new[1] + tcoeffs[2] * bxyz_new[2] + tcoeffs[3];
cst_out[0] = scoeffs[0] * cxyz_new[0] + scoeffs[1] * cxyz_new[1] + scoeffs[2] * cxyz_new[2] + scoeffs[3];
cst_out[1] = tcoeffs[0] * cxyz_new[0] + tcoeffs[1] * cxyz_new[1] + tcoeffs[2] * cxyz_new[2] + tcoeffs[3];
}
/*
ScaleBSPMain()
amaze and confuse your enemies with wierd scaled maps!
*/
int ScaleBSPMain( int argc, char **argv ){
int i, j;
float f, a;
vec3_t scale;
vec3_t vec;
char str[ 1024 ];
int uniform, axis;
qboolean texscale;
float *old_xyzst = NULL;
float spawn_ref = 0;
/* arg checking */
if ( argc < 3 ) {
Sys_Printf( "Usage: q3map [-v] -scale [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
return 0;
}
texscale = qfalse;
for (i = 1; i < argc - 2; i++)
{
if ( !Q_stricmp( argv[i], "-tex" ) ) {
texscale = qtrue;
}
else if ( !Q_stricmp( argv[i], "-spawn_ref" ) ) {
spawn_ref = atof( argv[i + 1] );
i++;
}
}
/* get scale */
// if(argc-2 >= i) // always true
scale[2] = scale[1] = scale[0] = atof( argv[ argc - 2 ] );
if ( argc - 3 >= i ) {
scale[1] = scale[0] = atof( argv[ argc - 3 ] );
}
if ( argc - 4 >= i ) {
scale[0] = atof( argv[ argc - 4 ] );
}
uniform = ( ( scale[0] == scale[1] ) && ( scale[1] == scale[2] ) );
if ( scale[0] == 0.0f || scale[1] == 0.0f || scale[2] == 0.0f ) {
Sys_Printf( "Usage: q3map [-v] -scale [-tex] [-spawn_ref <value>] <value> <mapname>\n" );
Sys_Printf( "Non-zero scale value required.\n" );
return 0;
}
/* do some path mangling */
strcpy( source, ExpandArg( argv[ argc - 1 ] ) );
StripExtension( source );
DefaultExtension( source, ".bsp" );
/* load the bsp */
Sys_Printf( "Loading %s\n", source );
LoadBSPFile( source );
ParseEntities();
/* note it */
Sys_Printf( "--- ScaleBSP ---\n" );
Sys_FPrintf( SYS_VRB, "%9d entities\n", numEntities );
/* scale entity keys */
for ( i = 0; i < numBSPEntities && i < numEntities; i++ )
{
/* scale origin */
GetVectorForKey( &entities[ i ], "origin", vec );
if ( ( vec[ 0 ] || vec[ 1 ] || vec[ 2 ] ) ) {
if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
vec[2] += spawn_ref;
}
vec[0] *= scale[0];
vec[1] *= scale[1];
vec[2] *= scale[2];
if ( !strncmp( ValueForKey( &entities[i], "classname" ), "info_player_", 12 ) ) {
vec[2] -= spawn_ref;
}
sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
SetKeyValue( &entities[ i ], "origin", str );
}
a = FloatForKey( &entities[ i ], "angle" );
if ( a == -1 || a == -2 ) { // z scale
axis = 2;
}
else if ( fabs( sin( DEG2RAD( a ) ) ) < 0.707 ) {
axis = 0;
}
else{
axis = 1;
}
/* scale door lip */
f = FloatForKey( &entities[ i ], "lip" );
if ( f ) {
f *= scale[axis];
sprintf( str, "%f", f );
SetKeyValue( &entities[ i ], "lip", str );
}
/* scale plat height */
f = FloatForKey( &entities[ i ], "height" );
if ( f ) {
f *= scale[2];
sprintf( str, "%f", f );
SetKeyValue( &entities[ i ], "height", str );
}
// TODO maybe allow a definition file for entities to specify which values are scaled how?
}
/* scale models */
for ( i = 0; i < numBSPModels; i++ )
{
bspModels[ i ].mins[0] *= scale[0];
bspModels[ i ].mins[1] *= scale[1];
bspModels[ i ].mins[2] *= scale[2];
bspModels[ i ].maxs[0] *= scale[0];
bspModels[ i ].maxs[1] *= scale[1];
bspModels[ i ].maxs[2] *= scale[2];
}
/* scale nodes */
for ( i = 0; i < numBSPNodes; i++ )
{
bspNodes[ i ].mins[0] *= scale[0];
bspNodes[ i ].mins[1] *= scale[1];
bspNodes[ i ].mins[2] *= scale[2];
bspNodes[ i ].maxs[0] *= scale[0];
bspNodes[ i ].maxs[1] *= scale[1];
bspNodes[ i ].maxs[2] *= scale[2];
}
/* scale leafs */
for ( i = 0; i < numBSPLeafs; i++ )
{
bspLeafs[ i ].mins[0] *= scale[0];
bspLeafs[ i ].mins[1] *= scale[1];
bspLeafs[ i ].mins[2] *= scale[2];
bspLeafs[ i ].maxs[0] *= scale[0];
bspLeafs[ i ].maxs[1] *= scale[1];
bspLeafs[ i ].maxs[2] *= scale[2];
}
if ( texscale ) {
Sys_Printf( "Using texture unlocking (and probably breaking texture alignment a lot)\n" );
old_xyzst = static_cast<float *>(safe_malloc(sizeof(*old_xyzst) * numBSPDrawVerts * 5));
for ( i = 0; i < numBSPDrawVerts; i++ )
{
old_xyzst[5 * i + 0] = bspDrawVerts[i].xyz[0];
old_xyzst[5 * i + 1] = bspDrawVerts[i].xyz[1];
old_xyzst[5 * i + 2] = bspDrawVerts[i].xyz[2];
old_xyzst[5 * i + 3] = bspDrawVerts[i].st[0];
old_xyzst[5 * i + 4] = bspDrawVerts[i].st[1];
}
}
/* scale drawverts */
for ( i = 0; i < numBSPDrawVerts; i++ )
{
bspDrawVerts[i].xyz[0] *= scale[0];
bspDrawVerts[i].xyz[1] *= scale[1];
bspDrawVerts[i].xyz[2] *= scale[2];
bspDrawVerts[i].normal[0] /= scale[0];
bspDrawVerts[i].normal[1] /= scale[1];
bspDrawVerts[i].normal[2] /= scale[2];
VectorNormalize( bspDrawVerts[i].normal, bspDrawVerts[i].normal );
}
if ( texscale ) {
for ( i = 0; i < numBSPDrawSurfaces; i++ )
{
switch ( bspDrawSurfaces[i].surfaceType )
{
case SURFACE_FACE:
case SURFACE_META:
if ( bspDrawSurfaces[i].numIndexes % 3 ) {
Error( "Not a triangulation!" );
}
for ( j = bspDrawSurfaces[i].firstIndex; j < bspDrawSurfaces[i].firstIndex + bspDrawSurfaces[i].numIndexes; j += 3 )
{
int ia = bspDrawIndexes[j] + bspDrawSurfaces[i].firstVert, ib = bspDrawIndexes[j + 1] + bspDrawSurfaces[i].firstVert, ic = bspDrawIndexes[j + 2] + bspDrawSurfaces[i].firstVert;
bspDrawVert_t *a = &bspDrawVerts[ia], *b = &bspDrawVerts[ib], *c = &bspDrawVerts[ic];
float *oa = &old_xyzst[ia * 5], *ob = &old_xyzst[ib * 5], *oc = &old_xyzst[ic * 5];
// extrapolate:
// a->xyz -> oa
// b->xyz -> ob
// c->xyz -> oc
ExtrapolateTexcoords(
&oa[0], &oa[3],
&ob[0], &ob[3],
&oc[0], &oc[3],
a->xyz, a->st,
b->xyz, b->st,
c->xyz, c->st );
}
break;
}
}
}
/* scale planes */
if ( uniform ) {
for ( i = 0; i < numBSPPlanes; i++ )
{
bspPlanes[ i ].dist *= scale[0];
}
}
else
{
for ( i = 0; i < numBSPPlanes; i++ )
{
bspPlanes[ i ].normal[0] /= scale[0];
bspPlanes[ i ].normal[1] /= scale[1];
bspPlanes[ i ].normal[2] /= scale[2];
f = 1 / VectorLength( bspPlanes[i].normal );
VectorScale( bspPlanes[i].normal, f, bspPlanes[i].normal );
bspPlanes[ i ].dist *= f;
}
}
/* scale gridsize */
GetVectorForKey( &entities[ 0 ], "gridsize", vec );
if ( ( vec[ 0 ] + vec[ 1 ] + vec[ 2 ] ) == 0.0f ) {
VectorCopy( gridSize, vec );
}
vec[0] *= scale[0];
vec[1] *= scale[1];
vec[2] *= scale[2];
sprintf( str, "%f %f %f", vec[ 0 ], vec[ 1 ], vec[ 2 ] );
SetKeyValue( &entities[ 0 ], "gridsize", str );
/* write the bsp */
UnparseEntities();
StripExtension( source );
DefaultExtension( source, "_s.bsp" );
Sys_Printf( "Writing %s\n", source );
WriteBSPFile( source );
/* return to sender */
return 0;
}