]> de.git.xonotic.org Git - xonotic/netradiant.git/commitdiff
Merge commit 'origin/ailmanki/helpchange'
authorRudolf Polzer <divverent@alientrap.org>
Thu, 23 Dec 2010 09:12:10 +0000 (10:12 +0100)
committerRudolf Polzer <divverent@alientrap.org>
Thu, 23 Dec 2010 09:12:10 +0000 (10:12 +0100)
Makefile
tools/quake3/q3map2/convert_ase.c
tools/quake3/q3map2/convert_obj.c [new file with mode: 0644]
tools/quake3/q3map2/main.c
tools/quake3/q3map2/q3map2.h

index 1d9f068f1f277860c693cf58656e8e102c0b4fb4..9a5eec6218ba53d42fc843560469edd59d229bde 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -425,6 +425,7 @@ $(INSTALLDIR)/q3map2.$(EXE): \
        tools/quake3/q3map2/bspfile_rbsp.o \
        tools/quake3/q3map2/bsp.o \
        tools/quake3/q3map2/convert_ase.o \
+       tools/quake3/q3map2/convert_obj.o \
        tools/quake3/q3map2/convert_map.o \
        tools/quake3/q3map2/decals.o \
        tools/quake3/q3map2/facebsp.o \
index 59cc3d9cc41aa7291ab07a6e814160d9bd0b938f..ca070630005bc433f4e654a0dd0c1ca3fd62b20f 100644 (file)
@@ -162,7 +162,7 @@ static void ConvertSurface( FILE *f, bspModel_t *model, int modelNum, bspDrawSur
        fprintf( f, "\t*PROP_MOTIONBLUR\t0\r\n" );
        fprintf( f, "\t*PROP_CASTSHADOW\t1\r\n" );
        fprintf( f, "\t*PROP_RECVSHADOW\t1\r\n" );
-       fprintf( f, "\t*MATERIAL_REF\t%d\r\n", ds->shaderNum );
+       fprintf( f, "\t*MATERIAL_REF\t%d\r\n", lightmapsAsTexcoord ? ds->lightmapNum : ds->shaderNum );
        fprintf( f, "}\r\n" );
 }
 
@@ -280,6 +280,31 @@ static void ConvertShader( FILE *f, bspShader_t *shader, int shaderNum )
        
        fprintf( f, "\t}\r\n" );
 }
+static void ConvertLightmap( FILE *f, const char *base, int lightmapNum )
+{
+       shaderInfo_t    *si;
+       char                    *c, filename[ 1024 ];
+       
+       /* print shader info */
+       fprintf( f, "\t*MATERIAL\t%d\t{\r\n", lightmapNum );
+       fprintf( f, "\t\t*MATERIAL_NAME\t\"lm_%04d\"\r\n", lightmapNum );
+       fprintf( f, "\t\t*MATERIAL_CLASS\t\"Standard\"\r\n" );
+       fprintf( f, "\t\t*MATERIAL_DIFFUSE\t1\t1\t1\r\n");
+       fprintf( f, "\t\t*MATERIAL_SHADING Phong\r\n" );
+       
+       /* print map info */
+       fprintf( f, "\t\t*MAP_DIFFUSE\t{\r\n" );
+       fprintf( f, "\t\t\t*MAP_NAME\t\"lm_%04d\"\r\n", lightmapNum );
+       fprintf( f, "\t\t\t*MAP_CLASS\t\"Bitmap\"\r\n");
+       fprintf( f, "\t\t\t*MAP_SUBNO\t1\r\n" );
+       fprintf( f, "\t\t\t*MAP_AMOUNT\t1.0\r\n" );
+       fprintf( f, "\t\t\t*MAP_TYPE\tScreen\r\n" );
+       fprintf( f, "\t\t\t*BITMAP\t\"%s\\lm_%04d.tga\"\r\n", base, lightmapNum );
+       fprintf( f, "\t\t\t*BITMAP_FILTER\tPyramidal\r\n" );
+       fprintf( f, "\t\t}\r\n" );
+       
+       fprintf( f, "\t}\r\n" );
+}
 
 
 
@@ -332,11 +357,34 @@ int ConvertBSPToASE( char *bspName )
        
        /* print materials */
        fprintf( f, "*MATERIAL_LIST\t{\r\n" );
