forked from nillerusr/source-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmemodel.cpp
335 lines (277 loc) · 10.9 KB
/
dmemodel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Dme version of a skeletal model (gets compiled into a MDL)
//
//=============================================================================
#include "movieobjects/dmemodel.h"
#include "movieobjects_interfaces.h"
#include "datamodel/dmelementfactoryhelper.h"
#include "datacache/imdlcache.h"
#include "materialsystem/imaterialsystem.h"
#include "tier2/tier2.h"
#include "studio.h"
#include "materialsystem/imaterialsystemhardwareconfig.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Expose this class to the scene database
//-----------------------------------------------------------------------------
IMPLEMENT_ELEMENT_FACTORY( DmeModel, CDmeModel );
//-----------------------------------------------------------------------------
// Stack of DmeModels currently being rendered. Used to set up render state
//-----------------------------------------------------------------------------
CUtlStack< CDmeModel * > CDmeModel::s_ModelStack;
static CUtlVector< matrix3x4_t > s_PoseToWorld;
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CDmeModel::OnConstruction()
{
m_JointTransforms.Init( this, "jointTransforms" );
m_BaseStates.Init( this, "baseStates" );
}
void CDmeModel::OnDestruction()
{
}
//-----------------------------------------------------------------------------
// Add joint
//-----------------------------------------------------------------------------
int CDmeModel::AddJoint( CDmeDag *pJoint )
{
int nIndex = GetJointTransformIndex( pJoint->GetTransform() );
if ( nIndex >= 0 )
return nIndex;
return m_JointTransforms.AddToTail( pJoint->GetTransform() );
}
//-----------------------------------------------------------------------------
// Add joint
//-----------------------------------------------------------------------------
CDmeJoint *CDmeModel::AddJoint( const char *pJointName, CDmeDag *pParent )
{
CDmeJoint *pJoint = CreateElement<CDmeJoint>( pJointName, GetFileId() );
CDmeTransform *pTransform = pJoint->GetTransform();
pTransform->SetName( pJointName );
if ( !pParent )
{
pParent = this;
}
pParent->AddChild( pJoint );
m_JointTransforms.AddToTail( pTransform );
return pJoint;
}
//-----------------------------------------------------------------------------
// Returns the number of joint transforms we know about
//-----------------------------------------------------------------------------
int CDmeModel::GetJointTransformCount() const
{
return m_JointTransforms.Count();
}
//-----------------------------------------------------------------------------
// Determines joint transform index given a joint transform
//-----------------------------------------------------------------------------
int CDmeModel::GetJointTransformIndex( CDmeTransform *pTransform ) const
{
int nCount = m_JointTransforms.Count();
for ( int i = 0; i < nCount; ++i )
{
if ( pTransform == m_JointTransforms[i] )
return i;
}
return -1;
}
//-----------------------------------------------------------------------------
// Determines joint transform index given a joint
//-----------------------------------------------------------------------------
int CDmeModel::GetJointTransformIndex( CDmeDag *pJoint ) const
{
return GetJointTransformIndex( pJoint->GetTransform() );
}
//-----------------------------------------------------------------------------
// Determines joint transform index given a joint name
//-----------------------------------------------------------------------------
CDmeTransform *CDmeModel::GetJointTransform( int nIndex )
{
return m_JointTransforms[ nIndex ];
}
const CDmeTransform *CDmeModel::GetJointTransform( int nIndex ) const
{
return m_JointTransforms[ nIndex ];
}
//-----------------------------------------------------------------------------
// Finds a base state by name, returns NULL if not found
//-----------------------------------------------------------------------------
CDmeTransformList *CDmeModel::FindBaseState( const char *pBaseStateName )
{
int nCount = m_BaseStates.Count();
for ( int i = 0; i < nCount; ++i )
{
if ( !Q_stricmp( m_BaseStates[i]->GetName(), pBaseStateName ) )
return m_BaseStates[i];
}
return NULL;
}
//-----------------------------------------------------------------------------
// Captures the current joint transforms into a base state
//-----------------------------------------------------------------------------
void CDmeModel::CaptureJointsToBaseState( const char *pBaseStateName )
{
CDmeTransformList *pTransformList = FindBaseState( pBaseStateName );
if ( !pTransformList )
{
pTransformList = CreateElement<CDmeTransformList>( pBaseStateName, GetFileId() );
m_BaseStates.AddToTail( pTransformList );
}
// Make the transform list have the correct number of elements
int nJointCount = m_JointTransforms.Count();
int nCurrentCount = pTransformList->GetTransformCount();
if ( nJointCount > nCurrentCount )
{
for ( int i = nCurrentCount; i < nJointCount; ++i )
{
CDmeTransform *pTransform = CreateElement<CDmeTransform>( m_JointTransforms[i]->GetName(), pTransformList->GetFileId() );
pTransformList->m_Transforms.AddToTail( pTransform );
}
}
else if ( nJointCount < nCurrentCount )
{
pTransformList->m_Transforms.RemoveMultiple( nJointCount, nCurrentCount - nJointCount );
}
// Copy the state over
for ( int i = 0; i < nJointCount; ++i )
{
matrix3x4_t mat;
m_JointTransforms[i]->GetTransform( mat );
pTransformList->SetTransform( i, mat );
}
}
//-----------------------------------------------------------------------------
// Loads up joint transforms for this model
//-----------------------------------------------------------------------------
void CDmeModel::LoadJointTransform( CDmeDag *pJoint, CDmeTransformList *pBindPose, const matrix3x4_t &parentToWorld, const matrix3x4_t &parentToBindPose, bool bSetHardwareState )
{
CDmeTransform *pTransform = pJoint->GetTransform();
// Determines joint transform index; no index, no traversing lower in the hierarchy
int nJointIndex = GetJointTransformIndex( pTransform );
if ( nJointIndex < 0 )
return;
// FIXME: Sucky search here necessary to find bone matrix index
matrix3x4_t jointToWorld, jointToParent;
pTransform->GetTransform( jointToParent );
ConcatTransforms( parentToWorld, jointToParent, jointToWorld );
matrix3x4_t bindJointToParent, bindPoseToJoint, bindPoseToWorld, jointToBindPose;
if ( pBindPose )
{
if ( nJointIndex >= pBindPose->GetTransformCount() )
{
Warning( "Model is in an invalid state! There are different numbers of bones in the bind pose and joint transform list!\n" );
return;
}
pBindPose->GetTransform( nJointIndex )->GetTransform( bindJointToParent );
}
else
{
MatrixCopy( jointToParent, bindJointToParent );
}
ConcatTransforms( parentToBindPose, bindJointToParent, jointToBindPose );
MatrixInvert( jointToBindPose, bindPoseToJoint );
ConcatTransforms( jointToWorld, bindPoseToJoint, bindPoseToWorld );
if ( bSetHardwareState )
{
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->LoadBoneMatrix( nJointIndex, bindPoseToWorld );
}
MatrixCopy( bindPoseToWorld, s_PoseToWorld[ nJointIndex ] );
int nChildCount = pJoint->GetChildCount();
for ( int i = 0; i < nChildCount; ++i )
{
CDmeDag *pChildJoint = pJoint->GetChild(i);
if ( !pChildJoint )
continue;
LoadJointTransform( pChildJoint, pBindPose, jointToWorld, jointToBindPose, bSetHardwareState );
}
}
//-----------------------------------------------------------------------------
// Sets up the render state for the model
//-----------------------------------------------------------------------------
CDmeModel::SetupBoneRetval_t CDmeModel::SetupBoneMatrixState( const matrix3x4_t& shapeToWorld, bool bForceSoftwareSkin )
{
int nJointCount = m_JointTransforms.Count();
if ( nJointCount <= 0 )
return NO_SKIN_DATA;
int nBoneBatchCount = g_pMaterialSystemHardwareConfig->MaxVertexShaderBlendMatrices();
bool bSetHardwareState = ( nJointCount <= nBoneBatchCount ) && !bForceSoftwareSkin;
s_PoseToWorld.EnsureCount( nJointCount );
// Finds a base state by name, returns NULL if not found
CDmeTransformList *pBindPose = FindBaseState( "bind" );
matrix3x4_t parentToBindPose;
SetIdentityMatrix( parentToBindPose );
int nChildCount = GetChildCount();
for ( int i = 0; i < nChildCount; ++i )
{
CDmeDag *pChildJoint = GetChild(i);
if ( !pChildJoint )
continue;
LoadJointTransform( pChildJoint, pBindPose, shapeToWorld, parentToBindPose, bSetHardwareState );
}
return bSetHardwareState ? BONES_SET_UP : TOO_MANY_BONES;
}
matrix3x4_t *CDmeModel::SetupModelRenderState( const matrix3x4_t& shapeToWorld, bool bHasSkinningData, bool bForceSoftwareSkin )
{
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
if ( bHasSkinningData && ( s_ModelStack.Count() > 0 ) )
{
SetupBoneRetval_t retVal = s_ModelStack.Top()->SetupBoneMatrixState( shapeToWorld, bForceSoftwareSkin );
if ( retVal == TOO_MANY_BONES )
{
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->LoadIdentity( );
return s_PoseToWorld.Base();
}
if ( retVal != NO_SKIN_DATA )
return NULL;
}
if ( bForceSoftwareSkin )
{
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->LoadIdentity( );
s_PoseToWorld.EnsureCount( 1 );
MatrixCopy( shapeToWorld, s_PoseToWorld[0] );
return s_PoseToWorld.Base();
}
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->LoadMatrix( shapeToWorld );
return NULL;
}
void CDmeModel::CleanupModelRenderState()
{
CMatRenderContextPtr pRenderContext( g_pMaterialSystem );
pRenderContext->MatrixMode( MATERIAL_MODEL );
pRenderContext->LoadIdentity();
}
//-----------------------------------------------------------------------------
// Recursively render the Dag hierarchy
//-----------------------------------------------------------------------------
void CDmeModel::Draw( CDmeDrawSettings *pDrawSettings /* = NULL */ )
{
s_ModelStack.Push( this );
BaseClass::Draw( pDrawSettings );
s_ModelStack.Pop( );
}
//-----------------------------------------------------------------------------
// Set if Z is the up axis of the model
//-----------------------------------------------------------------------------
void CDmeModel::ZUp( bool bZUp )
{
SetValue( "upAxis", bZUp ? "Z" : "Y" );
}
//-----------------------------------------------------------------------------
// Returns true if the DmeModel is Z Up.
// NOTE: Since Y & Z are the only supported modes and Y is the default
// because that's how DmeModel data was originally defined,
// assume Y is up if the m_UpAxis attribute is not "Z"
//-----------------------------------------------------------------------------
bool CDmeModel::IsZUp() const
{
const char *pszZUp = this->GetValueString( "upAxis" );
return ( pszZUp && *pszZUp ) ? StringHasPrefix( pszZUp, "Z" ) : false;
}