forked from JACoders/OpenJK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathG2_bones.cpp
4889 lines (4269 loc) · 140 KB
/
G2_bones.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 (C) 2000 - 2013, Raven Software, Inc.
Copyright (C) 2001 - 2013, Activision, Inc.
Copyright (C) 2013 - 2015, OpenJK contributors
This file is part of the OpenJK source code.
OpenJK is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/
#include "qcommon/matcomp.h"
#include "ghoul2/G2.h"
#include "ghoul2/g2_local.h"
//rww - RAGDOLL_BEGIN
#ifndef __linux__
#include <float.h>
#else
#include <math.h>
#endif
#include "ghoul2/G2_gore.h"
#include "tr_local.h"
//#define RAG_TRACE_DEBUG_LINES
#include "client/client.h" //while this is all "shared" code, there are some places where we want to make cgame callbacks (for ragdoll) only if the cgvm exists
//rww - RAGDOLL_END
//=====================================================================================================================
// Bone List handling routines - so entities can override bone info on a bone by bone level, and also interrogate this info
// Given a bone name, see if that bone is already in our bone list - note the model_t pointer that gets passed in here MUST point at the
// gla file, not the glm file type.
int G2_Find_Bone(const model_t *mod, boneInfo_v &blist, const char *boneName)
{
mdxaSkel_t *skel;
mdxaSkelOffsets_t *offsets;
offsets = (mdxaSkelOffsets_t *)((byte *)mod->mdxa + sizeof(mdxaHeader_t));
skel = (mdxaSkel_t *)((byte *)mod->mdxa + sizeof(mdxaHeader_t) + offsets->offsets[0]);
// look through entire list
for(size_t i=0; i<blist.size(); i++)
{
// if this bone entry has no info in it, bounce over it
if (blist[i].boneNumber == -1)
{
continue;
}
// figure out what skeletal info structure this bone entry is looking at
skel = (mdxaSkel_t *)((byte *)mod->mdxa + sizeof(mdxaHeader_t) + offsets->offsets[blist[i].boneNumber]);
// if name is the same, we found it
if (!Q_stricmp(skel->name, boneName))
{
return i;
}
}
// didn't find it
return -1;
}
// we need to add a bone to the list - find a free one and see if we can find a corresponding bone in the gla file
int G2_Add_Bone (const model_t *mod, boneInfo_v &blist, const char *boneName)
{
int x;
mdxaSkel_t *skel;
mdxaSkelOffsets_t *offsets;
boneInfo_t tempBone;
//rww - RAGDOLL_BEGIN
memset(&tempBone, 0, sizeof(tempBone));
//rww - RAGDOLL_END
offsets = (mdxaSkelOffsets_t *)((byte *)mod->mdxa + sizeof(mdxaHeader_t));
// walk the entire list of bones in the gla file for this model and see if any match the name of the bone we want to find
for (x=0; x< mod->mdxa->numBones; x++)
{
skel = (mdxaSkel_t *)((byte *)mod->mdxa + sizeof(mdxaHeader_t) + offsets->offsets[x]);
// if name is the same, we found it
if (!Q_stricmp(skel->name, boneName))
{
break;
}
}
// check to see we did actually make a match with a bone in the model
if (x == mod->mdxa->numBones)
{
// didn't find it? Error
//assert(0);
#ifdef _DEBUG
ri.Printf( PRINT_ALL, "WARNING: Failed to add bone %s\n", boneName);
#endif
#ifdef _RAG_PRINT_TEST
ri.Printf( PRINT_ALL, "WARNING: Failed to add bone %s\n", boneName);
#endif
return -1;
}
// look through entire list - see if it's already there first
for(size_t i=0; i<blist.size(); i++)
{
// if this bone entry has info in it, bounce over it
if (blist[i].boneNumber != -1)
{
skel = (mdxaSkel_t *)((byte *)mod->mdxa + sizeof(mdxaHeader_t) + offsets->offsets[blist[i].boneNumber]);
// if name is the same, we found it
if (!Q_stricmp(skel->name, boneName))
{
return i;
}
}
else
{
// if we found an entry that had a -1 for the bonenumber, then we hit a bone slot that was empty
blist[i].boneNumber = x;
blist[i].flags = 0;
return i;
}
}
#ifdef _RAG_PRINT_TEST
ri.Printf( PRINT_ALL, "New bone added for %s\n", boneName);
#endif
// ok, we didn't find an existing bone of that name, or an empty slot. Lets add an entry
tempBone.boneNumber = x;
tempBone.flags = 0;
blist.push_back(tempBone);
return blist.size()-1;
}
// Given a model handle, and a bone name, we want to remove this bone from the bone override list
qboolean G2_Remove_Bone_Index ( boneInfo_v &blist, int index)
{
if (index != -1)
{
if (blist[index].flags & BONE_ANGLES_RAGDOLL)
{
return qtrue; // don't accept any calls on ragdoll bones
}
}
// did we find it?
if (index != -1)
{
// check the flags first - if it's still being used Do NOT remove it
if (!blist[index].flags)
{
// set this bone to not used
blist[index].boneNumber = -1;
unsigned int newSize = blist.size();
// now look through the list from the back and see if there is a block of -1's we can resize off the end of the list
for (int i=blist.size()-1; i>-1; i--)
{
if (blist[i].boneNumber == -1)
{
newSize = i;
}
// once we hit one that isn't a -1, we are done.
else
{
break;
}
}
// do we need to resize?
if (newSize != blist.size())
{
// yes, so lets do it
blist.resize(newSize);
}
return qtrue;
}
}
// assert(0);
// no
return qfalse;
}
// given a bone number, see if there is an override bone in the bone list
int G2_Find_Bone_In_List(boneInfo_v &blist, const int boneNum)
{
// look through entire list
for(size_t i=0; i<blist.size(); i++)
{
if (blist[i].boneNumber == boneNum)
{
return i;
}
}
return -1;
}
// given a model, bonelist and bonename, lets stop an anim if it's playing.
qboolean G2_Stop_Bone_Index( boneInfo_v &blist, int index, int flags)
{
// did we find it?
if (index != -1)
{
blist[index].flags &= ~(flags);
// try and remove this bone if we can
return G2_Remove_Bone_Index(blist, index);
}
assert(0);
return qfalse;
}
// generate a matrix for a given bone given some new angles for it.
void G2_Generate_Matrix(const model_t *mod, boneInfo_v &blist, int index, const float *angles, int flags,
const Eorientations up, const Eorientations left, const Eorientations forward)
{
mdxaSkel_t *skel;
mdxaSkelOffsets_t *offsets;
mdxaBone_t temp1;
mdxaBone_t permutation;
mdxaBone_t *boneOverride = &blist[index].matrix;
vec3_t newAngles;
if (flags & (BONE_ANGLES_PREMULT | BONE_ANGLES_POSTMULT))
{
// build us a matrix out of the angles we are fed - but swap y and z because of wacky Quake setup
vec3_t newAngles;
// determine what axis newAngles Yaw should revolve around
switch (up)
{
case NEGATIVE_X:
newAngles[1] = angles[2] + 180;
break;
case POSITIVE_X:
newAngles[1] = angles[2];
break;
case NEGATIVE_Y:
newAngles[1] = angles[0];
break;
case POSITIVE_Y:
newAngles[1] = angles[0];
break;
case NEGATIVE_Z:
newAngles[1] = angles[1] + 180;
break;
case POSITIVE_Z:
newAngles[1] = angles[1];
break;
default:
break;
}
// determine what axis newAngles pitch should revolve around
switch (left)
{
case NEGATIVE_X:
newAngles[0] = angles[2];
break;
case POSITIVE_X:
newAngles[0] = angles[2] + 180;
break;
case NEGATIVE_Y:
newAngles[0] = angles[0];
break;
case POSITIVE_Y:
newAngles[0] = angles[0] + 180;
break;
case NEGATIVE_Z:
newAngles[0] = angles[1];
break;
case POSITIVE_Z:
newAngles[0] = angles[1];
break;
default:
break;
}
// determine what axis newAngles Roll should revolve around
switch (forward)
{
case NEGATIVE_X:
newAngles[2] = angles[2];
break;
case POSITIVE_X:
newAngles[2] = angles[2];
break;
case NEGATIVE_Y:
newAngles[2] = angles[0];
break;
case POSITIVE_Y:
newAngles[2] = angles[0] + 180;
break;
case NEGATIVE_Z:
newAngles[2] = angles[1];
break;
case POSITIVE_Z:
newAngles[2] = angles[1] + 180;
break;
default:
break;
}
Create_Matrix(newAngles, boneOverride);
// figure out where the bone hirearchy info is
offsets = (mdxaSkelOffsets_t *)((byte *)mod->mdxa + sizeof(mdxaHeader_t));
skel = (mdxaSkel_t *)((byte *)mod->mdxa + sizeof(mdxaHeader_t) + offsets->offsets[blist[index].boneNumber]);
Multiply_3x4Matrix(&temp1, boneOverride,&skel->BasePoseMatInv);
Multiply_3x4Matrix(boneOverride,&skel->BasePoseMat, &temp1);
}
else
{
VectorCopy(angles, newAngles);
// why I should need do this Fuck alone knows. But I do.
if (left == POSITIVE_Y)
{
newAngles[0] +=180;
}
Create_Matrix(newAngles, &temp1);
permutation.matrix[0][0] = permutation.matrix[0][1] = permutation.matrix[0][2] = permutation.matrix[0][3] = 0;
permutation.matrix[1][0] = permutation.matrix[1][1] = permutation.matrix[1][2] = permutation.matrix[1][3] = 0;
permutation.matrix[2][0] = permutation.matrix[2][1] = permutation.matrix[2][2] = permutation.matrix[2][3] = 0;
// determine what axis newAngles Yaw should revolve around
switch (forward)
{
case NEGATIVE_X:
permutation.matrix[0][0] = -1; // works
break;
case POSITIVE_X:
permutation.matrix[0][0] = 1; // works
break;
case NEGATIVE_Y:
permutation.matrix[1][0] = -1;
break;
case POSITIVE_Y:
permutation.matrix[1][0] = 1;
break;
case NEGATIVE_Z:
permutation.matrix[2][0] = -1;
break;
case POSITIVE_Z:
permutation.matrix[2][0] = 1;
break;
default:
break;
}
// determine what axis newAngles pitch should revolve around
switch (left)
{
case NEGATIVE_X:
permutation.matrix[0][1] = -1;
break;
case POSITIVE_X:
permutation.matrix[0][1] = 1;
break;
case NEGATIVE_Y:
permutation.matrix[1][1] = -1; // works
break;
case POSITIVE_Y:
permutation.matrix[1][1] = 1; // works
break;
case NEGATIVE_Z:
permutation.matrix[2][1] = -1;
break;
case POSITIVE_Z:
permutation.matrix[2][1] = 1;
break;
default:
break;
}
// determine what axis newAngles Roll should revolve around
switch (up)
{
case NEGATIVE_X:
permutation.matrix[0][2] = -1;
break;
case POSITIVE_X:
permutation.matrix[0][2] = 1;
break;
case NEGATIVE_Y:
permutation.matrix[1][2] = -1;
break;
case POSITIVE_Y:
permutation.matrix[1][2] = 1;
break;
case NEGATIVE_Z:
permutation.matrix[2][2] = -1; // works
break;
case POSITIVE_Z:
permutation.matrix[2][2] = 1; // works
break;
default:
break;
}
Multiply_3x4Matrix(boneOverride, &temp1,&permutation);
}
// keep a copy of the matrix in the newmatrix which is actually what we use
memcpy(&blist[index].newMatrix, &blist[index].matrix, sizeof(mdxaBone_t));
}
//=========================================================================================
//// Public Bone Routines
// Given a model handle, and a bone name, we want to remove this bone from the bone override list
qboolean G2_Remove_Bone (CGhoul2Info *ghlInfo, boneInfo_v &blist, const char *boneName)
{
int index;
assert(ghlInfo->animModel);
index = G2_Find_Bone(ghlInfo->animModel, blist, boneName);
return G2_Remove_Bone_Index(blist, index);
}
#define DEBUG_PCJ (0)
// Given a model handle, and a bone name, we want to set angles specifically for overriding
qboolean G2_Set_Bone_Angles_Index( boneInfo_v &blist, const int index,
const float *angles, const int flags, const Eorientations yaw,
const Eorientations pitch, const Eorientations roll, qhandle_t *modelList,
const int modelIndex, const int blendTime, const int currentTime)
{
if ((index >= (int)blist.size()) || (blist[index].boneNumber == -1))
{
// we are attempting to set a bone override that doesn't exist
assert(0);
return qfalse;
}
if (index != -1)
{
if (blist[index].flags & BONE_ANGLES_RAGDOLL)
{
return qtrue; // don't accept any calls on ragdoll bones
}
}
if (flags & (BONE_ANGLES_PREMULT | BONE_ANGLES_POSTMULT))
{
// you CANNOT call this with an index with these kinds of bone overrides - we need the model details for these kinds of bone angle overrides
assert(0);
return qfalse;
}
// yes, so set the angles and flags correctly
blist[index].flags &= ~(BONE_ANGLES_TOTAL);
blist[index].flags |= flags;
blist[index].boneBlendStart = currentTime;
blist[index].boneBlendTime = blendTime;
#if DEBUG_PCJ
Com_OPrintf("PCJ %2d %6d (%6.2f,%6.2f,%6.2f) %d %d %d %d\n",index,currentTime,angles[0],angles[1],angles[2],yaw,pitch,roll,flags);
#endif
G2_Generate_Matrix(NULL, blist, index, angles, flags, yaw, pitch, roll);
return qtrue;
}
// Given a model handle, and a bone name, we want to set angles specifically for overriding
qboolean G2_Set_Bone_Angles(CGhoul2Info *ghlInfo, boneInfo_v &blist, const char *boneName, const float *angles,
const int flags, const Eorientations up, const Eorientations left, const Eorientations forward,
qhandle_t *modelList, const int modelIndex, const int blendTime, const int currentTime)
{
model_t *mod_a;
mod_a = (model_t *)ghlInfo->animModel;
int index = G2_Find_Bone(mod_a, blist, boneName);
// did we find it?
if (index != -1)
{
if (blist[index].flags & BONE_ANGLES_RAGDOLL)
{
return qtrue; // don't accept any calls on ragdoll bones
}
// yes, so set the angles and flags correctly
blist[index].flags &= ~(BONE_ANGLES_TOTAL);
blist[index].flags |= flags;
blist[index].boneBlendStart = currentTime;
blist[index].boneBlendTime = blendTime;
#if DEBUG_PCJ
Com_OPrintf("%2d %6d (%6.2f,%6.2f,%6.2f) %d %d %d %d\n",index,currentTime,angles[0],angles[1],angles[2],up,left,forward,flags);
#endif
G2_Generate_Matrix(mod_a, blist, index, angles, flags, up, left, forward);
return qtrue;
}
// no - lets try and add this bone in
index = G2_Add_Bone(mod_a, blist, boneName);
// did we find a free one?
if (index != -1)
{
// yes, so set the angles and flags correctly
blist[index].flags &= ~(BONE_ANGLES_TOTAL);
blist[index].flags |= flags;
blist[index].boneBlendStart = currentTime;
blist[index].boneBlendTime = blendTime;
#if DEBUG_PCJ
Com_OPrintf("%2d %6d (%6.2f,%6.2f,%6.2f) %d %d %d %d\n",index,currentTime,angles[0],angles[1],angles[2],up,left,forward,flags);
#endif
G2_Generate_Matrix(mod_a, blist, index, angles, flags, up, left, forward);
return qtrue;
}
// assert(0);
//Jeese, we don't need an assert here too. There's already a warning in G2_Add_Bone if it fails.
// no
return qfalse;
}
// Given a model handle, and a bone name, we want to set angles specifically for overriding - using a matrix directly
qboolean G2_Set_Bone_Angles_Matrix_Index(boneInfo_v &blist, const int index,
const mdxaBone_t &matrix, const int flags, qhandle_t *modelList,
const int modelIndex, const int blendTime, const int currentTime)
{
if ((index >= (int)blist.size()) || (blist[index].boneNumber == -1))
{
// we are attempting to set a bone override that doesn't exist
assert(0);
return qfalse;
}
if (index != -1)
{
if (blist[index].flags & BONE_ANGLES_RAGDOLL)
{
return qtrue; // don't accept any calls on ragdoll bones
}
}
// yes, so set the angles and flags correctly
blist[index].flags &= ~(BONE_ANGLES_TOTAL);
blist[index].flags |= flags;
blist[index].boneBlendStart = currentTime;
blist[index].boneBlendTime = blendTime;
memcpy(&blist[index].matrix, &matrix, sizeof(mdxaBone_t));
memcpy(&blist[index].newMatrix, &matrix, sizeof(mdxaBone_t));
return qtrue;
}
// Given a model handle, and a bone name, we want to set angles specifically for overriding - using a matrix directly
qboolean G2_Set_Bone_Angles_Matrix(const char *fileName, boneInfo_v &blist, const char *boneName, const mdxaBone_t &matrix,
const int flags, qhandle_t *modelList, const int modelIndex, const int blendTime, const int currentTime)
{
model_t *mod_m;
if (!fileName[0])
{
mod_m = R_GetModelByHandle(modelList[modelIndex]);
}
else
{
mod_m = R_GetModelByHandle(RE_RegisterModel(fileName));
}
model_t *mod_a = R_GetModelByHandle(mod_m->mdxm->animIndex);
int index = G2_Find_Bone(mod_a, blist, boneName);
if (index != -1)
{
if (blist[index].flags & BONE_ANGLES_RAGDOLL)
{
return qtrue; // don't accept any calls on ragdoll bones
}
}
// did we find it?
if (index != -1)
{
// yes, so set the angles and flags correctly
blist[index].flags &= ~(BONE_ANGLES_TOTAL);
blist[index].flags |= flags;
memcpy(&blist[index].matrix, &matrix, sizeof(mdxaBone_t));
memcpy(&blist[index].newMatrix, &matrix, sizeof(mdxaBone_t));
return qtrue;
}
// no - lets try and add this bone in
index = G2_Add_Bone(mod_a, blist, boneName);
// did we find a free one?
if (index != -1)
{
// yes, so set the angles and flags correctly
blist[index].flags &= ~(BONE_ANGLES_TOTAL);
blist[index].flags |= flags;
memcpy(&blist[index].matrix, &matrix, sizeof(mdxaBone_t));
memcpy(&blist[index].newMatrix, &matrix, sizeof(mdxaBone_t));
return qtrue;
}
assert(0);
// no
return qfalse;
}
#define DEBUG_G2_TIMING (0)
// given a model, bone name, a bonelist, a start/end frame number, a anim speed and some anim flags, set up or modify an existing bone entry for a new set of anims
qboolean G2_Set_Bone_Anim_Index(
boneInfo_v &blist,
const int index,
const int startFrame,
const int endFrame,
const int flags,
const float animSpeed,
const int currentTime,
const float setFrame,
const int blendTime,
const int numFrames)
{
int modFlags = flags;
if ((index >= (int)blist.size()) || (blist[index].boneNumber == -1))
{
// we are attempting to set a bone override that doesn't exist
assert(0);
return qfalse;
}
if (index != -1)
{
if (blist[index].flags & BONE_ANGLES_RAGDOLL)
{
return qtrue; // don't accept any calls on ragdoll bones
}
//mark it for needing a transform for the cached trace transform stuff
blist[index].flags |= BONE_NEED_TRANSFORM;
}
if (setFrame != -1)
{
assert((setFrame >= startFrame) && (setFrame <= endFrame));
}
if (flags & BONE_ANIM_BLEND)
{
float currentFrame, animSpeed;
int startFrame, endFrame, flags;
// figure out where we are now
if (G2_Get_Bone_Anim_Index(blist, index, currentTime, ¤tFrame, &startFrame, &endFrame, &flags, &animSpeed, NULL, numFrames))
{
if (blist[index].blendStart == currentTime) //we're replacing a blend in progress which hasn't started
{
// set the amount of time it's going to take to blend this anim with the last frame of the last one
blist[index].blendTime = blendTime;
}
else
{
if (animSpeed<0.0f)
{
blist[index].blendFrame = floor(currentFrame);
blist[index].blendLerpFrame = floor(currentFrame);
}
else
{
blist[index].blendFrame = currentFrame;
blist[index].blendLerpFrame = currentFrame+1;
// cope with if the lerp frame is actually off the end of the anim
if (blist[index].blendFrame >= endFrame )
{
// we only want to lerp with the first frame of the anim if we are looping
if (blist[index].flags & BONE_ANIM_OVERRIDE_LOOP)
{
blist[index].blendFrame = startFrame;
}
// if we intend to end this anim or freeze after this, then just keep on the last frame
else
{
// assert(endFrame>0);
if (endFrame <= 0)
{
blist[index].blendLerpFrame = 0;
}
else
{
blist[index].blendFrame = endFrame -1;
}
}
}
// cope with if the lerp frame is actually off the end of the anim
if (blist[index].blendLerpFrame >= endFrame )
{
// we only want to lerp with the first frame of the anim if we are looping
if (blist[index].flags & BONE_ANIM_OVERRIDE_LOOP)
{
blist[index].blendLerpFrame = startFrame;
}
// if we intend to end this anim or freeze after this, then just keep on the last frame
else
{
// assert(endFrame>0);
if (endFrame <= 0)
{
blist[index].blendLerpFrame = 0;
}
else
{
blist[index].blendLerpFrame = endFrame - 1;
}
}
}
}
// set the amount of time it's going to take to blend this anim with the last frame of the last one
blist[index].blendTime = blendTime;
blist[index].blendStart = currentTime;
}
}
// hmm, we weren't animating on this bone. In which case disable the blend
else
{
blist[index].blendFrame = blist[index].blendLerpFrame = 0;
blist[index].blendTime = 0;
modFlags &= ~(BONE_ANIM_BLEND);
}
}
else
{
blist[index].blendFrame = blist[index].blendLerpFrame = 0;
blist[index].blendTime = blist[index].blendStart = 0;
// we aren't blending, so remove the option to do so
modFlags &= ~BONE_ANIM_BLEND;
}
// yes, so set the anim data and flags correctly
blist[index].endFrame = endFrame;
blist[index].startFrame = startFrame;
blist[index].animSpeed = animSpeed;
blist[index].pauseTime = 0;
// start up the animation:)
if (setFrame != -1)
{
blist[index].lastTime = blist[index].startTime = (currentTime - (((setFrame - (float)startFrame) * 50.0)/ animSpeed));
}
else
{
blist[index].lastTime = blist[index].startTime = currentTime;
}
blist[index].flags &= ~(BONE_ANIM_TOTAL);
if (blist[index].flags < 0)
{
blist[index].flags = 0;
}
blist[index].flags |= modFlags;
#if DEBUG_G2_TIMING
if (index==2)
{
const boneInfo_t &bone=blist[index];
char mess[1000];
if (bone.flags&BONE_ANIM_BLEND)
{
sprintf(mess,"sab[%2d] %5d %5d (%5d-%5d) %4.2f %4x bt(%5d-%5d) %7.2f %5d\n",
index,
currentTime,
bone.startTime,
bone.startFrame,
bone.endFrame,
bone.animSpeed,
bone.flags,
bone.blendStart,
bone.blendStart+bone.blendTime,
bone.blendFrame,
bone.blendLerpFrame
);
}
else
{
sprintf(mess,"saa[%2d] %5d %5d (%5d-%5d) %4.2f %4x\n",
index,
currentTime,
bone.startTime,
bone.startFrame,
bone.endFrame,
bone.animSpeed,
bone.flags
);
}
Com_OPrintf("%s",mess);
}
#endif
return qtrue;
}
// given a model, bone name, a bonelist, a start/end frame number, a anim speed and some anim flags, set up or modify an existing bone entry for a new set of anims
qboolean G2_Set_Bone_Anim(CGhoul2Info *ghlInfo,
boneInfo_v &blist,
const char *boneName,
const int startFrame,
const int endFrame,
const int flags,
const float animSpeed,
const int currentTime,
const float setFrame,
const int blendTime)
{
model_t *mod_a = (model_t *)ghlInfo->animModel;
int index = G2_Find_Bone(mod_a, blist, boneName);
if (index == -1)
{
index = G2_Add_Bone(mod_a, blist, boneName);
}
if (index != -1)
{
if (blist[index].flags & BONE_ANGLES_RAGDOLL)
{
return qtrue; // don't accept any calls on ragdoll bones
}
}
if (index != -1)
{
return G2_Set_Bone_Anim_Index(blist,index,startFrame,endFrame,flags,animSpeed,currentTime,setFrame,blendTime,ghlInfo->aHeader->numFrames);
}
return qfalse;
}
qboolean G2_Get_Bone_Anim_Range(CGhoul2Info *ghlInfo, boneInfo_v &blist, const char *boneName, int *startFrame, int *endFrame)
{
model_t *mod_a = (model_t *)ghlInfo->animModel;
int index = G2_Find_Bone(mod_a, blist, boneName);
// did we find it?
if (index != -1)
{
// are we an animating bone?
if (blist[index].flags & (BONE_ANIM_OVERRIDE_LOOP | BONE_ANIM_OVERRIDE))
{
*startFrame = blist[index].startFrame;
*endFrame = blist[index].endFrame;
return qtrue;
}
}
return qfalse;
}
// given a model, bonelist and bonename, return the current frame, startframe and endframe of the current animation
// NOTE if we aren't running an animation, then qfalse is returned
void G2_TimingModel(boneInfo_t &bone,int currentTime,int numFramesInFile,int ¤tFrame,int &newFrame,float &lerp);
qboolean G2_Get_Bone_Anim_Index( boneInfo_v &blist, const int index, const int currentTime,
float *currentFrame, int *startFrame, int *endFrame, int *flags, float *retAnimSpeed, qhandle_t *modelList, int numFrames)
{
// did we find it?
if ((index>=0) && !((index >= (int)blist.size()) || (blist[index].boneNumber == -1)))
{
// are we an animating bone?
if (blist[index].flags & (BONE_ANIM_OVERRIDE_LOOP | BONE_ANIM_OVERRIDE))
{
int lcurrentFrame,newFrame;
float lerp;
G2_TimingModel(blist[index],currentTime,numFrames,lcurrentFrame,newFrame,lerp);
*currentFrame =float(lcurrentFrame)+lerp;
*startFrame = blist[index].startFrame;
*endFrame = blist[index].endFrame;
*flags = blist[index].flags;
*retAnimSpeed = blist[index].animSpeed;
return qtrue;
}
}
*startFrame=0;
*endFrame=1;
*currentFrame=0.0f;
*flags=0;
*retAnimSpeed=0.0f;
return qfalse;
}
// given a model, bonelist and bonename, return the current frame, startframe and endframe of the current animation
// NOTE if we aren't running an animation, then qfalse is returned
qboolean G2_Get_Bone_Anim(CGhoul2Info *ghlInfo, boneInfo_v &blist, const char *boneName, const int currentTime,
float *currentFrame, int *startFrame, int *endFrame, int *flags, float *retAnimSpeed, qhandle_t *modelList, int modelIndex)
{
model_t *mod_a = (model_t *)ghlInfo->animModel;
int index = G2_Find_Bone(mod_a, blist, boneName);
if (index==-1)
{
index = G2_Add_Bone(mod_a, blist, boneName);
if (index == -1)
{
return qfalse;
}
}
assert(ghlInfo->aHeader);
if (G2_Get_Bone_Anim_Index(blist, index, currentTime, currentFrame, startFrame, endFrame, flags, retAnimSpeed, modelList, ghlInfo->aHeader->numFrames))
{
assert(*startFrame>=0&&*startFrame<ghlInfo->aHeader->numFrames);
assert(*endFrame>0&&*endFrame<=ghlInfo->aHeader->numFrames);
assert(*currentFrame>=0.0f&&((int)(*currentFrame))<ghlInfo->aHeader->numFrames);
return qtrue;
}
return qfalse;
}
// given a model, bonelist and bonename, lets pause an anim if it's playing.
qboolean G2_Pause_Bone_Anim(CGhoul2Info *ghlInfo, boneInfo_v &blist, const char *boneName, const int currentTime)
{
model_t *mod_a = (model_t *)ghlInfo->animModel;
int index = G2_Find_Bone(mod_a, blist, boneName);
// did we find it?
if (index != -1)
{
// are we pausing or un pausing?
if (blist[index].pauseTime)
{
int startFrame = 0, endFrame = 0, flags = 0;
float currentFrame = 0.0f, animSpeed = 1.0f;
// figure out what frame we are on now
G2_Get_Bone_Anim(ghlInfo, blist, boneName, blist[index].pauseTime, ¤tFrame, &startFrame, &endFrame, &flags, &animSpeed, NULL, 0);
// reset start time so we are actually on this frame right now
G2_Set_Bone_Anim(ghlInfo, blist, boneName, startFrame, endFrame, flags, animSpeed, currentTime, currentFrame, 0);
// no pausing anymore
blist[index].pauseTime = 0;
}
// ahh, just pausing, the easy bit
else
{
blist[index].pauseTime = currentTime;
}
return qtrue;
}
assert(0);
return qfalse;
}
qboolean G2_IsPaused(const char *fileName, boneInfo_v &blist, const char *boneName)
{
model_t *mod_m = R_GetModelByHandle(RE_RegisterModel(fileName));
model_t *mod_a = R_GetModelByHandle(mod_m->mdxm->animIndex);
int index = G2_Find_Bone(mod_a, blist, boneName);
// did we find it?
if (index != -1)
{
// are we paused?
if (blist[index].pauseTime)
{
// yup. paused.
return qtrue;
}
return qfalse;