forked from sears2424/Source-PlusPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbone_setup.h
449 lines (364 loc) · 15.3 KB
/
bone_setup.h
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//=============================================================================//
#ifndef BONE_SETUP_H
#define BONE_SETUP_H
#ifdef _WIN32
#pragma once
#endif
#include "studio.h"
#include "cmodel.h"
#include "bitvec.h"
class CBoneToWorld;
class CIKContext;
class CBoneAccessor;
class IPoseDebugger;
// This provides access to networked arrays, so if this code actually changes a value,
// the entity is marked as changed.
abstract_class IParameterAccess
{
public:
virtual float GetParameter( int iParam ) = 0;
virtual void SetParameter( int iParam, float flValue ) = 0;
};
class CBoneBitList : public CBitVec<MAXSTUDIOBONES>
{
public:
inline void MarkBone(int iBone)
{
Set(iBone);
}
inline bool IsBoneMarked(int iBone)
{
return Get(iBone) != 0 ? true : false;
}
};
class CBoneSetup;
class IBoneSetup
{
public:
IBoneSetup( const CStudioHdr *pStudioHdr, int boneMask, const float poseParameter[], IPoseDebugger *pPoseDebugger = NULL );
~IBoneSetup( void );
void InitPose( Vector pos[], Quaternion[] );
void AccumulatePose( Vector pos[], Quaternion q[], int sequence, float cycle, float flWeight, float flTime, CIKContext *pIKContext );
void CalcAutoplaySequences( Vector pos[], Quaternion q[], float flRealTime, CIKContext *pIKContext );
void CalcBoneAdj( Vector pos[], Quaternion q[], const float controllers[] );
CStudioHdr *GetStudioHdr();
private:
CBoneSetup *m_pBoneSetup;
};
//-----------------------------------------------------------------------------
// Purpose: blends together all the bones from two p:q lists
//
// p1 = p1 * (1 - s) + p2 * s
// q1 = q1 * (1 - s) + q2 * s
//-----------------------------------------------------------------------------
void SlerpBones(
const CStudioHdr *pStudioHdr,
Quaternion q1[MAXSTUDIOBONES],
Vector pos1[MAXSTUDIOBONES],
mstudioseqdesc_t &seqdesc, // source of q2 and pos2
int sequence,
const Quaternion q2[MAXSTUDIOBONES],
const Vector pos2[MAXSTUDIOBONES],
float s,
int boneMask
);
// Given two samples of a bone separated in time by dt,
// compute the velocity and angular velocity of that bone
void CalcBoneDerivatives( Vector &velocity, AngularImpulse &angVel, const matrix3x4_t &prev, const matrix3x4_t ¤t, float dt );
// Give a derivative of a bone, compute the velocity & angular velocity of that bone
void CalcBoneVelocityFromDerivative( const QAngle &vecAngles, Vector &velocity, AngularImpulse &angVel, const matrix3x4_t ¤t );
// This function sets up the local transform for a single frame of animation. It doesn't handle
// pose parameters or interpolation between frames.
void SetupSingleBoneMatrix(
CStudioHdr *pOwnerHdr,
int nSequence,
int iFrame,
int iBone,
matrix3x4_t &mBoneLocal );
// Purpose: build boneToWorld transforms for a specific bone
void BuildBoneChain(
const CStudioHdr *pStudioHdr,
const matrix3x4_t &rootxform,
const Vector pos[],
const Quaternion q[],
int iBone,
matrix3x4_t *pBoneToWorld );
void BuildBoneChain(
const CStudioHdr *pStudioHdr,
const matrix3x4_t &rootxform,
const Vector pos[],
const Quaternion q[],
int iBone,
matrix3x4_t *pBoneToWorld,
CBoneBitList &boneComputed );
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
// ik info
class CIKTarget
{
public:
void SetOwner( int entindex, const Vector &pos, const QAngle &angles );
void ClearOwner( void );
int GetOwner( void );
void UpdateOwner( int entindex, const Vector &pos, const QAngle &angles );
void SetPos( const Vector &pos );
void SetAngles( const QAngle &angles );
void SetQuaternion( const Quaternion &q );
void SetNormal( const Vector &normal );
void SetPosWithNormalOffset( const Vector &pos, const Vector &normal );
void SetOnWorld( bool bOnWorld = true );
bool IsActive( void );
void IKFailed( void );
int chain;
int type;
void MoveReferenceFrame( Vector &deltaPos, QAngle &deltaAngles );
// accumulated offset from ideal footplant location
public:
struct x2 {
char *pAttachmentName;
Vector pos;
Quaternion q;
} offset;
private:
struct x3 {
Vector pos;
Quaternion q;
} ideal;
public:
struct x4 {
float latched;
float release;
float height;
float floor;
float radius;
float flTime;
float flWeight;
Vector pos;
Quaternion q;
bool onWorld;
} est; // estimate contact position
struct x5 {
float hipToFoot; // distance from hip
float hipToKnee; // distance from hip to knee
float kneeToFoot; // distance from knee to foot
Vector hip; // location of hip
Vector closest; // closest valid location from hip to foot that the foot can move to
Vector knee; // pre-ik location of knee
Vector farthest; // farthest valid location from hip to foot that the foot can move to
Vector lowest; // lowest position directly below hip that the foot can drop to
} trace;
private:
// internally latched footset, position
struct x1 {
// matrix3x4_t worldTarget;
bool bNeedsLatch;
bool bHasLatch;
float influence;
int iFramecounter;
int owner;
Vector absOrigin;
QAngle absAngles;
Vector pos;
Quaternion q;
Vector deltaPos; // acculated error
Quaternion deltaQ;
Vector debouncePos;
Quaternion debounceQ;
} latched;
struct x6 {
float flTime; // time last error was detected
float flErrorTime;
float ramp;
bool bInError;
} error;
friend class CIKContext;
};
struct ikchainresult_t
{
// accumulated offset from ideal footplant location
int target;
Vector pos;
Quaternion q;
float flWeight;
};
struct ikcontextikrule_t
{
int index;
int type;
int chain;
int bone;
int slot; // iktarget slot. Usually same as chain.
float height;
float radius;
float floor;
Vector pos;
Quaternion q;
float start; // beginning of influence
float peak; // start of full influence
float tail; // end of full influence
float end; // end of all influence
float top;
float drop;
float commit; // frame footstep target should be committed
float release; // frame ankle should end rotation from latched orientation
float flWeight; // processed version of start-end cycle
float flRuleWeight; // blending weight
float latched; // does the IK rule use a latched value?
char *szLabel;
Vector kneeDir;
Vector kneePos;
ikcontextikrule_t() {}
private:
// No copy constructors allowed
ikcontextikrule_t(const ikcontextikrule_t& vOther);
};
void Studio_AlignIKMatrix( matrix3x4_t &mMat, const Vector &vAlignTo );
bool Studio_SolveIK( int iThigh, int iKnee, int iFoot, Vector &targetFoot, matrix3x4_t* pBoneToWorld );
bool Studio_SolveIK( int iThigh, int iKnee, int iFoot, Vector &targetFoot, Vector &targetKneePos, Vector &targetKneeDir, matrix3x4_t* pBoneToWorld );
class CIKContext
{
public:
CIKContext( );
void Init( const CStudioHdr *pStudioHdr, const QAngle &angles, const Vector &pos, float flTime, int iFramecounter, int boneMask );
void AddDependencies( mstudioseqdesc_t &seqdesc, int iSequence, float flCycle, const float poseParameters[], float flWeight = 1.0f );
void ClearTargets( void );
void UpdateTargets( Vector pos[], Quaternion q[], matrix3x4_t boneToWorld[], CBoneBitList &boneComputed );
void AutoIKRelease( void );
void SolveDependencies( Vector pos[], Quaternion q[], matrix3x4_t boneToWorld[], CBoneBitList &boneComputed );
void AddAutoplayLocks( Vector pos[], Quaternion q[] );
void SolveAutoplayLocks( Vector pos[], Quaternion q[] );
void AddSequenceLocks( mstudioseqdesc_t &SeqDesc, Vector pos[], Quaternion q[] );
void SolveSequenceLocks( mstudioseqdesc_t &SeqDesc, Vector pos[], Quaternion q[] );
void AddAllLocks( Vector pos[], Quaternion q[] );
void SolveAllLocks( Vector pos[], Quaternion q[] );
void SolveLock( const mstudioiklock_t *plock, int i, Vector pos[], Quaternion q[], matrix3x4_t boneToWorld[], CBoneBitList &boneComputed );
CUtlVectorFixed< CIKTarget, 12 > m_target;
private:
CStudioHdr const *m_pStudioHdr;
bool Estimate( int iSequence, float flCycle, int iTarget, const float poseParameter[], float flWeight = 1.0f );
void BuildBoneChain( const Vector pos[], const Quaternion q[], int iBone, matrix3x4_t *pBoneToWorld, CBoneBitList &boneComputed );
// virtual IK rules, filtered and combined from each sequence
CUtlVector< CUtlVector< ikcontextikrule_t > > m_ikChainRule;
CUtlVector< ikcontextikrule_t > m_ikLock;
matrix3x4_t m_rootxform;
int m_iFramecounter;
float m_flTime;
int m_boneMask;
};
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
// replaces the bonetoworld transforms for all bones that are procedural
bool CalcProceduralBone(
const CStudioHdr *pStudioHdr,
int iBone,
CBoneAccessor &bonetoworld
);
void Studio_BuildMatrices(
const CStudioHdr *pStudioHdr,
const QAngle& angles,
const Vector& origin,
const Vector pos[],
const Quaternion q[],
int iBone,
float flScale,
matrix3x4_t bonetoworld[MAXSTUDIOBONES],
int boneMask
);
// Get a bone->bone relative transform
void Studio_CalcBoneToBoneTransform( const CStudioHdr *pStudioHdr, int inputBoneIndex, int outputBoneIndex, matrix3x4_t &matrixOut );
// Given a bone rotation value, figures out the value you need to give to the controller
// to have the bone at that value.
// [in] flValue = the desired bone rotation value
// [out] ctlValue = the (0-1) value to set the controller t.
// return value = flValue, unwrapped to lie between the controller's start and end.
float Studio_SetController( const CStudioHdr *pStudioHdr, int iController, float flValue, float &ctlValue );
// Given a 0-1 controller value, maps it into the controller's start and end and returns the bone rotation angle.
// [in] ctlValue = value in controller space (0-1).
// return value = value in bone space
float Studio_GetController( const CStudioHdr *pStudioHdr, int iController, float ctlValue );
void Studio_CalcDefaultPoseParameters( const CStudioHdr *pStudioHdr, float flPoseParameter[MAXSTUDIOPOSEPARAM], int nCount );
float Studio_GetPoseParameter( const CStudioHdr *pStudioHdr, int iParameter, float ctlValue );
float Studio_SetPoseParameter( const CStudioHdr *pStudioHdr, int iParameter, float flValue, float &ctlValue );
// converts a global 0..1 pose parameter into the local sequences blending value
void Studio_LocalPoseParameter( const CStudioHdr *pStudioHdr, const float poseParameter[], mstudioseqdesc_t &seqdesc, int iSequence, int iLocalIndex, float &flSetting, int &index );
void Studio_SeqAnims( const CStudioHdr *pStudioHdr, mstudioseqdesc_t &seqdesc, int iSequence, const float poseParameter[], mstudioanimdesc_t *panim[4], float *weight );
int Studio_MaxFrame( const CStudioHdr *pStudioHdr, int iSequence, const float poseParameter[] );
float Studio_FPS( const CStudioHdr *pStudioHdr, int iSequence, const float poseParameter[] );
float Studio_CPS( const CStudioHdr *pStudioHdr, mstudioseqdesc_t &seqdesc, int iSequence, const float poseParameter[] );
float Studio_Duration( const CStudioHdr *pStudioHdr, int iSequence, const float poseParameter[] );
void Studio_MovementRate( const CStudioHdr *pStudioHdr, int iSequence, const float poseParameter[], Vector *pVec );
// void Studio_Movement( const CStudioHdr *pStudioHdr, int iSequence, const float poseParameter[], Vector *pVec );
//void Studio_AnimPosition( mstudioanimdesc_t *panim, float flCycle, Vector &vecPos, Vector &vecAngle );
//void Studio_AnimVelocity( mstudioanimdesc_t *panim, float flCycle, Vector &vecVelocity );
//float Studio_FindAnimDistance( mstudioanimdesc_t *panim, float flDist );
bool Studio_AnimMovement( mstudioanimdesc_t *panim, float flCycleFrom, float flCycleTo, Vector &deltaPos, QAngle &deltaAngle );
bool Studio_SeqMovement( const CStudioHdr *pStudioHdr, int iSequence, float flCycleFrom, float flCycleTo, const float poseParameter[], Vector &deltaMovement, QAngle &deltaAngle );
bool Studio_SeqVelocity( const CStudioHdr *pStudioHdr, int iSequence, float flCycle, const float poseParameter[], Vector &vecVelocity );
float Studio_FindSeqDistance( const CStudioHdr *pStudioHdr, int iSequence, const float poseParameter[], float flDist );
float Studio_FindSeqVelocity( const CStudioHdr *pStudioHdr, int iSequence, const float poseParameter[], float flVelocity );
int Studio_FindAttachment( const CStudioHdr *pStudioHdr, const char *pAttachmentName );
int Studio_FindRandomAttachment( const CStudioHdr *pStudioHdr, const char *pAttachmentName );
int Studio_BoneIndexByName( const CStudioHdr *pStudioHdr, const char *pName );
const char *Studio_GetDefaultSurfaceProps( CStudioHdr *pstudiohdr );
float Studio_GetMass( CStudioHdr *pstudiohdr );
const char *Studio_GetKeyValueText( const CStudioHdr *pStudioHdr, int iSequence );
FORWARD_DECLARE_HANDLE( memhandle_t );
struct bonecacheparams_t
{
CStudioHdr *pStudioHdr;
matrix3x4_t *pBoneToWorld;
float curtime;
int boneMask;
};
class CBoneCache
{
public:
// you must implement these static functions for the ResourceManager
// -----------------------------------------------------------
static CBoneCache *CreateResource( const bonecacheparams_t ¶ms );
static unsigned int EstimatedSize( const bonecacheparams_t ¶ms );
// -----------------------------------------------------------
// member functions that must be present for the ResourceManager
void DestroyResource();
CBoneCache *GetData() { return this; }
unsigned int Size() { return m_size; }
// -----------------------------------------------------------
CBoneCache();
// was constructor, but placement new is messy wrt memdebug - so cast & init instead
void Init( const bonecacheparams_t ¶ms, unsigned int size, short *pStudioToCached, short *pCachedToStudio, int cachedBoneCount );
void UpdateBones( const matrix3x4_t *pBoneToWorld, int numbones, float curtime );
matrix3x4_t *GetCachedBone( int studioIndex );
void ReadCachedBones( matrix3x4_t *pBoneToWorld );
void ReadCachedBonePointers( matrix3x4_t **bones, int numbones );
bool IsValid( float curtime, float dt = 0.1f );
public:
float m_timeValid;
int m_boneMask;
private:
matrix3x4_t *BoneArray();
short *StudioToCached();
short *CachedToStudio();
unsigned int m_size;
unsigned short m_cachedBoneCount;
unsigned short m_matrixOffset;
unsigned short m_cachedToStudioOffset;
unsigned short m_boneOutOffset;
};
CBoneCache *Studio_GetBoneCache( memhandle_t cacheHandle );
memhandle_t Studio_CreateBoneCache( bonecacheparams_t ¶ms );
void Studio_DestroyBoneCache( memhandle_t cacheHandle );
void Studio_InvalidateBoneCache( memhandle_t cacheHandle );
// Given a ray, trace for an intersection with this studiomodel. Get the array of bones from StudioSetupHitboxBones
bool TraceToStudio( class IPhysicsSurfaceProps *pProps, const Ray_t& ray, CStudioHdr *pStudioHdr, mstudiohitboxset_t *set, matrix3x4_t **hitboxbones, int fContentsMask, const Vector &vecOrigin, float flScale, trace_t &trace );
void QuaternionSM( float s, const Quaternion &p, const Quaternion &q, Quaternion &qt );
void QuaternionMA( const Quaternion &p, float s, const Quaternion &q, Quaternion &qt );
bool Studio_PrefetchSequence( const CStudioHdr *pStudioHdr, int iSequence );
void Studio_RunBoneFlexDrivers( float *pFlexController, const CStudioHdr *pStudioHdr, const Vector *pPositions, const matrix3x4_t *pBoneToWorld, const matrix3x4_t &mRootToWorld );
#endif // BONE_SETUP_H