OpenBarnyard
 
Loading...
Searching...
No Matches
AModelLoader Class Reference

#include <AModelLoader.h>

Inheritance diagram for AModelLoader:

Classes

struct  AlphaRefMaterial
 
struct  ClampedMaterial
 
struct  MaterialNode
 

Public Member Functions

 TDECLARE_CLASS (AModelLoader, Toshi::TObject)
 
 AModelLoader ()
 
 ~AModelLoader ()
 

Static Public Member Functions

static Toshi::TMaterial * CreateMaterial (Toshi::TShader *a_pShader, const TCHAR *a_szMaterialName)
 
static void DestroyMaterial (Toshi::TMaterial *a_pMaterial)
 
static void InitialiseStatic ()
 

Static Public Attributes

static constexpr TUINT MAX_NUM_ALLOCATED_MATERIALS = 512
 
static constexpr TUINT MAX_NUM_MODEL_MATERIALS = 150
 

Friends

class AModelInstance
 

Detailed Description

Definition at line 18 of file AModelLoader.h.

Constructor & Destructor Documentation

◆ AModelLoader()

AModelLoader::AModelLoader ( )

Definition at line 26 of file AModelLoader.cpp.

27{
28 for ( TUINT i = 0; i < TARRAYSIZE( ms_oNodesAlloc ); i++ )
29 {
30 ms_oFreeMaterials.PushBack( &ms_oNodesAlloc[ i ] );
31 ms_oNodesAlloc[ i ].iId = TUINT16( i );
32 }
33
34 // Load material properties
35 const TBOOL bAssetPackLoaded = AAssetLoader::GetAssetTRB( AAssetType_AssetPack ) != TNULL;
36
37 if ( bAssetPackLoaded )
38 {
39 // Use asset pack
40 ms_pWorldEndRender = AAssetLoader::GetSymbolAddress( "materialproperties", "WorldEndRender", AAssetType_AssetPack );
41 ms_pWorldStartRender = AAssetLoader::GetSymbolAddress( "materialproperties", "WorldStartRender", AAssetType_AssetPack );
42 ms_pWorldUVOffsetsPerSec = AAssetLoader::GetSymbolAddress( "materialproperties", "WorldUVOffsetsPerSec", AAssetType_AssetPack );
43 ms_pAlphaRefMaterials = AAssetLoader::CastSymbol<AlphaRefMaterial>( "materialproperties", "AlphaRefMaterials", AAssetType_AssetPack );
44 ms_pClampedMaterials = AAssetLoader::CastSymbol<ClampedMaterial>( "materialproperties", "ClampedMaterials", AAssetType_AssetPack );
45 ms_pGlowMaterials = AAssetLoader::CastSymbol<const TCHAR*>( "materialproperties", "GlowMaterials", AAssetType_AssetPack );
46 }
47 else
48 {
49 // Load from file
50 m_oTRB.Load( "Data\\MiscTextures\\MaterialProperties.trb" );
51 ms_pWorldEndRender = m_oTRB.GetSymbolAddress( "WorldEndRender" );
52 ms_pWorldStartRender = m_oTRB.GetSymbolAddress( "WorldStartRender" );
53 ms_pWorldUVOffsetsPerSec = m_oTRB.GetSymbolAddress( "WorldUVOffsetsPerSec" );
54 ms_pAlphaRefMaterials = m_oTRB.CastSymbol<AlphaRefMaterial>( "AlphaRefMaterials" );
55 ms_pClampedMaterials = m_oTRB.CastSymbol<ClampedMaterial>( "ClampedMaterials" );
56 ms_pGlowMaterials = m_oTRB.CastSymbol<const TCHAR*>( "GlowMaterials" );
57 }
58
59 TModel::SetLoaderTMDCallback( AModelLoaderLoadTMDCallback );
60 TModel::SetLoaderTRBCallback( AModelLoaderLoadTRBCallback );
61
62 // Count glow materials
63 ms_iNumGlowMaterials = 0;
64 auto pGlowMaterial = ms_pGlowMaterials;
65
66 while ( *pGlowMaterial )
67 {
68 ms_iNumGlowMaterials += 1;
69 pGlowMaterial++;
70 }
71
72 // Load double sided materials
73 ms_pDoubleSidedMaterials = ( bAssetPackLoaded ) ?
74 AAssetLoader::CastSymbol<const TCHAR*>( "materialproperties", "DoubleSidedMaterials", AAssetType_AssetPack ) :
75 m_oTRB.CastSymbol<const TCHAR*>( "DoubleSidedMaterials" );
76
77 // Count double sided materials
78 ms_iNumDoubleSidedMaterials = 0;
79 auto pDoubleSidedMaterial = ms_pDoubleSidedMaterials;
80
81 while ( *pDoubleSidedMaterial )
82 {
83 ms_iNumDoubleSidedMaterials += 1;
84 pDoubleSidedMaterial++;
85 }
86
87 // Symbol table is not needed anymore
88 if ( !bAssetPackLoaded )
89 m_oTRB.DeleteSymbolTable();
90
91 if ( TNULL == ms_pDefaultSkinMaterial )
92 {
93 ASkinMaterial* pSkinMaterial = ASkinShader::GetSingleton()->CreateMaterial( TNULL );
94 pSkinMaterial->Create( 7 );
95 pSkinMaterial->SetFlags( TMaterial::FLAGS_NO_CULL, TFALSE );
96 pSkinMaterial->SetTexture( TNULL );
97
98 ms_pDefaultSkinMaterial = pSkinMaterial;
99 }
100
101 if ( TNULL == ms_pDefaultWorldMaterial )
102 {
103 AWorldMaterial* pWorldMaterial = AWorldShader::GetSingleton()->CreateMaterial( TNULL );
104 pWorldMaterial->Create( 6 );
105 pWorldMaterial->SetFlags( TMaterial::FLAGS_NO_CULL, TFALSE );
106 pWorldMaterial->SetTexture( 0, TNULL );
107 pWorldMaterial->SetTextureNum( 1 );
108
109 ms_pDefaultWorldMaterial = pWorldMaterial;
110 }
111}
#define TARRAYSIZE(ARRAY)
Definition Defines.h:70
uint16_t TUINT16
Definition Typedefs.h:15
unsigned int TUINT
Definition Typedefs.h:8
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
#define TFALSE
Definition Typedefs.h:24
bool TBOOL
Definition Typedefs.h:6
@ AAssetType_AssetPack
Definition AAssetLoader.h:7
@ FLAGS_NO_CULL
Definition TMaterial.h:24
static void SetLoaderTMDCallback(t_ModelLoaderTMDCallback a_cbLoader)
Definition TModel.h:138
static void SetLoaderTRBCallback(t_ModelLoaderTRBCallback a_cbLoader)
Definition TModel.h:139
static Toshi::TTRB * GetAssetTRB(AAssetType a_eAssetType)
static void * GetSymbolAddress(const TCHAR *a_szFileName, const TCHAR *a_szSymbolName, AAssetType a_eAssetType)
static T * CastSymbol(const TCHAR *a_szFileName, const TCHAR *a_szSymbolName, AAssetType a_eAssetType)
void SetTexture(Toshi::TTexture *a_pTexture)
virtual TBOOL Create(BLENDMODE a_eBlendMode)
void SetTexture(TUINT a_uiStage, Toshi::TTexture *a_pTexture)
virtual TBOOL Create(BLENDMODE a_eBlendMode)

