//========= Copyright Valve Corporation, All rights reserved. ============// // // Purpose: // // A simple tool for converting SpeedTree .spt files into .smd files // for use in Source // //===========================================================================// #include #include #include #include "filesystem_tools.h" #include "cmdlib.h" #include "mathlib/mathlib.h" #include "tier0/icommandline.h" #include "SpeedTreeRT.h" void OutputMaterialFile( char *filestub, bool bEnableAlphaTest, float alphaTest=0.0f ) { char filename[MAX_PATH]; _snprintf( filename, MAX_PATH, "%smaterials\\Trees\\%s_VertexLit.vmt", gamedir, filestub ); FILE *file = fopen( filename, "wt" ); if( !file ) return; fprintf( file, "\"VertexLitGeneric\"\n" ); fprintf( file, "{\n" ); fprintf( file, "\t\"$basetexture\" \"trees/%s\"\n", filestub); fprintf( file, "\t\"$model\" \"1\"\n" ); fprintf( file, "\t\"$alphatest\" \"%d\"\n", bEnableAlphaTest?1:0 ); fprintf( file, "\t\"$alphatestreference\" \"%f\"\n", alphaTest ); fprintf( file, "}\n\n" ); fclose( file ); } void OutputLeafMaterialFile( char *filestub, bool bEnableAlphaTest, float alphaTest=0.0f, float *pCenter=NULL ) { char filename[MAX_PATH]; _snprintf( filename, MAX_PATH, "%smaterials\\Trees\\%s_TreeLeaf.vmt", gamedir, filestub ); FILE *file = fopen( filename, "wt" ); if( !file ) return; fprintf( file, "\"TreeLeaf\"\n" ); fprintf( file, "{\n" ); fprintf( file, "\t\"$basetexture\" \"trees/%s\"\n", filestub); fprintf( file, "\t\"$model\" \"1\"\n" ); fprintf( file, "\t\"$alphatest\" \"%d\"\n", bEnableAlphaTest?1:0 ); fprintf( file, "\t\"$alphatestreference\" \"%f\"\n", alphaTest ); fprintf( file, "\t\"$leafcenter\" \"[ %f %f %f ]\"\n", pCenter[0], pCenter[1], pCenter[2] ); fprintf( file, "}\n\n" ); fclose( file ); } void OutputTreeGeometry( CSpeedTreeRT &speedTree, CSpeedTreeRT::SGeometry &sGeom, char *filename ) { FILE *file = fopen( filename, "wt" ); if( !file ) return; fprintf( file, "version 1\n" ); fprintf( file, "nodes\n" ); fprintf( file, "0 \"Tree\" -1\n" ); fprintf( file, "end\n" ); fprintf( file, "skeleton\n" ); fprintf( file, "time 0\n" ); fprintf( file, "0 0.0 0.0 0.0 0.0 0.0 0.0\n" ); fprintf( file, "end\n" ); fprintf( file, "triangles\n" ); CSpeedTreeRT::STextures sTextures; speedTree.GetTextures( sTextures ); char branchTextureName[ MAX_PATH ]; _splitpath( sTextures.m_pBranchTextureFilename, NULL, NULL, branchTextureName, NULL ); float alphaTest = sGeom.m_fBranchAlphaTestValue/255.0f; OutputMaterialFile( branchTextureName, false, alphaTest ); for( int nStrip=0;nStripCreateCmdLine( argc, argv ); if( CommandLine()->ParmCount()!=2 ) { printf( "usage : sptconvert \n" ); exit(0); } MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f, false, false, false, false ); CmdLib_InitFileSystem( CommandLine()->GetParm(1) ); CSpeedTreeRT speedTree; char treeName[MAX_PATH]; _splitpath( argv[1], NULL, NULL, treeName, NULL ); char smdFileName[MAX_PATH]; sprintf( smdFileName, "%s.smd", treeName ); char qcFileName[MAX_PATH]; sprintf( qcFileName, "%s.qc", treeName ); OutputQCFile( treeName ); if( speedTree.LoadTree( argv[1] ) ) { if( speedTree.Compute( NULL, 1, false ) ) { CSpeedTreeRT::SGeometry sGeom; speedTree.GetGeometry( sGeom, SpeedTree_AllGeometry ); OutputTreeGeometry( speedTree, sGeom, smdFileName ); } else { // Trouble with compute } } else { // Trouble with load } }