forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbone_merge_cache.cpp
288 lines (237 loc) · 8.61 KB
/
bone_merge_cache.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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//=============================================================================//
#include "cbase.h"
#include "bone_merge_cache.h"
#include "bone_setup.h"
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// CBoneMergeCache
//-----------------------------------------------------------------------------
CBoneMergeCache::CBoneMergeCache()
{
m_pOwner = NULL;
m_pFollow = NULL;
m_pFollowHdr = NULL;
m_pFollowRenderHdr = NULL;
m_pOwnerHdr = NULL;
m_nFollowBoneSetupMask = 0;
}
void CBoneMergeCache::Init( C_BaseAnimating *pOwner )
{
m_pOwner = pOwner;
m_pFollow = NULL;
m_pFollowHdr = NULL;
m_pFollowRenderHdr = NULL;
m_pOwnerHdr = NULL;
m_nFollowBoneSetupMask = 0;
}
void CBoneMergeCache::UpdateCache()
{
CStudioHdr *pOwnerHdr = m_pOwner ? m_pOwner->GetModelPtr() : NULL;
if ( !pOwnerHdr )
{
if ( m_pOwnerHdr )
{
// Owner's model got swapped out
m_MergedBones.Purge();
m_BoneMergeBits.Purge();
m_pFollow = NULL;
m_pFollowHdr = NULL;
m_pFollowRenderHdr = NULL;
m_pOwnerHdr = NULL;
m_nFollowBoneSetupMask = 0;
}
return;
}
C_BaseAnimating *pTestFollow = m_pOwner->FindFollowedEntity();
CStudioHdr *pTestHdr = (pTestFollow ? pTestFollow->GetModelPtr() : NULL);
const studiohdr_t *pTestStudioHDR = (pTestHdr ? pTestHdr->GetRenderHdr() : NULL);
if ( pTestFollow != m_pFollow || pTestHdr != m_pFollowHdr || pTestStudioHDR != m_pFollowRenderHdr || pOwnerHdr != m_pOwnerHdr )
{
m_MergedBones.Purge();
m_BoneMergeBits.Purge();
// Update the cache.
if ( pTestFollow && pTestHdr && pOwnerHdr )
{
m_pFollow = pTestFollow;
m_pFollowHdr = pTestHdr;
m_pFollowRenderHdr = pTestStudioHDR;
m_pOwnerHdr = pOwnerHdr;
m_BoneMergeBits.SetSize( pOwnerHdr->numbones() / 8 + 1 );
memset( m_BoneMergeBits.Base(), 0, m_BoneMergeBits.Count() );
mstudiobone_t *pOwnerBones = m_pOwnerHdr->pBone( 0 );
m_nFollowBoneSetupMask = BONE_USED_BY_BONE_MERGE;
for ( int i = 0; i < m_pOwnerHdr->numbones(); i++ )
{
int parentBoneIndex = Studio_BoneIndexByName( m_pFollowHdr, pOwnerBones[i].pszName() );
if ( parentBoneIndex < 0 )
continue;
// Add a merged bone here.
CMergedBone mergedBone;
mergedBone.m_iMyBone = i;
mergedBone.m_iParentBone = parentBoneIndex;
m_MergedBones.AddToTail( mergedBone );
m_BoneMergeBits[i>>3] |= ( 1 << ( i & 7 ) );
if ( ( m_pFollowHdr->boneFlags( parentBoneIndex ) & BONE_USED_BY_BONE_MERGE ) == 0 )
{
m_nFollowBoneSetupMask = BONE_USED_BY_ANYTHING;
// Warning("Performance warning: Merge with '%s'. Mark bone '%s' in model '%s' as being used by bone merge in the .qc!\n",
// pOwnerHdr->pszName(), m_pFollowHdr->pBone( parentBoneIndex )->pszName(), m_pFollowHdr->pszName() );
}
}
// No merged bones found? Slam the mask to 0
if ( !m_MergedBones.Count() )
{
m_nFollowBoneSetupMask = 0;
}
}
else
{
m_pFollow = NULL;
m_pFollowHdr = NULL;
m_pFollowRenderHdr = NULL;
m_pOwnerHdr = NULL;
m_nFollowBoneSetupMask = 0;
}
}
}
#ifdef STAGING_ONLY
ConVar r_captain_canteen_is_angry ( "r_captain_canteen_is_angry", "1" );
#endif
void CBoneMergeCache::MergeMatchingBones( int boneMask )
{
UpdateCache();
// If this is set, then all the other cache data is set.
if ( !m_pOwnerHdr || m_MergedBones.Count() == 0 )
return;
// Have the entity we're following setup its bones.
bool bWorked = m_pFollow->SetupBones( NULL, -1, m_nFollowBoneSetupMask, gpGlobals->curtime );
// We suspect there's some cases where SetupBones couldn't do its thing, and then this causes Captain Canteen.
Assert ( bWorked );
if ( !bWorked )
{
// Usually this means your parent is invisible or gone or whatever.
// This routine has no way to tell its caller not to draw itself unfortunately.
// But we can shrink all the bones down to zero size.
// But it might still spawn particle systems? :-(
matrix3x4_t NewBone;
MatrixScaleByZero ( NewBone );
MatrixSetTranslation ( Vector ( 0.0f, 0.0f, 0.0f ), NewBone );
#ifdef STAGING_ONLY
if ( r_captain_canteen_is_angry.GetBool() )
{
// We actually want to see when Captain Canteen happened, and make it really obvious that (a) he was here and (b) this code would have fixed him.
float HowAngry = 20.0f; // Leon's getting larger!
MatrixSetColumn ( Vector ( HowAngry, 0.0f, 0.0f ), 0, NewBone );
MatrixSetColumn ( Vector ( 0.0f, HowAngry, 0.0f ), 1, NewBone );
MatrixSetColumn ( Vector ( 0.0f, 0.0f, HowAngry ), 2, NewBone );
}
#endif
for ( int i=0; i < m_MergedBones.Count(); i++ )
{
int iOwnerBone = m_MergedBones[i].m_iMyBone;
// Only update bones reference by the bone mask.
if ( !( m_pOwnerHdr->boneFlags( iOwnerBone ) & boneMask ) )
continue;
m_pOwner->GetBoneForWrite( iOwnerBone ) = NewBone;
}
}
else
{
// Now copy the bone matrices.
for ( int i=0; i < m_MergedBones.Count(); i++ )
{
int iOwnerBone = m_MergedBones[i].m_iMyBone;
int iParentBone = m_MergedBones[i].m_iParentBone;
// Only update bones reference by the bone mask.
if ( !( m_pOwnerHdr->boneFlags( iOwnerBone ) & boneMask ) )
continue;
MatrixCopy( m_pFollow->GetBone( iParentBone ), m_pOwner->GetBoneForWrite( iOwnerBone ) );
}
}
}
// copy bones instead of matrices
void CBoneMergeCache::CopyParentToChild( const Vector parentPos[], const Quaternion parentQ[], Vector childPos[], Quaternion childQ[], int boneMask )
{
UpdateCache();
// If this is set, then all the other cache data is set.
if ( !m_pOwnerHdr || m_MergedBones.Count() == 0 )
return;
// Now copy the bone matrices.
for ( int i=0; i < m_MergedBones.Count(); i++ )
{
int iOwnerBone = m_MergedBones[i].m_iMyBone;
int iParentBone = m_MergedBones[i].m_iParentBone;
if ( m_pOwnerHdr->boneParent( iOwnerBone ) == -1 || m_pFollowHdr->boneParent( iParentBone ) == -1 )
continue;
// Only update bones reference by the bone mask.
if ( !( m_pOwnerHdr->boneFlags( iOwnerBone ) & boneMask ) )
continue;
childPos[ iOwnerBone ] = parentPos[ iParentBone ];
childQ[ iOwnerBone ] = parentQ[ iParentBone ];
}
}
void CBoneMergeCache::CopyChildToParent( const Vector childPos[], const Quaternion childQ[], Vector parentPos[], Quaternion parentQ[], int boneMask )
{
UpdateCache();
// If this is set, then all the other cache data is set.
if ( !m_pOwnerHdr || m_MergedBones.Count() == 0 )
return;
// Now copy the bone matrices.
for ( int i=0; i < m_MergedBones.Count(); i++ )
{
int iOwnerBone = m_MergedBones[i].m_iMyBone;
int iParentBone = m_MergedBones[i].m_iParentBone;
if ( m_pOwnerHdr->boneParent( iOwnerBone ) == -1 || m_pFollowHdr->boneParent( iParentBone ) == -1 )
continue;
// Only update bones reference by the bone mask.
if ( !( m_pOwnerHdr->boneFlags( iOwnerBone ) & boneMask ) )
continue;
parentPos[ iParentBone ] = childPos[ iOwnerBone ];
parentQ[ iParentBone ] = childQ[ iOwnerBone ];
}
}
bool CBoneMergeCache::GetAimEntOrigin( Vector *pAbsOrigin, QAngle *pAbsAngles )
{
UpdateCache();
// If this is set, then all the other cache data is set.
if ( !m_pOwnerHdr || m_MergedBones.Count() == 0 )
return false;
// We want the abs origin such that if we put the entity there, the first merged bone
// will be aligned. This way the entity will be culled in the correct position.
//
// ie: mEntity * mBoneLocal = mFollowBone
// so: mEntity = mFollowBone * Inverse( mBoneLocal )
//
// Note: the code below doesn't take animation into account. If the attached entity animates
// all over the place, then this won't get the right results.
// Get mFollowBone.
m_pFollow->SetupBones( NULL, -1, m_nFollowBoneSetupMask, gpGlobals->curtime );
const matrix3x4_t &mFollowBone = m_pFollow->GetBone( m_MergedBones[0].m_iParentBone );
// Get Inverse( mBoneLocal )
matrix3x4_t mBoneLocal, mBoneLocalInv;
SetupSingleBoneMatrix( m_pOwnerHdr, m_pOwner->GetSequence(), 0, m_MergedBones[0].m_iMyBone, mBoneLocal );
MatrixInvert( mBoneLocal, mBoneLocalInv );
// Now calculate mEntity = mFollowBone * Inverse( mBoneLocal )
matrix3x4_t mEntity;
ConcatTransforms( mFollowBone, mBoneLocalInv, mEntity );
MatrixAngles( mEntity, *pAbsAngles, *pAbsOrigin );
return true;
}
bool CBoneMergeCache::GetRootBone( matrix3x4_t &rootBone )
{
UpdateCache();
// If this is set, then all the other cache data is set.
if ( !m_pOwnerHdr || m_MergedBones.Count() == 0 )
return false;
// Get mFollowBone.
m_pFollow->SetupBones( NULL, -1, m_nFollowBoneSetupMask, gpGlobals->curtime );
rootBone = m_pFollow->GetBone( m_MergedBones[0].m_iParentBone );
return true;
}