]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/convert_ase.c
set eol-style
[xonotic/netradiant.git] / tools / quake3 / q3map2 / convert_ase.c
1 /*
2 Copyright (C) 1999-2007 id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 ----------------------------------------------------------------------------------
22
23 This code has been altered significantly from its original form, to support
24 several games based on the Quake III Arena engine, in the form of "Q3Map2."
25
26 ------------------------------------------------------------------------------- */
27
28
29
30 /* marker */
31 #define CONVERT_ASE_C
32
33
34
35 /* dependencies */
36 #include "q3map2.h"
37
38
39
40 /*
41 ConvertSurface()
42 converts a bsp drawsurface to an ase chunk
43 */
44
45 static void ConvertSurface( FILE *f, bspModel_t *model, int modelNum, bspDrawSurface_t *ds, int surfaceNum, vec3_t origin )
46 {
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         /* print object header for each dsurf */
58         sprintf( name, "mat%dmodel%dsurf%d", ds->shaderNum, modelNum, surfaceNum );
59         fprintf( f, "*GEOMOBJECT\t{\r\n" );
60         fprintf( f, "\t*NODE_NAME\t\"%s\"\r\n", name );
61         fprintf( f, "\t*NODE_TM\t{\r\n" );
62         fprintf( f, "\t\t*NODE_NAME\t\"%s\"\r\n", name );
63         fprintf( f, "\t\t*INHERIT_POS\t0\t0\t0\r\n" );
64         fprintf( f, "\t\t*INHERIT_ROT\t0\t0\t0\r\n" );
65         fprintf( f, "\t\t*INHERIT_SCL\t0\t0\t0\r\n" );
66         fprintf( f, "\t\t*TM_ROW0\t1.0\t0\t0\r\n" );
67         fprintf( f, "\t\t*TM_ROW1\t0\t1.0\t0\r\n" );
68         fprintf( f, "\t\t*TM_ROW2\t0\t0\t1.0\r\n" );
69         fprintf( f, "\t\t*TM_ROW3\t0\t0\t0\r\n" );
70         fprintf( f, "\t\t*TM_POS\t%f\t%f\t%f\r\n", origin[ 0 ], origin[ 1 ], origin[ 2 ] );
71         fprintf( f, "\t}\r\n" );
72         
73         /* print mesh header */
74         fprintf( f, "\t*MESH\t{\r\n" );
75         fprintf( f, "\t\t*TIMEVALUE\t0\r\n" );
76         fprintf( f, "\t\t*MESH_NUMVERTEX\t%d\r\n", ds->numVerts );
77         fprintf( f, "\t\t*MESH_NUMFACES\t%d\r\n", ds->numIndexes / 3 );
78         switch( ds->surfaceType )
79         {
80                 case MST_PLANAR:
81                         fprintf( f, "\t\t*COMMENT\t\"SURFACETYPE\tMST_PLANAR\"\r\n" );
82                         break;
83                 case MST_TRIANGLE_SOUP:
84                         fprintf( f, "\t\t*COMMENT\t\"SURFACETYPE\tMST_TRIANGLE_SOUP\"\r\n" );
85                         break;
86         }
87         
88         /* export vertex xyz */
89         fprintf( f, "\t\t*MESH_VERTEX_LIST\t{\r\n" );
90         for( i = 0; i < ds->numVerts; i++ )
91         {
92                 v = i + ds->firstVert;
93                 dv = &bspDrawVerts[ v ];
94                 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 ] );
95         }
96         fprintf( f, "\t\t}\r\n" );
97
98         /* export vertex normals */
99         fprintf( f, "\t\t*MESH_NORMALS\t{\r\n" );
100         for( i = 0; i < ds->numIndexes; i += 3 )
101         {
102                 face = (i / 3);
103                 a = bspDrawIndexes[ i + ds->firstIndex ];
104                 b = bspDrawIndexes[ i + ds->firstIndex + 1 ];
105                 c = bspDrawIndexes[ i + ds->firstIndex + 2 ];
106                 VectorCopy( bspDrawVerts[ a ].normal, normal );
107                 VectorAdd( normal, bspDrawVerts[ b ].normal, normal );
108                 VectorAdd( normal, bspDrawVerts[ c ].normal, normal );
109                 if( VectorNormalize( normal, normal ) )
110                         fprintf( f, "\t\t\t*MESH_FACENORMAL\t%d\t%f\t%f\t%f\r\n", face, normal[ 0 ], normal[ 1 ], normal[ 2 ] );
111         }
112         for( i = 0; i < ds->numVerts; i++ )
113         {
114                 v = i + ds->firstVert;
115                 dv = &bspDrawVerts[ v ];
116                 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 ] );
117         }
118         fprintf( f, "\t\t}\r\n" );
119         
120         /* export faces */
121         fprintf( f, "\t\t*MESH_FACE_LIST\t{\r\n" );
122         for( i = 0; i < ds->numIndexes; i += 3 )
123         {
124                 face = (i / 3);
125                 a = bspDrawIndexes[ i + ds->firstIndex ];
126                 c = bspDrawIndexes[ i + ds->firstIndex + 1 ];
127                 b = bspDrawIndexes[ i + ds->firstIndex + 2 ];
128                 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",
129                         face, a, b, c );
130         }
131         fprintf( f, "\t\t}\r\n" );
132         
133         /* export vertex st */
134         fprintf( f, "\t\t*MESH_NUMTVERTEX\t%d\r\n", ds->numVerts );
135         fprintf( f, "\t\t*MESH_TVERTLIST\t{\r\n" );
136         for( i = 0; i < ds->numVerts; i++ )
137         {
138                 v = i + ds->firstVert;
139                 dv = &bspDrawVerts[ v ];
140                 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 );
141         }
142         fprintf( f, "\t\t}\r\n" );
143         
144         /* export texture faces */
145         fprintf( f, "\t\t*MESH_NUMTVFACES\t%d\r\n", ds->numIndexes / 3 );
146         fprintf( f, "\t\t*MESH_TFACELIST\t{\r\n" );
147         for( i = 0; i < ds->numIndexes; i += 3 )
148         {
149                 face = (i / 3);
150                 a = bspDrawIndexes[ i + ds->firstIndex ];
151                 c = bspDrawIndexes[ i + ds->firstIndex + 1 ];
152                 b = bspDrawIndexes[ i + ds->firstIndex + 2 ];
153                 fprintf( f, "\t\t\t*MESH_TFACE\t%d\t%d\t%d\t%d\r\n", face, a, b, c );
154         }
155         fprintf( f, "\t\t}\r\n" );
156         
157         /* print mesh footer */
158         fprintf( f, "\t}\r\n" );
159
160         /* print object footer */
161         fprintf( f, "\t*PROP_MOTIONBLUR\t0\r\n" );
162         fprintf( f, "\t*PROP_CASTSHADOW\t1\r\n" );
163         fprintf( f, "\t*PROP_RECVSHADOW\t1\r\n" );
164         fprintf( f, "\t*MATERIAL_REF\t%d\r\n", ds->shaderNum );
165         fprintf( f, "}\r\n" );
166 }
167
168
169
170 /*
171 ConvertModel()
172 exports a bsp model to an ase chunk
173 */
174
175 static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )
176 {
177         int                                     i, s;
178         bspDrawSurface_t        *ds;
179         
180         
181         /* go through each drawsurf in the model */
182         for( i = 0; i < model->numBSPSurfaces; i++ )
183         {
184                 s = i + model->firstBSPSurface;
185                 ds = &bspDrawSurfaces[ s ];
186                 ConvertSurface( f, model, modelNum, ds, s, origin );
187         }
188 }
189
190
191
192 /*
193 ConvertShader()
194 exports a bsp shader to an ase chunk
195 */
196
197 /*
198         *MATERIAL 0 {
199                 *MATERIAL_NAME "models/test/rock16l"
200                 *MATERIAL_CLASS "Standard"
201                 *MATERIAL_AMBIENT 0.5882        0.5882  0.5882
202                 *MATERIAL_DIFFUSE 0.5882        0.5882  0.5882
203                 *MATERIAL_SPECULAR 0.5882       0.5882  0.5882
204                 *MATERIAL_SHINE 0.0000
205                 *MATERIAL_SHINESTRENGTH 0.0000
206                 *MATERIAL_TRANSPARENCY 0.0000
207                 *MATERIAL_WIRESIZE 1.0000
208                 *MATERIAL_SHADING Phong
209                 *MATERIAL_XP_FALLOFF 0.0000
210                 *MATERIAL_SELFILLUM 0.0000
211                 *MATERIAL_FALLOFF In
212                 *MATERIAL_XP_TYPE Filter
213                 *MAP_DIFFUSE {
214                         *MAP_NAME "Map #2"
215                         *MAP_CLASS "Bitmap"
216                         *MAP_SUBNO 1
217                         *MAP_AMOUNT 1.0000
218                         *BITMAP "models/test/rock16l"
219                         *MAP_TYPE Screen
220                         *UVW_U_OFFSET 0.0000
221                         *UVW_V_OFFSET 0.0000
222                         *UVW_U_TILING 1.0000
223                         *UVW_V_TILING 1.0000
224                         *UVW_ANGLE 0.0000
225                         *UVW_BLUR 1.0000
226                         *UVW_BLUR_OFFSET 0.0000
227                         *UVW_NOUSE_AMT 1.0000
228                         *UVW_NOISE_SIZE 1.0000
229                         *UVW_NOISE_LEVEL 1
230                         *UVW_NOISE_PHASE 0.0000
231                         *BITMAP_FILTER Pyramidal
232                 }
233         }
234 */
235
236 static void ConvertShader( FILE *f, bspShader_t *shader, int shaderNum )
237 {
238         shaderInfo_t    *si;
239         char                    *c, filename[ 1024 ];
240         
241         
242         /* get shader */
243         si = ShaderInfoForShader( shader->shader );
244         if( si == NULL )
245         {
246                 Sys_Printf( "WARNING: NULL shader in BSP\n" );
247                 return;
248         }
249         
250         /* set bitmap filename */
251         if( si->shaderImage->filename[ 0 ] != '*' )
252                 strcpy( filename, si->shaderImage->filename );
253         else
254                 sprintf( filename, "%s.tga", si->shader );
255         for( c = filename; *c != '\0'; c++ )
256                 if( *c == '/' )
257                         *c = '\\';
258         
259         /* print shader info */
260         fprintf( f, "\t*MATERIAL\t%d\t{\r\n", shaderNum );
261         fprintf( f, "\t\t*MATERIAL_NAME\t\"%s\"\r\n", shader->shader );
262         fprintf( f, "\t\t*MATERIAL_CLASS\t\"Standard\"\r\n" );
263         fprintf( f, "\t\t*MATERIAL_DIFFUSE\t%f\t%f\t%f\r\n", si->color[ 0 ], si->color[ 1 ], si->color[ 2 ] );
264         fprintf( f, "\t\t*MATERIAL_SHADING Phong\r\n" );
265         
266         /* print map info */
267         fprintf( f, "\t\t*MAP_DIFFUSE\t{\r\n" );
268         fprintf( f, "\t\t\t*MAP_NAME\t\"%s\"\r\n", shader->shader );
269         fprintf( f, "\t\t\t*MAP_CLASS\t\"Bitmap\"\r\n");
270         fprintf( f, "\t\t\t*MAP_SUBNO\t1\r\n" );
271         fprintf( f, "\t\t\t*MAP_AMOUNT\t1.0\r\n" );
272         fprintf( f, "\t\t\t*MAP_TYPE\tScreen\r\n" );
273         fprintf( f, "\t\t\t*BITMAP\t\"..\\%s\"\r\n", filename );
274         fprintf( f, "\t\t\t*BITMAP_FILTER\tPyramidal\r\n" );
275         fprintf( f, "\t\t}\r\n" );
276         
277         fprintf( f, "\t}\r\n" );
278 }
279
280
281
282 /*
283 ConvertBSPToASE()
284 exports an 3d studio ase file from the bsp
285 */
286
287 int ConvertBSPToASE( char *bspName )
288 {
289         int                             i, modelNum;
290         FILE                    *f;
291         bspShader_t             *shader;
292         bspModel_t              *model;
293         entity_t                *e;
294         vec3_t                  origin;
295         const char              *key;
296         char                    name[ 1024 ], base[ 1024 ];
297         
298         
299         /* note it */
300         Sys_Printf( "--- Convert BSP to ASE ---\n" );
301
302         /* create the ase filename from the bsp name */
303         strcpy( name, bspName );
304         StripExtension( name );
305         strcat( name, ".ase" );
306         Sys_Printf( "writing %s\n", name );
307         
308         ExtractFileBase( bspName, base );
309         strcat( base, ".bsp" );
310         
311         /* open it */
312         f = fopen( name, "wb" );
313         if( f == NULL )
314                 Error( "Open failed on %s\n", name );
315         
316         /* print header */
317         fprintf( f, "*3DSMAX_ASCIIEXPORT\t200\r\n" );
318         fprintf( f, "*COMMENT\t\"Generated by Q3Map2 (ydnar) -convert -format ase\"\r\n" );
319         fprintf( f, "*SCENE\t{\r\n" );
320         fprintf( f, "\t*SCENE_FILENAME\t\"%s\"\r\n", base );
321         fprintf( f, "\t*SCENE_FIRSTFRAME\t0\r\n" );
322         fprintf( f, "\t*SCENE_LASTFRAME\t100\r\n" );
323         fprintf( f, "\t*SCENE_FRAMESPEED\t30\r\n" );
324         fprintf( f, "\t*SCENE_TICKSPERFRAME\t160\r\n" );
325         fprintf( f, "\t*SCENE_BACKGROUND_STATIC\t0.0000\t0.0000\t0.0000\r\n" );
326         fprintf( f, "\t*SCENE_AMBIENT_STATIC\t0.0000\t0.0000\t0.0000\r\n" );
327         fprintf( f, "}\r\n" );
328         
329         /* print materials */
330         fprintf( f, "*MATERIAL_LIST\t{\r\n" );
331         fprintf( f, "\t*MATERIAL_COUNT\t%d\r\n", numBSPShaders );
332         for( i = 0; i < numBSPShaders; i++ )
333         {
334                 shader = &bspShaders[ i ];
335                 ConvertShader( f, shader, i );
336         }
337         fprintf( f, "}\r\n" );
338         
339         /* walk entity list */
340         for( i = 0; i < numEntities; i++ )
341         {
342                 /* get entity and model */
343                 e = &entities[ i ];
344                 if( i == 0 )
345                         modelNum = 0;
346                 else
347                 {
348                         key = ValueForKey( e, "model" );
349                         if( key[ 0 ] != '*' )
350                                 continue;
351                         modelNum = atoi( key + 1 );
352                 }
353                 model = &bspModels[ modelNum ];
354                 
355                 /* get entity origin */
356                 key = ValueForKey( e, "origin" );
357                 if( key[ 0 ] == '\0' )
358                         VectorClear( origin );
359                 else
360                         GetVectorForKey( e, "origin", origin );
361                 
362                 /* convert model */
363                 ConvertModel( f, model, modelNum, origin );
364         }
365         
366         /* close the file and return */
367         fclose( f );
368         
369         /* return to sender */
370         return 0;
371 }
372
373
374