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

#include <AModel.h>

Inheritance diagram for AModel:

Public Member Functions

 AModel (const Toshi::TPString8 &a_rName, Toshi::TTRB *a_pTRB)
 
virtual ~AModel ()
 
void Update (TFLOAT a_fDeltaTime)
 
void Render (TBOOL a_bIsSomething)
 
AModelInstanceRef CreateInstance ()
 
void DestroyInstance (AModelInstance *a_pInstance)
 
TSIZE GetNumInstances () const
 
Toshi::T2Vector< AModelInstanceRef, MAX_NUM_INSTANCES > & GetInstances ()
 
const Toshi::TPString8 & GetName () const
 
Toshi::TSkeleton * GetSkeleton () const
 

Static Public Member Functions

static Toshi::TManagedModel * Create (const Toshi::TPString8 &a_rFilePath, Toshi::TTRB *a_pTRB)
 
static void GetNameFromPath (const Toshi::TPString8 &a_FilePath, Toshi::TString8 &a_rName)
 
static Toshi::TString8 GenerateInstanceName (const Toshi::TPString8 &a_FilePath)
 

Static Public Attributes

static constexpr TUINT32 MAX_NUM_INSTANCES = 250
 

Detailed Description

Definition at line 80 of file AModel.h.

Constructor & Destructor Documentation

◆ AModel()

AModel::AModel ( const Toshi::TPString8 & a_rName,
Toshi::TTRB * a_pTRB )

Definition at line 28 of file AModel.cpp.

29 : m_Name( a_rName )
30 , m_uiID( ms_uiNumCreated )
31 , m_pManagedModel( TNULL )
32 , m_Vec1( TVector3::VEC_ZERO )
33 , m_Vec2( TVector3::VEC_ZERO )
34{
35 m_pManagedModel = AModel::Create( a_rName, a_pTRB );
36
37 ms_uiNumCreated += 1;
38}
#define TNULL
Definition Typedefs.h:23
static constinit const TVector3 VEC_ZERO
Definition TVector3.h:150
static Toshi::TManagedModel * Create(const Toshi::TPString8 &a_rFilePath, Toshi::TTRB *a_pTRB)
Definition AModel.cpp:161

◆ ~AModel()

AModel::~AModel ( )
virtual

Definition at line 42 of file AModel.cpp.

43{
44 if ( m_pManagedModel )
45 {
46 delete m_pManagedModel;
47 }
48}

Member Function Documentation

◆ Create()

TManagedModel * AModel::Create ( const Toshi::TPString8 & a_rFilePath,
Toshi::TTRB * a_pTRB )
static

Definition at line 161 of file AModel.cpp.

162{
163 // TODO: use this name in debug?
164 TString8 modelName;
165 GetNameFromPath( a_rFilePath, modelName );
166
167 auto pModelPtr = new TManagedModel();
168
169 if ( pModelPtr )
170 {
171 TString8 filepath = a_rFilePath.GetString8();
172 filepath.MakeLower();
173
174 if ( filepath.Find( ".trb" ) < 0 &&
175 filepath.Find( ".trz" ) < 0 )
176 {
177 filepath += ".trb";
178 }
179
180 pModelPtr->Create( filepath, a_pTRB );
181
182 ASkinLightingManager::GetSingleton()->ApplySkinLight( pModelPtr, TNULL, TNULL );
183 }
184
185 return pModelPtr;
186}
TString8 & MakeLower()
Definition TString8.h:87
TINT Find(TCHAR character, TINT pos=0) const
Definition TString8.cpp:107
static void GetNameFromPath(const Toshi::TPString8 &a_FilePath, Toshi::TString8 &a_rName)
Definition AModel.cpp:189

◆ CreateInstance()

AModelInstanceRef AModel::CreateInstance ( )

Definition at line 134 of file AModel.cpp.

135{
136 // TODO: use this name in debug?
137 TString8 instanceName = GenerateInstanceName( m_Name );
138
139 AModelInstanceRef modelInstanceRef = new AModelInstance(
140 this,
141 m_pManagedModel->CreateSceneObject(),
142 TFALSE
143 );
144
145 m_vecInstanceRefs.PushBack( modelInstanceRef );
146 return modelInstanceRef;
147}
#define TFALSE
Definition Typedefs.h:24
Toshi::TRef< AModelInstance > AModelInstanceRef
Definition AModel.h:78
static Toshi::TString8 GenerateInstanceName(const Toshi::TPString8 &a_FilePath)
Definition AModel.cpp:234

