1 /* -------------------------------------------------------------------------------
3 Copyright (C) 1999-2007 id Software, Inc. and contributors.
4 For a list of contributors, see the accompanying CONTRIBUTORS file.
6 This file is part of GtkRadiant.
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.
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.
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
22 ----------------------------------------------------------------------------------
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."
27 ------------------------------------------------------------------------------- */
43 converts a bsp drawsurface to an ase chunk
46 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;
54 /* ignore patches for now */
55 if( ds->surfaceType != MST_PLANAR && ds->surfaceType != MST_TRIANGLE_SOUP )
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" );
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 )
82 fprintf( f, "\t\t*COMMENT\t\"SURFACETYPE\tMST_PLANAR\"\r\n" );
84 case MST_TRIANGLE_SOUP:
85 fprintf( f, "\t\t*COMMENT\t\"SURFACETYPE\tMST_TRIANGLE_SOUP\"\r\n" );
89 /* export vertex xyz */
90 fprintf( f, "\t\t*MESH_VERTEX_LIST\t{\r\n" );
91 for( i = 0; i < ds->numVerts; i++ )
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 ] );
97 fprintf( f, "\t\t}\r\n" );
99 /* export vertex normals */
100 fprintf( f, "\t\t*MESH_NORMALS\t{\r\n" );
101 for( i = 0; i < ds->numIndexes; 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 ] );
113 for( i = 0; i < ds->numVerts; i++ )
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 ] );
119 fprintf( f, "\t\t}\r\n" );
122 fprintf( f, "\t\t*MESH_FACE_LIST\t{\r\n" );
123 for( i = 0; i < ds->numIndexes; 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",
132 fprintf( f, "\t\t}\r\n" );
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++ )
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 );
143 fprintf( f, "\t\t}\r\n" );
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 )
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 );
156 fprintf( f, "\t\t}\r\n" );
158 /* print mesh footer */
159 fprintf( f, "\t}\r\n" );
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" );
173 exports a bsp model to an ase chunk
176 static void ConvertModel( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )
179 bspDrawSurface_t *ds;
182 /* go through each drawsurf in the model */
183 for( i = 0; i < model->numBSPSurfaces; i++ )
185 s = i + model->firstBSPSurface;
186 ds = &bspDrawSurfaces[ s ];
187 ConvertSurface( f, model, modelNum, ds, s, origin );
195 exports a bsp shader to an ase chunk
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
213 *MATERIAL_XP_TYPE Filter
219 *BITMAP "models/test/rock16l"
227 *UVW_BLUR_OFFSET 0.0000
228 *UVW_NOUSE_AMT 1.0000
229 *UVW_NOISE_SIZE 1.0000
231 *UVW_NOISE_PHASE 0.0000
232 *BITMAP_FILTER Pyramidal
237 static void ConvertShader( FILE *f, bspShader_t *shader, int shaderNum )
240 char *c, filename[ 1024 ];
244 si = ShaderInfoForShader( shader->shader );
247 Sys_Printf( "WARNING: NULL shader in BSP\n" );
251 /* set bitmap filename */
252 if( si->shaderImage->filename[ 0 ] != '*' )
253 strcpy( filename, si->shaderImage->filename );
255 sprintf( filename, "%s.tga", si->shader );
256 for( c = filename; *c != '\0'; c++ )
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" );
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" );
275 fprintf( f, "\t\t\t*BITMAP\t\"%s\"\r\n", shader->shader );
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" );
281 fprintf( f, "\t}\r\n" );
288 exports an 3d studio ase file from the bsp
291 int ConvertBSPToASE( char *bspName )
300 char name[ 1024 ], base[ 1024 ];
304 Sys_Printf( "--- Convert BSP to ASE ---\n" );
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 );
312 ExtractFileBase( bspName, base );
313 strcat( base, ".bsp" );
316 f = fopen( name, "wb" );
318 Error( "Open failed on %s\n", name );
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" );
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++ )
338 shader = &bspShaders[ i ];
339 ConvertShader( f, shader, i );
341 fprintf( f, "}\r\n" );
343 /* walk entity list */
344 for( i = 0; i < numEntities; i++ )
346 /* get entity and model */
352 key = ValueForKey( e, "model" );
353 if( key[ 0 ] != '*' )
355 modelNum = atoi( key + 1 );
357 model = &bspModels[ modelNum ];
359 /* get entity origin */
360 key = ValueForKey( e, "origin" );
361 if( key[ 0 ] == '\0' )
362 VectorClear( origin );
364 GetVectorForKey( e, "origin", origin );
367 ConvertModel( f, model, modelNum, origin );
370 /* close the file and return */
373 /* return to sender */