-       fprintf( f, "\t*MATERIAL_COUNT\t%d\r\n", numBSPShaders );
-       for( i = 0; i < numBSPShaders; i++ )
+       if(lightmapsAsTexcoord)
        {
-               shader = &bspShaders[ i ];
-               ConvertShader( f, shader, i );
+               int lightmapCount;
+               for( lightmapCount = 0; lightmapCount < numBSPLightmaps; lightmapCount++ )
+                       ;
+               for( ; ; lightmapCount++ )
+               {
+                       char buf[1024];
+                       FILE *tmp;
+                       snprintf(buf, sizeof(buf), "%s/lm_%04d.tga", base, lightmapCount);
+                       buf[sizeof(buf) - 1] = 0;
+                       tmp = fopen(buf, "rb");
+                       if(!tmp)
+                               break;
+                       fclose(tmp);
+               }
+               fprintf( f, "\t*MATERIAL_COUNT\t%d\r\n", lightmapCount );
+               for( i = 0; i < lightmapCount; i++ )
+                       ConvertLightmap( f, base, i );
+       }
+       else
+       {
+               fprintf( f, "\t*MATERIAL_COUNT\t%d\r\n", numBSPShaders );
+               for( i = 0; i < numBSPShaders; i++ )
+               {
+                       shader = &bspShaders[ i ];
+                       ConvertShader( f, shader, i );
+               }
        }
        fprintf( f, "}\r\n" );
        
