/
CSkinnedMesh.cpp
1707 lines (1440 loc) · 42.6 KB
/
CSkinnedMesh.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) 2002-2012 Nikolaus Gebhardt
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h
#include "IrrCompileConfig.h"
#ifdef _IRR_COMPILE_WITH_SKINNED_MESH_SUPPORT_
#include "CSkinnedMesh.h"
#include "CBoneSceneNode.h"
#include "IAnimatedMeshSceneNode.h"
#include "os.h"
#include "irrMap.h"
inline irr::s16 float_16(irr::f32 in)
{
irr::s32 i;
memcpy(&i, &in, 4);
irr::s32 s = (i >> 16) & 0x00008000;
irr::s32 e = ((i >> 23) & 0x000000ff) - (127 - 15);
irr::s32 m = i & 0x007fffff;
if (e <= 0)
{
if (e < -10)
{
return irr::s16(s);
}
m = (m | 0x00800000) >> (1 - e);
if (m & 0x00001000)
m += 0x00002000;
return irr::s16(s | (m >> 13));
}
else if (e == 0xff - (127 - 15))
{
if (m == 0)
{
return irr::s16(s | 0x7c00);
}
else
{
m >>= 13;
return irr::s16(s | 0x7c00 | m | (m == 0));
}
}
else
{
if (m & 0x00001000)
{
m += 0x00002000;
if (m & 0x00800000)
{
m = 0; // overflow in significand,
e += 1; // adjust exponent
}
}
if (e > 30)
{
return irr::s16(s | 0x7c00);
}
return irr::s16(s | (e << 10) | (m >> 13));
}
}
namespace irr
{
namespace scene
{
//! constructor
CSkinnedMesh::CSkinnedMesh()
: AnimationFrames(0.f), SkinningBuffers(0), FramesPerSecond(25.f),
LastAnimatedFrame(-1), SkinnedLastFrame(false),
InterpolationMode(EIM_LINEAR),
HasAnimation(false), PreparedForSkinning(false),
AnimateNormals(true), HardwareSkinning(false), m_total_joints(0),
m_current_joint(0)
{
#ifdef _DEBUG
setDebugName("CSkinnedMesh");
#endif
SkinningBuffers=&LocalBuffers;
}
//! destructor
CSkinnedMesh::~CSkinnedMesh()
{
for (u32 i=0; i<AllJoints.size(); ++i)
delete AllJoints[i];
for (u32 j=0; j<LocalBuffers.size(); ++j)
{
if (LocalBuffers[j])
LocalBuffers[j]->drop();
}
}
//! returns the amount of frames in milliseconds.
//! If the amount is 1, it is a static (=non animated) mesh.
u32 CSkinnedMesh::getFrameCount() const
{
return core::floor32(AnimationFrames);
}
//! Gets the default animation speed of the animated mesh.
/** \return Amount of frames per second. If the amount is 0, it is a static, non animated mesh. */
f32 CSkinnedMesh::getAnimationSpeed() const
{
return FramesPerSecond;
}
//! Gets the frame count of the animated mesh.
/** \param fps Frames per second to play the animation with. If the amount is 0, it is not animated.
The actual speed is set in the scene node the mesh is instantiated in.*/
void CSkinnedMesh::setAnimationSpeed(f32 fps)
{
FramesPerSecond=fps;
}
//! returns the animated mesh based on a detail level. 0 is the lowest, 255 the highest detail. Note, that some Meshes will ignore the detail level.
IMesh* CSkinnedMesh::getMesh(s32 frame, s32 detailLevel, s32 startFrameLoop, s32 endFrameLoop)
{
const bool is_hw_skinning_before = HardwareSkinning;
if (is_hw_skinning_before)
{
HardwareSkinning = false;
LastAnimatedFrame = -1;
}
//animate(frame,startFrameLoop, endFrameLoop);
if (frame==-1)
return this;
animateMesh((f32)frame, 1.0f);
skinMesh();
if (is_hw_skinning_before)
HardwareSkinning = true;
return this;
}
//--------------------------------------------------------------------------
// Keyframe Animation
//--------------------------------------------------------------------------
//! Animates this mesh's joints based on frame input
//! blend: {0-old position, 1-New position}
void CSkinnedMesh::animateMesh(f32 frame, f32 blend)
{
if (HardwareSkinning && LastAnimatedFrame==frame)
{
SkinnedLastFrame=false;
return;
}
if (!HasAnimation || LastAnimatedFrame==frame)
return;
LastAnimatedFrame=frame;
SkinnedLastFrame=false;
if (blend<=0.f)
return; //No need to animate
for (u32 i=0; i<AllJoints.size(); ++i)
{
//The joints can be animated here with no input from their
//parents, but for setAnimationMode extra checks are needed
//to their parents
SJoint *joint = AllJoints[i];
const core::vector3df oldPosition = joint->Animatedposition;
const core::vector3df oldScale = joint->Animatedscale;
const core::quaternion oldRotation = joint->Animatedrotation;
core::vector3df position = oldPosition;
core::vector3df scale = oldScale;
core::quaternion rotation = oldRotation;
getFrameData(frame, joint,
position, joint->positionHint,
scale, joint->scaleHint,
rotation, joint->rotationHint);
if (blend==1.0f)
{
//No blending needed
joint->Animatedposition = position;
joint->Animatedscale = scale;
joint->Animatedrotation = rotation;
}
else
{
//Blend animation
joint->Animatedposition = core::lerp(oldPosition, position, blend);
joint->Animatedscale = core::lerp(oldScale, scale, blend);
joint->Animatedrotation.slerp(oldRotation, rotation, blend);
}
}
//Note:
//LocalAnimatedMatrix needs to be built at some point, but this function may be called lots of times for
//one render (to play two animations at the same time) LocalAnimatedMatrix only needs to be built once.
//a call to buildAllLocalAnimatedMatrices is needed before skinning the mesh, and before the user gets the joints to move
//----------------
// Temp!
buildAllLocalAnimatedMatrices();
//-----------------
updateBoundingBox();
}
void CSkinnedMesh::buildAllLocalAnimatedMatrices()
{
for (u32 i=0; i<AllJoints.size(); ++i)
{
SJoint *joint = AllJoints[i];
//Could be faster:
if (joint->UseAnimationFrom &&
(joint->UseAnimationFrom->PositionKeys.size() ||
joint->UseAnimationFrom->ScaleKeys.size() ||
joint->UseAnimationFrom->RotationKeys.size() ))
{
joint->GlobalSkinningSpace=false;
// IRR_TEST_BROKEN_QUATERNION_USE: TODO - switched to getMatrix_transposed instead of getMatrix for downward compatibility.
// Not tested so far if this was correct or wrong before quaternion fix!
joint->Animatedrotation.getMatrix_transposed(joint->LocalAnimatedMatrix);
// --- joint->LocalAnimatedMatrix *= joint->Animatedrotation.getMatrix() ---
f32 *m1 = joint->LocalAnimatedMatrix.pointer();
core::vector3df &Pos = joint->Animatedposition;
m1[0] += Pos.X*m1[3];
m1[1] += Pos.Y*m1[3];
m1[2] += Pos.Z*m1[3];
m1[4] += Pos.X*m1[7];
m1[5] += Pos.Y*m1[7];
m1[6] += Pos.Z*m1[7];
m1[8] += Pos.X*m1[11];
m1[9] += Pos.Y*m1[11];
m1[10] += Pos.Z*m1[11];
m1[12] += Pos.X*m1[15];
m1[13] += Pos.Y*m1[15];
m1[14] += Pos.Z*m1[15];
// -----------------------------------
if (joint->ScaleKeys.size())
{
/*
core::matrix4 scaleMatrix;
scaleMatrix.setScale(joint->Animatedscale);
joint->LocalAnimatedMatrix *= scaleMatrix;
*/
// -------- joint->LocalAnimatedMatrix *= scaleMatrix -----------------
core::matrix4& mat = joint->LocalAnimatedMatrix;
mat[0] *= joint->Animatedscale.X;
mat[1] *= joint->Animatedscale.X;
mat[2] *= joint->Animatedscale.X;
mat[3] *= joint->Animatedscale.X;
mat[4] *= joint->Animatedscale.Y;
mat[5] *= joint->Animatedscale.Y;
mat[6] *= joint->Animatedscale.Y;
mat[7] *= joint->Animatedscale.Y;
mat[8] *= joint->Animatedscale.Z;
mat[9] *= joint->Animatedscale.Z;
mat[10] *= joint->Animatedscale.Z;
mat[11] *= joint->Animatedscale.Z;
// -----------------------------------
}
}
else
{
joint->LocalAnimatedMatrix=joint->LocalMatrix;
}
}
SkinnedLastFrame=false;
}
void CSkinnedMesh::buildAllGlobalAnimatedMatrices(SJoint *joint, SJoint *parentJoint)
{
if (!joint)
{
for (u32 i=0; i<RootJoints.size(); ++i)
buildAllGlobalAnimatedMatrices(RootJoints[i], 0);
return;
}
else
{
// Find global matrix...
if (!parentJoint || joint->GlobalSkinningSpace)
joint->GlobalAnimatedMatrix = joint->LocalAnimatedMatrix;
else
joint->GlobalAnimatedMatrix = parentJoint->GlobalAnimatedMatrix * joint->LocalAnimatedMatrix;
}
for (u32 j=0; j<joint->Children.size(); ++j)
buildAllGlobalAnimatedMatrices(joint->Children[j], joint);
}
void CSkinnedMesh::getFrameData(f32 frame, SJoint *joint,
core::vector3df &position, s32 &positionHint,
core::vector3df &scale, s32 &scaleHint,
core::quaternion &rotation, s32 &rotationHint)
{
s32 foundPositionIndex = -1;
s32 foundScaleIndex = -1;
s32 foundRotationIndex = -1;
if (joint->UseAnimationFrom)
{
const core::array<SPositionKey> &PositionKeys=joint->UseAnimationFrom->PositionKeys;
const core::array<SScaleKey> &ScaleKeys=joint->UseAnimationFrom->ScaleKeys;
const core::array<SRotationKey> &RotationKeys=joint->UseAnimationFrom->RotationKeys;
if (PositionKeys.size())
{
foundPositionIndex = -1;
//Test the Hints...
if (positionHint>=0 && (u32)positionHint < PositionKeys.size())
{
//check this hint
if (positionHint>0 && PositionKeys[positionHint].frame>=frame && PositionKeys[positionHint-1].frame<frame )
foundPositionIndex=positionHint;
else if (positionHint+1 < (s32)PositionKeys.size())
{
//check the next index
if ( PositionKeys[positionHint+1].frame>=frame &&
PositionKeys[positionHint+0].frame<frame)
{
positionHint++;
foundPositionIndex=positionHint;
}
}
}
//The hint test failed, do a full scan...
if (foundPositionIndex==-1)
{
for (u32 i=0; i<PositionKeys.size(); ++i)
{
if (PositionKeys[i].frame >= frame) //Keys should to be sorted by frame
{
foundPositionIndex=i;
positionHint=i;
break;
}
}
}
//Do interpolation...
if (foundPositionIndex!=-1)
{
if (InterpolationMode==EIM_CONSTANT || foundPositionIndex==0)
{
position = PositionKeys[foundPositionIndex].position;
}
else if (InterpolationMode==EIM_LINEAR)
{
const SPositionKey& KeyA = PositionKeys[foundPositionIndex];
const SPositionKey& KeyB = PositionKeys[foundPositionIndex-1];
const f32 fd1 = frame - KeyA.frame;
const f32 fd2 = KeyB.frame - frame;
position = ((KeyB.position-KeyA.position)/(fd1+fd2))*fd1 + KeyA.position;
}
}
}
//------------------------------------------------------------
if (ScaleKeys.size())
{
foundScaleIndex = -1;
//Test the Hints...
if (scaleHint>=0 && (u32)scaleHint < ScaleKeys.size())
{
//check this hint
if (scaleHint>0 && ScaleKeys[scaleHint].frame>=frame && ScaleKeys[scaleHint-1].frame<frame )
foundScaleIndex=scaleHint;
else if (scaleHint+1 < (s32)ScaleKeys.size())
{
//check the next index
if ( ScaleKeys[scaleHint+1].frame>=frame &&
ScaleKeys[scaleHint+0].frame<frame)
{
scaleHint++;
foundScaleIndex=scaleHint;
}
}
}
//The hint test failed, do a full scan...
if (foundScaleIndex==-1)
{
for (u32 i=0; i<ScaleKeys.size(); ++i)
{
if (ScaleKeys[i].frame >= frame) //Keys should to be sorted by frame
{
foundScaleIndex=i;
scaleHint=i;
break;
}
}
}
//Do interpolation...
if (foundScaleIndex!=-1)
{
if (InterpolationMode==EIM_CONSTANT || foundScaleIndex==0)
{
scale = ScaleKeys[foundScaleIndex].scale;
}
else if (InterpolationMode==EIM_LINEAR)
{
const SScaleKey& KeyA = ScaleKeys[foundScaleIndex];
const SScaleKey& KeyB = ScaleKeys[foundScaleIndex-1];
const f32 fd1 = frame - KeyA.frame;
const f32 fd2 = KeyB.frame - frame;
scale = ((KeyB.scale-KeyA.scale)/(fd1+fd2))*fd1 + KeyA.scale;
}
}
}
//-------------------------------------------------------------
if (RotationKeys.size())
{
foundRotationIndex = -1;
//Test the Hints...
if (rotationHint>=0 && (u32)rotationHint < RotationKeys.size())
{
//check this hint
if (rotationHint>0 && RotationKeys[rotationHint].frame>=frame && RotationKeys[rotationHint-1].frame<frame )
foundRotationIndex=rotationHint;
else if (rotationHint+1 < (s32)RotationKeys.size())
{
//check the next index
if ( RotationKeys[rotationHint+1].frame>=frame &&
RotationKeys[rotationHint+0].frame<frame)
{
rotationHint++;
foundRotationIndex=rotationHint;
}
}
}
//The hint test failed, do a full scan...
if (foundRotationIndex==-1)
{
for (u32 i=0; i<RotationKeys.size(); ++i)
{
if (RotationKeys[i].frame >= frame) //Keys should be sorted by frame
{
foundRotationIndex=i;
rotationHint=i;
break;
}
}
}
//Do interpolation...
if (foundRotationIndex!=-1)
{
if (InterpolationMode==EIM_CONSTANT || foundRotationIndex==0)
{
rotation = RotationKeys[foundRotationIndex].rotation;
}
else if (InterpolationMode==EIM_LINEAR)
{
const SRotationKey& KeyA = RotationKeys[foundRotationIndex];
const SRotationKey& KeyB = RotationKeys[foundRotationIndex-1];
const f32 fd1 = frame - KeyA.frame;
const f32 fd2 = KeyB.frame - frame;
const f32 t = fd1/(fd1+fd2);
/*
f32 t = 0;
if (KeyA.frame!=KeyB.frame)
t = (frame-KeyA.frame) / (KeyB.frame - KeyA.frame);
*/
rotation.slerp(KeyA.rotation, KeyB.rotation, t);
}
}
}
}
}
//--------------------------------------------------------------------------
// Software Skinning
//--------------------------------------------------------------------------
//! Preforms a software skin on this mesh based of joint positions
void CSkinnedMesh::skinMesh(f32 strength, SkinningCallback sc, int offset)
{
if (!HasAnimation || SkinnedLastFrame)
return;
//----------------
// This is marked as "Temp!". A shiny dubloon to whomever can tell me why.
buildAllGlobalAnimatedMatrices();
//-----------------
SkinnedLastFrame=true;
m_current_joint = 0;
if (HardwareSkinning)
{
for (u32 i = 0; i < RootJoints.size(); i++)
skinJoint(RootJoints[i], 0, strength, sc, offset);
}
else
{
//Software skin....
u32 i;
//rigid animation
for (i=0; i<AllJoints.size(); ++i)
{
for (u32 j=0; j<AllJoints[i]->AttachedMeshes.size(); ++j)
{
SSkinMeshBuffer* Buffer=(*SkinningBuffers)[ AllJoints[i]->AttachedMeshes[j] ];
Buffer->Transformation=AllJoints[i]->GlobalAnimatedMatrix;
}
}
//clear skinning helper array
for (i=0; i<Vertices_Moved.size(); ++i)
for (u32 j=0; j<Vertices_Moved[i].size(); ++j)
Vertices_Moved[i][j]=false;
//skin starting with the root joints
for (i=0; i<RootJoints.size(); ++i)
skinJoint(RootJoints[i], 0, strength);
for (i=0; i<SkinningBuffers->size(); ++i)
(*SkinningBuffers)[i]->setDirty(EBT_VERTEX);
}
updateBoundingBox();
}
void CSkinnedMesh::skinJoint(SJoint *joint, SJoint *parentJoint, f32 strength,
SkinningCallback sc, int offset)
{
if (joint->Weights.size())
{
//Find this joints pull on vertices...
core::matrix4 jointVertexPull(core::matrix4::EM4CONST_NOTHING);
jointVertexPull.setbyproduct(joint->GlobalAnimatedMatrix, joint->GlobalInversedMatrix);
if (HardwareSkinning)
{
if (sc != NULL) sc(jointVertexPull, m_current_joint, offset);
m_current_joint++;
}
else
{
core::vector3df thisVertexMove, thisNormalMove;
core::array<scene::SSkinMeshBuffer*> &buffersUsed=*SkinningBuffers;
//Skin Vertices Positions and Normals...
for (u32 i=0; i<joint->Weights.size(); ++i)
{
SWeight& weight = joint->Weights[i];
// Pull this vertex...
jointVertexPull.transformVect(thisVertexMove, weight.StaticPos);
if (AnimateNormals)
jointVertexPull.rotateVect(thisNormalMove, weight.StaticNormal);
// Apply animation strength
if(strength != 1.f)
{
thisVertexMove = core::lerp(weight.StaticPos, thisVertexMove, strength);
if(AnimateNormals)
thisNormalMove = core::lerp(weight.StaticNormal, thisNormalMove, strength);
}
if (! (*(weight.Moved)) )
{
*(weight.Moved) = true;
buffersUsed[weight.buffer_id]->getVertex(weight.vertex_id)->Pos = thisVertexMove * weight.strength;
if (AnimateNormals)
buffersUsed[weight.buffer_id]->getVertex(weight.vertex_id)->Normal = thisNormalMove * weight.strength;
//*(weight._Pos) = thisVertexMove * weight.strength;
}
else
{
buffersUsed[weight.buffer_id]->getVertex(weight.vertex_id)->Pos += thisVertexMove * weight.strength;
if (AnimateNormals)
buffersUsed[weight.buffer_id]->getVertex(weight.vertex_id)->Normal += thisNormalMove * weight.strength;
//*(weight._Pos) += thisVertexMove * weight.strength;
}
buffersUsed[weight.buffer_id]->boundingBoxNeedsRecalculated();
}
}
}
//Skin all children
for (u32 j=0; j<joint->Children.size(); ++j)
skinJoint(joint->Children[j], joint, strength, sc, offset);
}
E_ANIMATED_MESH_TYPE CSkinnedMesh::getMeshType() const
{
return EAMT_SKINNED;
}
//! Gets joint count.
u32 CSkinnedMesh::getJointCount() const
{
return AllJoints.size();
}
//! Gets the name of a joint.
const c8* CSkinnedMesh::getJointName(u32 number) const
{
if (number >= AllJoints.size())
return 0;
return AllJoints[number]->Name.c_str();
}
//! Gets a joint number from its name
s32 CSkinnedMesh::getJointNumber(const c8* name) const
{
for (u32 i=0; i<AllJoints.size(); ++i)
{
if (AllJoints[i]->Name == name)
return i;
}
return -1;
}
//! returns amount of mesh buffers.
u32 CSkinnedMesh::getMeshBufferCount() const
{
return LocalBuffers.size();
}
//! returns pointer to a mesh buffer
IMeshBuffer* CSkinnedMesh::getMeshBuffer(u32 nr) const
{
if (nr < LocalBuffers.size())
return LocalBuffers[nr];
else
return 0;
}
//! Returns pointer to a mesh buffer which fits a material
IMeshBuffer* CSkinnedMesh::getMeshBuffer(const video::SMaterial &material) const
{
for (u32 i=0; i<LocalBuffers.size(); ++i)
{
if (LocalBuffers[i]->getMaterial() == material)
return LocalBuffers[i];
}
return 0;
}
//! returns an axis aligned bounding box
const core::aabbox3d<f32>& CSkinnedMesh::getBoundingBox() const
{
return BoundingBox;
}
//! set user axis aligned bounding box
void CSkinnedMesh::setBoundingBox( const core::aabbox3df& box)
{
BoundingBox = box;
}
//! sets a flag of all contained materials to a new value
void CSkinnedMesh::setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
{
for (u32 i=0; i<LocalBuffers.size(); ++i)
LocalBuffers[i]->Material.setFlag(flag,newvalue);
}
//! set the hardware mapping hint, for driver
void CSkinnedMesh::setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint,
E_BUFFER_TYPE buffer)
{
for (u32 i=0; i<LocalBuffers.size(); ++i)
LocalBuffers[i]->setHardwareMappingHint(newMappingHint, buffer);
}
//! flags the meshbuffer as changed, reloads hardware buffers
void CSkinnedMesh::setDirty(E_BUFFER_TYPE buffer)
{
for (u32 i=0; i<LocalBuffers.size(); ++i)
LocalBuffers[i]->setDirty(buffer);
}
//! uses animation from another mesh
bool CSkinnedMesh::useAnimationFrom(const ISkinnedMesh *mesh)
{
bool unmatched=false;
for(u32 i=0;i<AllJoints.size();++i)
{
SJoint *joint=AllJoints[i];
joint->UseAnimationFrom=0;
if (joint->Name=="")
unmatched=true;
else
{
for(u32 j=0;j<mesh->getAllJoints().size();++j)
{
SJoint *otherJoint=mesh->getAllJoints()[j];
if (joint->Name==otherJoint->Name)
{
joint->UseAnimationFrom=otherJoint;
}
}
if (!joint->UseAnimationFrom)
unmatched=true;
}
}
checkForAnimation();
return !unmatched;
}
//!Update Normals when Animating
//!False= Don't animate them, faster
//!True= Update normals (default)
void CSkinnedMesh::updateNormalsWhenAnimating(bool on)
{
AnimateNormals = on;
}
//!Sets Interpolation Mode
void CSkinnedMesh::setInterpolationMode(E_INTERPOLATION_MODE mode)
{
InterpolationMode = mode;
}
core::array<scene::SSkinMeshBuffer*> &CSkinnedMesh::getMeshBuffers()
{
return LocalBuffers;
}
core::array<CSkinnedMesh::SJoint*> &CSkinnedMesh::getAllJoints()
{
return AllJoints;
}
const core::array<CSkinnedMesh::SJoint*> &CSkinnedMesh::getAllJoints() const
{
return AllJoints;
}
//! (This feature is not implementated in irrlicht yet)
bool CSkinnedMesh::setHardwareSkinning(bool on)
{
if (HardwareSkinning!=on)
{
if (on)
toStaticPose();
HardwareSkinning=on;
}
return HardwareSkinning;
}
void CSkinnedMesh::toStaticPose()
{
for (u32 i=0; i<AllJoints.size(); ++i)
{
SJoint *joint=AllJoints[i];
for (u32 j=0; j<joint->Weights.size(); ++j)
{
const u16 buffer_id=joint->Weights[j].buffer_id;
const u32 vertex_id=joint->Weights[j].vertex_id;
LocalBuffers[buffer_id]->getVertex(vertex_id)->Pos = joint->Weights[j].StaticPos;
LocalBuffers[buffer_id]->getVertex(vertex_id)->Normal = joint->Weights[j].StaticNormal;
LocalBuffers[buffer_id]->boundingBoxNeedsRecalculated();
}
}
}
void CSkinnedMesh::calculateGlobalMatrices(SJoint *joint,SJoint *parentJoint)
{
if (!joint && parentJoint) // bit of protection from endless loops
return;
//Go through the root bones
if (!joint)
{
for (u32 i=0; i<RootJoints.size(); ++i)
calculateGlobalMatrices(RootJoints[i],0);
return;
}
if (!parentJoint)
joint->GlobalMatrix = joint->LocalMatrix;
else
joint->GlobalMatrix = parentJoint->GlobalMatrix * joint->LocalMatrix;
joint->LocalAnimatedMatrix=joint->LocalMatrix;
joint->GlobalAnimatedMatrix=joint->GlobalMatrix;
if (joint->GlobalInversedMatrix.isIdentity())//might be pre calculated
{
joint->GlobalInversedMatrix = joint->GlobalMatrix;
joint->GlobalInversedMatrix.makeInverse(); // slow
}
for (u32 j=0; j<joint->Children.size(); ++j)
calculateGlobalMatrices(joint->Children[j],joint);
SkinnedLastFrame=false;
}
void CSkinnedMesh::checkForAnimation()
{
u32 i,j;
//Check for animation...
HasAnimation = false;
for(i=0;i<AllJoints.size();++i)
{
if (AllJoints[i]->UseAnimationFrom)
{
if (AllJoints[i]->UseAnimationFrom->PositionKeys.size() ||
AllJoints[i]->UseAnimationFrom->ScaleKeys.size() ||
AllJoints[i]->UseAnimationFrom->RotationKeys.size() )
{
HasAnimation = true;
break;
}
}
}
//meshes with weights, are still counted as animated for ragdolls, etc
if (!HasAnimation)
{
for(i=0;i<AllJoints.size();++i)
{
if (AllJoints[i]->Weights.size())
{
HasAnimation = true;
break;
}
}
}
if (HasAnimation)
{
//--- Find the length of the animation ---
AnimationFrames=0;
for(i=0;i<AllJoints.size();++i)
{
if (AllJoints[i]->UseAnimationFrom)
{
if (AllJoints[i]->UseAnimationFrom->PositionKeys.size())
if (AllJoints[i]->UseAnimationFrom->PositionKeys.getLast().frame > AnimationFrames)
AnimationFrames=AllJoints[i]->UseAnimationFrom->PositionKeys.getLast().frame;
if (AllJoints[i]->UseAnimationFrom->ScaleKeys.size())
if (AllJoints[i]->UseAnimationFrom->ScaleKeys.getLast().frame > AnimationFrames)
AnimationFrames=AllJoints[i]->UseAnimationFrom->ScaleKeys.getLast().frame;
if (AllJoints[i]->UseAnimationFrom->RotationKeys.size())
if (AllJoints[i]->UseAnimationFrom->RotationKeys.getLast().frame > AnimationFrames)
AnimationFrames=AllJoints[i]->UseAnimationFrom->RotationKeys.getLast().frame;
}
}
}
if (HasAnimation && !PreparedForSkinning)
{
PreparedForSkinning=true;
//check for bugs:
for(i=0; i < AllJoints.size(); ++i)
{
SJoint *joint = AllJoints[i];
for (j=0; j<joint->Weights.size(); ++j)
{
const u16 buffer_id=joint->Weights[j].buffer_id;
const u32 vertex_id=joint->Weights[j].vertex_id;
//check for invalid ids
if (buffer_id>=LocalBuffers.size())
{
os::Printer::log("Skinned Mesh: Weight buffer id too large", ELL_WARNING);
joint->Weights[j].buffer_id = joint->Weights[j].vertex_id =0;
}
else if (vertex_id>=LocalBuffers[buffer_id]->getVertexCount())
{
os::Printer::log("Skinned Mesh: Weight vertex id too large", ELL_WARNING);
joint->Weights[j].buffer_id = joint->Weights[j].vertex_id =0;
}
}
}
//An array used in skinning
for (i=0; i<Vertices_Moved.size(); ++i)
for (j=0; j<Vertices_Moved[i].size(); ++j)
Vertices_Moved[i][j] = false;
// For skinning: cache weight values for speed
for (i=0; i<AllJoints.size(); ++i)
{
SJoint *joint = AllJoints[i];
for (j=0; j<joint->Weights.size(); ++j)
{
const u16 buffer_id=joint->Weights[j].buffer_id;
const u32 vertex_id=joint->Weights[j].vertex_id;
joint->Weights[j].Moved = &Vertices_Moved[buffer_id] [vertex_id];
joint->Weights[j].StaticPos = LocalBuffers[buffer_id]->getVertex(vertex_id)->Pos;
joint->Weights[j].StaticNormal = LocalBuffers[buffer_id]->getVertex(vertex_id)->Normal;
//joint->Weights[j]._Pos=&Buffers[buffer_id]->getVertex(vertex_id)->Pos;
}
}
// normalize weights
normalizeWeights();
}
SkinnedLastFrame=false;
}
//! called by loader after populating with mesh and bone data
void CSkinnedMesh::finalize()
{
u32 i;
// Make sure we recalc the next frame
LastAnimatedFrame=-1;
SkinnedLastFrame=false;
//calculate bounding box
for (i=0; i<LocalBuffers.size(); ++i)
{
LocalBuffers[i]->recalculateBoundingBox();
}
if (AllJoints.size() || RootJoints.size())
{
// populate AllJoints or RootJoints, depending on which is empty
if (!RootJoints.size())
{
for(u32 CheckingIdx=0; CheckingIdx < AllJoints.size(); ++CheckingIdx)
{
bool foundParent=false;