Debian Codegraph @

tools/quake3/q3map2/convert_ase.c

raw symbols SCIP
1 /* -------------------------------------------------------------------------------
2
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
5
6 This file is part of GtkRadiant.
7
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.
12
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.
17
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
21
22 ----------------------------------------------------------------------------------
23
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."
26
27 ------------------------------------------------------------------------------- */
28
29
30
31 /* marker */
32 #define CONVERT_ASE_C
33
34
35
36 /* dependencies */
37 #include "q3map2.h"
38
39
40
41 /*
42 ConvertSurface()
43 converts a bsp drawsurface to an ase chunk
44 */
45
46 int numLightmapsASE = 0;
47 static void ConvertSurface( FILE *f, bspModel_t *model, int modelNum, bspDrawSurface_t *ds, int surfaceNum, vec3_t origin ){
48 int i, v, face, a, b, c;
49 bspDrawVert_t *dv;
50 vec3_t normal;
51 char name[ 1024 ];
52
53
54 /* ignore patches for now */
55 if ( ds->surfaceType != MST_PLANAR && ds->surfaceType != MST_TRIANGLE_SOUP ) {
56 return;
57 }
58
59 /* print object header for each dsurf */
60 sprintf( name, "mat%dmodel%dsurf%d", ds->shaderNum, modelNum, surfaceNum );
61 fprintf( f, "*GEOMOBJECT\t{\r\n" );
62 fprintf( f, "\t*NODE_NAME\t\"%s\"\r\n", name );
63 fprintf( f, "\t*NODE_TM\t{\r\n" );
64 fprintf( f, "\t\t*NODE_NAME\t\"%s\"\r\n", name );
65 fprintf( f, "\t\t*INHERIT_POS\t0\t0\t0\r\n" );
66 fprintf( f, "\t\t*INHERIT_ROT\t0\t0\t0\r\n" );
67 fprintf( f, "\t\t*INHERIT_SCL\t0\t0\t0\r\n" );
68 fprintf( f, "\t\t*TM_ROW0\t1.0\t0\t0\r\n" );
69 fprintf( f, "\t\t*TM_ROW1\t0\t1.0\t0\r\n" );
70 fprintf( f, "\t\t*TM_ROW2\t0\t0\t1.0\r\n" );
71 fprintf( f, "\t\t*TM_ROW3\t0\t0\t0\r\n" );
72 fprintf( f, "\t\t*TM_POS\t%f\t%f\t%f\r\n", origin[ 0 ], origin[ 1 ], origin[ 2 ] );
73 fprintf( f, "\t}\r\n" );
74
75 /* print mesh header */
76 fprintf( f, "\t*MESH\t{\r\n" );
77 fprintf( f, "\t\t*TIMEVALUE\t0\r\n" );
78 fprintf( f, "\t\t*MESH_NUMVERTEX\t%d\r\n", ds->numVerts );
79 fprintf( f, "\t\t*MESH_NUMFACES\t%d\r\n", ds->numIndexes / 3 );
80 switch ( ds->surfaceType )
81 {
82 case MST_PLANAR:
83 fprintf( f, "\t\t*COMMENT\t\"SURFACETYPE\tMST_PLANAR\"\r\n" );
84 break;
85 case MST_TRIANGLE_SOUP:
86 fprintf( f, "\t\t*COMMENT\t\"SURFACETYPE\tMST_TRIANGLE_SOUP\"\r\n" );
87 break;
88 }
89
90 /* export vertex xyz */
91 fprintf( f, "\t\t*MESH_VERTEX_LIST\t{\r\n" );
92 for ( i = 0; i < ds->numVerts; i++ )
93 {
94 v = i + ds->firstVert;
95 dv = &bspDrawVerts[ v ];
96 fprintf( f, "\t\t\t*MESH_VERTEX\t%d\t%f\t%f\t%f\r\n", i, dv->xyz[ 0 ], dv->xyz[ 1 ], dv->xyz[ 2 ] );
97 }
98 fprintf( f, "\t\t}\r\n" );
99
100 /* export vertex normals */
101 fprintf( f, "\t\t*MESH_NORMALS\t{\r\n" );
102 for ( i = 0; i < ds->numIndexes; i += 3 )
103 {
104 face = ( i / 3 );
105 a = bspDrawIndexes[ i + ds->firstIndex ];
106 b = bspDrawIndexes[ i + ds->firstIndex + 1 ];
107 c = bspDrawIndexes[ i + ds->firstIndex + 2 ];
108 VectorCopy( bspDrawVerts[ a ].normal, normal );
109 VectorAdd( normal, bspDrawVerts[ b ].normal, normal );
110 VectorAdd( normal, bspDrawVerts[ c ].normal, normal );
111 if ( VectorNormalize( normal, normal ) ) {
112 fprintf( f, "\t\t\t*MESH_FACENORMAL\t%d\t%f\t%f\t%f\r\n", face, normal[ 0 ], normal[ 1 ], normal[ 2 ] );
113 }
114 }
115 for ( i = 0; i < ds->numVerts; i++ )
116 {
117 v = i + ds->firstVert;
118 dv = &bspDrawVerts[ v ];
119 fprintf( f, "\t\t\t*MESH_VERTEXNORMAL\t%d\t%f\t%f\t%f\r\n", i, dv->normal[ 0 ], dv->normal[ 1 ], dv->normal[ 2 ] );
120 }
121 fprintf( f, "\t\t}\r\n" );
122
123 /* export faces */
124 fprintf( f, "\t\t*MESH_FACE_LIST\t{\r\n" );
125 for ( i = 0; i < ds->numIndexes; i += 3 )
126 {
127 face = ( i / 3 );
128 a = bspDrawIndexes[ i + ds->firstIndex ];
129 c = bspDrawIndexes[ i + ds->firstIndex + 1 ];
130 b = bspDrawIndexes[ i + ds->firstIndex + 2 ];
131 fprintf( f, "\t\t\t*MESH_FACE\t%d\tA:\t%d\tB:\t%d\tC:\t%d\tAB:\t1\tBC:\t1\tCA:\t1\t*MESH_SMOOTHING\t0\t*MESH_MTLID\t0\r\n",
132 face, a, b, c );
133 }
134 fprintf( f, "\t\t}\r\n" );
135
136 /* export vertex st */
137 fprintf( f, "\t\t*MESH_NUMTVERTEX\t%d\r\n", ds->numVerts );
138 fprintf( f, "\t\t*MESH_TVERTLIST\t{\r\n" );
139 for ( i = 0; i < ds->numVerts; i++ )
140 {
141 v = i + ds->firstVert;
142 dv = &bspDrawVerts[ v ];
143 fprintf( f, "\t\t\t*MESH_TVERT\t%d\t%f\t%f\t%f\r\n", i, dv->st[ 0 ], ( 1.0 - dv->st[ 1 ] ), 1.0f );
144 }
145 fprintf( f, "\t\t}\r\n" );
146
147 /* export texture faces */
148 fprintf( f, "\t\t*MESH_NUMTVFACES\t%d\r\n", ds->numIndexes / 3 );
149 fprintf( f, "\t\t*MESH_TFACELIST\t{\r\n" );
150 for ( i = 0; i < ds->numIndexes; i += 3 )
151 {
152 face = ( i / 3 );
153 a = bspDrawIndexes[ i + ds->firstIndex ];
154 c = bspDrawIndexes[ i + ds->firstIndex + 1 ];
155 b = bspDrawIndexes[ i + ds->firstIndex + 2 ];
156 fprintf( f, "\t\t\t*MESH_TFACE\t%d\t%d\t%d\t%d\r\n", face, a, b, c );
157 }
158 fprintf( f, "\t\t}\r\n" );
159
160 /* print mesh footer */
161 fprintf( f, "\t}\r\n" );
162
163 /* print object footer */
164 fprintf( f, "\t*PROP_MOTIONBLUR\t0\r\n" );
165 fprintf( f, "\t*PROP_CASTSHADOW\t1\r\n" );
166 fprintf( f, "\t*PROP_RECVSHADOW\t1\r\n" );
167 if ( lightmapsAsTexcoord ) {
168 if ( ds->lightmapNum[0] >= 0 && ds->lightmapNum[0] + (int)deluxemap < numLightmapsASE ) {
169 fprintf( f, "\t*MATERIAL_REF\t%d\r\n", ds->lightmapNum[0] + deluxemap );
170 }
171 else{
172 Sys_FPrintf( SYS_WRN, "WARNING: lightmap %d out of range, not exporting\n", ds->lightmapNum[0] + deluxemap );
173 }
174 }
175 else{
176 fprintf( f, "\t*MATERIAL_REF\t%d\r\n", ds->shaderNum );
177 }
178 fprintf( f, "}\r\n" );
179 }
180
181
182
183 /*
184 ConvertModel()
185 exports a bsp model to an ase chunk
186 */
187
188 static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin ){
189 int i, s;
190 bspDrawSurface_t *ds;
191
192
193 /* go through each drawsurf in the model */
194 for ( i = 0; i < model->numBSPSurfaces; i++ )
195 {
196 s = i + model->firstBSPSurface;
197 ds = &bspDrawSurfaces[ s ];
198 ConvertSurface( f, model, modelNum, ds, s, origin );
199 }
200 }
201
202
203
204 /*
205 ConvertShader()
206 exports a bsp shader to an ase chunk
207 */
208
209 /*
210 *MATERIAL 0 {
211 *MATERIAL_NAME "models/test/rock16l"
212 *MATERIAL_CLASS "Standard"
213 *MATERIAL_AMBIENT 0.5882 0.5882 0.5882
214 *MATERIAL_DIFFUSE 0.5882 0.5882 0.5882
215 *MATERIAL_SPECULAR 0.5882 0.5882 0.5882
216 *MATERIAL_SHINE 0.0000
217 *MATERIAL_SHINESTRENGTH 0.0000
218 *MATERIAL_TRANSPARENCY 0.0000
219 *MATERIAL_WIRESIZE 1.0000
220 *MATERIAL_SHADING Phong
221 *MATERIAL_XP_FALLOFF 0.0000
222 *MATERIAL_SELFILLUM 0.0000
223 *MATERIAL_FALLOFF In
224 *MATERIAL_XP_TYPE Filter
225 *MAP_DIFFUSE {
226 *MAP_NAME "Map #2"
227 *MAP_CLASS "Bitmap"
228 *MAP_SUBNO 1
229 *MAP_AMOUNT 1.0000
230 *BITMAP "models/test/rock16l"
231 *MAP_TYPE Screen
232 *UVW_U_OFFSET 0.0000
233 *UVW_V_OFFSET 0.0000
234 *UVW_U_TILING 1.0000
235 *UVW_V_TILING 1.0000
236 *UVW_ANGLE 0.0000
237 *UVW_BLUR 1.0000
238 *UVW_BLUR_OFFSET 0.0000
239 *UVW_NOUSE_AMT 1.0000
240 *UVW_NOISE_SIZE 1.0000
241 *UVW_NOISE_LEVEL 1
242 *UVW_NOISE_PHASE 0.0000
243 *BITMAP_FILTER Pyramidal
244 }
245 }
246 */
247
248 static void ConvertShader( FILE *f, bspShader_t *shader, int shaderNum ){
249 shaderInfo_t *si;
250 char *c, filename[ 1024 ];
251
252
253 /* get shader */
254 si = ShaderInfoForShader( shader->shader );
255 if ( si == NULL ) {
256 Sys_FPrintf( SYS_WRN, "WARNING: NULL shader in BSP\n" );
257 return;
258 }
259
260 /* set bitmap filename */
261 if ( si->shaderImage->filename[ 0 ] != '*' ) {
262 strcpy( filename, si->shaderImage->filename );
263 }
264 else{
265 sprintf( filename, "%s.tga", si->shader );
266 }
267 for ( c = filename; *c != '\0'; c++ )
268 if ( *c == '/' ) {
269 *c = '\\';
270 }
271
272 /* print shader info */
273 fprintf( f, "\t*MATERIAL\t%d\t{\r\n", shaderNum );
274 fprintf( f, "\t\t*MATERIAL_NAME\t\"%s\"\r\n", shader->shader );
275 fprintf( f, "\t\t*MATERIAL_CLASS\t\"Standard\"\r\n" );
276 fprintf( f, "\t\t*MATERIAL_DIFFUSE\t%f\t%f\t%f\r\n", si->color[ 0 ], si->color[ 1 ], si->color[ 2 ] );
277 fprintf( f, "\t\t*MATERIAL_SHADING Phong\r\n" );
278
279 /* print map info */
280 fprintf( f, "\t\t*MAP_DIFFUSE\t{\r\n" );
281 fprintf( f, "\t\t\t*MAP_NAME\t\"%s\"\r\n", shader->shader );
282 fprintf( f, "\t\t\t*MAP_CLASS\t\"Bitmap\"\r\n" );
283 fprintf( f, "\t\t\t*MAP_SUBNO\t1\r\n" );
284 fprintf( f, "\t\t\t*MAP_AMOUNT\t1.0\r\n" );
285 fprintf( f, "\t\t\t*MAP_TYPE\tScreen\r\n" );
286 if ( shadersAsBitmap ) {
287 fprintf( f, "\t\t\t*BITMAP\t\"%s\"\r\n", shader->shader );
288 }
289 else{
290 fprintf( f, "\t\t\t*BITMAP\t\"..\\%s\"\r\n", filename );
291 }
292 fprintf( f, "\t\t\t*BITMAP_FILTER\tPyramidal\r\n" );
293 fprintf( f, "\t\t}\r\n" );
294
295 fprintf( f, "\t}\r\n" );
296 }
297 static void ConvertLightmap( FILE *f, const char *base, int lightmapNum ){
298 /* print shader info */
299 fprintf( f, "\t*MATERIAL\t%d\t{\r\n", lightmapNum );
300 fprintf( f, "\t\t*MATERIAL_NAME\t\"lm_%04d\"\r\n", lightmapNum );
301 fprintf( f, "\t\t*MATERIAL_CLASS\t\"Standard\"\r\n" );
302 fprintf( f, "\t\t*MATERIAL_DIFFUSE\t1\t1\t1\r\n" );
303 fprintf( f, "\t\t*MATERIAL_SHADING Phong\r\n" );
304
305 /* print map info */
306 if ( lightmapNum >= 0 ) {
307 fprintf( f, "\t\t*MAP_DIFFUSE\t{\r\n" );
308 fprintf( f, "\t\t\t*MAP_NAME\t\"lm_%04d\"\r\n", lightmapNum );
309 fprintf( f, "\t\t\t*MAP_CLASS\t\"Bitmap\"\r\n" );
310 fprintf( f, "\t\t\t*MAP_SUBNO\t1\r\n" );
311 fprintf( f, "\t\t\t*MAP_AMOUNT\t1.0\r\n" );
312 fprintf( f, "\t\t\t*MAP_TYPE\tScreen\r\n" );
313 fprintf( f, "\t\t\t*BITMAP\t\"%s\\lm_%04d.tga\"\r\n", base, lightmapNum );
314 fprintf( f, "\t\t\t*BITMAP_FILTER\tPyramidal\r\n" );
315 fprintf( f, "\t\t}\r\n" );
316 }
317
318 fprintf( f, "\t}\r\n" );
319 }
320
321
322
323 /*
324 ConvertBSPToASE()
325 exports an 3d studio ase file from the bsp
326 */
327
328 int ConvertBSPToASE( char *bspName ){
329 int i, modelNum;
330 FILE *f;
331 bspShader_t *shader;
332 bspModel_t *model;
333 entity_t *e;
334 vec3_t origin;
335 const char *key;
336 char name[ 1024 ], base[ 1024 ], dirname[ 1024 ];
337
338
339 /* note it */
340 Sys_Printf( "--- Convert BSP to ASE ---\n" );
341
342 /* create the ase filename from the bsp name */
343 strcpy( dirname, bspName );
344 StripExtension( dirname );
345 strcpy( name, bspName );
346 StripExtension( name );
347 strcat( name, ".ase" );
348 Sys_Printf( "writing %s\n", name );
349
350 ExtractFileBase( bspName, base );
351
352 /* open it */
353 f = fopen( name, "wb" );
354 if ( f == NULL ) {
355 Error( "Open failed on %s\n", name );
356 }
357
358 /* print header */
359 fprintf( f, "*3DSMAX_ASCIIEXPORT\t200\r\n" );
360 fprintf( f, "*COMMENT\t\"Generated by Q3Map2 (ydnar) -convert -format ase\"\r\n" );
361 fprintf( f, "*SCENE\t{\r\n" );
362 fprintf( f, "\t*SCENE_FILENAME\t\"%s.bsp\"\r\n", base );
363 fprintf( f, "\t*SCENE_FIRSTFRAME\t0\r\n" );
364 fprintf( f, "\t*SCENE_LASTFRAME\t100\r\n" );
365 fprintf( f, "\t*SCENE_FRAMESPEED\t30\r\n" );
366 fprintf( f, "\t*SCENE_TICKSPERFRAME\t160\r\n" );
367 fprintf( f, "\t*SCENE_BACKGROUND_STATIC\t0.0000\t0.0000\t0.0000\r\n" );
368 fprintf( f, "\t*SCENE_AMBIENT_STATIC\t0.0000\t0.0000\t0.0000\r\n" );
369 fprintf( f, "}\r\n" );
370
371 /* print materials */
372 fprintf( f, "*MATERIAL_LIST\t{\r\n" );
373 if ( lightmapsAsTexcoord ) {
374 int lightmapCount;
375 for ( lightmapCount = 0; lightmapCount < numBSPLightmaps; lightmapCount++ )
376 ;
377 for ( ; ; lightmapCount++ )
378 {
379 char buf[1024];
380 FILE *tmp;
381 snprintf( buf, sizeof( buf ), "%s/lm_%04d.tga", dirname, lightmapCount );
382 buf[sizeof( buf ) - 1] = 0;
383 tmp = fopen( buf, "rb" );
384 if ( !tmp ) {
385 break;
386 }
387 fclose( tmp );
388 }
389 fprintf( f, "\t*MATERIAL_COUNT\t%d\r\n", lightmapCount );
390 for ( i = 0; i < lightmapCount; i++ )
391 ConvertLightmap( f, base, i );
392 numLightmapsASE = lightmapCount;
393 }
394 else
395 {
396 fprintf( f, "\t*MATERIAL_COUNT\t%d\r\n", numBSPShaders );
397 for ( i = 0; i < numBSPShaders; i++ )
398 {
399 shader = &bspShaders[ i ];
400 ConvertShader( f, shader, i );
401 }
402 }
403 fprintf( f, "}\r\n" );
404
405 /* walk entity list */
406 for ( i = 0; i < numEntities; i++ )
407 {
408 /* get entity and model */
409 e = &entities[ i ];
410 if ( i == 0 ) {
411 modelNum = 0;
412 }
413 else
414 {
415 key = ValueForKey( e, "model" );
416 if ( key[ 0 ] != '*' ) {
417 continue;
418 }
419 modelNum = atoi( key + 1 );
420 }
421 model = &bspModels[ modelNum ];
422
423 /* get entity origin */
424 key = ValueForKey( e, "origin" );
425 if ( key[ 0 ] == '\0' ) {
426 VectorClear( origin );
427 }
428 else{
429 GetVectorForKey( e, "origin", origin );
430 }
431
432 /* convert model */
433 ConvertModel( f, model, modelNum, origin );
434 }
435
436 /* close the file and return */
437 fclose( f );
438
439 /* return to sender */
440 return 0;
441 }