diff --git a/tools/quake3/q3map2/convert_obj.c b/tools/quake3/q3map2/convert_obj.c
new file mode 100644 (file)
index 0000000..dd11537
--- /dev/null
@@ -0,0 +1,298 @@
+/* -------------------------------------------------------------------------------
+
+Copyright (C) 1999-2007 id Software, Inc. and contributors.
+For a list of contributors, see the accompanying CONTRIBUTORS file.
+
+This file is part of GtkRadiant.
+
+GtkRadiant is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+GtkRadiant is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GtkRadiant; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+----------------------------------------------------------------------------------
+
+This code has been altered significantly from its original form, to support
+several games based on the Quake III Arena engine, in the form of "Q3Map2."
+
+------------------------------------------------------------------------------- */
+
+
+
+/* marker */
+#define CONVERT_ASE_C
+
+
+
+/* dependencies */
+#include "q3map2.h"
+
+
+
+/*
+ConvertSurface()
+converts a bsp drawsurface to an obj chunk
+*/
+
+int objVertexCount = 0;
+int objLastShaderNum = -1;
+static void ConvertSurfaceToOBJ( FILE *f, bspModel_t *model, int modelNum, bspDrawSurface_t *ds, int surfaceNum, vec3_t origin )
+{
+       int                             i, v, face, a, b, c;
+       bspDrawVert_t   *dv;
+       
+       /* ignore patches for now */
+       if( ds->surfaceType != MST_PLANAR && ds->surfaceType != MST_TRIANGLE_SOUP )
+               return;
+
+       fprintf(f, "g mat%dmodel%dsurf%d\r\n", ds->shaderNum, modelNum, surfaceNum);
+       switch( ds->surfaceType )
+       {
+               case MST_PLANAR:
+                       fprintf( f, "# SURFACETYPE MST_PLANAR\r\n" );
+                       break;
+               case MST_TRIANGLE_SOUP:
+                       fprintf( f, "# SURFACETYPE MST_TRIANGLE_SOUP\r\n" );
+                       break;
+       }
+
+       /* export shader */
+       if(lightmapsAsTexcoord)
+       {
+               if(objLastShaderNum != ds->lightmapNum[0])
+               {
+                       fprintf(f, "usemtl lm_%04d\r\n", ds->lightmapNum[0]);
+                       objLastShaderNum = ds->lightmapNum[0];
+               }
+       }
+       else
+       {
+               if(objLastShaderNum != ds->shaderNum)
+               {
+                       fprintf(f, "usemtl %s\r\n", bspShaders[ds->shaderNum].shader);
+                       objLastShaderNum = ds->shaderNum;
+               }
+       }
+       
+       /* export vertex */
+       for( i = 0; i < ds->numVerts; i++ )
+       {
+               v = i + ds->firstVert;
+               dv = &bspDrawVerts[ v ];
+               fprintf(f, "# vertex %d\r\n", i + objVertexCount + 1);
+               fprintf(f, "v %f %f %f\r\n", dv->xyz[ 0 ], dv->xyz[ 1 ], dv->xyz[ 2 ]);
+               fprintf(f, "vn %f %f %f\r\n", dv->normal[ 0 ], dv->normal[ 1 ], dv->normal[ 2 ]);
+               if(lightmapsAsTexcoord)
+                       fprintf(f, "vt %f %f\r\n", dv->lightmap[0][0], 1.0 - dv->lightmap[0][1]);
+               else
+                       fprintf(f, "vt %f %f\r\n", dv->st[ 0 ], 1.0 - dv->st[ 1 ]);
+       }
+
+       /* export faces */
+       for( i = 0; i < ds->numIndexes; i += 3 )
+       {
+               face = (i / 3);
+               a = bspDrawIndexes[ i + ds->firstIndex ];
+               c = bspDrawIndexes[ i + ds->firstIndex + 1 ];
+               b = bspDrawIndexes[ i + ds->firstIndex + 2 ];
+               fprintf(f, "f %d/%d/%d %d/%d/%d %d/%d/%d\r\n",
+                       a + objVertexCount + 1, a + objVertexCount + 1, a + objVertexCount + 1, 
+                       b + objVertexCount + 1, b + objVertexCount + 1, b + objVertexCount + 1, 
+                       c + objVertexCount + 1, c + objVertexCount + 1, c + objVertexCount + 1
+               );
+       }
+
+       objVertexCount += ds->numVerts;
+}
+
+
+
+/*
+ConvertModel()
+exports a bsp model to an ase chunk
+*/
+
+static void ConvertModelToOBJ( FILE *f, bspModel_t *model, int modelNum, vec3_t origin )
+{
+       int                                     i, s;
+       bspDrawSurface_t        *ds;
+       
+       
+       /* go through each drawsurf in the model */
+       for( i = 0; i < model->numBSPSurfaces; i++ )
+       {
+               s = i + model->firstBSPSurface;
+               ds = &bspDrawSurfaces[ s ];
+               ConvertSurfaceToOBJ( f, model, modelNum, ds, s, origin );
+       }
+}
+
+
+
+/*
+ConvertShader()
+exports a bsp shader to an ase chunk
+*/
+
+static void ConvertShaderToMTL( FILE *f, bspShader_t *shader, int shaderNum )
+{
+       shaderInfo_t    *si;
+       char                    *c, filename[ 1024 ];
+       
+       
+       /* get shader */
+       si = ShaderInfoForShader( shader->shader );
+       if( si == NULL )
+       {
+               Sys_Printf( "WARNING: NULL shader in BSP\n" );
+               return;
+       }
+       
+       /* set bitmap filename */
+       if( si->shaderImage->filename[ 0 ] != '*' )
+               strcpy( filename, si->shaderImage->filename );
+       else
+               sprintf( filename, "%s.tga", si->shader );
+       for( c = filename; *c != '\0'; c++ )
+               if( *c == '/' )
+                       *c = '\\';
+       
+       /* print shader info */
+       fprintf( f, "newmtl %s\r\n", shader->shader );
+       fprintf( f, "Kd %f %f %f\r\n", si->color[ 0 ], si->color[ 1 ], si->color[ 2 ] );
+       if(shadersAsBitmap)
+               fprintf( f, "map_Kd %s\r\n", shader->shader );
+       else
+               fprintf( f, "map_Kd ..\\%s\r\n", filename );
+}
+
+static void ConvertLightmapToMTL( FILE *f, const char *base, int lightmapNum )
+{
+       /* print shader info */
+       fprintf( f, "newmtl lm_%04d\r\n", lightmapNum );
+       fprintf( f, "map_Kd %s\\lm_%04d.tga\r\n", base, lightmapNum );
+}
+
+
+
+/*
+ConvertBSPToASE()
+exports an 3d studio ase file from the bsp
+*/
+
+int ConvertBSPToOBJ( char *bspName )
+{
+       int                             i, modelNum;
+       FILE                    *f, *fmtl;
+       bspShader_t             *shader;
+       bspModel_t              *model;
+       entity_t                *e;
+       vec3_t                  origin;
+       const char              *key;
+       char                    name[ 1024 ], base[ 1024 ], mtlname[ 1024 ];
+       
+       
+       /* note it */
+       Sys_Printf( "--- Convert BSP to OBJ ---\n" );
+
+       /* create the ase filename from the bsp name */
+       strcpy( name, bspName );
+       StripExtension( name );
+       strcat( name, ".obj" );
+       Sys_Printf( "writing %s\n", name );
+       strcpy( mtlname, bspName );
+       StripExtension( mtlname );
+       strcat( mtlname, ".mtl" );
+       Sys_Printf( "writing %s\n", mtlname );
+       
+       ExtractFileBase( bspName, base );
+       strcat( base, ".bsp" );
+       
+       /* open it */
+       f = fopen( name, "wb" );
+       if( f == NULL )
+               Error( "Open failed on %s\n", name );
+       fmtl = fopen( mtlname, "wb" );
+       if( fmtl == NULL )
+               Error( "Open failed on %s\n", mtlname );
+       
+       /* print header */
+       fprintf( f, "o %s\r\n", base );
+       fprintf( f, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" );
+       fprintf( f, "mtllib %s\r\n", mtlname );
+
+       fprintf( fmtl, "# Generated by Q3Map2 (ydnar) -convert -format obj\r\n" );
+       if(lightmapsAsTexcoord)
+       {
+               int lightmapCount;
+               for( lightmapCount = 0; lightmapCount < numBSPLightmaps; lightmapCount++ )
+                       ;
+               for( ; ; lightmapCount++ )
+               {
+                       char buf[1024];
+                       FILE *tmp;
+                       snprintf(buf, sizeof(buf), "%s/lm_%04d.tga", base, lightmapCount);
+                       buf[sizeof(buf) - 1] = 0;
+                       tmp = fopen(buf, "rb");
+                       if(!tmp)
+                               break;
+                       fclose(tmp);
+               }
+               for( i = 0; i < lightmapCount; i++ )
+                       ConvertLightmapToMTL( fmtl, base, i );
+       }
+       else
+       {
+               for( i = 0; i < numBSPShaders; i++ )
+               {
+                       shader = &bspShaders[ i ];
+                       ConvertShaderToMTL( fmtl, shader, i );
+               }
+       }
+       
+       /* walk entity list */
+       for( i = 0; i < numEntities; i++ )
+       {
+               /* get entity and model */
+               e = &entities[ i ];
+               if( i == 0 )
+                       modelNum = 0;
+               else
+               {
+                       key = ValueForKey( e, "model" );
+                       if( key[ 0 ] != '*' )
+                               continue;
+                       modelNum = atoi( key + 1 );
+               }
+               model = &bspModels[ modelNum ];
+               
+               /* get entity origin */
+               key = ValueForKey( e, "origin" );
+               if( key[ 0 ] == '\0' )
+                       VectorClear( origin );
+               else
+                       GetVectorForKey( e, "origin", origin );
+               
+               /* convert model */
+               ConvertModelToOBJ( f, model, modelNum, origin );
+       }
+       
+       /* close the file and return */
+       fclose( f );
+       fclose( fmtl );
+       
+       /* return to sender */
+       return 0;
+}
+
+
+
index 2deb142989b4c6c76becbe0fce027f6be8449ed2..325235fa57a0e66cdf8c432a970a1a35b2fabd67 100644 (file)
@@ -1565,6 +1565,11 @@ int ConvertBSPMain( int argc, char **argv )
                                convertFunc = ConvertBSPToASE;
                                map_allowed = qfalse;
                        }
