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 objVertexCount = 0;
47 int objLastShaderNum = -1;
48 static void ConvertSurfaceToOBJ( FILE *f, bspModel_t *model, int modelNum, bspDrawSurface_t *ds, int surfaceNum, vec3_t origin )
50 int i, v, face, a, b, c;
53 /* ignore patches for now */
54 if( ds->surfaceType != MST_PLANAR && ds->surfaceType != MST_TRIANGLE_SOUP )
57 fprintf(f, "g mat%dmodel%dsurf%d\r\n", ds->shaderNum, modelNum, surfaceNum);
58 switch( ds->surfaceType )
61 fprintf( f, "# SURFACETYPE MST_PLANAR\r\n" );
63 case MST_TRIANGLE_SOUP:
64 fprintf( f, "# SURFACETYPE MST_TRIANGLE_SOUP\r\n" );
69 if(lightmapsAsTexcoord)
71 if(objLastShaderNum != ds->lightmapNum[0])
73 fprintf(f, "usemtl lm_%04d\r\n", ds->lightmapNum[0]);
74 objLastShaderNum = ds->lightmapNum[0];
79 if(objLastShaderNum != ds->shaderNum)
81 fprintf(f, "usemtl %s\r\n", bspShaders[ds->shaderNum].shader);
82 objLastShaderNum = ds->shaderNum;
87 for( i = 0; i < ds->numVerts; i++ )
89 v = i + ds->firstVert;
90 dv = &bspDrawVerts[ v ];
91 fprintf(f, "# vertex %d\r\n", i + objVertexCount + 1);
92 fprintf(f, "v %f %f %f\r\n", dv->xyz[ 0 ], dv->xyz[ 1 ], dv->xyz[ 2 ]);
93 fprintf(f, "vn %f %f %f\r\n", dv->normal[ 0 ], dv->normal[ 1 ], dv->normal[ 2 ]);
94 if(lightmapsAsTexcoord)
95 fprintf(f, "vt %f %f\r\n", dv->lightmap[0][0], 1.0 - dv->lightmap[0][1]);
97 fprintf(f, "vt %f %f\r\n", dv->st[ 0 ], 1.0 - dv->st[ 1 ]);
101 for( i = 0; i < ds->numIndexes; i += 3 )
104 a = bspDrawIndexes[ i + ds->firstIndex ];
105 c = bspDrawIndexes[ i + ds->firstIndex + 1 ];
106 b = bspDrawIndexes[ i + ds->firstIndex + 2 ];
107 fprintf(f, "f %d/%d/%d %d/%d/%d %d/%d/%d\r\n",
108 a + objVertexCount + 1, a + objVertexCount + 1, a + objVertexCount + 1,
109 b + objVertexCount + 1, b + objVertexCount + 1, b + objVertexCount + 1,
110 c + objVertexCount + 1, c + objVertexCount + 1, c + objVertexCount + 1
114 objVertexCount += ds->numVerts;
121 exports a bsp model to an ase chunk
124 static void ConvertModelToOBJ( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )
127 bspDrawSurface_t *ds;
130 /* go through each drawsurf in the model */
131 for( i = 0; i < model->numBSPSurfaces; i++ )
133 s = i + model->firstBSPSurface;
134 ds = &bspDrawSurfaces[ s ];
135 ConvertSurfaceToOBJ( f, model, modelNum, ds, s, origin );
143 exports a bsp shader to an ase chunk
146 static void ConvertShaderToMTL( FILE *f, bspShader_t *shader, int shaderNum )
149 char *c, filename[ 1024 ];
153 si = ShaderInfoForShader( shader->shader );
156 Sys_Printf( "WARNING: NULL shader in BSP\n" );
160 /* set bitmap filename */
161 if( si->shaderImage->filename[ 0 ] != '*' )
162 strcpy( filename, si->shaderImage->filename );
164 sprintf( filename, "%s.tga", si->shader );
165 for( c = filename; *c != '\0'; c++ )
169 /* print shader info */
170 fprintf( f, "newmtl %s\r\n", shader->shader );
171 fprintf( f, "Kd %f %f %f\r\n", si->color[ 0 ], si->color[ 1 ], si->color[ 2 ] );
173 fprintf( f, "map_Kd %s\r\n", shader->shader );
175 fprintf( f, "map_Kd ..\\%s\r\n", filename );
178 static void ConvertLightmapToMTL( FILE *f, const char *base, int lightmapNum )
180 /* print shader info */
181 fprintf( f, "newmtl lm_%04d\r\n", lightmapNum );
182 fprintf( f, "map_Kd %s\\lm_%04d.tga\r\n", base, lightmapNum );
189 exports an 3d studio ase file from the bsp
192 int ConvertBSPToOBJ( char *bspName )
201 char name[ 1024 ], base[ 1024 ], mtlname[ 1024 ];
205 Sys_Printf( "--- Convert BSP to OBJ ---\n" );
207 /* create the ase filename from the bsp name */
208 strcpy( name, bspName );
209 StripExtension( name );
210 strcat( name, ".obj" );
211 Sys_Printf( "writing %s\n", name );
212 strcpy( mtlname, bspName );
213 StripExtension( mtlname );
214 strcat( mtlname, ".mtl" );
215 Sys_Printf( "writing %s\n", mtlname );
217 ExtractFileBase( bspName, base );
218 strcat( base, ".bsp" );
221 f = fopen( name, "wb" );
223 Error( "Open failed on %s\n", name );
224 fmtl = fopen( mtlname, "wb" );
226 Error( "Open failed on %s\n", mtlname );
229 fprintf( f, "o %s\r\n", base );
230 fprintf( f, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" );
231 fprintf( f, "mtllib %s\r\n", mtlname );
233 fprintf( fmtl, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" );
234 if(lightmapsAsTexcoord)
237 for( lightmapCount = 0; lightmapCount < numBSPLightmaps; lightmapCount++ )
239 for( ; ; lightmapCount++ )
243 snprintf(buf, sizeof(buf), "%s/lm_%04d.tga", base, lightmapCount);
244 buf[sizeof(buf) - 1] = 0;
245 tmp = fopen(buf, "rb");
250 for( i = 0; i < lightmapCount; i++ )
251 ConvertLightmapToMTL( fmtl, base, i );
255 for( i = 0; i < numBSPShaders; i++ )
257 shader = &bspShaders[ i ];
258 ConvertShaderToMTL( fmtl, shader, i );
262 /* walk entity list */
263 for( i = 0; i < numEntities; i++ )
265 /* get entity and model */
271 key = ValueForKey( e, "model" );
272 if( key[ 0 ] != '*' )
274 modelNum = atoi( key + 1 );
276 model = &bspModels[ modelNum ];
278 /* get entity origin */
279 key = ValueForKey( e, "origin" );
280 if( key[ 0 ] == '\0' )
281 VectorClear( origin );
283 GetVectorForKey( e, "origin", origin );
286 ConvertModelToOBJ( f, model, modelNum, origin );
289 /* close the file and return */
293 /* return to sender */