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 obj chunk
46 int firstLightmap = 0;
47 int lastLightmap = -1;
48 static void ConvertLightmapToMTL( FILE *f, const char *base, int lightmapNum );
50 int objVertexCount = 0;
51 int objLastShaderNum = -1;
52 static void ConvertSurfaceToOBJ( FILE *f, bspModel_t *model, int modelNum, bspDrawSurface_t *ds, int surfaceNum, vec3_t origin )
57 /* ignore patches for now */
58 if( ds->surfaceType != MST_PLANAR && ds->surfaceType != MST_TRIANGLE_SOUP )
61 fprintf(f, "g mat%dmodel%dsurf%d\r\n", ds->shaderNum, modelNum, surfaceNum);
62 switch( ds->surfaceType )
65 fprintf( f, "# SURFACETYPE MST_PLANAR\r\n" );
67 case MST_TRIANGLE_SOUP:
68 fprintf( f, "# SURFACETYPE MST_TRIANGLE_SOUP\r\n" );
73 if(lightmapsAsTexcoord)
75 if(objLastShaderNum != ds->lightmapNum[0])
77 fprintf(f, "usemtl lm_%04d\r\n", ds->lightmapNum[0] + deluxemap);
78 objLastShaderNum = ds->lightmapNum[0] + deluxemap;
80 if(ds->lightmapNum[0] + (int)deluxemap < firstLightmap)
82 Sys_Printf( "WARNING: lightmap %d out of range (exporting anyway)\n", ds->lightmapNum[0] + deluxemap );
83 firstLightmap = ds->lightmapNum[0] + deluxemap;
85 if(ds->lightmapNum[0] > lastLightmap)
87 Sys_Printf( "WARNING: lightmap %d out of range (exporting anyway)\n", ds->lightmapNum[0] + deluxemap );
88 lastLightmap = ds->lightmapNum[0] + deluxemap;
93 if(objLastShaderNum != ds->shaderNum)
95 fprintf(f, "usemtl %s\r\n", bspShaders[ds->shaderNum].shader);
96 objLastShaderNum = ds->shaderNum;
101 for( i = 0; i < ds->numVerts; i++ )
103 v = i + ds->firstVert;
104 dv = &bspDrawVerts[ v ];
105 fprintf(f, "# vertex %d\r\n", i + objVertexCount + 1);
106 fprintf(f, "v %f %f %f\r\n", dv->xyz[ 0 ], dv->xyz[ 1 ], dv->xyz[ 2 ]);
107 fprintf(f, "vn %f %f %f\r\n", dv->normal[ 0 ], dv->normal[ 1 ], dv->normal[ 2 ]);
108 if(lightmapsAsTexcoord)
109 fprintf(f, "vt %f %f\r\n", dv->lightmap[0][0], 1.0 - dv->lightmap[0][1]);
111 fprintf(f, "vt %f %f\r\n", dv->st[ 0 ], 1.0 - dv->st[ 1 ]);
115 for( i = 0; i < ds->numIndexes; i += 3 )
117 a = bspDrawIndexes[ i + ds->firstIndex ];
118 c = bspDrawIndexes[ i + ds->firstIndex + 1 ];
119 b = bspDrawIndexes[ i + ds->firstIndex + 2 ];
120 fprintf(f, "f %d/%d/%d %d/%d/%d %d/%d/%d\r\n",
121 a + objVertexCount + 1, a + objVertexCount + 1, a + objVertexCount + 1,
122 b + objVertexCount + 1, b + objVertexCount + 1, b + objVertexCount + 1,
123 c + objVertexCount + 1, c + objVertexCount + 1, c + objVertexCount + 1
127 objVertexCount += ds->numVerts;
134 exports a bsp model to an ase chunk
137 static void ConvertModelToOBJ( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )
140 bspDrawSurface_t *ds;
143 /* go through each drawsurf in the model */
144 for( i = 0; i < model->numBSPSurfaces; i++ )
146 s = i + model->firstBSPSurface;
147 ds = &bspDrawSurfaces[ s ];
148 ConvertSurfaceToOBJ( f, model, modelNum, ds, s, origin );
156 exports a bsp shader to an ase chunk
159 static void ConvertShaderToMTL( FILE *f, bspShader_t *shader, int shaderNum )
162 char filename[ 1024 ];
166 si = ShaderInfoForShader( shader->shader );
169 Sys_Printf( "WARNING: NULL shader in BSP\n" );
173 /* set bitmap filename */
174 if( si->shaderImage->filename[ 0 ] != '*' )
175 strcpy( filename, si->shaderImage->filename );
177 sprintf( filename, "%s.tga", si->shader );
179 /* blender hates this, so let's not do it
180 for( c = filename; *c != '\0'; c++ )
185 /* print shader info */
186 fprintf( f, "newmtl %s\r\n", shader->shader );
187 fprintf( f, "Kd %f %f %f\r\n", si->color[ 0 ], si->color[ 1 ], si->color[ 2 ] );
189 fprintf( f, "map_Kd %s\r\n", shader->shader );
191 /* blender hates this, so let's not do it
192 fprintf( f, "map_Kd ..\\%s\r\n", filename );
194 fprintf( f, "map_Kd ../%s\r\n", filename );
197 static void ConvertLightmapToMTL( FILE *f, const char *base, int lightmapNum )
199 /* print shader info */
200 fprintf( f, "newmtl lm_%04d\r\n", lightmapNum );
202 /* blender hates this, so let's not do it
203 fprintf( f, "map_Kd %s\\lm_%04d.tga\r\n", base, lightmapNum );
205 fprintf( f, "map_Kd %s/lm_%04d.tga\r\n", base, lightmapNum );
212 exports an 3d studio ase file from the bsp
215 int ConvertBSPToOBJ( char *bspName )
224 char name[ 1024 ], base[ 1024 ], mtlname[ 1024 ], dirname[ 1024 ];
228 Sys_Printf( "--- Convert BSP to OBJ ---\n" );
230 /* create the ase filename from the bsp name */
231 strcpy( dirname, bspName );
232 StripExtension( dirname );
233 strcpy( name, bspName );
234 StripExtension( name );
235 strcat( name, ".obj" );
236 Sys_Printf( "writing %s\n", name );
237 strcpy( mtlname, bspName );
238 StripExtension( mtlname );
239 strcat( mtlname, ".mtl" );
240 Sys_Printf( "writing %s\n", mtlname );
242 ExtractFileBase( bspName, base );
245 f = fopen( name, "wb" );
247 Error( "Open failed on %s\n", name );
248 fmtl = fopen( mtlname, "wb" );
250 Error( "Open failed on %s\n", mtlname );
253 fprintf( f, "o %s\r\n", base );
254 fprintf( f, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" );
255 fprintf( f, "mtllib %s.mtl\r\n", base );
257 fprintf( fmtl, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" );
258 if(lightmapsAsTexcoord)
261 for( lightmapCount = 0; lightmapCount < numBSPLightmaps; lightmapCount++ )
263 for( ; ; lightmapCount++ )
267 snprintf(buf, sizeof(buf), "%s/lm_%04d.tga", dirname, lightmapCount);
268 buf[sizeof(buf) - 1] = 0;
269 tmp = fopen(buf, "rb");
274 lastLightmap = lightmapCount - 1;
278 for( i = 0; i < numBSPShaders; i++ )
280 shader = &bspShaders[ i ];
281 ConvertShaderToMTL( fmtl, shader, i );
285 /* walk entity list */
286 for( i = 0; i < numEntities; i++ )
288 /* get entity and model */
294 key = ValueForKey( e, "model" );
295 if( key[ 0 ] != '*' )
297 modelNum = atoi( key + 1 );
299 model = &bspModels[ modelNum ];
301 /* get entity origin */
302 key = ValueForKey( e, "origin" );
303 if( key[ 0 ] == '\0' )
304 VectorClear( origin );
306 GetVectorForKey( e, "origin", origin );
309 ConvertModelToOBJ( f, model, modelNum, origin );
312 if(lightmapsAsTexcoord)
314 for( i = firstLightmap; i <= lastLightmap; i++ )
315 ConvertLightmapToMTL( fmtl, base, i );
318 /* close the file and return */
322 /* return to sender */