+                       else if( !Q_stricmp( argv[ i ], "obj" ) )
+                       {
+                               convertFunc = ConvertBSPToOBJ;
+                               map_allowed = qfalse;
+                       }
                        else if( !Q_stricmp( argv[ i ], "map_bp" ) )
                        {
                                convertFunc = ConvertBSPToMap_BP;
@@ -1597,6 +1602,8 @@ int ConvertBSPMain( int argc, char **argv )
                }
                else if( !strcmp( argv[ i ],  "-shadersasbitmap" ) )
                        shadersAsBitmap = qtrue;
+               else if( !strcmp( argv[ i ],  "-lightmapsastexcoord" ) )
+                       lightmapsAsTexcoord = qtrue;
                else if( !strcmp( argv[ i ],  "-forcereadbsp" ) )
                        force_bsp = qtrue;
                else if( !strcmp( argv[ i ],  "-meta" ) )
index baa29c7c31b15f32937e2cf1de37e65afa129db9..f01d353edbfb12986d31a66417a48500be91f456 100644 (file)
@@ -1530,6 +1530,9 @@ int                                                       ConvertBSPToMap_BP( char *bspName );
 /* convert_ase.c */
 int                                                    ConvertBSPToASE( char *bspName );
 
+/* convert_obj.c */
+int                                                    ConvertBSPToOBJ( char *bspName );
+
 
 /* brush.c */
 sideRef_t                                      *AllocSideRef( side_t *side, sideRef_t *next );
@@ -2272,6 +2275,7 @@ Q_EXTERN float                            linearScale Q_ASSIGN( 1.0f / 8000.0f );
 
 // for .ase conversion
 Q_EXTERN qboolean                      shadersAsBitmap Q_ASSIGN( qfalse );
+Q_EXTERN qboolean                      lightmapsAsTexcoord Q_ASSIGN( qfalse );
 
 Q_EXTERN light_t                       *lights;
 Q_EXTERN int                           numPointLights;