◆ ~AModelLoader()

AModelLoader::~AModelLoader ( )

Definition at line 113 of file AModelLoader.cpp.

114{
115}

Member Function Documentation

◆ CreateMaterial()

TMaterial * AModelLoader::CreateMaterial ( Toshi::TShader * a_pShader,
const TCHAR * a_szMaterialName )
static

Definition at line 183 of file AModelLoader.cpp.

184{
186
187 char prefixName[ 32 ];
188
189 if ( a_pShader->IsA( &TGetClass( ASkinShader ) ) )
190 {
191 TStringManager::String8Copy( prefixName, "ss_" );
192 }
193 else if ( a_pShader->IsA( &TGetClass( AWorldShader ) ) )
194 {
195 TStringManager::String8Copy( prefixName, "ws_" );
196 }
197 else if ( a_pShader->IsA( &TGetClass( AGrassShader ) ) )
198 {
199 TStringManager::String8Copy( prefixName, "gs_" );
200 }
201 else
202 {
203 TASSERT( !"Unknown shader" );
204 return TNULL;
205 }
206
207 TString8 materialName = prefixName;
208 materialName += a_szMaterialName;
209
210 auto pResultMaterial = FindMaterial( materialName );
211 if ( pResultMaterial ) return pResultMaterial;
212
213 TTextureResourceHAL* pTexture;
214 const char* szTextureName;
215 auto pTMDMaterial = FindMaterialInModel( a_szMaterialName );
216
217 if ( pTMDMaterial )
218 {
219 pTexture = TDYNAMICCAST(
220 TTextureResourceHAL,
221 AMaterialLibraryManager::GetSingleton()->FindTexture( pTMDMaterial->szTextureFile )
222 );
223
224 szTextureName = pTMDMaterial->szTextureFile;
225 }
226 else
227 {
228 TTRACE( "Couldn't find texture for material '%s'", a_szMaterialName );
229 pTexture = TDYNAMICCAST(
230 TTextureResourceHAL,
231 TRenderD3DInterface::Interface()->GetInvalidTexture()
232 );
233 szTextureName = "invalid";
234 }
235
236 TVALIDPTR( pTexture );
237
238 auto eTextureFormat = pTexture->GetImageInfo().Format;
239 const TBOOL bIsAlpha = eTextureFormat == D3DFMT_A8R8G8B8 ||
240 eTextureFormat == D3DFMT_A4R4G4B4 ||
241 eTextureFormat == D3DFMT_A8R3G3B2 ||
242 eTextureFormat == D3DFMT_DXT5;
243
244 if ( a_pShader->IsA( &TGetClass( ASkinShader ) ) )
245 {
246 ASkinShaderHAL* pShader = TDYNAMICCAST( ASkinShaderHAL, a_pShader );
247 ASkinMaterial* pMaterial = pShader->CreateMaterial( TNULL );
248 pMaterial->Create( bIsAlpha ? 1 : 0 );
249 pMaterial->SetTexture( pTexture );
250
251 pResultMaterial = pMaterial;
252 }
253 else if ( a_pShader->IsA( &TGetClass( AWorldShader ) ) )
254 {
255 AWorldShaderHAL* pShader = TDYNAMICCAST( AWorldShaderHAL, a_pShader );
256 AWorldMaterial* pMaterial = pShader->CreateMaterial( TNULL );
257 pMaterial->Create( bIsAlpha ? 1 : 0 );
258 pMaterial->SetTexture( 0, pTexture );
259 pMaterial->SetTextureNum( 1 );
260
261 pResultMaterial = pMaterial;
262
263 TTODO( "Setup material using properties" );
264 }
265 else if ( a_pShader->IsA( &TGetClass( AGrassShader ) ) )
266 {
267 AGrassShaderHAL* pShader = TDYNAMICCAST( AGrassShaderHAL, a_pShader );
268 AGrassMaterial* pMaterial = pShader->CreateMaterial( TNULL );
269 pMaterial->Create( 0 );
270 pMaterial->SetTexture( 0, pTexture );
271 pMaterial->SetTextureNum( 1 );
272
273 pResultMaterial = pMaterial;
274 }
275 else
276 {
277 TASSERT( !"Support other shaders" );
278 }
279
280 MaterialApplyFlags( pResultMaterial, a_szMaterialName );
281 MaterialApplyClamp( pResultMaterial, a_szMaterialName, pTexture );
282 MaterialApplyAlphaRef( pResultMaterial, a_szMaterialName, pTexture );
283
284 TASSERT( !ms_oFreeMaterials.IsEmpty() );
285 auto pMatNode = ms_oFreeMaterials.PopFront();
286 pMatNode->pMaterial = pResultMaterial;
287 pMatNode->iNumRefs = 1;
288
289 TStringManager::String8Copy( pMatNode->szTextureName, szTextureName, TARRAYSIZE( pMatNode->szTextureName ) );
290 TStringManager::String8Copy( pMatNode->szName, materialName, TARRAYSIZE( pMatNode->szName ) );
291
292 // Truncate material name so it fits the limit
293 if ( materialName.Length() > TMaterial::NAMESIZELIMIT )
294 {
295 materialName[ TMaterial::NAMESIZELIMIT ] = '\0';
296 }
297
298 pResultMaterial->SetName( materialName );
299 ms_oUsedMaterials.PushFront( pMatNode );
300
301 AddMaterial( materialName, pMatNode );
302 return pResultMaterial;
303}
#define TASSERT(X,...)
Definition Defines.h:138
#define TTODO(DESC)
Definition Defines.h:134
#define TTRACE(...)
Definition Defines.h:155
#define TVALIDPTR(PTR)
Definition Defines.h:139
#define TPROFILER_SCOPE()
Definition Profiler.h:17
#define TGetClass(CLASS)
Definition TObject.h:13
#define TDYNAMICCAST(T, OBJECT)
Definition TObject.h:227
static TFORCEINLINE TRenderD3DInterface * Interface()
Gets the render interface singleton.
D3DXIMAGE_INFO & GetImageInfo()
static constexpr TUINT NAMESIZELIMIT
Definition TMaterial.h:18
TINT Length() const
Definition TString8.h:93
static TCHAR * String8Copy(TCHAR *dst, const TCHAR *src, TSIZE size=-1)
virtual AGrassMaterial * CreateMaterial(const TCHAR *a_szName) override
virtual ASkinMaterial * CreateMaterial(const TCHAR *a_szName) override
virtual AWorldMaterial * CreateMaterial(const TCHAR *a_szName) override
virtual TBOOL Create(BLENDMODE a_eBlendMode)
void SetTexture(TUINT a_uiStage, Toshi::TTexture *a_pTexture)