◆ DestroyInstance()

void AModel::DestroyInstance ( AModelInstance * a_pInstance)

Definition at line 150 of file AModel.cpp.

151{
152 m_vecInstanceRefs.FindAndEraseFast( a_pInstance );
153}

◆ GenerateInstanceName()

TString8 AModel::GenerateInstanceName ( const Toshi::TPString8 & a_FilePath)
static

Definition at line 234 of file AModel.cpp.

235{
236 static TUINT ms_uiDebugObjectIndex = 0;
237
238 TString8 name;
239 GetNameFromPath( a_FilePath, name );
240
241 TString8 debugName;
242 debugName.Format( "%s%i", name.GetString(), ms_uiDebugObjectIndex % 1000 );
243 ms_uiDebugObjectIndex += 1;
244
245 return debugName;
246}
unsigned int TUINT
Definition Typedefs.h:8
TString8 & Format(const TCHAR *a_pcFormat,...)
Definition TString8.cpp:176
const TCHAR * GetString(TINT a_iIndex=0) const
Definition TString8.cpp:286

◆ GetInstances()

Toshi::T2Vector< AModelInstanceRef, MAX_NUM_INSTANCES > & AModel::GetInstances ( )
inline

Definition at line 96 of file AModel.h.

96{ return m_vecInstanceRefs; }

◆ GetName()

const Toshi::TPString8 & AModel::GetName ( ) const
inline

Definition at line 98 of file AModel.h.

98{ return m_Name; }

◆ GetNameFromPath()

void AModel::GetNameFromPath ( const Toshi::TPString8 & a_FilePath,
Toshi::TString8 & a_rName )
static

Definition at line 189 of file AModel.cpp.

190{
191 TString8 name = a_FilePath.GetString8();
192
193 if ( name.Length() <= 8 )
194 {
195 a_rName = name;
196 }
197 else
198 {
199 TINT iPos1 = 0;
200 TINT iPos2;
201
202 do {
203 iPos2 = iPos1;
204 iPos1 = name.Find( '\\', iPos1 + 1 );
205 } while ( -1 < iPos1 );
206
207 do {
208 iPos1 = iPos2;
209 iPos2 = name.Find( '/', iPos1 + 1 );
210 } while ( -1 < iPos2 );
211
212 if ( iPos1 < 0 || name.Length() - 1 <= iPos1 ) iPos1 = 0;
213 iPos1 += 1;
214 iPos2 = name.Find( '_', iPos1 );
215 if ( iPos2 < 0 ) iPos2 = name.Length() - 1;
216
217 TUINT iSize = iPos2 - iPos1;
218
219 if ( iSize < 1 )
220 {
221 iPos1 = 0;
222 iSize = 8;
223 }
224 else if ( 8 < iSize )
225 {
226 iSize = 8;
227 }
228
229 a_rName.Copy( name.GetString( iPos1 ), iSize );
230 }
231}
int TINT
Definition Typedefs.h:7
TINT Length() const
Definition TString8.h:93

◆ GetNumInstances()

TSIZE AModel::GetNumInstances ( ) const

Definition at line 155 of file AModel.cpp.

156{
157 return m_vecInstanceRefs.Size();
158}

◆ GetSkeleton()

Toshi::TSkeleton * AModel::GetSkeleton ( ) const
inline

Definition at line 99 of file AModel.h.

99{ return m_pManagedModel->GetModel()->GetSkeleton(); }

◆ Render()

void AModel::Render ( TBOOL a_bIsSomething)

Definition at line 102 of file AModel.cpp.

