]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/convert_ase.c
uncrustify! now the code is only ugly on the *inside*
[xonotic/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         int i, v, face, a, b, c;
48         bspDrawVert_t   *dv;
49         vec3_t normal;
50         char name[ 1024 ];
51
52
53         /* ignore patches for now */
54         if ( ds->surfaceType != MST_PLANAR && ds->surfaceType != MST_TRIANGLE_SOUP ) {
55                 return;
56         }
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         }
114         for ( i = 0; i < ds->numVerts; i++ )
115         {
116                 v = i + ds->firstVert;
117                 dv = &bspDrawVerts[ v ];
118                 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 ] );
119         }
120         fprintf( f, "\t\t}\r\n" );
121
122         /* export faces */
123         fprintf( f, "\t\t*MESH_FACE_LIST\t{\r\n" );
124         for ( i = 0; i < ds->numIndexes; i += 3 )
125         {
126                 face = ( i / 3 );
127                 a = bspDrawIndexes[ i + ds->firstIndex ];
128                 c = bspDrawIndexes[ i + ds->firstIndex + 1 ];
129                 b = bspDrawIndexes[ i + ds->firstIndex + 2 ];
130                 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",
131                                  face, a, b, c );
132         }
133         fprintf( f, "\t\t}\r\n" );
134
135         /* export vertex st */
136         fprintf( f, "\t\t*MESH_NUMTVERTEX\t%d\r\n", ds->numVerts );
137         fprintf( f, "\t\t*MESH_TVERTLIST\t{\r\n" );
138         for ( i = 0; i < ds->numVerts; i++ )
139         {
140                 v = i + ds->firstVert;
141                 dv = &bspDrawVerts[ v ];
142                 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 );
143         }
144         fprintf( f, "\t\t}\r\n" );
145
146         /* export texture faces */
147         fprintf( f, "\t\t*MESH_NUMTVFACES\t%d\r\n", ds->numIndexes / 3 );
148         fprintf( f, "\t\t*MESH_TFACELIST\t{\r\n" );
149         for ( i = 0; i < ds->numIndexes; i += 3 )
150         {
151                 face = ( i / 3 );
152                 a = bspDrawIndexes[ i + ds->firstIndex ];
153                 c = bspDrawIndexes[ i + ds->firstIndex + 1 ];
154                 b = bspDrawIndexes[ i + ds->firstIndex + 2 ];
155                 fprintf( f, "\t\t\t*MESH_TFACE\t%d\t%d\t%d\t%d\r\n", face, a, b, c );
156         }
157         fprintf( f, "\t\t}\r\n" );
158
159         /* print mesh footer */
160         fprintf( f, "\t}\r\n" );
161
162         /* print object footer */
163         fprintf( f, "\t*PROP_MOTIONBLUR\t0\r\n" );
164         fprintf( f, "\t*PROP_CASTSHADOW\t1\r\n" );
165         fprintf( f, "\t*PROP_RECVSHADOW\t1\r\n" );
166         fprintf( f, "\t*MATERIAL_REF\t%d\r\n", ds->shaderNum );
167         fprintf( f, "}\r\n" );
168 }
169
170
171
172 /*
173    ConvertModel()
174    exports a bsp model to an ase chunk
175  */
176
177 static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin ){
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         shaderInfo_t    *si;
239         char            *c, filename[ 1024 ];
240
241
242         /* get shader */
243         si = ShaderInfoForShader( shader->shader );
244         if ( si == NULL ) {
245                 Sys_Printf( "WARNING: NULL shader in BSP\n" );
246                 return;
247         }
248
249         /* set bitmap filename */
250         if ( si->shaderImage->filename[ 0 ] != '*' ) {
251                 strcpy( filename, si->shaderImage->filename );
252         }
253         else{
254                 sprintf( filename, "%s.tga", si->shader );
255         }
256         for ( c = filename; *c != '\0'; c++ )
257                 if ( *c == '/' ) {
258                         *c = '\\';
259                 }
260
261         /* print shader info */
262         fprintf( f, "\t*MATERIAL\t%d\t{\r\n", shaderNum );
263         fprintf( f, "\t\t*MATERIAL_NAME\t\"%s\"\r\n", shader->shader );
264         fprintf( f, "\t\t*MATERIAL_CLASS\t\"Standard\"\r\n" );
265         fprintf( f, "\t\t*MATERIAL_DIFFUSE\t%f\t%f\t%f\r\n", si->color[ 0 ], si->color[ 1 ], si->color[ 2 ] );
266         fprintf( f, "\t\t*MATERIAL_SHADING Phong\r\n" );
267
268         /* print map info */
269         fprintf( f, "\t\t*MAP_DIFFUSE\t{\r\n" );
270         fprintf( f, "\t\t\t*MAP_NAME\t\"%s\"\r\n", shader->shader );
271         fprintf( f, "\t\t\t*MAP_CLASS\t\"Bitmap\"\r\n" );
272         fprintf( f, "\t\t\t*MAP_SUBNO\t1\r\n" );
273         fprintf( f, "\t\t\t*MAP_AMOUNT\t1.0\r\n" );
274         fprintf( f, "\t\t\t*MAP_TYPE\tScreen\r\n" );
275         fprintf( f, "\t\t\t*BITMAP\t\"..\\%s\"\r\n", filename );
276         fprintf( f, "\t\t\t*BITMAP_FILTER\tPyramidal\r\n" );
277         fprintf( f, "\t\t}\r\n" );
278
279         fprintf( f, "\t}\r\n" );
280 }
281
282
283
284 /*
285    ConvertBSPToASE()
286    exports an 3d studio ase file from the bsp
287  */
288
289 int ConvertBSPToASE( char *bspName ){
290         int i, modelNum;
291         FILE            *f;
292         bspShader_t     *shader;
293         bspModel_t      *model;
294         entity_t        *e;
295         vec3_t origin;
296         const char      *key;
297         char name[ 1024 ], base[ 1024 ];
298
299
300         /* note it */
301         Sys_Printf( "--- Convert BSP to ASE ---\n" );
302
303         /* create the ase filename from the bsp name */
304         strcpy( name, bspName );
305         StripExtension( name );
306         strcat( name, ".ase" );
307         Sys_Printf( "writing %s\n", name );
308
309         ExtractFileBase( bspName, base );
310         strcat( base, ".bsp" );
311
312         /* open it */
313         f = fopen( name, "wb" );
314         if ( f == NULL ) {
315                 Error( "Open failed on %s\n", name );
316         }
317
318         /* print header */
319         fprintf( f, "*3DSMAX_ASCIIEXPORT\t200\r\n" );
320         fprintf( f, "*COMMENT\t\"Generated by Q3Map2 (ydnar) -convert -format ase\"\r\n" );
321         fprintf( f, "*SCENE\t{\r\n" );
322         fprintf( f, "\t*SCENE_FILENAME\t\"%s\"\r\n", base );
323         fprintf( f, "\t*SCENE_FIRSTFRAME\t0\r\n" );
324         fprintf( f, "\t*SCENE_LASTFRAME\t100\r\n" );
325         fprintf( f, "\t*SCENE_FRAMESPEED\t30\r\n" );
326         fprintf( f, "\t*SCENE_TICKSPERFRAME\t160\r\n" );
327         fprintf( f, "\t*SCENE_BACKGROUND_STATIC\t0.0000\t0.0000\t0.0000\r\n" );
328         fprintf( f, "\t*SCENE_AMBIENT_STATIC\t0.0000\t0.0000\t0.0000\r\n" );
329         fprintf( f, "}\r\n" );
330
331         /* print materials */
332         fprintf( f, "*MATERIAL_LIST\t{\r\n" );
333         fprintf( f, "\t*MATERIAL_COUNT\t%d\r\n", numBSPShaders );
334         for ( i = 0; i < numBSPShaders; i++ )
335         {
336                 shader = &bspShaders[ i ];
337                 ConvertShader( f, shader, i );
338         }
339         fprintf( f, "}\r\n" );
340
341         /* walk entity list */
342         for ( i = 0; i < numEntities; i++ )
343         {
344                 /* get entity and model */
345                 e = &entities[ i ];
346                 if ( i == 0 ) {
347                         modelNum = 0;
348                 }
349                 else
350                 {
351                         key = ValueForKey( e, "model" );
352                         if ( key[ 0 ] != '*' ) {
353                                 continue;
354                         }
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                 }
364                 else{
365                         GetVectorForKey( e, "origin", origin );
366                 }
367
368                 /* convert model */
369                 ConvertModel( f, model, modelNum, origin );
370         }
371
372         /* close the file and return */
373         fclose( f );
374
375         /* return to sender */
376         return 0;
377 }