This commit is contained in:
FluorescentCIAAfricanAmerican
2020-04-22 12:56:21 -04:00
commit 3bf9df6b27
15370 changed files with 5489726 additions and 0 deletions
@@ -0,0 +1,545 @@
//========= Copyright Valve Corporation, All rights reserved. ============//
///////////////////////////////////////////////////////////////////////
// SpeedTreeRT.h
//
// *** INTERACTIVE DATA VISUALIZATION (IDV) PROPRIETARY INFORMATION ***
//
// This software is supplied under the terms of a license agreement or
// nondisclosure agreement with Interactive Data Visualization and may
// not be copied or disclosed except in accordance with the terms of
// that agreement.
//
// Copyright (c) 2003-2005 IDV, Inc.
// All Rights Reserved.
//
// IDV, Inc.
// http://www.idvinc.com
//
// Release version 1.8.0
#pragma once
// storage-class specification
#if (defined(WIN32) || defined(XENON)) && defined(SPEEDTREE_DLL_EXPORTS)
#define ST_STORAGE_CLASS __declspec(dllexport)
#else
#define ST_STORAGE_CLASS
#endif
// Macintosh export control
#ifdef __APPLE__
#pragma export on
#endif
// specify calling convention
#if defined(WIN32) || defined(XENON)
#define CALL_CONV __cdecl
#else
#define CALL_CONV
#endif
// forward references
class CIndexedGeometry;
class CTreeEngine;
class CLeafGeometry;
class CLightingEngine;
class CWindEngine;
class CTreeFileAccess;
class CSimpleBillboard;
class CFrondEngine;
struct STreeInstanceData;
struct SInstanceList;
struct SEmbeddedTexCoords;
struct SCollisionObjects;
class CProjectedShadow;
///////////////////////////////////////////////////////////////////////
// class SpeedTreeRT
//
// In an effort to make the SpeedTreeRT.h header file dependency free
// and easy to include into almost any project, a number of steps have
// been taken:
//
// 1. No external header files need to be included by SpeedTreeRT.h
// or by the application before including it.
//
// 2. Most of the implementation of the class is hidden by pointers
// to the major sections of the library (the internal classes
// can then just be forward-referenced)
//
// 3. Where possible, basic C++ datatypes are used to define the
// member functions' parameters.
//
// Because almost all of the implementation details are hidden, none of
// the functions for CSpeedTreeRT are inlined. However, inlined functions
// were copiously used within the library.
class ST_STORAGE_CLASS CSpeedTreeRT
{
public:
///////////////////////////////////////////////////////////////////////
// Enumerations
enum EWindMethod
{
WIND_GPU, WIND_CPU, WIND_NONE
};
enum ELodMethod
{
LOD_POP, LOD_SMOOTH, LOD_NONE = 3
};
enum ELightingMethod
{
LIGHT_DYNAMIC, LIGHT_STATIC
};
enum EStaticLightingStyle
{
SLS_BASIC, SLS_USE_LIGHT_SOURCES, SLS_SIMULATE_SHADOWS
};
enum ECollisionObjectType
{
CO_SPHERE, CO_CYLINDER, CO_BOX
};
///////////////////////////////////////////////////////////////////////
// SGeometry bit vectors
//
// Passed into GetGeometry() in order to mask out unneeded geometric elements
#define SpeedTree_BranchGeometry (1 << 0)
#define SpeedTree_FrondGeometry (1 << 1)
#define SpeedTree_LeafGeometry (1 << 2)
#define SpeedTree_BillboardGeometry (1 << 3)
#define SpeedTree_SimpleBillboardOverride (1 << 4)
#define SpeedTree_Nearest360Override (1 << 5)
#define SpeedTree_AllGeometry (SpeedTree_BranchGeometry + SpeedTree_FrondGeometry + SpeedTree_LeafGeometry + SpeedTree_BillboardGeometry)
///////////////////////////////////////////////////////////////////////
// struct SGeometry declaration
struct ST_STORAGE_CLASS SGeometry
{
SGeometry( );
~SGeometry( );
///////////////////////////////////////////////////////////////////////
// struct SGeometry::SIndexed declaration
struct ST_STORAGE_CLASS SIndexed
{
SIndexed( );
~SIndexed( );
// these values change depending on the active discrete LOD level
int m_nDiscreteLodLevel; // range: [0, GetNumBranch/FrondLodLevels( ) - 1], -1 if inactive
unsigned short m_usNumStrips; // total number of strips in current LOD
const unsigned short* m_pStripLengths; // lengths of strips in current LOD (m_usNumStrips in length)
const unsigned short** m_pStrips; // triangle strip indices (m_usNumStrips in length)
// these values are shared across all discete LOD levels
unsigned short m_usVertexCount; // total vertex count in tables, referenced by all LOD levels
const unsigned int* m_pColors; // RGBA values for each leaf - static lighting only (m_usVertexCount in length)
const float* m_pNormals; // normals for each vertex (3 * m_usVertexCount in length)
const float* m_pBinormals; // binormals (bump mapping) for each vertex (3 * m_usVertexCount in length)
const float* m_pTangents; // tangents (bump mapping) for each vertex (3 * m_usVertexCount in length)
const float* m_pCoords; // coordinates for each vertex (3 * m_usVertexCount in length)
const float* m_pTexCoords0; // 1st layer (s,t) texcoords for each vertex (2 * m_usVertexCount in length)
const float* m_pTexCoords1; // 2nd layer (s,t) texcoords for each vertex (2 * m_usVertexCount in length)
const float* m_pWindWeights; // values from from 0.0 for rigid to 1.0 for flexible (m_usVertexCount in length)
const unsigned char* m_pWindMatrixIndices; // table of wind matrix indices (m_usVertexCount in length)
};
///////////////////////////////////////////////////////////////////////
// struct SGeometry::SLeaf declaration
struct ST_STORAGE_CLASS SLeaf
{
SLeaf( );
~SLeaf( );
// active LOD level data
bool m_bIsActive; // flag indicating visibility
float m_fAlphaTestValue; // 0.0 to 255.0 alpha testing value, used for fading
int m_nDiscreteLodLevel; // range: [0, GetNumLeafLodLevels( ) - 1]
unsigned short m_usLeafCount; // number of leaves stored in this structure
// tables for referencing the leaf cluster table
const unsigned char* m_pLeafMapIndices; // references which leaf texture map used for each leaf (m_usLeafCount in length)
const unsigned char* m_pLeafClusterIndices; // references which leaf cluster used for each leaf (m_usLeafCount in length)
const float* m_pCenterCoords; // (x,y,z) values for the centers of leaf clusters (3 * m_usLeafCount in length)
const float** m_pLeafMapTexCoords; // table of unique leaf cluster texcoords (m_usLeafCount in length) - each entry
// points to 4 pairs of (s,t) texcoords stored in one contiguous array
const float** m_pLeafMapCoords; // table of unique leaf cluster coordinates (m_usLeafCount in length) - each entry
// points to 4 sets of (x,y,z,0) coordinates stored in one contiguous array
// remaining vertex attributes
const unsigned int* m_pColors; // RGBA values for each leaf (m_usLeafCount in length)
const float* m_pNormals; // normals for each leaf (3 * m_usLeafCount in length)
const float* m_pBinormals; // binormals (bump mapping) for each leaf (3 * m_usLeafCount in length)
const float* m_pTangents; // tangents (bump mapping) for each leaf (3 * m_usLeafCount in length)
const float* m_pWindWeights; // values from from 0.0 for rigid to 1.0 for flexible (m_usLeafCount in length)
const unsigned char* m_pWindMatrixIndices; // table of wind matrix indices (m_usLeafCount in length)
};
///////////////////////////////////////////////////////////////////////
// struct SGeometry::SBillboard declaration
struct ST_STORAGE_CLASS SBillboard
{
SBillboard( );
~SBillboard( );
bool m_bIsActive; // flag indicating visibility
const float* m_pTexCoords; // 4 pairs of (s,t) texcoords stored in one contiguous array
const float* m_pCoords; // 4 sets of (x,y,z) coordindates stored in one contiguous array
float m_fAlphaTestValue; // 0.0 to 255.0 alpha testing value, used for fading
};
///////////////////////////////////////////////////////////////////////
// branch geometry
SIndexed m_sBranches; // holds the branch vertices and index buffers for all
// of the discrete LOD levels
float m_fBranchAlphaTestValue; // 0.0 to 255.0 alpha testing value, used for fading
///////////////////////////////////////////////////////////////////////
// frond geometry
SIndexed m_sFronds; // holds the frond vertices and index buffers for all
// of the discrete LOD levels
float m_fFrondAlphaTestValue; // 0.0 to 255.0 alpha testing value, used for fading
///////////////////////////////////////////////////////////////////////
// leaf geometry
SLeaf m_sLeaves0; // holds the primary leaf geometry, alpha fades into
SLeaf m_sLeaves1; // m_sLeaves1 during LOD transitions
///////////////////////////////////////////////////////////////////////
// billboard geometry
SBillboard m_sBillboard0; // holds the main simple billboard geometry, alpha fades
SBillboard m_sBillboard1; // into m_sBillboard1 in 360 degree mode
SBillboard m_sHorizontalBillboard; // optional horizontal billboard used for aerial views
};
///////////////////////////////////////////////////////////////////////
// struct SGeometry::STextures declaration
struct ST_STORAGE_CLASS STextures
{
STextures( );
~STextures( );
// branches
const char* m_pBranchTextureFilename; // null-terminated string
// leaves
unsigned int m_uiLeafTextureCount; // the number of char* elements in m_pLeafTextureFilenames
const char** m_pLeafTextureFilenames; // array of null-terminated strings m_uiLeafTextureCount in size
// fronds
unsigned int m_uiFrondTextureCount; // the number of char* elements in m_pFrondTextureFilenames
const char** m_pFrondTextureFilenames; // array of null-terminated strings m_uiFrondTextureCount in size
// composite
const char* m_pCompositeFilename; // null-terminated string
// self-shadow
const char* m_pSelfShadowFilename; // null-terminated string
};
///////////////////////////////////////////////////////////////////////
// Constructor/Destructor
CSpeedTreeRT( );
~CSpeedTreeRT( );
///////////////////////////////////////////////////////////////////////
// Specifying a tree model
bool Compute(const float* pTransform = 0, unsigned int nSeed = 1, bool bCompositeStrips = true);
CSpeedTreeRT* Clone(float x = 0.0f, float y = 0.0f, float z = 0.0f, unsigned int nSeed = 0) const;
const CSpeedTreeRT* InstanceOf(void) const;
CSpeedTreeRT* MakeInstance(void);
void DeleteTransientData(void);
bool LoadTree(const char* pFilename);
bool LoadTree(const unsigned char* pBlock, unsigned int nNumBytes);
unsigned char* SaveTree(unsigned int& nNumBytes, bool bSaveLeaves = false) const;
void GetTreeSize(float& fSize, float& fVariance) const;
void SetTreeSize(float fNewSize, float fNewVariance = 0.0f);
unsigned int GetSeed(void) const;
const float* GetTreePosition(void) const;
void SetTreePosition(float x, float y, float z);
void SetLeafTargetAlphaMask(unsigned char ucMask = 0x54);
///////////////////////////////////////////////////////////////////////
// Lighting
// lighting style
ELightingMethod GetBranchLightingMethod(void) const;
void SetBranchLightingMethod(ELightingMethod eMethod);
ELightingMethod GetLeafLightingMethod(void) const;
void SetLeafLightingMethod(ELightingMethod eMethod);
ELightingMethod GetFrondLightingMethod(void) const;
void SetFrondLightingMethod(ELightingMethod eMethod);
EStaticLightingStyle GetStaticLightingStyle(void) const;
void SetStaticLightingStyle(EStaticLightingStyle eStyle);
float GetLeafLightingAdjustment( ) const;
void SetLeafLightingAdjustment(float fScalar);
// global lighting state
static bool CALL_CONV GetLightState(unsigned int nLightIndex);
static void CALL_CONV SetLightState(unsigned int nLightIndex, bool bLightOn);
static const float* CALL_CONV GetLightAttributes(unsigned int nLightIndex);
static void CALL_CONV SetLightAttributes(unsigned int nLightIndex, const float* pLightAttributes);
// branch material
const float* GetBranchMaterial(void) const;
void SetBranchMaterial(const float* pMaterial);
// leaf material
const float* GetLeafMaterial(void) const;
void SetLeafMaterial(const float* pMaterial);
// frond material
const float* GetFrondMaterial(void) const;
void SetFrondMaterial(const float* pMaterial);
///////////////////////////////////////////////////////////////////////
// Camera
static void CALL_CONV GetCamera(float* pPosition, float* pDirection);
static void CALL_CONV SetCamera(const float* pPosition, const float* pDirection);
///////////////////////////////////////////////////////////////////////
// Wind
static void CALL_CONV SetTime(float fTime);
void ComputeWindEffects(bool bBranches, bool bLeaves, bool bFronds = true);
void ResetLeafWindState(void);
bool GetLeafRockingState(void) const;
void SetLeafRockingState(bool bFlag);
void SetNumLeafRockingGroups(unsigned int nRockingGroups);
EWindMethod GetLeafWindMethod(void) const;
void SetLeafWindMethod(EWindMethod eMethod);
EWindMethod GetBranchWindMethod(void) const;
void SetBranchWindMethod(EWindMethod eMethod);
EWindMethod GetFrondWindMethod(void) const;
void SetFrondWindMethod(EWindMethod eMethod);
float GetWindStrength(void) const;
float SetWindStrength(float fNewStrength, float fOldStrength = -1.0f, float fFrequencyTimeOffset = -1.0f);
void SetWindStrengthAndLeafAngles(float fNewStrength, const float* pRockAngles = 0, const float* pRustleAngles = 0, unsigned int uiNumRockAngles = 0);
static void CALL_CONV SetNumWindMatrices(int nNumMatrices);
static void CALL_CONV SetWindMatrix(unsigned int nMatrixIndex, const float* pMatrix);
void GetLocalMatrices(unsigned int& nStartingIndex, unsigned int& nMatrixSpan);
void SetLocalMatrices(unsigned int nStartingMatrix, unsigned int nMatrixSpan);
///////////////////////////////////////////////////////////////////////
// LOD
void ComputeLodLevel(void);
float GetLodLevel(void) const;
void SetLodLevel(float fLodLevel);
static void CALL_CONV SetDropToBillboard(bool bFlag);
void GetLodLimits(float& fNear, float& fFar) const;
void SetLodLimits(float fNear, float fFar);
short GetDiscreteBranchLodLevel(float fLodLevel = -1.0f) const;
unsigned short GetDiscreteLeafLodLevel(float fLodLevel = -1.0f) const;
short GetDiscreteFrondLodLevel(float fLodLevel = -1.0f) const;
unsigned short GetNumBranchLodLevels(void) const;
unsigned short GetNumLeafLodLevels(void) const;
unsigned short GetNumFrondLodLevels(void) const;
static void CALL_CONV SetHorzBillboardFadeAngles(float fStart, float fEnd); // in degrees
static void CALL_CONV GetHorzBillboardFadeAngles(float& fStart, float& fEnd); // in degrees
///////////////////////////////////////////////////////////////////////
// Geometry
void DeleteBranchGeometry(void);
void DeleteFrondGeometry(void);
void DeleteLeafGeometry(void);
unsigned char* GetFrondGeometryMapIndexes(int nLodLevel) const;
const float* GetLeafBillboardTable(unsigned int& nEntryCount) const;
const float* GetLeafLodSizeAdjustments(void);
void GetGeometry(SGeometry& sGeometry, unsigned int uiBitVector = SpeedTree_AllGeometry, short sOverrideBranchLodValue = -1, short sOverrideFrondLodValue = -1, short sOverrideLeafLodValue = -1);
///////////////////////////////////////////////////////////////////////
// Textures
void GetTextures(STextures& sTextures) const;
void SetLeafTextureCoords(unsigned int nLeafMapIndex, const float* pTexCoords);
void SetFrondTextureCoords(unsigned int nFrondMapIndex, const float* pTexCoords);
static bool CALL_CONV GetTextureFlip(void);
static void CALL_CONV SetTextureFlip(bool bFlag);
void SetBranchTextureFilename(const char* pFilename);
void SetLeafTextureFilename(unsigned int nLeafMapIndex, const char* pFilename);
void SetFrondTextureFilename(unsigned int nFrondMapIndex, const char* pFilename);
///////////////////////////////////////////////////////////////////////
// Statistics & information
static bool CALL_CONV Authorize(const char* pKey);
static bool CALL_CONV IsAuthorized(void);
static const char* CALL_CONV GetCurrentError(void);
static void CALL_CONV ResetError(void);
static const char* CALL_CONV Version(void);
void GetBoundingBox(float* pBounds) const;
unsigned int GetLeafTriangleCount(float fLodLevel = -1.0f);
unsigned int GetBranchTriangleCount(float fLodLevel = -1.0f);
unsigned int GetFrondTriangleCount(float fLodLevel = -1.0f);
///////////////////////////////////////////////////////////////////////
// Collision objects
unsigned int GetCollisionObjectCount(void);
void GetCollisionObject(unsigned int nIndex, ECollisionObjectType& eType, float* pPosition, float* pDimensions);
///////////////////////////////////////////////////////////////////////
// User Data
const char* GetUserData(void) const;
private:
CSpeedTreeRT(const CSpeedTreeRT* pOrig);
void ComputeLeafStaticLighting(void);
void ComputeSelfShadowTexCoords(void);
static void CALL_CONV NotifyAllTreesOfEvent(int nMessage);
static void CALL_CONV SetError(const char* pError);
///////////////////////////////////////////////////////////////////////
// File I/O
void ParseLodInfo(CTreeFileAccess* pFile);
void ParseWindInfo(CTreeFileAccess* pFile);
void ParseTextureCoordInfo(CTreeFileAccess* pFile);
void ParseCollisionObjects(CTreeFileAccess* pFile);
void SaveTextureCoords(CTreeFileAccess* pFile) const;
void SaveCollisionObjects(CTreeFileAccess* pFile) const;
void ParseShadowProjectionInfo(CTreeFileAccess* pFile);
void SaveUserData(CTreeFileAccess* pFile) const;
void ParseUserData(CTreeFileAccess* pFile);
void SaveSupplementalTexCoordInfo(CTreeFileAccess* pFile) const;
void ParseSupplementalTexCoordInfo(CTreeFileAccess* pFile);
static char* CALL_CONV CopyUserData(const char* pData);
///////////////////////////////////////////////////////////////////////
// Geometry
void GetBranchGeometry(SGeometry& sGeometry, short sOverrideLodValue = -1);
void GetFrondGeometry(SGeometry& sGeometry, short sOverrideLodValue = -1);
void GetLeafGeometry(SGeometry& sGeometry, short sOverrideLodValue = -1);
void Get360BillboardGeometry(SGeometry& sGeometry, unsigned int uiBitVector);
void GetSimpleBillboardGeometry(SGeometry& sGeometry);
static void CALL_CONV GetTransitionValues(float fLodLevel, unsigned short usLodCount, float fOverlapRadius,
float fTransitionFactor, float fCurveExponent, float fTargetAlphaValue,
float& fHighValue, float& fLowValue, short& sHighLod, short& sLowLod);
void SetupHorizontalBillboard(void);
float ComputeLodCurve(float fStart, float fEnd, float fPercent, bool bConcaveUp);
///////////////////////////////////////////////////////////////////////
// Member variables
// general
CTreeEngine* m_pEngine; // core tree-generating engine
CIndexedGeometry* m_pBranchGeometry; // abstraction mechanism for branch geometry
CLeafGeometry* m_pLeafGeometry; // abstraction mechanism for leaf geometry
CLightingEngine* m_pLightingEngine; // engine for computing static/dynamic lighting data
CWindEngine* m_pWindEngine; // engine for computing CPU/GPU wind effects
CSimpleBillboard* m_pSimpleBillboard;
// leaf lod
ELodMethod m_eLeafLodMethod; // which leaf wind method is currently being used
float m_fLeafLodTransitionRadius; // determines how much blending occurs between two separate leaf LOD levels
float m_fLeafLodCurveExponent; // exponent value used in the leaf LOD blending equation
float m_fLeafSizeIncreaseFactor; // value that controls how much larger leaf clusters get as LOD decreases
float m_fLeafTransitionFactor; // value that controls the intersection point of SMOOTH_1 transitions
float* m_pLeafLodSizeFactors; // array, GetNumLeafLodLevels()'s in size, containing leaf LOD scale factors
// instancing & ref counting
unsigned int* m_pInstanceRefCount; // single value shared among instances - number of active instances
STreeInstanceData* m_pInstanceData; // if instance, differntiating data is stored here
SInstanceList* m_pInstanceList; // each tree contains a list of its instances
static unsigned int m_uiAllRefCount; // single value shared by all CSpeedTreeRT instances
// other
int m_nFrondLevel; // from SpeedTreeCAD - branch level where fronds begin
float* m_pTreeSizes; // contains all tree extents, including billboard sizes
unsigned char m_ucTargetAlphaValue; // value used for leaf alpha mask function
bool m_bTreeComputed; // some operations are not valid once the geometry has been computed
int m_nBranchWindLevel; // from SpeedTreeCAD - branch level where wind effects are active
// texture coords
SEmbeddedTexCoords* m_pEmbeddedTexCoords; // embedded leaf and billboard texture coords
static bool m_bTextureFlip; // used to flip coordinates for DirectX, Gamebryo, etc.
// shadow projection
CProjectedShadow* m_pProjectedShadow; // self-shadow projection
// billboard
static bool m_bDropToBillboard; // flag specifying if last LOD will be simple single billboard
// collision objects
SCollisionObjects* m_pCollisionObjects; // collision objects
// fronds
CFrondEngine* m_pFrondEngine; // engine for computing fronds based on branch geometry
CIndexedGeometry* m_pFrondGeometry; // abstraction mechanism for frond geometry
unsigned short m_usNumFrondLodLevels; // place to store LOD count after m_pFrondGeometry is deleted
// user data
char* m_pUserData; // user specified data
// horizontal billboard
bool m_b360Billboard; // indicates that a 360 degree billboard sequence is present
bool m_bHorizontalBillboard; // indicates that a horizontal billboard is present in the embedded tex coords
float m_afHorizontalCoords[12]; // vertices of the horizontal billboard
static float m_fHorizontalFadeStartAngle;// in degrees
static float m_fHorizontalFadeEndAngle; // in degrees
static float m_fHorizontalFadeValue;
};
// Macintosh export control
#ifdef __APPLE__
#pragma export off
#endif
Binary file not shown.
Binary file not shown.
+344
View File
@@ -0,0 +1,344 @@
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// A simple tool for converting SpeedTree .spt files into .smd files
// for use in Source
//
//===========================================================================//
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#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;nStrip<sGeom.m_sBranches.m_usNumStrips;nStrip++ )
{
int nStripLength = sGeom.m_sBranches.m_pStripLengths[ nStrip ];
const unsigned short *pStripIndices = sGeom.m_sBranches.m_pStrips[ nStrip ];
for( int i=0;i<nStripLength-2;i++ )
{
int nIndices[3] = { pStripIndices[i], pStripIndices[i+1], pStripIndices[i+2] };
if( i%2 )
{
int tmp = nIndices[2];
nIndices[2] = nIndices[1];
nIndices[1] = tmp;
}
fprintf( file, "%s_VertexLit\n", branchTextureName );
for( int j=0;j<3;j++ )
{
const float *pPos = &sGeom.m_sBranches.m_pCoords[ nIndices[j]*3 ];
const float *pNormal = &sGeom.m_sBranches.m_pNormals[ nIndices[j]*3 ];
const float *pTexCoord = &sGeom.m_sBranches.m_pTexCoords0[ nIndices[j]*2 ];
fprintf( file, "0 %f %f %f %f %f %f %f %f 0\n", pPos[0], pPos[1], pPos[2],
pNormal[0], pNormal[1], pNormal[2],
pTexCoord[0], pTexCoord[1] );
}
}
}
for( unsigned int i=0;i<sTextures.m_uiFrondTextureCount;i++ )
{
char filestub[MAX_PATH];
_splitpath( sTextures.m_pFrondTextureFilenames[i], NULL, NULL, filestub, NULL );
alphaTest = sGeom.m_fFrondAlphaTestValue/255.0f;
OutputMaterialFile( filestub, true, alphaTest );
}
for( int nStrip=0;nStrip<sGeom.m_sFronds.m_usNumStrips;nStrip++ )
{
int nStripLength = sGeom.m_sFronds.m_pStripLengths[ nStrip ];
const unsigned short *pStripIndices = sGeom.m_sFronds.m_pStrips[ nStrip ];
for( int i=0;i<nStripLength-2;i++ )
{
int nIndices[3] = { pStripIndices[i], pStripIndices[i+1], pStripIndices[i+2] };
if( i%2 )
{
int tmp = nIndices[2];
nIndices[2] = nIndices[1];
nIndices[1] = tmp;
}
char frondTextureName[ MAX_PATH ];
_splitpath( sTextures.m_pFrondTextureFilenames[0], NULL, NULL, frondTextureName, NULL );
fprintf( file, "%s_VertexLit\n", frondTextureName );
for( int j=0;j<3;j++ )
{
const float *pPos = &sGeom.m_sFronds.m_pCoords[ nIndices[j]*3 ];
const float *pNormal = &sGeom.m_sFronds.m_pNormals[ nIndices[j]*3 ];
const float *pTexCoord = &sGeom.m_sFronds.m_pTexCoords0[ nIndices[j]*2 ];
fprintf( file, "0 %f %f %f %f %f %f %f %f 0\n", pPos[0], pPos[1], pPos[2],
pNormal[0], pNormal[1], pNormal[2],
pTexCoord[0], pTexCoord[1] );
}
}
}
float *pLeafCentres = new float[ sTextures.m_uiLeafTextureCount*3 ];
int *pLeafCounts = new int[ sTextures.m_uiLeafTextureCount ];
for( unsigned int i=0;i<sTextures.m_uiLeafTextureCount;i++ )
{
pLeafCentres[ i*3 ] = 0.0f;
pLeafCentres[ i*3+1 ] = 0.0f;
pLeafCentres[ i*3+2 ] = 0.0f;
pLeafCounts[ i ] = 0;
}
CSpeedTreeRT::SGeometry::SLeaf &leaves = sGeom.m_sLeaves0;
for( int i=0;i<leaves.m_usLeafCount;i++ )
{
int index = leaves.m_pLeafMapIndices[i] / 2;
char leafTextureName[ MAX_PATH ];
_splitpath( sTextures.m_pLeafTextureFilenames[ index ], NULL, NULL, leafTextureName, NULL );
const float *pPos = &leaves.m_pCenterCoords[ i*3 ];
const float *pCoords = leaves.m_pLeafMapCoords[ i ];
const float *pTex = leaves.m_pLeafMapTexCoords[i];
float d[3] = { pCoords[8] - pCoords[0], pCoords[9] - pCoords[1], pCoords[10] - pCoords[2] };
float size = sqrtf( d[0]*d[0] + d[1]*d[1] + d[2]*d[2] ) * 0.5f;
fprintf( file, "%s_TreeLeaf\n", leafTextureName );
fprintf( file, "0 %f %f %f %f %f 0.0 %f %f\n", pPos[0], pPos[1], pPos[2], pTex[0], pTex[1], -size, -size );
fprintf( file, "0 %f %f %f %f %f 0.0 %f %f\n", pPos[0], pPos[1], pPos[2], pTex[2], pTex[3], size, -size );
fprintf( file, "0 %f %f %f %f %f 0.0 %f %f\n", pPos[0], pPos[1], pPos[2], pTex[4], pTex[5], size, size );
fprintf( file, "%s_TreeLeaf\n", leafTextureName );
fprintf( file, "0 %f %f %f %f %f 0.0 %f %f\n", pPos[0], pPos[1], pPos[2], pTex[0], pTex[1], -size, -size );
fprintf( file, "0 %f %f %f %f %f 0.0 %f %f\n", pPos[0], pPos[1], pPos[2], pTex[4], pTex[5], size, size );
fprintf( file, "0 %f %f %f %f %f 0.0 %f %f\n", pPos[0], pPos[1], pPos[2], pTex[6], pTex[7], -size, size );
pLeafCentres[ index*3 ] += pPos[0];
pLeafCentres[ index*3+1 ] += pPos[1];
pLeafCentres[ index*3+2 ] += pPos[2];
pLeafCounts[ index ]++;
}
for( unsigned int i=0;i<sTextures.m_uiLeafTextureCount;i++ )
{
float oneOnCount = 1.0f / pLeafCounts[i];
pLeafCentres[ i*3 ] *= oneOnCount;
pLeafCentres[ i*3+1 ] *= oneOnCount;
pLeafCentres[ i*3+2 ] *= oneOnCount;
}
for( unsigned int i=0;i<sTextures.m_uiLeafTextureCount;i++ )
{
char filestub[ MAX_PATH ];
_splitpath( sTextures.m_pLeafTextureFilenames[i], NULL, NULL, filestub, NULL );
alphaTest = sGeom.m_sLeaves0.m_fAlphaTestValue/255.0f;
OutputLeafMaterialFile( filestub, true, alphaTest, &pLeafCentres[i*3] );
}
fprintf( file, "end\n\n" );
fclose( file );
}
void OutputTreeTextures( CSpeedTreeRT &speedTree )
{
CSpeedTreeRT::STextures sTextures;
speedTree.GetTextures( sTextures );
CSpeedTreeRT::SGeometry sGeom;
speedTree.GetGeometry( sGeom, SpeedTree_AllGeometry );
char filestub[MAX_PATH];
_splitpath( sTextures.m_pBranchTextureFilename, NULL, NULL, filestub, NULL );
float alphaTest = sGeom.m_fBranchAlphaTestValue/255.0f;
OutputMaterialFile( filestub, false, alphaTest );
for( unsigned int i=0;i<sTextures.m_uiFrondTextureCount;i++ )
{
_splitpath( sTextures.m_pFrondTextureFilenames[i], NULL, NULL, filestub, NULL );
alphaTest = sGeom.m_fFrondAlphaTestValue/255.0f;
OutputMaterialFile( filestub, true, alphaTest );
}
for( unsigned int i=0;i<sTextures.m_uiLeafTextureCount;i++ )
{
_splitpath( sTextures.m_pLeafTextureFilenames[i], NULL, NULL, filestub, NULL );
alphaTest = sGeom.m_sLeaves0.m_fAlphaTestValue/255.0f;
OutputLeafMaterialFile( filestub, true, alphaTest );
}
}
void OutputQCFile( char *treeName )
{
char smdFileName[MAX_PATH];
sprintf( smdFileName, "%s.smd", treeName );
char qcFileName[MAX_PATH];
sprintf( qcFileName, "%s.qc", treeName );
char smdAnimFileName[MAX_PATH];
sprintf( smdAnimFileName, "%s_anim.smd", treeName );
char mdlFileName[MAX_PATH];
sprintf( mdlFileName, "%s.mdl", treeName );
FILE *file = fopen( qcFileName, "wt" );
fprintf( file, "$modelname %s\n", mdlFileName );
fprintf( file, "$cdmaterials trees\n" );
fprintf( file, "$scale 1\n" );
fprintf( file, "$model %s \"%s\"\n", treeName, smdFileName );
fprintf( file, "$sequence idle \"%s_anim\" loop fps 15\n", treeName );
fclose( file );
file = fopen( smdAnimFileName, "wt" );
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" );
fclose( file );
}
void main( int argc, char **argv )
{
CommandLine()->CreateCmdLine( argc, argv );
if( CommandLine()->ParmCount()!=2 )
{
printf( "usage : sptconvert <SPT file name>\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
}
}
+191
View File
@@ -0,0 +1,191 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="7.10"
Name="sptconvert"
ProjectGUID="{25191756-B845-42CE-BDB6-D1213911ABEC}"
Keyword="Win32Proj">
<Platforms>
<Platform
Name="Win32"/>
</Platforms>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="Debug"
IntermediateDirectory="Debug"
ConfigurationType="1"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="..\common,..\..\public,..\..\public\tier1,..\..\Game_Shared,SpeedTreeRT/include"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="TRUE"
BasicRuntimeChecks="3"
RuntimeLibrary="1"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="4"/>
<Tool
Name="VCCustomBuildTool"
CommandLine="if exist ..\..\..\game\bin\&quot;$(TargetName)&quot;.exe attrib -r ..\..\..\game\bin\&quot;$(TargetName)&quot;.exe
copy &quot;$(TargetPath)&quot; ..\..\..\game\bin\&quot;$(TargetName)&quot;.exe
if exist ..\..\..\game\bin\&quot;$(TargetName)&quot;.pdb attrib -r ..\..\..\game\bin\&quot;$(TargetName)&quot;.pdb
copy &quot;$(TargetPath)&quot; ..\..\..\game\bin\&quot;$(TargetName)&quot;.pdb
"
Outputs="..\..\..\game\bin\$(TargetName).exe;..\..\..\game\bin\$(TargetName).pdb"/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="SpeedTreeRT/lib/debug/SpeedTreeRT_d.lib"
OutputFile="$(OutDir)/sptconvert.exe"
LinkIncremental="2"
GenerateDebugInformation="TRUE"
ProgramDatabaseFile="$(OutDir)/sptconvert.pdb"
SubSystem="1"
TargetMachine="1"/>
<Tool
Name="VCMIDLTool"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="Release"
IntermediateDirectory="Release"
ConfigurationType="1"
CharacterSet="2">
<Tool
Name="VCCLCompilerTool"
AdditionalIncludeDirectories="..\common,..\..\public,..\..\public\tier1,..\..\Game_Shared,SpeedTreeRT/include"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="0"
UsePrecompiledHeader="0"
WarningLevel="3"
Detect64BitPortabilityProblems="TRUE"
DebugInformationFormat="3"/>
<Tool
Name="VCCustomBuildTool"
CommandLine="if exist ..\..\..\game\bin\&quot;$(TargetName)&quot;.exe attrib -r ..\..\..\game\bin\&quot;$(TargetName)&quot;.exe
copy &quot;$(TargetPath)&quot; ..\..\..\game\bin\&quot;$(TargetName)&quot;.exe
if exist ..\..\..\game\bin\&quot;$(TargetName)&quot;.pdb attrib -r ..\..\..\game\bin\&quot;$(TargetName)&quot;.pdb
copy &quot;$(TargetPath)&quot; ..\..\..\game\bin\&quot;$(TargetName)&quot;.pdb
"
Outputs="..\..\..\game\bin\$(TargetName).exe;..\..\..\game\bin\$(TargetName).pdb"/>
<Tool
Name="VCLinkerTool"
AdditionalDependencies="SpeedTreeRT/lib/release/SpeedTreeRT.lib"
OutputFile="$(OutDir)/sptconvert.exe"
LinkIncremental="1"
GenerateDebugInformation="TRUE"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"/>
<Tool
Name="VCMIDLTool"/>
<Tool
Name="VCPostBuildEventTool"/>
<Tool
Name="VCPreBuildEventTool"/>
<Tool
Name="VCPreLinkEventTool"/>
<Tool
Name="VCResourceCompilerTool"/>
<Tool
Name="VCWebServiceProxyGeneratorTool"/>
<Tool
Name="VCXMLDataGeneratorTool"/>
<Tool
Name="VCWebDeploymentTool"/>
<Tool
Name="VCManagedWrapperGeneratorTool"/>
<Tool
Name="VCAuxiliaryManagedWrapperGeneratorTool"/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}">
<File
RelativePath="..\common\cmdlib.cpp">
</File>
<File
RelativePath="..\..\public\filesystem_helpers.cpp">
</File>
<File
RelativePath="..\..\public\filesystem_helpers.h">
</File>
<File
RelativePath="..\..\public\filesystem_init.cpp">
</File>
<File
RelativePath="..\..\public\filesystem_init.h">
</File>
<File
RelativePath="..\common\filesystem_tools.cpp">
</File>
<File
RelativePath=".\sptconvert.cpp">
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}">
<File
RelativePath="..\common\cmdlib.h">
</File>
<File
RelativePath="..\common\filesystem_tools.h">
</File>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}">
</Filter>
<Filter
Name="Link Libraries"
Filter="">
<File
RelativePath="..\..\lib\public\mathlib.lib">
</File>
<File
RelativePath="..\..\lib\public\tier0.lib">
</File>
<File
RelativePath="..\..\lib\public\tier1.lib">
</File>
<File
RelativePath="..\..\lib\public\tier2.lib">
</File>
<File
RelativePath="..\..\lib\public\vstdlib.lib">
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>