◆ DestroyMaterial()

void AModelLoader::DestroyMaterial ( Toshi::TMaterial * a_pMaterial)
static

Definition at line 305 of file AModelLoader.cpp.

306{
308
309 TINT iId = 0;
310 TINT16 iIndex;
311
312 if ( 0 < ms_iNumCreatedMaterials )
313 {
314 while ( true )
315 {
316 iIndex = ms_oMaterialIds[ iId ];
317 if ( a_pMaterial == ms_oNodesAlloc[ iIndex ].pMaterial ) break;
318
319 if ( ms_iNumCreatedMaterials <= ++iId )
320 {
321 TASSERT( !"Trying to destroy some unknown material" );
322 return;
323 }
324 }
325
326 ms_oNodesAlloc[ iIndex ].iNumRefs -= 1;
327
328 if ( ms_oNodesAlloc[ iIndex ].iNumRefs < 1 )
329 {
330 ms_oNodesAlloc[ iIndex ].Remove();
331 ms_oNodesAlloc[ iIndex ].pMaterial->Destroy();
332 ms_oNodesAlloc[ iIndex ].pMaterial = TNULL;
333
334 ms_oFreeMaterials.PushFront( &ms_oNodesAlloc[ iIndex ] );
335 ms_iNumCreatedMaterials -= 1;
336
337 if ( iId < ms_iNumCreatedMaterials )
338 {
339 TINT16* pNext = ms_oMaterialIds + iId + 1;
340 TINT16* pCurr = ms_oMaterialIds + iId;
341
342 for ( TINT i = ms_iNumCreatedMaterials - iId; i != 0; i-- )
343 {
344 *( pCurr++ ) = *( pNext++ );
345 }
346 }
347 }
348 }
349}
int16_t TINT16
Definition Typedefs.h:14
int TINT
Definition Typedefs.h:7

◆ InitialiseStatic()

void AModelLoader::InitialiseStatic ( )
static

Definition at line 351 of file AModelLoader.cpp.

352{
353 InitialiseGrassLayersTextures();
354}

◆ TDECLARE_CLASS()

AModelLoader::TDECLARE_CLASS ( AModelLoader ,
Toshi::TObject  )

Friends And Related Symbol Documentation

◆ AModelInstance

friend class AModelInstance
friend

Definition at line 49 of file AModelLoader.h.

Member Data Documentation

◆ MAX_NUM_ALLOCATED_MATERIALS

TUINT AModelLoader::MAX_NUM_ALLOCATED_MATERIALS = 512
staticconstexpr

Definition at line 46 of file AModelLoader.h.

◆ MAX_NUM_MODEL_MATERIALS

TUINT AModelLoader::MAX_NUM_MODEL_MATERIALS = 150
staticconstexpr

Definition at line 47 of file AModelLoader.h.


The documentation for this class was generated from the following files: