fix if(...)
[divverent/netradiant.git] / tools / quake3 / q3map2 / convert_ase.c
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 static void ConvertSurface( FILE *f, bspModel_t *model, int modelNum, bspDrawSurface_t *ds, int surfaceNum, vec3_t origin )
47 {
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         /* print object header for each dsurf */
59         sprintf( name, "mat%dmodel%dsurf%d", ds->shaderNum, modelNum, surfaceNum );
60         fprintf( f, "*GEOMOBJECT\t{\r\n" );
61         fprintf( f, "\t*NODE_NAME\t\"%s\"\r\n", name );
62         fprintf( f, "\t*NODE_TM\t{\r\n" );
63         fprintf( f, "\t\t*NODE_NAME\t\"%s\"\r\n", name );
64         fprintf( f, "\t\t*INHERIT_POS\t0\t0\t0\r\n" );
65         fprintf( f, "\t\t*INHERIT_ROT\t0\t0\t0\r\n" );
66         fprintf( f, "\t\t*INHERIT_SCL\t0\t0\t0\r\n" );
67         fprintf( f, "\t\t*TM_ROW0\t1.0\t0\t0\r\n" );
68         fprintf( f, "\t\t*TM_ROW1\t0\t1.0\t0\r\n" );
69         fprintf( f, "\t\t*TM_ROW2\t0\t0\t1.0\r\n" );
70         fprintf( f, "\t\t*TM_ROW3\t0\t0\t0\r\n" );
71         fprintf( f, "\t\t*TM_POS\t%f\t%f\t%f\r\n", origin[ 0 ], origin[ 1 ], origin[ 2 ] );
72         fprintf( f, "\t}\r\n" );
73         
74         /* print mesh header */
75         fprintf( f, "\t*MESH\t{\r\n" );
76         fprintf( f, "\t\t*TIMEVALUE\t0\r\n" );
77         fprintf( f, "\t\t*MESH_NUMVERTEX\t%d\r\n", ds->numVerts );
78         fprintf( f, "\t\t*MESH_NUMFACES\t%d\r\n", ds->numIndexes / 3 );
79         switch( ds->surfaceType )
80         {
81                 case MST_PLANAR:
82                         fprintf( f, "\t\t*COMMENT\t\"SURFACETYPE\tMST_PLANAR\"\r\n" );
83                         break;
84                 case MST_TRIANGLE_SOUP:
85                         fprintf( f, "\t\t*COMMENT\t\"SURFACETYPE\tMST_TRIANGLE_SOUP\"\r\n" );
86                         break;
87         }
88         
89         /* export vertex xyz */
90         fprintf( f, "\t\t*MESH_VERTEX_LIST\t{\r\n" );
91         for( i = 0; i < ds->numVerts; i++ )
92         {
93                 v = i + ds->firstVert;
94                 dv = &bspDrawVerts[ v ];
95                 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 ] );
96         }
97         fprintf( f, "\t\t}\r\n" );
98
99         /* export vertex normals */
100         fprintf( f, "\t\t*MESH_NORMALS\t{\r\n" );
101         for( i = 0; i < ds->numIndexes; i += 3 )
102         {
103                 face = (i / 3);
104                 a = bspDrawIndexes[ i + ds->firstIndex ];
105                 b = bspDrawIndexes[ i + ds->firstIndex + 1 ];
106                 c = bspDrawIndexes[ i + ds->firstIndex + 2 ];
107                 VectorCopy( bspDrawVerts[ a ].normal, normal );
108                 VectorAdd( normal, bspDrawVerts[ b ].normal, normal );
109                 VectorAdd( normal, bspDrawVerts[ c ].normal, normal );
110                 if( VectorNormalize( normal, normal ) )
111                         fprintf( f, "\t\t\t*MESH_FACENORMAL\t%d\t%f\t%f\t%f\r\n", face, normal[ 0 ], normal[ 1 ], normal[ 2 ] );
112         }
113         for( i = 0; i < ds->numVerts; i++ )
114         {
115                 v = i + ds->firstVert;
116                 dv = &bspDrawVerts[ v ];
117                 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 ] );
118         }
119         fprintf( f, "\t\t}\r\n" );
120         
121         /* export faces */
122         fprintf( f, "\t\t*MESH_FACE_LIST\t{\r\n" );
123         for( i = 0; i < ds->numIndexes; i += 3 )
124         {
125                 face = (i / 3);
126                 a = bspDrawIndexes[ i + ds->firstIndex ];
127                 c = bspDrawIndexes[ i + ds->firstIndex + 1 ];
128                 b = bspDrawIndexes[ i + ds->firstIndex + 2 ];
129                 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",
130                         face, a, b, c );
131         }
132         fprintf( f, "\t\t}\r\n" );
133         
134         /* export vertex st */
135         fprintf( f, "\t\t*MESH_NUMTVERTEX\t%d\r\n", ds->numVerts );
136         fprintf( f, "\t\t*MESH_TVERTLIST\t{\r\n" );
137         for( i = 0; i < ds->numVerts; i++ )
138         {
139                 v = i + ds->firstVert;
140                 dv = &bspDrawVerts[ v ];
141                 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 );
142         }
143         fprintf( f, "\t\t}\r\n" );
144         
145         /* export texture faces */
146         fprintf( f, "\t\t*MESH_NUMTVFACES\t%d\r\n", ds->numIndexes / 3 );
147         fprintf( f, "\t\t*MESH_TFACELIST\t{\r\n" );
148         for( i = 0; i < ds->numIndexes; i += 3 )
149         {
150                 face = (i / 3);
151                 a = bspDrawIndexes[ i + ds->firstIndex ];
152                 c = bspDrawIndexes[ i + ds->firstIndex + 1 ];
153                 b = bspDrawIndexes[ i + ds->firstIndex + 2 ];
154                 fprintf( f, "\t\t\t*MESH_TFACE\t%d\t%d\t%d\t%d\r\n", face, a, b, c );
155         }
156         fprintf( f, "\t\t}\r\n" );
157         
158         /* print mesh footer */
159         fprintf( f, "\t}\r\n" );
160
161         /* print object footer */
162         fprintf( f, "\t*PROP_MOTIONBLUR\t0\r\n" );
163         fprintf( f, "\t*PROP_CASTSHADOW\t1\r\n" );
164         fprintf( f, "\t*PROP_RECVSHADOW\t1\r\n" );
165         fprintf( f, "\t*MATERIAL_REF\t%d\r\n", ds->shaderNum );
166         fprintf( f, "}\r\n" );
167 }
168
169
170
171 /*
172 ConvertModel()
173 exports a bsp model to an ase chunk
174 */
175
176 static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )
177 {
178         int                                     i, s;
179         bspDrawSurface_t        *ds;
180         
181         
182         /* go through each drawsurf in the model */
183         for( i = 0; i < model->numBSPSurfaces; i++ )
184         {
185                 s = i + model->firstBSPSurface;
186                 ds = &bspDrawSurfaces[ s ];
187                 ConvertSurface( f, model, modelNum, ds, s, origin );
188         }
189 }
190
191
192
193 /*
194 ConvertShader()
195 exports a bsp shader to an ase chunk
196 */
197
198 /*
199         *MATERIAL 0 {
200                 *MATERIAL_NAME "models/test/rock16l"
201                 *MATERIAL_CLASS "Standard"
202                 *MATERIAL_AMBIENT 0.5882        0.5882  0.5882
203                 *MATERIAL_DIFFUSE 0.5882        0.5882  0.5882
204                 *MATERIAL_SPECULAR 0.5882       0.5882  0.5882
205                 *MATERIAL_SHINE 0.0000
206                 *MATERIAL_SHINESTRENGTH 0.0000
207                 *MATERIAL_TRANSPARENCY 0.0000
208                 *MATERIAL_WIRESIZE 1.0000
209                 *MATERIAL_SHADING Phong
210                 *MATERIAL_XP_FALLOFF 0.0000
211                 *MATERIAL_SELFILLUM 0.0000
212                 *MATERIAL_FALLOFF In
213                 *MATERIAL_XP_TYPE Filter
214                 *MAP_DIFFUSE {
215                         *MAP_NAME "Map #2"
216                         *MAP_CLASS "Bitmap"
217                         *MAP_SUBNO 1
218                         *MAP_AMOUNT 1.0000
219                         *BITMAP "models/test/rock16l"
220                         *MAP_TYPE Screen
221                         *UVW_U_OFFSET 0.0000
222                         *UVW_V_OFFSET 0.0000
223                         *UVW_U_TILING 1.0000
224                         *UVW_V_TILING 1.0000
225                         *UVW_ANGLE 0.0000
226                         *UVW_BLUR 1.0000
227                         *UVW_BLUR_OFFSET 0.0000
228                         *UVW_NOUSE_AMT 1.0000
229                         *UVW_NOISE_SIZE 1.0000
230                         *UVW_NOISE_LEVEL 1
231                         *UVW_NOISE_PHASE 0.0000
232                         *BITMAP_FILTER Pyramidal
233                 }
234         }
235 */
236
237 static void ConvertShader( FILE *f, bspShader_t *shader, int shaderNum )
238 {
239         shaderInfo_t    *si;
240         char                    *c, filename[ 1024 ];
241         
242         
243         /* get shader */
244         si = ShaderInfoForShader( shader->shader );
245         if( si == NULL )
246         {
247                 Sys_Printf( "WARNING: NULL shader in BSP\n" );
248                 return;
249         }
250         
251         /* set bitmap filename */
252         if( si->shaderImage->filename[ 0 ] != '*' )
253                 strcpy( filename, si->shaderImage->filename );
254         else
255                 sprintf( filename, "%s.tga", si->shader );
256         for( c = filename; *c != '\0'; c++ )
257                 if( *c == '/' )
258                         *c = '\\';
259         
260         /* print shader info */
261         fprintf( f, "\t*MATERIAL\t%d\t{\r\n", shaderNum );
262         fprintf( f, "\t\t*MATERIAL_NAME\t\"%s\"\r\n", shader->shader );
263         fprintf( f, "\t\t*MATERIAL_CLASS\t\"Standard\"\r\n" );
264         fprintf( f, "\t\t*MATERIAL_DIFFUSE\t%f\t%f\t%f\r\n", si->color[ 0 ], si->color[ 1 ], si->color[ 2 ] );
265         fprintf( f, "\t\t*MATERIAL_SHADING Phong\r\n" );
266         
267         /* print map info */
268         fprintf( f, "\t\t*MAP_DIFFUSE\t{\r\n" );
269         fprintf( f, "\t\t\t*MAP_NAME\t\"%s\"\r\n", shader->shader );
270         fprintf( f, "\t\t\t*MAP_CLASS\t\"Bitmap\"\r\n");
271         fprintf( f, "\t\t\t*MAP_SUBNO\t1\r\n" );
272         fprintf( f, "\t\t\t*MAP_AMOUNT\t1.0\r\n" );
273         fprintf( f, "\t\t\t*MAP_TYPE\tScreen\r\n" );
274         if(shadersAsBitmap)
275                 fprintf( f, "\t\t\t*BITMAP\t\"%s\"\r\n", shader->shader );
276         else
277                 fprintf( f, "\t\t\t*BITMAP\t\"..\\%s\"\r\n", filename );
278         fprintf( f, "\t\t\t*BITMAP_FILTER\tPyramidal\r\n" );
279         fprintf( f, "\t\t}\r\n" );
280         
281         fprintf( f, "\t}\r\n" );
282 }
283
284
285
286 /*
287 ConvertBSPToASE()
288 exports an 3d studio ase file from the bsp
289 */
290
291 int ConvertBSPToASE( char *bspName )
292 {
293         int                             i, modelNum;
294         FILE                    *f;
295         bspShader_t             *shader;
296         bspModel_t              *model;
297         entity_t                *e;
298         vec3_t                  origin;
299         const char              *key;
300         char                    name[ 1024 ], base[ 1024 ];
301         
302         
303         /* note it */
304         Sys_Printf( "--- Convert BSP to ASE ---\n" );
305
306         /* create the ase filename from the bsp name */
307         strcpy( name, bspName );
308         StripExtension( name );
309         strcat( name, ".ase" );
310         Sys_Printf( "writing %s\n", name );
311         
312         ExtractFileBase( bspName, base );
313         strcat( base, ".bsp" );
314         
315         /* open it */
316         f = fopen( name, "wb" );
317         if( f == NULL )
318                 Error( "Open failed on %s\n", name );
319         
320         /* print header */
321         fprintf( f, "*3DSMAX_ASCIIEXPORT\t200\r\n" );
322         fprintf( f, "*COMMENT\t\"Generated by Q3Map2 (ydnar) -convert -format ase\"\r\n" );
323         fprintf( f, "*SCENE\t{\r\n" );
324         fprintf( f, "\t*SCENE_FILENAME\t\"%s\"\r\n", base );
325         fprintf( f, "\t*SCENE_FIRSTFRAME\t0\r\n" );
326         fprintf( f, "\t*SCENE_LASTFRAME\t100\r\n" );
327         fprintf( f, "\t*SCENE_FRAMESPEED\t30\r\n" );
328         fprintf( f, "\t*SCENE_TICKSPERFRAME\t160\r\n" );
329         fprintf( f, "\t*SCENE_BACKGROUND_STATIC\t0.0000\t0.0000\t0.0000\r\n" );
330         fprintf( f, "\t*SCENE_AMBIENT_STATIC\t0.0000\t0.0000\t0.0000\r\n" );
331         fprintf( f, "}\r\n" );
332         
333         /* print materials */
334         fprintf( f, "*MATERIAL_LIST\t{\r\n" );
335         fprintf( f, "\t*MATERIAL_COUNT\t%d\r\n", numBSPShaders );
336         for( i = 0; i < numBSPShaders; i++ )
337         {
338                 shader = &bspShaders[ i ];
339                 ConvertShader( f, shader, i );
340         }
341         fprintf( f, "}\r\n" );
342         
343         /* walk entity list */
344         for( i = 0; i < numEntities; i++ )
345         {
346                 /* get entity and model */
347                 e = &entities[ i ];
348                 if( i == 0 )
349                         modelNum = 0;
350                 else
351                 {
352                         key = ValueForKey( e, "model" );
353                         if( key[ 0 ] != '*' )
354                                 continue;
355                         modelNum = atoi( key + 1 );
356                 }
357                 model = &bspModels[ modelNum ];
358                 
359                 /* get entity origin */
360                 key = ValueForKey( e, "origin" );
361                 if( key[ 0 ] == '\0' )
362                         VectorClear( origin );
363                 else
364                         GetVectorForKey( e, "origin", origin );
365                 
366                 /* convert model */
367                 ConvertModel( f, model, modelNum, origin );
368         }
369         
370         /* close the file and return */
371         fclose( f );
372         
373         /* return to sender */
374         return 0;
375 }
376
377
378