103{
104 for ( TINT i = 0; i < m_vecInstanceRefs.Size(); i++ )
105 {
106 auto& pModelInstance = m_vecInstanceRefs[ i ];
107 auto eFlags = pModelInstance->m_eFlags;
108
109 if ( HASANYFLAG( eFlags, AModelInstance::Flags_Unknown ) == a_bIsSomething &&
112 {
113 auto pSceneObject = pModelInstance->GetSceneObject();
114 auto pModel = pSceneObject->GetInstance()->GetModel();
115
116 auto& transform = pSceneObject->GetTransform();
117 auto& transformScale = transform.GetScale();
118 auto& lod = pModel->GetLOD( 0 );
119
120 TFLOAT fRadiusScale = TMath::Max( TMath::Max( transformScale.x, transformScale.y ), transformScale.z );
121 TFLOAT fRadius = lod.BoundingSphere.GetRadius() * fRadiusScale;
122
123 TMatrix44 transformMatrix;
124 transform.GetLocalMatrixImp( transformMatrix );
125
126 TVector3 boundingPos;
127 TMatrix44::TransformVector( boundingPos, transformMatrix, lod.BoundingSphere.GetOrigin() );
128 pSceneObject->Render( pModelInstance->GetClipFlags(), boundingPos );
129 }
130 }
131}
#define HASANYFLAG(STATE, FLAG)
Definition Defines.h:5
float TFLOAT
Definition Typedefs.h:4
TFORCEINLINE const T & Max(const T &a, const T &b)
static constexpr void TransformVector(TVector3 &a_rOutVector, const TMatrix44 &a_rMatrix, const TVector3 &a_rVector)
Definition TMatrix44.h:280
@ Flags_UpdatingSkeleton
Definition AModel.h:24

◆ Update()

void AModel::Update ( TFLOAT a_fDeltaTime)

Definition at line 51 of file AModel.cpp.

52{
53 TRenderContext* pRenderCtx = ARenderer::GetSingleton()->GetMainViewport()->GetRenderContext();
54
55 for ( TINT i = 0; i < m_vecInstanceRefs.Size(); i++ )
56 {
57 AModelInstance* pInstance = m_vecInstanceRefs[ i ].Get();
58 TSceneObject* pSceneObject = pInstance->GetSceneObject();
59 TModel* pModel = pSceneObject->GetInstance()->GetModel();
60
61 auto& transform = pSceneObject->GetTransform();
62 auto& transformScale = transform.GetScale();
63 auto& lod = pModel->GetLOD( 0 );
64
65 TVector4 vecBoundingSphere;
66 vecBoundingSphere.w = lod.BoundingSphere.GetRadius() * TMath::Max( TMath::Max( transformScale.x, transformScale.y ), transformScale.z );
67
68 if ( vecBoundingSphere.w <= 0.0f )
69 // The instance's scale is zero, do not render
70 {
71 pInstance->SetVisible( TFALSE );
72 }
73 else
74 // Check if the bounding sphere currently visible
75 {
76 TMatrix44 transformMatrix;
77 transform.GetLocalMatrixImp( transformMatrix );
78
79 TMatrix44::TransformVector( vecBoundingSphere.AsVector3(), transformMatrix, lod.BoundingSphere.GetOrigin() );
80
81 if ( TRenderContext::CullSphereToFrustumSimple( vecBoundingSphere, pRenderCtx->GetWorldPlanes(), 6 ) )
82 {
83 // The object is visible, update clip flags to match the flags it was checked against
84 pInstance->SetVisible( TTRUE );
85 pInstance->SetClipFlags( pRenderCtx->GetClipFlags() );
86 }
87 else
88 {
89 // The object is not visible
90 pInstance->SetVisible( TFALSE );
91 }
92 }
93
94 if ( pInstance->IsUpdatingSkeleton() )
95 {
96 pInstance->GetSceneObject()->Update( a_fDeltaTime );
97 }
98 }
99}
#define TTRUE
Definition Typedefs.h:25
TVector3 & AsVector3()
Definition TVector4.h:321
TFLOAT w
Definition TVector4.h:367
TModel * GetModel() const
Definition TModel.cpp:332
TModelLOD & GetLOD(TUINT32 a_uiLOD)
Definition TModel.h:115
static TBOOL CullSphereToFrustumSimple(const TSphere &a_rSphere, const TPlane *a_pPlanes, TINT a_iNumPlanes)
const TPlane * GetWorldPlanes()
TUINT GetClipFlags() const
const TVector3 & GetScale() const
TModelInstance * GetInstance()
TTransformObject & GetTransform()
TBOOL IsUpdatingSkeleton() const
Definition AModel.h:60
void SetVisible(TBOOL a_bVisible)
Definition AModel.cpp:460
Toshi::TSceneObject * GetSceneObject() const
Definition AModel.h:52
void SetClipFlags(TUINT a_uiClipFlags)
Definition AModel.h:56

Member Data Documentation

◆ MAX_NUM_INSTANCES

TUINT32 AModel::MAX_NUM_INSTANCES = 250
staticconstexpr

Definition at line 83 of file AModel.h.


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