-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathbone_setup.cpp
6048 lines (5081 loc) · 167 KB
/
bone_setup.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
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
// $NoKeywords: $
//
//===========================================================================//
#include "tier0/dbg.h"
#include "mathlib/mathlib.h"
#include "bone_setup.h"
#include <string.h>
#include "collisionutils.h"
#include "vstdlib/random.h"
#include "tier0/vprof.h"
#include "bone_accessor.h"
#include "mathlib/ssequaternion.h"
#include "bitvec.h"
#include "datamanager.h"
#include "convar.h"
#include "tier0/tslist.h"
#include "vphysics_interface.h"
#ifdef CLIENT_DLL
#include "posedebugger.h"
#endif
// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"
class CBoneSetup
{
public:
CBoneSetup( const CStudioHdr *pStudioHdr, int boneMask, const float poseParameter[], IPoseDebugger *pPoseDebugger = NULL );
void InitPose( Vector pos[], Quaternion q[] );
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 );
private:
void AddSequenceLayers( Vector pos[], Quaternion q[], mstudioseqdesc_t &seqdesc, int sequence, float cycle, float flWeight, float flTime, CIKContext *pIKContext );
void AddLocalLayers( Vector pos[], Quaternion q[], mstudioseqdesc_t &seqdesc, int sequence, float cycle, float flWeight, float flTime, CIKContext *pIKContext );
public:
const CStudioHdr *m_pStudioHdr;
int m_boneMask;
const float *m_flPoseParameter;
IPoseDebugger *m_pPoseDebugger;
};
// -----------------------------------------------------------------
template <typename T>
class CBoneSetupMemoryPool
{
public:
T *Alloc()
{
T *p = (T *)m_FreeBlocks.Pop();
if ( !p )
{
p = new T[MAXSTUDIOBONES];
if ( ((size_t)p) % TSLIST_NODE_ALIGNMENT != 0 )
{
DebuggerBreak();
}
}
return p;
}
void Free( T *p )
{
m_FreeBlocks.Push( (TSLNodeBase_t *)p );
}
private:
CTSListBase m_FreeBlocks;
};
CBoneSetupMemoryPool<Quaternion> g_QaternionPool;
CBoneSetupMemoryPool<Vector> g_VectorPool;
CBoneSetupMemoryPool<matrix3x4_t> g_MatrixPool;
// -----------------------------------------------------------------
CBoneCache *CBoneCache::CreateResource( const bonecacheparams_t ¶ms )
{
short studioToCachedIndex[MAXSTUDIOBONES];
short cachedToStudioIndex[MAXSTUDIOBONES];
int cachedBoneCount = 0;
for ( int i = 0; i < params.pStudioHdr->numbones(); i++ )
{
// skip bones that aren't part of the boneMask (and aren't the root bone)
if (i != 0 && !(params.pStudioHdr->boneFlags(i) & params.boneMask))
{
studioToCachedIndex[i] = -1;
continue;
}
studioToCachedIndex[i] = cachedBoneCount;
cachedToStudioIndex[cachedBoneCount] = i;
cachedBoneCount++;
}
int tableSizeStudio = sizeof(short) * params.pStudioHdr->numbones();
int tableSizeCached = sizeof(short) * cachedBoneCount;
int matrixSize = sizeof(matrix3x4_t) * cachedBoneCount;
int size = ( sizeof(CBoneCache) + tableSizeStudio + tableSizeCached + matrixSize + 3 ) & ~3;
CBoneCache *pMem = (CBoneCache *)malloc( size );
Construct( pMem );
pMem->Init( params, size, studioToCachedIndex, cachedToStudioIndex, cachedBoneCount );
return pMem;
}
unsigned int CBoneCache::EstimatedSize( const bonecacheparams_t ¶ms )
{
// conservative estimate - max size
return ( params.pStudioHdr->numbones() * (sizeof(short) + sizeof(short) + sizeof(matrix3x4_t)) + 3 ) & ~3;
}
void CBoneCache::DestroyResource()
{
free( this );
}
CBoneCache::CBoneCache()
{
m_size = 0;
m_cachedBoneCount = 0;
}
void CBoneCache::Init( const bonecacheparams_t ¶ms, unsigned int size, short *pStudioToCached, short *pCachedToStudio, int cachedBoneCount )
{
m_cachedBoneCount = cachedBoneCount;
m_size = size;
m_timeValid = params.curtime;
m_boneMask = params.boneMask;
int studioTableSize = params.pStudioHdr->numbones() * sizeof(short);
m_cachedToStudioOffset = studioTableSize;
memcpy( StudioToCached(), pStudioToCached, studioTableSize );
int cachedTableSize = cachedBoneCount * sizeof(short);
memcpy( CachedToStudio(), pCachedToStudio, cachedTableSize );
m_matrixOffset = ( m_cachedToStudioOffset + cachedTableSize + 3 ) & ~3;
UpdateBones( params.pBoneToWorld, params.pStudioHdr->numbones(), params.curtime );
}
void CBoneCache::UpdateBones( const matrix3x4_t *pBoneToWorld, int numbones, float curtime )
{
matrix3x4_t *pBones = BoneArray();
const short *pCachedToStudio = CachedToStudio();
for ( int i = 0; i < m_cachedBoneCount; i++ )
{
int index = pCachedToStudio[i];
MatrixCopy( pBoneToWorld[index], pBones[i] );
}
m_timeValid = curtime;
}
matrix3x4_t *CBoneCache::GetCachedBone( int studioIndex )
{
int cachedIndex = StudioToCached()[studioIndex];
if ( cachedIndex >= 0 )
{
return BoneArray() + cachedIndex;
}
return NULL;
}
void CBoneCache::ReadCachedBones( matrix3x4_t *pBoneToWorld )
{
matrix3x4_t *pBones = BoneArray();
const short *pCachedToStudio = CachedToStudio();
for ( int i = 0; i < m_cachedBoneCount; i++ )
{
MatrixCopy( pBones[i], pBoneToWorld[pCachedToStudio[i]] );
}
}
void CBoneCache::ReadCachedBonePointers( matrix3x4_t **bones, int numbones )
{
memset( bones, 0, sizeof(matrix3x4_t *) * numbones );
matrix3x4_t *pBones = BoneArray();
const short *pCachedToStudio = CachedToStudio();
for ( int i = 0; i < m_cachedBoneCount; i++ )
{
bones[pCachedToStudio[i]] = pBones + i;
}
}
bool CBoneCache::IsValid( float curtime, float dt )
{
if ( curtime - m_timeValid <= dt )
return true;
return false;
}
// private functions
matrix3x4_t *CBoneCache::BoneArray()
{
return (matrix3x4_t *)( (char *)(this+1) + m_matrixOffset );
}
short *CBoneCache::StudioToCached()
{
return (short *)( (char *)(this+1) );
}
short *CBoneCache::CachedToStudio()
{
return (short *)( (char *)(this+1) + m_cachedToStudioOffset );
}
// Construct a singleton
static CDataManager<CBoneCache, bonecacheparams_t, CBoneCache *, CThreadFastMutex> g_StudioBoneCache( 128 * 1024L );
CBoneCache *Studio_GetBoneCache( memhandle_t cacheHandle )
{
AUTO_LOCK( g_StudioBoneCache.AccessMutex() );
return g_StudioBoneCache.GetResource_NoLock( cacheHandle );
}
memhandle_t Studio_CreateBoneCache( bonecacheparams_t ¶ms )
{
AUTO_LOCK( g_StudioBoneCache.AccessMutex() );
return g_StudioBoneCache.CreateResource( params );
}
void Studio_DestroyBoneCache( memhandle_t cacheHandle )
{
AUTO_LOCK( g_StudioBoneCache.AccessMutex() );
g_StudioBoneCache.DestroyResource( cacheHandle );
}
void Studio_InvalidateBoneCache( memhandle_t cacheHandle )
{
AUTO_LOCK( g_StudioBoneCache.AccessMutex() );
CBoneCache *pCache = g_StudioBoneCache.GetResource_NoLock( cacheHandle );
if ( pCache )
{
pCache->m_timeValid = -1.0f;
}
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void BuildBoneChain(
const CStudioHdr *pStudioHdr,
const matrix3x4_t &rootxform,
const Vector pos[],
const Quaternion q[],
int iBone,
matrix3x4_t *pBoneToWorld )
{
CBoneBitList boneComputed;
BuildBoneChain( pStudioHdr, rootxform, pos, q, iBone, pBoneToWorld, boneComputed );
return;
}
//-----------------------------------------------------------------------------
// Purpose: return a sub frame rotation for a single bone
//-----------------------------------------------------------------------------
void ExtractAnimValue( int frame, mstudioanimvalue_t *panimvalue, float scale, float &v1, float &v2 )
{
if ( !panimvalue )
{
v1 = v2 = 0;
return;
}
// Avoids a crash reading off the end of the data
// There is probably a better long-term solution; Ken is going to look into it.
if ( ( panimvalue->num.total == 1 ) && ( panimvalue->num.valid == 1 ) )
{
v1 = v2 = panimvalue[1].value * scale;
return;
}
int k = frame;
// find the data list that has the frame
while (panimvalue->num.total <= k)
{
k -= panimvalue->num.total;
panimvalue += panimvalue->num.valid + 1;
if ( panimvalue->num.total == 0 )
{
Assert( 0 ); // running off the end of the animation stream is bad
v1 = v2 = 0;
return;
}
}
if (panimvalue->num.valid > k)
{
// has valid animation data
v1 = panimvalue[k+1].value * scale;
if (panimvalue->num.valid > k + 1)
{
// has valid animation blend data
v2 = panimvalue[k+2].value * scale;
}
else
{
if (panimvalue->num.total > k + 1)
{
// data repeats, no blend
v2 = v1;
}
else
{
// pull blend from first data block in next list
v2 = panimvalue[panimvalue->num.valid+2].value * scale;
}
}
}
else
{
// get last valid data block
v1 = panimvalue[panimvalue->num.valid].value * scale;
if (panimvalue->num.total > k + 1)
{
// data repeats, no blend
v2 = v1;
}
else
{
// pull blend from first data block in next list
v2 = panimvalue[panimvalue->num.valid + 2].value * scale;
}
}
}
void ExtractAnimValue( int frame, mstudioanimvalue_t *panimvalue, float scale, float &v1 )
{
if ( !panimvalue )
{
v1 = 0;
return;
}
int k = frame;
while (panimvalue->num.total <= k)
{
k -= panimvalue->num.total;
panimvalue += panimvalue->num.valid + 1;
if ( panimvalue->num.total == 0 )
{
Assert( 0 ); // running off the end of the animation stream is bad
v1 = 0;
return;
}
}
if (panimvalue->num.valid > k)
{
v1 = panimvalue[k+1].value * scale;
}
else
{
// get last valid data block
v1 = panimvalue[panimvalue->num.valid].value * scale;
}
}
//-----------------------------------------------------------------------------
// Purpose: return a sub frame rotation for a single bone
//-----------------------------------------------------------------------------
void CalcBoneQuaternion( int frame, float s,
const Quaternion &baseQuat, const RadianEuler &baseRot, const Vector &baseRotScale,
int iBaseFlags, const Quaternion &baseAlignment,
const mstudioanim_t *panim, Quaternion &q )
{
if ( panim->flags & STUDIO_ANIM_RAWROT )
{
q = *(panim->pQuat48());
Assert( q.IsValid() );
return;
}
if ( panim->flags & STUDIO_ANIM_RAWROT2 )
{
q = *(panim->pQuat64());
Assert( q.IsValid() );
return;
}
if ( !(panim->flags & STUDIO_ANIM_ANIMROT) )
{
if (panim->flags & STUDIO_ANIM_DELTA)
{
q.Init( 0.0f, 0.0f, 0.0f, 1.0f );
}
else
{
q = baseQuat;
}
return;
}
mstudioanim_valueptr_t *pValuesPtr = panim->pRotV();
if (s > 0.001f)
{
QuaternionAligned q1, q2;
RadianEuler angle1, angle2;
ExtractAnimValue( frame, pValuesPtr->pAnimvalue( 0 ), baseRotScale.x, angle1.x, angle2.x );
ExtractAnimValue( frame, pValuesPtr->pAnimvalue( 1 ), baseRotScale.y, angle1.y, angle2.y );
ExtractAnimValue( frame, pValuesPtr->pAnimvalue( 2 ), baseRotScale.z, angle1.z, angle2.z );
if (!(panim->flags & STUDIO_ANIM_DELTA))
{
angle1.x = angle1.x + baseRot.x;
angle1.y = angle1.y + baseRot.y;
angle1.z = angle1.z + baseRot.z;
angle2.x = angle2.x + baseRot.x;
angle2.y = angle2.y + baseRot.y;
angle2.z = angle2.z + baseRot.z;
}
Assert( angle1.IsValid() && angle2.IsValid() );
if (angle1.x != angle2.x || angle1.y != angle2.y || angle1.z != angle2.z)
{
AngleQuaternion( angle1, q1 );
AngleQuaternion( angle2, q2 );
#ifdef _X360
fltx4 q1simd, q2simd, qsimd;
q1simd = LoadAlignedSIMD( q1 );
q2simd = LoadAlignedSIMD( q2 );
qsimd = QuaternionBlendSIMD( q1simd, q2simd, s );
StoreUnalignedSIMD( q.Base(), qsimd );
#else
QuaternionBlend( q1, q2, s, q );
#endif
}
else
{
AngleQuaternion( angle1, q );
}
}
else
{
RadianEuler angle;
ExtractAnimValue( frame, pValuesPtr->pAnimvalue( 0 ), baseRotScale.x, angle.x );
ExtractAnimValue( frame, pValuesPtr->pAnimvalue( 1 ), baseRotScale.y, angle.y );
ExtractAnimValue( frame, pValuesPtr->pAnimvalue( 2 ), baseRotScale.z, angle.z );
if (!(panim->flags & STUDIO_ANIM_DELTA))
{
angle.x = angle.x + baseRot.x;
angle.y = angle.y + baseRot.y;
angle.z = angle.z + baseRot.z;
}
Assert( angle.IsValid() );
AngleQuaternion( angle, q );
}
Assert( q.IsValid() );
// align to unified bone
if (!(panim->flags & STUDIO_ANIM_DELTA) && (iBaseFlags & BONE_FIXED_ALIGNMENT))
{
QuaternionAlign( baseAlignment, q, q );
}
}
inline void CalcBoneQuaternion( int frame, float s,
const mstudiobone_t *pBone,
const mstudiolinearbone_t *pLinearBones,
const mstudioanim_t *panim, Quaternion &q )
{
if (pLinearBones)
{
CalcBoneQuaternion( frame, s, pLinearBones->quat(panim->bone), pLinearBones->rot(panim->bone), pLinearBones->rotscale(panim->bone), pLinearBones->flags(panim->bone), pLinearBones->qalignment(panim->bone), panim, q );
}
else
{
CalcBoneQuaternion( frame, s, pBone->quat, pBone->rot, pBone->rotscale, pBone->flags, pBone->qAlignment, panim, q );
}
}
//-----------------------------------------------------------------------------
// Purpose: return a sub frame position for a single bone
//-----------------------------------------------------------------------------
void CalcBonePosition( int frame, float s,
const Vector &basePos, const Vector &baseBoneScale,
const mstudioanim_t *panim, Vector &pos )
{
if (panim->flags & STUDIO_ANIM_RAWPOS)
{
pos = *(panim->pPos());
Assert( pos.IsValid() );
return;
}
else if (!(panim->flags & STUDIO_ANIM_ANIMPOS))
{
if (panim->flags & STUDIO_ANIM_DELTA)
{
pos.Init( 0.0f, 0.0f, 0.0f );
}
else
{
pos = basePos;
}
return;
}
mstudioanim_valueptr_t *pPosV = panim->pPosV();
int j;
if (s > 0.001f)
{
float v1, v2;
for (j = 0; j < 3; j++)
{
ExtractAnimValue( frame, pPosV->pAnimvalue( j ), baseBoneScale[j], v1, v2 );
pos[j] = v1 * (1.0 - s) + v2 * s;
}
}
else
{
for (j = 0; j < 3; j++)
{
ExtractAnimValue( frame, pPosV->pAnimvalue( j ), baseBoneScale[j], pos[j] );
}
}
if (!(panim->flags & STUDIO_ANIM_DELTA))
{
pos.x = pos.x + basePos.x;
pos.y = pos.y + basePos.y;
pos.z = pos.z + basePos.z;
}
Assert( pos.IsValid() );
}
inline void CalcBonePosition( int frame, float s,
const mstudiobone_t *pBone,
const mstudiolinearbone_t *pLinearBones,
const mstudioanim_t *panim, Vector &pos )
{
if (pLinearBones)
{
CalcBonePosition( frame, s, pLinearBones->pos(panim->bone), pLinearBones->posscale(panim->bone), panim, pos );
}
else
{
CalcBonePosition( frame, s, pBone->pos, pBone->posscale, panim, pos );
}
}
void SetupSingleBoneMatrix(
CStudioHdr *pOwnerHdr,
int nSequence,
int iFrame,
int iBone,
matrix3x4_t &mBoneLocal )
{
mstudioseqdesc_t &seqdesc = pOwnerHdr->pSeqdesc( nSequence );
mstudioanimdesc_t &animdesc = pOwnerHdr->pAnimdesc( seqdesc.anim( 0, 0 ) );
int iLocalFrame = iFrame;
mstudioanim_t *panim = animdesc.pAnim( &iLocalFrame );
float s = 0;
mstudiobone_t *pbone = pOwnerHdr->pBone( iBone );
Quaternion boneQuat;
Vector bonePos;
// search for bone
while (panim && panim->bone != iBone)
{
panim = panim->pNext();
}
// look up animation if found, if not, initialize
if (panim && seqdesc.weight(iBone) > 0)
{
CalcBoneQuaternion( iLocalFrame, s, pbone, NULL, panim, boneQuat );
CalcBonePosition ( iLocalFrame, s, pbone, NULL, panim, bonePos );
}
else if (animdesc.flags & STUDIO_DELTA)
{
boneQuat.Init( 0.0f, 0.0f, 0.0f, 1.0f );
bonePos.Init( 0.0f, 0.0f, 0.0f );
}
else
{
boneQuat = pbone->quat;
bonePos = pbone->pos;
}
QuaternionMatrix( boneQuat, bonePos, mBoneLocal );
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
static void CalcDecompressedAnimation( const mstudiocompressedikerror_t *pCompressed, int iFrame, float fraq, Vector &pos, Quaternion &q )
{
if (fraq > 0.0001f)
{
Vector p1, p2;
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 0 ), pCompressed->scale[0], p1.x, p2.x );
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 1 ), pCompressed->scale[1], p1.y, p2.y );
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 2 ), pCompressed->scale[2], p1.z, p2.z );
pos = p1 * (1 - fraq) + p2 * fraq;
Quaternion q1, q2;
RadianEuler angle1, angle2;
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 3 ), pCompressed->scale[3], angle1.x, angle2.x );
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 4 ), pCompressed->scale[4], angle1.y, angle2.y );
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 5 ), pCompressed->scale[5], angle1.z, angle2.z );
if (angle1.x != angle2.x || angle1.y != angle2.y || angle1.z != angle2.z)
{
AngleQuaternion( angle1, q1 );
AngleQuaternion( angle2, q2 );
QuaternionBlend( q1, q2, fraq, q );
}
else
{
AngleQuaternion( angle1, q );
}
}
else
{
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 0 ), pCompressed->scale[0], pos.x );
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 1 ), pCompressed->scale[1], pos.y );
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 2 ), pCompressed->scale[2], pos.z );
RadianEuler angle;
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 3 ), pCompressed->scale[3], angle.x );
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 4 ), pCompressed->scale[4], angle.y );
ExtractAnimValue( iFrame, pCompressed->pAnimvalue( 5 ), pCompressed->scale[5], angle.z );
AngleQuaternion( angle, q );
}
}
//-----------------------------------------------------------------------------
// Purpose: translate animations done in a non-standard parent space
//-----------------------------------------------------------------------------
static void CalcLocalHierarchyAnimation(
const CStudioHdr *pStudioHdr,
matrix3x4_t *boneToWorld,
CBoneBitList &boneComputed,
Vector *pos,
Quaternion *q,
//const mstudioanimdesc_t &animdesc,
const mstudiobone_t *pbone,
mstudiolocalhierarchy_t *pHierarchy,
int iBone,
int iNewParent,
float cycle,
int iFrame,
float flFraq,
int boneMask
)
{
#ifdef STAGING_ONLY
Assert( iNewParent == -1 || (iNewParent >= 0 && iNewParent < MAXSTUDIOBONES) );
Assert( iBone > 0 );
Assert( iBone < MAXSTUDIOBONES );
#endif // STAGING_ONLY
Vector localPos;
Quaternion localQ;
// make fake root transform
static ALIGN16 matrix3x4_t rootXform ALIGN16_POST ( 1.0f, 0, 0, 0, 0, 1.0f, 0, 0, 0, 0, 1.0f, 0 );
// FIXME: missing check to see if seq has a weight for this bone
float weight = 1.0f;
// check to see if there's a ramp on the influence
if ( pHierarchy->tail - pHierarchy->peak < 1.0f )
{
float index = cycle;
if (pHierarchy->end > 1.0f && index < pHierarchy->start)
index += 1.0f;
if (index < pHierarchy->start)
return;
if (index >= pHierarchy->end)
return;
if (index < pHierarchy->peak && pHierarchy->start != pHierarchy->peak)
{
weight = (index - pHierarchy->start) / (pHierarchy->peak - pHierarchy->start);
}
else if (index > pHierarchy->tail && pHierarchy->end != pHierarchy->tail)
{
weight = (pHierarchy->end - index) / (pHierarchy->end - pHierarchy->tail);
}
weight = SimpleSpline( weight );
}
CalcDecompressedAnimation( pHierarchy->pLocalAnim(), iFrame - pHierarchy->iStart, flFraq, localPos, localQ );
BuildBoneChain( pStudioHdr, rootXform, pos, q, iBone, boneToWorld, boneComputed );
matrix3x4_t localXform;
AngleMatrix( localQ, localPos, localXform );
if ( iNewParent != -1 )
{
BuildBoneChain( pStudioHdr, rootXform, pos, q, iNewParent, boneToWorld, boneComputed );
ConcatTransforms( boneToWorld[iNewParent], localXform, boneToWorld[iBone] );
}
else
{
boneToWorld[iBone] = localXform;
}
// back solve
Vector p1;
Quaternion q1;
int n = pbone[iBone].parent;
if (n == -1)
{
if (weight == 1.0f)
{
MatrixAngles( boneToWorld[iBone], q[iBone], pos[iBone] );
}
else
{
MatrixAngles( boneToWorld[iBone], q1, p1 );
QuaternionSlerp( q[iBone], q1, weight, q[iBone] );
pos[iBone] = Lerp( weight, p1, pos[iBone] );
}
}
else
{
matrix3x4_t worldToBone;
MatrixInvert( boneToWorld[n], worldToBone );
matrix3x4_t local;
ConcatTransforms( worldToBone, boneToWorld[iBone], local );
if (weight == 1.0f)
{
MatrixAngles( local, q[iBone], pos[iBone] );
}
else
{
MatrixAngles( local, q1, p1 );
QuaternionSlerp( q[iBone], q1, weight, q[iBone] );
pos[iBone] = Lerp( weight, p1, pos[iBone] );
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Calc Zeroframe Data
//-----------------------------------------------------------------------------
static void CalcZeroframeData( const CStudioHdr *pStudioHdr, const studiohdr_t *pAnimStudioHdr, const virtualgroup_t *pAnimGroup, const mstudiobone_t *pAnimbone, mstudioanimdesc_t &animdesc, float fFrame, Vector *pos, Quaternion *q, int boneMask, float flWeight )
{
byte *pData = animdesc.pZeroFrameData();
if (!pData)
return;
int i, j;
// Msg("zeroframe %s\n", animdesc.pszName() );
if (animdesc.zeroframecount == 1)
{
for (j = 0; j < pAnimStudioHdr->numbones; j++)
{
if (pAnimGroup)
i = pAnimGroup->masterBone[j];
else
i = j;
if (pAnimbone[j].flags & BONE_HAS_SAVEFRAME_POS)
{
if ((i >= 0) && (pStudioHdr->boneFlags(i) & boneMask))
{
Vector p = *(Vector48 *)pData;
pos[i] = pos[i] * (1.0f - flWeight) + p * flWeight;
Assert( pos[i].IsValid() );
}
pData += sizeof( Vector48 );
}
if (pAnimbone[j].flags & BONE_HAS_SAVEFRAME_ROT)
{
if ((i >= 0) && (pStudioHdr->boneFlags(i) & boneMask))
{
Quaternion q0 = *(Quaternion64 *)pData;
QuaternionBlend( q[i], q0, flWeight, q[i] );
Assert( q[i].IsValid() );
}
pData += sizeof( Quaternion64 );
}
}
}
else
{
float s1;
int index = fFrame / animdesc.zeroframespan;
if (index >= animdesc.zeroframecount - 1)
{
index = animdesc.zeroframecount - 2;
s1 = 1.0f;
}
else
{
s1 = clamp( (fFrame - index * animdesc.zeroframespan) / animdesc.zeroframespan, 0.0f, 1.0f );
}
int i0 = max( index - 1, 0 );
int i1 = index;
int i2 = min( index + 1, animdesc.zeroframecount - 1 );
for (j = 0; j < pAnimStudioHdr->numbones; j++)
{
if (pAnimGroup)
i = pAnimGroup->masterBone[j];
else
i = j;
if (pAnimbone[j].flags & BONE_HAS_SAVEFRAME_POS)
{
if ((i >= 0) && (pStudioHdr->boneFlags(i) & boneMask))
{
Vector p0 = *(((Vector48 *)pData) + i0);
Vector p1 = *(((Vector48 *)pData) + i1);
Vector p2 = *(((Vector48 *)pData) + i2);
Vector p3;
Hermite_Spline( p0, p1, p2, s1, p3 );
pos[i] = pos[i] * (1.0f - flWeight) + p3 * flWeight;
Assert( pos[i].IsValid() );
}
pData += sizeof( Vector48 ) * animdesc.zeroframecount;
}
if (pAnimbone[j].flags & BONE_HAS_SAVEFRAME_ROT)
{
if ((i >= 0) && (pStudioHdr->boneFlags(i) & boneMask))
{
Quaternion q0 = *(((Quaternion64 *)pData) + i0);
Quaternion q1 = *(((Quaternion64 *)pData) + i1);
Quaternion q2 = *(((Quaternion64 *)pData) + i2);
if (flWeight == 1.0f)
{
Hermite_Spline( q0, q1, q2, s1, q[i] );
}
else
{
Quaternion q3;
Hermite_Spline( q0, q1, q2, s1, q3 );
QuaternionBlend( q[i], q3, flWeight, q[i] );
}
Assert( q[i].IsValid() );
}
pData += sizeof( Quaternion64 ) * animdesc.zeroframecount;
}
}
}
}
//-----------------------------------------------------------------------------
// Purpose: Find and decode a sub-frame of animation, remapping the skeleton bone indexes
//-----------------------------------------------------------------------------
static void CalcVirtualAnimation( virtualmodel_t *pVModel, const CStudioHdr *pStudioHdr, Vector *pos, Quaternion *q,
mstudioseqdesc_t &seqdesc, int sequence, int animation,
float cycle, int boneMask )
{
int i, j, k;
const mstudiobone_t *pbone;
const virtualgroup_t *pSeqGroup;
const studiohdr_t *pSeqStudioHdr;
const mstudiolinearbone_t *pSeqLinearBones;
const mstudiobone_t *pSeqbone;
const mstudioanim_t *panim;
const studiohdr_t *pAnimStudioHdr;
const mstudiolinearbone_t *pAnimLinearBones;
const mstudiobone_t *pAnimbone;
const virtualgroup_t *pAnimGroup;
pSeqGroup = pVModel->pSeqGroup( sequence );
int baseanimation = pStudioHdr->iRelativeAnim( sequence, animation );
mstudioanimdesc_t &animdesc = ((CStudioHdr *)pStudioHdr)->pAnimdesc( baseanimation );
pSeqStudioHdr = ((CStudioHdr *)pStudioHdr)->pSeqStudioHdr( sequence );
pSeqLinearBones = pSeqStudioHdr->pLinearBones();
pSeqbone = pSeqStudioHdr->pBone( 0 );
pAnimGroup = pVModel->pAnimGroup( baseanimation );
pAnimStudioHdr = ((CStudioHdr *)pStudioHdr)->pAnimStudioHdr( baseanimation );
pAnimLinearBones = pAnimStudioHdr->pLinearBones();
pAnimbone = pAnimStudioHdr->pBone( 0 );
int iFrame;
float s;
float fFrame = cycle * (animdesc.numframes - 1);
iFrame = (int)fFrame;
s = (fFrame - iFrame);
int iLocalFrame = iFrame;
float flStall;
panim = animdesc.pAnim( &iLocalFrame, flStall );
float *pweight = seqdesc.pBoneweight( 0 );
pbone = pStudioHdr->pBone( 0 );
for (i = 0; i < pStudioHdr->numbones(); i++)
{
if (pStudioHdr->boneFlags(i) & boneMask)
{
int j = pSeqGroup->boneMap[i];
if (j >= 0 && pweight[j] > 0.0f)
{
if (animdesc.flags & STUDIO_DELTA)
{
q[i].Init( 0.0f, 0.0f, 0.0f, 1.0f );
pos[i].Init( 0.0f, 0.0f, 0.0f );
}
else if (pSeqLinearBones)
{
q[i] = pSeqLinearBones->quat(j);
pos[i] = pSeqLinearBones->pos(j);
}
else
{
q[i] = pSeqbone[j].quat;
pos[i] = pSeqbone[j].pos;
}
#ifdef STUDIO_ENABLE_PERF_COUNTERS
pStudioHdr->m_nPerfUsedBones++;
#endif
}
}
}
// if the animation isn't available, look for the zero frame cache
if (!panim)
{
CalcZeroframeData( ((CStudioHdr *)pStudioHdr), pAnimStudioHdr, pAnimGroup, pAnimbone, animdesc, fFrame, pos, q, boneMask, 1.0 );
return;
}
// FIXME: change encoding so that bone -1 is never the case
while (panim && panim->bone < 255)
{
j = pAnimGroup->masterBone[panim->bone];
if ( j >= 0 && ( pStudioHdr->boneFlags(j) & boneMask ) )
{
k = pSeqGroup->boneMap[j];
if (k >= 0 && pweight[k] > 0.0f)
{
CalcBoneQuaternion( iLocalFrame, s, &pAnimbone[panim->bone], pAnimLinearBones, panim, q[j] );
CalcBonePosition ( iLocalFrame, s, &pAnimbone[panim->bone], pAnimLinearBones, panim, pos[j] );
#ifdef STUDIO_ENABLE_PERF_COUNTERS
pStudioHdr->m_nPerfAnimatedBones++;
#endif
}
}
panim = panim->pNext();
}
// cross fade in previous zeroframe data
if (flStall > 0.0f)
{
CalcZeroframeData( pStudioHdr, pAnimStudioHdr, pAnimGroup, pAnimbone, animdesc, fFrame, pos, q, boneMask, flStall );
}
// calculate a local hierarchy override
if (animdesc.numlocalhierarchy)
{
matrix3x4_t *boneToWorld = g_MatrixPool.Alloc();
CBoneBitList boneComputed;
int i;