OpenBarnyard
 
Loading...
Searching...
No Matches
ATreeManager.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "ATreeManager.h"
6
7#ifdef TRENDERINTERFACE_DX8
10#endif // TRENDERINTERFACE_DX8
11
12//-----------------------------------------------------------------------------
13// Enables memory debugging.
14// Note: Should be the last include!
15//-----------------------------------------------------------------------------
16#include <Core/TMemoryDebugOn.h>
17
19
20// $Barnyard: FUNCTION 005efdf0
22 : m_pTrunks( TNULL )
23 , m_pFOBs( TNULL )
24 , m_bFlag( TFALSE )
25 , m_bCreateCollision( TTRUE )
26 , m_fUnk1( 30.0f )
27 , m_fUnk2( 110.0f )
28 , m_fUnk3( 98.0f )
29{
30 // TODO: initialise other members
31}
32
36
37// $Barnyard: FUNCTION 005ee170
39{
40 TASSERT( m_pTrunks == TNULL && m_pFOBs == TNULL );
41
42 const TCHAR szFOBBoneName[] = "FOB";
43
44 // Create tree trunks
45 {
46 struct ModelInfo
47 {
48 const TCHAR* szModelName;
49 TINT iIndex;
50 } aModels[] = {
51 { "", 6 },
52 { "", 0 },
53 { "data\\models\\treelittle.trb", 1 },
54 { "data\\models\\tree_bent_trunk.trb", 2 },
55 { "data\\models\\tree_standard_trunk.trb", 3 },
56 { "data\\models\\tree_tall_trunk.trb", 4 },
57 { "data\\models\\treewall_sim_trunks.trb", 5 },
58 { "", 7 }
59 };
60
61 m_iNumTrunks = TARRAYSIZE( aModels );
62 m_pTrunks = new Model[ m_iNumTrunks ];
63
64 for ( TINT i = 0; i < m_iNumTrunks; i++ )
65 {
66 const TCHAR* pchModelName = aModels[ i ].szModelName;
67 Model* pTreeModel = &m_pTrunks[ i ];
68
69 pTreeModel->m_uiID = aModels[ i ].iIndex;
70
71 if ( pchModelName[ 0 ] == '\0' )
72 // No model specified for this trunk
73 {
74 pTreeModel->m_iNumFOBs = 1;
75 pTreeModel->m_pFOBMatrices = new TMatrix44[ 1 ];
76 pTreeModel->m_pFOBMatrices[ 0 ].Identity();
77 pTreeModel->m_fUnk1 = 0.0f;
78 pTreeModel->m_fRadius = 1.6f;
79
80 if ( i != 1 ) // ?
81 pTreeModel->m_fRadius = 4.4f;
82 }
83 else
84 // This is definetely a tree trunk and it HAS a model
85 {
86 // Load the model
87 pTreeModel->Load( pchModelName );
88
89 // Do some other setups
90 TModel* pModel = pTreeModel->m_pManagedModel->GetModel();
91 TASSERT( pModel->GetNumLODs() >= 1 );
92
93 pTreeModel->m_fUnk1 = -pModel->GetLOD( 0 ).BoundingSphere.GetOrigin().z;
94 pTreeModel->m_fRadius = pModel->GetLOD( 0 ).BoundingSphere.GetRadius() * 1.5f;
95
96 TSkeletonInstance* pSkeletonInstance = pTreeModel->m_pSceneObject->GetInstance()->GetSkeletonInstance();
97 TSkeleton* pSkeleton = pSkeletonInstance->GetSkeleton();
98
99 // Count number of FOBs (foliage objects)
100 pTreeModel->m_iNumFOBs = 0;
101 for ( TINT i = 0; i < pSkeleton->GetBoneCount(); i++ )
102 {
103 if ( !TStringManager::String8CompareNoCase( pSkeleton->GetBone( i )->GetName(), szFOBBoneName, TARRAYSIZE( szFOBBoneName ) - 1 ) )
104 // Name of the bone starts with FOB
105 {
106 pTreeModel->m_iNumFOBs += 1;
107 }
108 }
109
110 // Create matrices for FOBs
111 TINT iCurrentFOB = 0;
112 pTreeModel->m_pFOBMatrices = new TMatrix44[ pTreeModel->m_iNumFOBs ];
113 for ( TINT i = 0; i < pSkeleton->GetBoneCount(); i++ )
114 {
115 if ( !TStringManager::String8CompareNoCase( pSkeleton->GetBone( i )->GetName(), szFOBBoneName, TARRAYSIZE( szFOBBoneName ) - 1 ) )
116 // Name of the bone starts with FOB
117 {
118 pTreeModel->m_pFOBMatrices[ iCurrentFOB++ ] = pSkeletonInstance->GetBoneTransformCurrent( i );
119 }
120 }
121 }
122 }
123 }
124
125 // Create foliage objects
126 {
127 const TCHAR* aFOBModelNames[] = {
128 "data\\models\\busha.trb",
129 "data\\models\\busha_LOD1.trb",
130 "data\\models\\foblittle.trb",
131 TNULL,
132 "data\\models\\tree_bent_fob.trb",
133 TNULL,
134 "data\\models\\tree_standard_fob.trb",
135 TNULL,
136 "data\\models\\tree_tall_fob.trb",
137 TNULL,
138 "data\\models\\treewall_sim_fobs.trb",
139 TNULL,
140 "data\\models\\fob_bens.trb",
141 TNULL,
142 "data\\models\\Fob_pond.trb",
143 TNULL
144 };
145
146 m_iNumFOBs = TARRAYSIZE( aFOBModelNames );
147 m_pFOBs = new Model[ m_iNumFOBs ];
148
149 for ( TINT i = 0; i < m_iNumFOBs; i++ )
150 {
151 if ( const TCHAR* pchModelName = aFOBModelNames[ i ] )
152 {
153 // Load the model
154 m_pFOBs[ i ].Load( pchModelName );
155 }
156 }
157 }
158
159 // Add all tree instances to the list of free instances
160 for ( TINT i = 0; i < MAX_INSTANCES; i++ )
161 {
162 m_llFreeTreeInstances.PushFront( &m_aTreeInstances[ i ] );
163 }
164
165 return TTRUE;
166}
167
168// $Barnyard: FUNCTION 005ee8e0
170{
171 TVALIDPTR( a_pLocatorList );
172
173 TASSERT( m_pFOBs != TNULL && m_pTrunks != TNULL );
174
175 struct LocatorInfo
176 {
177 TINT iIndex;
178 const TCHAR* szName;
179 TBOOL bHasCollision;
180 } aLocators[] = {
181 { 3, "tree02", TTRUE },
182 { 5, "tree03", TTRUE },
183 { 4, "tree05", TTRUE },
184 { 1, "fob2", TTRUE },
185 { 1, "fobw", TTRUE },
186 { 1, "busha", TFALSE },
187 { 6, "treewall2", TTRUE },
188 { 6, "treewall", TTRUE },
189 { 2, "treelittle", TFALSE },
190 { 3, "tree_bent", TTRUE },
191 { 4, "tree_standard", TTRUE },
192 { 5, "tree_tall", TTRUE },
193 { 6, "treewall_sim", TTRUE },
194 { 0, "fob_bens", TTRUE },
195 { 7, "fob_pond", TTRUE },
196 };
197
198 // Initialise transform that is going to be used for each of the tree
199 TMatrix44 matGlobalTransform;
200 matGlobalTransform.m_f11 = 1.0f;
201 matGlobalTransform.m_f12 = 0.0f;
202 matGlobalTransform.m_f13 = 0.0f;
203 matGlobalTransform.m_f14 = 0.0f;
204 matGlobalTransform.m_f21 = 0.0f;
205 matGlobalTransform.m_f22 = 0.0f;
206 matGlobalTransform.m_f23 = 1.0f;
207 matGlobalTransform.m_f24 = 0.0f;
208 matGlobalTransform.m_f31 = 0.0f;
209 matGlobalTransform.m_f32 = -1.0f;
210 matGlobalTransform.m_f33 = 0.0f;
211 matGlobalTransform.m_f34 = 0.0f;
212 matGlobalTransform.m_f41 = 0.0f;
213 matGlobalTransform.m_f42 = 0.0f;
214 matGlobalTransform.m_f43 = 0.0f;
215 matGlobalTransform.m_f44 = 1.0f;
216
217 TINT iNumCollisionObjects = 0;
218 TINT iNumLocators = a_pLocatorList->GetNumLocators();
219
220 if ( iNumLocators > 0 )
221 {
222 TBOOL aFlags[ MAX_INSTANCES - 2 ];
223 TINT aUnk[ MAX_INSTANCES ];
224
225 TBOOL* pFlag = aFlags;
226 TINT* pUnk = aUnk;
227
228 ATerrainLocatorTRBHeader* pLocatorHeader = a_pLocatorList->GetHeader();
229 ATerrainLocator* pLocators = pLocatorHeader->pLocators;
230
231 for ( TINT i = 0; i < iNumLocators; i++ )
232 {
233 const TCHAR* pchLocatorName = pLocatorHeader->ppNames[ pLocators[ i ].uiNameId ];
234 TVALIDPTR( pchLocatorName );
235
236 for ( TINT k = 0; k < TARRAYSIZE( aLocators ); k++ )
237 {
239 aLocators[ k ].szName,
240 pchLocatorName,
241 TStringManager::String8Length( aLocators[ k ].szName )
242 ) )
243 {
244 // This locator is valid, so let's create a tree instance now
245 if ( m_llFreeTreeInstances.IsEmpty() )
246 {
247 TERROR( "ATreeManager::CreateInstances - Failed to create all instances due to not having empty instance slots!\n" );
248 break;
249 }
250
251 TINT iIndex = aLocators[ k ].iIndex;
252 TBOOL bHasCollision = TFALSE;
253
254 // TODO: figure out this hell
255 if ( m_bFlag && m_pTrunks[ iIndex ].m_pManagedModel == TNULL )
256 {
257 *pFlag = TTRUE;
258 iNumCollisionObjects += 1;
259 }
260 else if ( pLocators[ i ].iFlags2 < 0 )
261 {
262 *pFlag = TFALSE;
263 }
264 else
265 {
266 *pFlag = TTRUE;
267 iNumCollisionObjects += 1;
268 bHasCollision = TTRUE;
269 }
270
271 // Move cursors
272 pUnk++;
273 pFlag++;
274
275 // Create the instance object
276 TreeInstance* pTreeInstance = m_llFreeTreeInstances.PopFront();
277
278 TMatrix44 matLocatorTransform;
279 pLocators[ i ].GetMatrix( matLocatorTransform );
280
281 TMatrix44 matFinalTransform;
282 matFinalTransform.Multiply( matGlobalTransform, matLocatorTransform );
283
284 // Initialise the instance object
285 pTreeInstance->iTreeIndex = iIndex;
286
287 // Get flags for this instance
288 if ( iIndex == 7 )
289 // force for "fob_pond"
290 {
291 pTreeInstance->bFlag1 = TTRUE;
292 pTreeInstance->bFlag2 = TTRUE;
293 }
294 else
295 // get flags based on the locator name
296 {
297 TINT iLocatorNameLength = TStringManager::String8Length( pchLocatorName );
298
299 if ( iLocatorNameLength >= 4 && pchLocatorName[ iLocatorNameLength - 4 ] == '_' )
300 {
301 if ( pchLocatorName[ iLocatorNameLength - 3 ] == 'b' &&
302 pchLocatorName[ iLocatorNameLength - 2 ] == 'r' &&
303 pchLocatorName[ iLocatorNameLength - 2 ] == 'n' )
304 // barn?
305 {
306 pTreeInstance->bFlag1 = TTRUE;
307 }
308 else if ( pchLocatorName[ iLocatorNameLength - 3 ] == 'd' && pchLocatorName[ iLocatorNameLength - 2 ] == 'k' && pchLocatorName[ iLocatorNameLength - 2 ] == 'g' )
309 // dankweed pond and some 'g'?
310 {
311 pTreeInstance->bFlag2 = TTRUE;
312 }
313 }
314 }
315
316 pTreeInstance->uiLocatorId = TUINT16( i );
317 pTreeInstance->pLocatorList = a_pLocatorList;
318 pTreeInstance->uiUnk3 = 255;
319 m_llUsedTreeInstances.PushBack( pTreeInstance );
320
321 TTODO( "Spawn FOBs, create collisions and probably something else, idk..." );
322
323 break;
324 }
325 }
326 }
327 }
328}
329
330// $Barnyard: FUNCTION 005ef3a0
332{
333 TASSERT( TFALSE );
335
336 TMatrix44 matModelView = pRenderContext->GetModelViewMatrix();
338
339 // something with field9_0x18
340 // ...
341}
342
343// $Barnyard: FUNCTION 005edd80
345{
346 m_pManagedModel = TNULL;
347 m_pSceneObject = TNULL;
348 m_uiID = 0;
349 m_iNumFOBs = 0;
350 m_pFOBMatrices = TNULL;
351}
352
353// $Barnyard: FUNCTION 005edfe0
355{
356 if ( m_pSceneObject )
357 m_pSceneObject->Delete();
358
359 if ( m_pManagedModel )
360 delete m_pManagedModel;
361
362 TVALIDPTR( m_pFOBMatrices );
363 delete[] m_pFOBMatrices;
364}
365
366void ATreeManager::Model::Load( const TCHAR* a_szModelName )
367{
368 m_pManagedModel = new TManagedModel();
369 m_pManagedModel->Create( a_szModelName, AAssetLoader::GetAssetTRB( AAssetType_Startup ) );
370
371 // Create scene object
372 m_pSceneObject = m_pManagedModel->CreateSceneObject();
373 m_pSceneObject->GetTransform().SetMatrix( TMatrix44::IDENTITY );
374 m_pSceneObject->GetTransform().SetEuler( TVector3( TMath::PI / 2, 0.0f, 0.0f ) );
375 m_pSceneObject->GetTransform().SetTranslate( TVector3::VEC_ZERO );
376
377 // ?????????
378 TMatrix44 matTransform;
379 m_pSceneObject->GetTransform().GetLocalMatrixImp( matTransform );
380 m_pSceneObject->GetTransform().SetMatrix( matTransform );
381 m_pSceneObject->DisableSkeletonUpdate();
382
383 TModel* pModel = m_pManagedModel->GetModel();
384 for ( TINT i = 0; i < pModel->GetNumLODs(); i++ )
385 {
386 TModelLOD* pLOD = &pModel->GetLOD( i );
387
388 for ( TINT k = 0; k < pLOD->iNumMeshes; k++ )
389 {
390 TMesh* pMesh = pLOD->ppMeshes[ k ];
391
392 // Use HD lighting on skinned materials
393 if ( ASkinMaterialHAL* pSkinMat = TDYNAMICCAST( ASkinMaterialHAL, pMesh->GetMaterial() ) )
394 pSkinMat->SetHDLighting( TTRUE );
395 }
396 }
397}
#define TASSERT(X,...)
Definition Defines.h:138
#define TERROR(...)
Definition Defines.h:153
#define TOSHI_NAMESPACE_USING
Definition Defines.h:46
#define TTODO(DESC)
Definition Defines.h:134
#define TARRAYSIZE(ARRAY)
Definition Defines.h:70
#define TVALIDPTR(PTR)
Definition Defines.h:139
#define TDYNAMICCAST(T, OBJECT)
Definition TObject.h:227
uint16_t TUINT16
Definition Typedefs.h:15
char TCHAR
Definition Typedefs.h:20
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
#define TFALSE
Definition Typedefs.h:24
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
@ AAssetType_Startup
Definition AAssetLoader.h:8
constexpr TFLOAT PI
Definition TMathInline.h:35
TFLOAT m_f21
Definition TMatrix44.h:361
TFLOAT m_f12
Definition TMatrix44.h:360
TFLOAT m_f44
Definition TMatrix44.h:363
TFLOAT m_f43
Definition TMatrix44.h:363
TFLOAT m_f32
Definition TMatrix44.h:362
TFLOAT m_f13
Definition TMatrix44.h:360
static constinit TMatrix44 IDENTITY
Definition TMatrix44.h:357
TFLOAT m_f14
Definition TMatrix44.h:360
TFLOAT m_f11
Definition TMatrix44.h:360
TFLOAT m_f23
Definition TMatrix44.h:361
TFLOAT m_f22
Definition TMatrix44.h:361
TFLOAT m_f41
Definition TMatrix44.h:363
TFLOAT m_f31
Definition TMatrix44.h:362
TFLOAT m_f24
Definition TMatrix44.h:361
TFLOAT m_f34
Definition TMatrix44.h:362
void Multiply(const TMatrix44 &a_rLeft, const TMatrix44 &a_rRight)
Definition TMatrix44.cpp:61
TFLOAT m_f42
Definition TMatrix44.h:363
TFLOAT m_f33
Definition TMatrix44.h:362
constexpr TFORCEINLINE TFLOAT GetRadius() const
Definition TSphere.h:106
constexpr TFORCEINLINE TVector3 & GetOrigin()
Definition TSphere.h:96
static constinit const TVector3 VEC_ZERO
Definition TVector3.h:150
TFLOAT z
Definition TVector3.h:163
TSphere BoundingSphere
Definition TModel.h:26
TINT iNumMeshes
Definition TModel.h:27
TMesh ** ppMeshes
Definition TModel.h:28
TINT GetNumLODs() const
Definition TModel.h:111
TModelLOD & GetLOD(TUINT32 a_uiLOD)
Definition TModel.h:115
TMatrix44 & GetModelViewMatrix()
const TMatrix44 & GetLightDirection() const
TRenderContext * GetCurrentContext() const
const TCHAR * GetName() const
Definition TSkeleton.h:103
TSkeletonBone * GetBone(const TCHAR *a_cBoneName, TUINT32 a_iLength=0)
Definition TSkeleton.h:141
TINT GetBoneCount() const
Definition TSkeleton.h:149
const TMatrix44 & GetBoneTransformCurrent(TINT a_iBone)
TSkeleton * GetSkeleton()
Definition TSkeleton.h:216
static TFORCEINLINE TRenderInterface * GetSingleton()
Definition TSingleton.h:49
static TSIZE String8Length(const TCHAR *str)
static TINT String8CompareNoCase(const TCHAR *str1, const TCHAR *str2, TSIZE size=-1)
static Toshi::TTRB * GetAssetTRB(AAssetType a_eAssetType)
TBOOL Initialise()
void CreateInstances(ATerrainLocatorList *a_pLocatorList)
static constexpr TINT MAX_INSTANCES
ATerrainLocatorList * pLocatorList
void Load(const TCHAR *a_szModelName)
void GetMatrix(Toshi::TMatrix44 &a_rOutMatrix)
ATerrainLocator * pLocators
TINT32 GetNumLocators() const
ATerrainLocatorTRBHeader * GetHeader() const