]> icculus.org git repositories - divverent/netradiant.git/blob - tools/quake3/q3map2/convert_obj.c
option -lightmapsastexcoord to extract lightmap UV
[divverent/netradiant.git] / tools / quake3 / q3map2 / convert_obj.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 obj chunk
44 */
45
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 )
49 {
50         int                             i, v, face, a, b, c;
51         bspDrawVert_t   *dv;
52         
53         /* ignore patches for now */
54         if( ds->surfaceType != MST_PLANAR && ds->surfaceType != MST_TRIANGLE_SOUP )
55                 return;
56
57         fprintf(f, "g mat%dmodel%dsurf%d\r\n", ds->shaderNum, modelNum, surfaceNum);
58         switch( ds->surfaceType )
59         {
60                 case MST_PLANAR:
61                         fprintf( f, "# SURFACETYPE MST_PLANAR\r\n" );
62                         break;
63                 case MST_TRIANGLE_SOUP:
64                         fprintf( f, "# SURFACETYPE MST_TRIANGLE_SOUP\r\n" );
65                         break;
66         }
67
68         /* export shader */
69         if(lightmapsAsTexcoord)
70         {
71                 if(objLastShaderNum != ds->lightmapNum[0])
72                 {
73                         fprintf(f, "usemtl lm_%04d\r\n", ds->lightmapNum[0]);
74                         objLastShaderNum = ds->lightmapNum[0];
75                 }
76         }
77         else
78         {
79                 if(objLastShaderNum != ds->shaderNum)
80                 {
81                         fprintf(f, "usemtl %s\r\n", bspShaders[ds->shaderNum].shader);
82                         objLastShaderNum = ds->shaderNum;
83                 }
84         }
85         
86         /* export vertex */
87         for( i = 0; i < ds->numVerts; i++ )
88         {
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]);
96                 else
97                         fprintf(f, "vt %f %f\r\n", dv->st[ 0 ], 1.0 - dv->st[ 1 ]);
98         }
99
100         /* export faces */
101         for( i = 0; i < ds->numIndexes; i += 3 )
102         {
103                 face = (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
111                 );
112         }
113
114         objVertexCount += ds->numVerts;
115 }
116
117
118
119 /*
120 ConvertModel()
121 exports a bsp model to an ase chunk
122 */
123
124 static void ConvertModelToOBJ( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )
125 {
126         int                                     i, s;
127         bspDrawSurface_t        *ds;
128         
129         
130         /* go through each drawsurf in the model */
131         for( i = 0; i < model->numBSPSurfaces; i++ )
132         {
133                 s = i + model->firstBSPSurface;
134                 ds = &bspDrawSurfaces[ s ];
135                 ConvertSurfaceToOBJ( f, model, modelNum, ds, s, origin );
136         }
137 }
138
139
140
141 /*
142 ConvertShader()
143 exports a bsp shader to an ase chunk
144 */
145
146 static void ConvertShaderToMTL( FILE *f, bspShader_t *shader, int shaderNum )
147 {
148         shaderInfo_t    *si;
149         char                    *c, filename[ 1024 ];
150         
151         
152         /* get shader */
153         si = ShaderInfoForShader( shader->shader );
154         if( si == NULL )
155         {
156                 Sys_Printf( "WARNING: NULL shader in BSP\n" );
157                 return;
158         }
159         
160         /* set bitmap filename */
161         if( si->shaderImage->filename[ 0 ] != '*' )
162                 strcpy( filename, si->shaderImage->filename );
163         else
164                 sprintf( filename, "%s.tga", si->shader );
165         for( c = filename; *c != '\0'; c++ )
166                 if( *c == '/' )
167                         *c = '\\';
168         
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 ] );
172         if(shadersAsBitmap)
173                 fprintf( f, "map_Kd %s\r\n", shader->shader );
174         else
175                 fprintf( f, "map_Kd ..\\%s\r\n", filename );
176 }
177
178 static void ConvertLightmapToMTL( FILE *f, const char *base, int lightmapNum )
179 {
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 );
183 }
184
185
186
187 /*
188 ConvertBSPToASE()
189 exports an 3d studio ase file from the bsp
190 */
191
192 int ConvertBSPToOBJ( char *bspName )
193 {
194         int                             i, modelNum;
195         FILE                    *f, *fmtl;
196         bspShader_t             *shader;
197         bspModel_t              *model;
198         entity_t                *e;
199         vec3_t                  origin;
200         const char              *key;
201         char                    name[ 1024 ], base[ 1024 ], mtlname[ 1024 ];
202         
203         
204         /* note it */
205         Sys_Printf( "--- Convert BSP to OBJ ---\n" );
206
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 );
216         
217         ExtractFileBase( bspName, base );
218         strcat( base, ".bsp" );
219         
220         /* open it */
221         f = fopen( name, "wb" );
222         if( f == NULL )
223                 Error( "Open failed on %s\n", name );
224         fmtl = fopen( mtlname, "wb" );
225         if( fmtl == NULL )
226                 Error( "Open failed on %s\n", mtlname );
227         
228         /* print header */
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 );
232
233         fprintf( fmtl, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" );
234         if(lightmapsAsTexcoord)
235         {
236                 int lightmapCount;
237                 for( lightmapCount = 0; lightmapCount < numBSPLightmaps; lightmapCount++ )
238                         ;
239                 for( ; ; lightmapCount++ )
240                 {
241                         char buf[1024];
242                         FILE *tmp;
243                         snprintf(buf, sizeof(buf), "%s/lm_%04d.tga", base, lightmapCount);
244                         buf[sizeof(buf) - 1] = 0;
245                         tmp = fopen(buf, "rb");
246                         if(!tmp)
247                                 break;
248                         fclose(tmp);
249                 }
250                 for( i = 0; i < lightmapCount; i++ )
251                         ConvertLightmapToMTL( fmtl, base, i );
252         }
253         else
254         {
255                 for( i = 0; i < numBSPShaders; i++ )
256                 {
257                         shader = &bspShaders[ i ];
258                         ConvertShaderToMTL( fmtl, shader, i );
259                 }
260         }
261         
262         /* walk entity list */
263         for( i = 0; i < numEntities; i++ )
264         {
265                 /* get entity and model */
266                 e = &entities[ i ];
267                 if( i == 0 )
268                         modelNum = 0;
269                 else
270                 {
271                         key = ValueForKey( e, "model" );
272                         if( key[ 0 ] != '*' )
273                                 continue;
274                         modelNum = atoi( key + 1 );
275                 }
276                 model = &bspModels[ modelNum ];
277                 
278                 /* get entity origin */
279                 key = ValueForKey( e, "origin" );
280                 if( key[ 0 ] == '\0' )
281                         VectorClear( origin );
282                 else
283                         GetVectorForKey( e, "origin", origin );
284                 
285                 /* convert model */
286                 ConvertModelToOBJ( f, model, modelNum, origin );
287         }
288         
289         /* close the file and return */
290         fclose( f );
291         fclose( fmtl );
292         
293         /* return to sender */
294         return 0;
295 }
296
297
298