]> git.xonotic.org Git - xonotic/netradiant.git/blob - tools/quake3/q3map2/convert_obj.c
fix file extension
[xonotic/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 ], dirname[ 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( dirname, bspName );
209         StripExtension( dirname );
210         strcpy( name, bspName );
211         StripExtension( name );
212         strcat( name, ".obj" );
213         Sys_Printf( "writing %s\n", name );
214         strcpy( mtlname, bspName );
215         StripExtension( mtlname );
216         strcat( mtlname, ".mtl" );
217         Sys_Printf( "writing %s\n", mtlname );
218         
219         ExtractFileBase( bspName, base );
220         
221         /* open it */
222         f = fopen( name, "wb" );
223         if( f == NULL )
224                 Error( "Open failed on %s\n", name );
225         fmtl = fopen( mtlname, "wb" );
226         if( fmtl == NULL )
227                 Error( "Open failed on %s\n", mtlname );
228         
229         /* print header */
230         fprintf( f, "o %s\r\n", base );
231         fprintf( f, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" );
232         fprintf( f, "mtllib %s.mtl\r\n", base );
233
234         fprintf( fmtl, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" );
235         if(lightmapsAsTexcoord)
236         {
237                 int lightmapCount;
238                 for( lightmapCount = 0; lightmapCount < numBSPLightmaps; lightmapCount++ )
239                         ;
240                 for( ; ; lightmapCount++ )
241                 {
242                         char buf[1024];
243                         FILE *tmp;
244                         snprintf(buf, sizeof(buf), "%s/lm_%04d.tga", dirname, lightmapCount);
245                         buf[sizeof(buf) - 1] = 0;
246                         tmp = fopen(buf, "rb");
247                         if(!tmp)
248                                 break;
249                         fclose(tmp);
250                 }
251                 for( i = 0; i < lightmapCount; i++ )
252                         ConvertLightmapToMTL( fmtl, base, i );
253         }
254         else
255         {
256                 for( i = 0; i < numBSPShaders; i++ )
257                 {
258                         shader = &bspShaders[ i ];
259                         ConvertShaderToMTL( fmtl, shader, i );
260                 }
261         }
262         
263         /* walk entity list */
264         for( i = 0; i < numEntities; i++ )
265         {
266                 /* get entity and model */
267                 e = &entities[ i ];
268                 if( i == 0 )
269                         modelNum = 0;
270                 else
271                 {
272                         key = ValueForKey( e, "model" );
273                         if( key[ 0 ] != '*' )
274                                 continue;
275                         modelNum = atoi( key + 1 );
276                 }
277                 model = &bspModels[ modelNum ];
278                 
279                 /* get entity origin */
280                 key = ValueForKey( e, "origin" );
281                 if( key[ 0 ] == '\0' )
282                         VectorClear( origin );
283                 else
284                         GetVectorForKey( e, "origin", origin );
285                 
286                 /* convert model */
287                 ConvertModelToOBJ( f, model, modelNum, origin );
288         }
289         
290         /* close the file and return */
291         fclose( f );
292         fclose( fmtl );
293         
294         /* return to sender */
295         return 0;
296 }
297
298
299