1 // Copyright (C) 2002-2012 Nikolaus Gebhardt
\r
2 // This file is part of the "Irrlicht Engine".
\r
3 // For conditions of distribution and use, see copyright notice in irrlicht.h
\r
5 #include "IrrCompileConfig.h"
\r
6 #ifdef _IRR_COMPILE_WITH_SKINNED_MESH_SUPPORT_
\r
8 #include "CSkinnedMesh.h"
\r
9 #include "CBoneSceneNode.h"
\r
10 #include "IAnimatedMeshSceneNode.h"
\r
15 // Frames must always be increasing, so we remove objects where this isn't the case
\r
16 // return number of kicked keys
\r
17 template <class T> // T = objects containing a "frame" variable
\r
18 irr::u32 dropBadKeys(irr::core::array<T>& array)
\r
23 irr::u32 n=1; // new index
\r
24 for(irr::u32 j=1;j<array.size();++j)
\r
26 if (array[j].frame < array[n-1].frame)
\r
27 continue; //bad frame, unneeded and may cause problems
\r
29 array[n] = array[j];
\r
32 irr::u32 d = array.size()-n; // remove already copied keys
\r
40 // drop identical middle keys - we only need the first and last
\r
41 // return number of kicked keys
\r
42 template <class T, typename Cmp> // Cmp = comparison for keys of type T
\r
43 irr::u32 dropMiddleKeys(irr::core::array<T>& array, Cmp & cmp)
\r
45 if ( array.size() < 3 )
\r
48 irr::u32 s = 0; // old index for current key
\r
49 irr::u32 n = 1; // new index for next key
\r
50 for(irr::u32 j=1;j<array.size();++j)
\r
52 if ( cmp(array[j], array[s]) )
\r
53 continue; // same key, handle later
\r
55 if ( j > s+1 ) // had there been identical keys?
\r
56 array[n++] = array[j-1]; // keep the last
\r
57 array[n++] = array[j]; // keep the new one
\r
60 if ( array.size() > s+1 ) // identical keys at the array end?
\r
61 array[n++] = array[array.size()-1]; // keep the last
\r
63 irr::u32 d = array.size()-n; // remove already copied keys
\r
71 bool identicalPos(const irr::scene::ISkinnedMesh::SPositionKey& a, const irr::scene::ISkinnedMesh::SPositionKey& b)
\r
73 return a.position == b.position;
\r
76 bool identicalScale(const irr::scene::ISkinnedMesh::SScaleKey& a, const irr::scene::ISkinnedMesh::SScaleKey& b)
\r
78 return a.scale == b.scale;
\r
81 bool identicalRotation(const irr::scene::ISkinnedMesh::SRotationKey& a, const irr::scene::ISkinnedMesh::SRotationKey& b)
\r
83 return a.rotation == b.rotation;
\r
94 CSkinnedMesh::CSkinnedMesh()
\r
95 : SkinningBuffers(0), EndFrame(0.f), FramesPerSecond(25.f),
\r
96 LastAnimatedFrame(-1), SkinnedLastFrame(false),
\r
97 InterpolationMode(EIM_LINEAR),
\r
98 HasAnimation(false), PreparedForSkinning(false),
\r
99 AnimateNormals(true), HardwareSkinning(false)
\r
102 setDebugName("CSkinnedMesh");
\r
105 SkinningBuffers=&LocalBuffers;
\r
110 CSkinnedMesh::~CSkinnedMesh()
\r
112 for (u32 i=0; i<AllJoints.size(); ++i)
\r
113 delete AllJoints[i];
\r
115 for (u32 j=0; j<LocalBuffers.size(); ++j)
\r
117 if (LocalBuffers[j])
\r
118 LocalBuffers[j]->drop();
\r
123 //! returns the amount of frames in milliseconds.
\r
124 //! If the amount is 1, it is a static (=non animated) mesh.
\r
125 u32 CSkinnedMesh::getFrameCount() const
\r
127 return core::floor32(EndFrame+1.f);
\r
131 //! Gets the default animation speed of the animated mesh.
\r
132 /** \return Amount of frames per second. If the amount is 0, it is a static, non animated mesh. */
\r
133 f32 CSkinnedMesh::getAnimationSpeed() const
\r
135 return FramesPerSecond;
\r
139 //! Gets the frame count of the animated mesh.
\r
140 /** \param fps Frames per second to play the animation with. If the amount is 0, it is not animated.
\r
141 The actual speed is set in the scene node the mesh is instantiated in.*/
\r
142 void CSkinnedMesh::setAnimationSpeed(f32 fps)
\r
144 FramesPerSecond=fps;
\r
148 //! 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.
\r
149 IMesh* CSkinnedMesh::getMesh(s32 frame, s32 detailLevel, s32 startFrameLoop, s32 endFrameLoop)
\r
151 //animate(frame,startFrameLoop, endFrameLoop);
\r
155 animateMesh((f32)frame, 1.0f);
\r
161 //--------------------------------------------------------------------------
\r
162 // Keyframe Animation
\r
163 //--------------------------------------------------------------------------
\r
166 //! Animates this mesh's joints based on frame input
\r
167 //! blend: {0-old position, 1-New position}
\r
168 void CSkinnedMesh::animateMesh(f32 frame, f32 blend)
\r
170 if (!HasAnimation || LastAnimatedFrame==frame)
\r
173 LastAnimatedFrame=frame;
\r
174 SkinnedLastFrame=false;
\r
177 return; //No need to animate
\r
179 for (u32 i=0; i<AllJoints.size(); ++i)
\r
181 //The joints can be animated here with no input from their
\r
182 //parents, but for setAnimationMode extra checks are needed
\r
184 SJoint *joint = AllJoints[i];
\r
186 const core::vector3df oldPosition = joint->Animatedposition;
\r
187 const core::vector3df oldScale = joint->Animatedscale;
\r
188 const core::quaternion oldRotation = joint->Animatedrotation;
\r
190 core::vector3df position = oldPosition;
\r
191 core::vector3df scale = oldScale;
\r
192 core::quaternion rotation = oldRotation;
\r
194 getFrameData(frame, joint,
\r
195 position, joint->positionHint,
\r
196 scale, joint->scaleHint,
\r
197 rotation, joint->rotationHint);
\r
201 //No blending needed
\r
202 joint->Animatedposition = position;
\r
203 joint->Animatedscale = scale;
\r
204 joint->Animatedrotation = rotation;
\r
209 joint->Animatedposition = core::lerp(oldPosition, position, blend);
\r
210 joint->Animatedscale = core::lerp(oldScale, scale, blend);
\r
211 joint->Animatedrotation.slerp(oldRotation, rotation, blend);
\r
216 //LocalAnimatedMatrix needs to be built at some point, but this function may be called lots of times for
\r
217 //one render (to play two animations at the same time) LocalAnimatedMatrix only needs to be built once.
\r
218 //a call to buildAllLocalAnimatedMatrices is needed before skinning the mesh, and before the user gets the joints to move
\r
222 buildAllLocalAnimatedMatrices();
\r
223 //-----------------
\r
225 updateBoundingBox();
\r
229 void CSkinnedMesh::buildAllLocalAnimatedMatrices()
\r
231 for (u32 i=0; i<AllJoints.size(); ++i)
\r
233 SJoint *joint = AllJoints[i];
\r
237 if (joint->UseAnimationFrom &&
\r
238 (joint->UseAnimationFrom->PositionKeys.size() ||
\r
239 joint->UseAnimationFrom->ScaleKeys.size() ||
\r
240 joint->UseAnimationFrom->RotationKeys.size() ))
\r
242 joint->GlobalSkinningSpace=false;
\r
244 // IRR_TEST_BROKEN_QUATERNION_USE: TODO - switched to getMatrix_transposed instead of getMatrix for downward compatibility.
\r
245 // Not tested so far if this was correct or wrong before quaternion fix!
\r
246 joint->Animatedrotation.getMatrix_transposed(joint->LocalAnimatedMatrix);
\r
248 // --- joint->LocalAnimatedMatrix *= joint->Animatedrotation.getMatrix() ---
\r
249 f32 *m1 = joint->LocalAnimatedMatrix.pointer();
\r
250 core::vector3df &Pos = joint->Animatedposition;
\r
251 m1[0] += Pos.X*m1[3];
\r
252 m1[1] += Pos.Y*m1[3];
\r
253 m1[2] += Pos.Z*m1[3];
\r
254 m1[4] += Pos.X*m1[7];
\r
255 m1[5] += Pos.Y*m1[7];
\r
256 m1[6] += Pos.Z*m1[7];
\r
257 m1[8] += Pos.X*m1[11];
\r
258 m1[9] += Pos.Y*m1[11];
\r
259 m1[10] += Pos.Z*m1[11];
\r
260 m1[12] += Pos.X*m1[15];
\r
261 m1[13] += Pos.Y*m1[15];
\r
262 m1[14] += Pos.Z*m1[15];
\r
263 // -----------------------------------
\r
265 if (joint->ScaleKeys.size())
\r
268 core::matrix4 scaleMatrix;
\r
269 scaleMatrix.setScale(joint->Animatedscale);
\r
270 joint->LocalAnimatedMatrix *= scaleMatrix;
\r
273 // -------- joint->LocalAnimatedMatrix *= scaleMatrix -----------------
\r
274 core::matrix4& mat = joint->LocalAnimatedMatrix;
\r
275 mat[0] *= joint->Animatedscale.X;
\r
276 mat[1] *= joint->Animatedscale.X;
\r
277 mat[2] *= joint->Animatedscale.X;
\r
278 mat[3] *= joint->Animatedscale.X;
\r
279 mat[4] *= joint->Animatedscale.Y;
\r
280 mat[5] *= joint->Animatedscale.Y;
\r
281 mat[6] *= joint->Animatedscale.Y;
\r
282 mat[7] *= joint->Animatedscale.Y;
\r
283 mat[8] *= joint->Animatedscale.Z;
\r
284 mat[9] *= joint->Animatedscale.Z;
\r
285 mat[10] *= joint->Animatedscale.Z;
\r
286 mat[11] *= joint->Animatedscale.Z;
\r
287 // -----------------------------------
\r
292 joint->LocalAnimatedMatrix=joint->LocalMatrix;
\r
295 SkinnedLastFrame=false;
\r
299 void CSkinnedMesh::buildAllGlobalAnimatedMatrices(SJoint *joint, SJoint *parentJoint)
\r
303 for (u32 i=0; i<RootJoints.size(); ++i)
\r
304 buildAllGlobalAnimatedMatrices(RootJoints[i], 0);
\r
309 // Find global matrix...
\r
310 if (!parentJoint || joint->GlobalSkinningSpace)
\r
311 joint->GlobalAnimatedMatrix = joint->LocalAnimatedMatrix;
\r
313 joint->GlobalAnimatedMatrix = parentJoint->GlobalAnimatedMatrix * joint->LocalAnimatedMatrix;
\r
316 for (u32 j=0; j<joint->Children.size(); ++j)
\r
317 buildAllGlobalAnimatedMatrices(joint->Children[j], joint);
\r
321 void CSkinnedMesh::getFrameData(f32 frame, SJoint *joint,
\r
322 core::vector3df &position, s32 &positionHint,
\r
323 core::vector3df &scale, s32 &scaleHint,
\r
324 core::quaternion &rotation, s32 &rotationHint)
\r
326 s32 foundPositionIndex = -1;
\r
327 s32 foundScaleIndex = -1;
\r
328 s32 foundRotationIndex = -1;
\r
330 if (joint->UseAnimationFrom)
\r
332 const core::array<SPositionKey> &PositionKeys=joint->UseAnimationFrom->PositionKeys;
\r
333 const core::array<SScaleKey> &ScaleKeys=joint->UseAnimationFrom->ScaleKeys;
\r
334 const core::array<SRotationKey> &RotationKeys=joint->UseAnimationFrom->RotationKeys;
\r
336 if (PositionKeys.size())
\r
338 foundPositionIndex = -1;
\r
340 //Test the Hints...
\r
341 if (positionHint>=0 && (u32)positionHint < PositionKeys.size())
\r
344 if (positionHint>0 && PositionKeys[positionHint].frame>=frame && PositionKeys[positionHint-1].frame<frame )
\r
345 foundPositionIndex=positionHint;
\r
346 else if (positionHint+1 < (s32)PositionKeys.size())
\r
348 //check the next index
\r
349 if ( PositionKeys[positionHint+1].frame>=frame &&
\r
350 PositionKeys[positionHint+0].frame<frame)
\r
353 foundPositionIndex=positionHint;
\r
358 //The hint test failed, do a full scan...
\r
359 if (foundPositionIndex==-1)
\r
361 for (u32 i=0; i<PositionKeys.size(); ++i)
\r
363 if (PositionKeys[i].frame >= frame) //Keys should to be sorted by frame
\r
365 foundPositionIndex=i;
\r
372 //Do interpolation...
\r
373 if (foundPositionIndex!=-1)
\r
375 if (InterpolationMode==EIM_CONSTANT || foundPositionIndex==0)
\r
377 position = PositionKeys[foundPositionIndex].position;
\r
379 else if (InterpolationMode==EIM_LINEAR)
\r
381 const SPositionKey& KeyA = PositionKeys[foundPositionIndex];
\r
382 const SPositionKey& KeyB = PositionKeys[foundPositionIndex-1];
\r
384 const f32 fd1 = frame - KeyA.frame;
\r
385 const f32 fd2 = KeyB.frame - frame;
\r
386 position = ((KeyB.position-KeyA.position)/(fd1+fd2))*fd1 + KeyA.position;
\r
391 //------------------------------------------------------------
\r
393 if (ScaleKeys.size())
\r
395 foundScaleIndex = -1;
\r
397 //Test the Hints...
\r
398 if (scaleHint>=0 && (u32)scaleHint < ScaleKeys.size())
\r
401 if (scaleHint>0 && ScaleKeys[scaleHint].frame>=frame && ScaleKeys[scaleHint-1].frame<frame )
\r
402 foundScaleIndex=scaleHint;
\r
403 else if (scaleHint+1 < (s32)ScaleKeys.size())
\r
405 //check the next index
\r
406 if ( ScaleKeys[scaleHint+1].frame>=frame &&
\r
407 ScaleKeys[scaleHint+0].frame<frame)
\r
410 foundScaleIndex=scaleHint;
\r
416 //The hint test failed, do a full scan...
\r
417 if (foundScaleIndex==-1)
\r
419 for (u32 i=0; i<ScaleKeys.size(); ++i)
\r
421 if (ScaleKeys[i].frame >= frame) //Keys should to be sorted by frame
\r
430 //Do interpolation...
\r
431 if (foundScaleIndex!=-1)
\r
433 if (InterpolationMode==EIM_CONSTANT || foundScaleIndex==0)
\r
435 scale = ScaleKeys[foundScaleIndex].scale;
\r
437 else if (InterpolationMode==EIM_LINEAR)
\r
439 const SScaleKey& KeyA = ScaleKeys[foundScaleIndex];
\r
440 const SScaleKey& KeyB = ScaleKeys[foundScaleIndex-1];
\r
442 const f32 fd1 = frame - KeyA.frame;
\r
443 const f32 fd2 = KeyB.frame - frame;
\r
444 scale = ((KeyB.scale-KeyA.scale)/(fd1+fd2))*fd1 + KeyA.scale;
\r
449 //-------------------------------------------------------------
\r
451 if (RotationKeys.size())
\r
453 foundRotationIndex = -1;
\r
455 //Test the Hints...
\r
456 if (rotationHint>=0 && (u32)rotationHint < RotationKeys.size())
\r
459 if (rotationHint>0 && RotationKeys[rotationHint].frame>=frame && RotationKeys[rotationHint-1].frame<frame )
\r
460 foundRotationIndex=rotationHint;
\r
461 else if (rotationHint+1 < (s32)RotationKeys.size())
\r
463 //check the next index
\r
464 if ( RotationKeys[rotationHint+1].frame>=frame &&
\r
465 RotationKeys[rotationHint+0].frame<frame)
\r
468 foundRotationIndex=rotationHint;
\r
474 //The hint test failed, do a full scan...
\r
475 if (foundRotationIndex==-1)
\r
477 for (u32 i=0; i<RotationKeys.size(); ++i)
\r
479 if (RotationKeys[i].frame >= frame) //Keys should be sorted by frame
\r
481 foundRotationIndex=i;
\r
488 //Do interpolation...
\r
489 if (foundRotationIndex!=-1)
\r
491 if (InterpolationMode==EIM_CONSTANT || foundRotationIndex==0)
\r
493 rotation = RotationKeys[foundRotationIndex].rotation;
\r
495 else if (InterpolationMode==EIM_LINEAR)
\r
497 const SRotationKey& KeyA = RotationKeys[foundRotationIndex];
\r
498 const SRotationKey& KeyB = RotationKeys[foundRotationIndex-1];
\r
500 const f32 fd1 = frame - KeyA.frame;
\r
501 const f32 fd2 = KeyB.frame - frame;
\r
502 const f32 t = fd1/(fd1+fd2);
\r
506 if (KeyA.frame!=KeyB.frame)
\r
507 t = (frame-KeyA.frame) / (KeyB.frame - KeyA.frame);
\r
510 rotation.slerp(KeyA.rotation, KeyB.rotation, t);
\r
517 //--------------------------------------------------------------------------
\r
518 // Software Skinning
\r
519 //--------------------------------------------------------------------------
\r
521 //! Preforms a software skin on this mesh based of joint positions
\r
522 void CSkinnedMesh::skinMesh()
\r
524 if (!HasAnimation || SkinnedLastFrame)
\r
528 // This is marked as "Temp!". A shiny dubloon to whomever can tell me why.
\r
529 buildAllGlobalAnimatedMatrices();
\r
530 //-----------------
\r
532 SkinnedLastFrame=true;
\r
533 if (!HardwareSkinning)
\r
535 //Software skin....
\r
539 for (i=0; i<AllJoints.size(); ++i)
\r
541 for (u32 j=0; j<AllJoints[i]->AttachedMeshes.size(); ++j)
\r
543 SSkinMeshBuffer* Buffer=(*SkinningBuffers)[ AllJoints[i]->AttachedMeshes[j] ];
\r
544 Buffer->Transformation=AllJoints[i]->GlobalAnimatedMatrix;
\r
548 //clear skinning helper array
\r
549 for (i=0; i<Vertices_Moved.size(); ++i)
\r
550 for (u32 j=0; j<Vertices_Moved[i].size(); ++j)
\r
551 Vertices_Moved[i][j]=false;
\r
553 //skin starting with the root joints
\r
554 for (i=0; i<RootJoints.size(); ++i)
\r
555 skinJoint(RootJoints[i], 0);
\r
557 for (i=0; i<SkinningBuffers->size(); ++i)
\r
558 (*SkinningBuffers)[i]->setDirty(EBT_VERTEX);
\r
560 updateBoundingBox();
\r
564 void CSkinnedMesh::skinJoint(SJoint *joint, SJoint *parentJoint)
\r
566 if (joint->Weights.size())
\r
568 //Find this joints pull on vertices...
\r
569 core::matrix4 jointVertexPull(core::matrix4::EM4CONST_NOTHING);
\r
570 jointVertexPull.setbyproduct(joint->GlobalAnimatedMatrix, joint->GlobalInversedMatrix);
\r
572 core::vector3df thisVertexMove, thisNormalMove;
\r
574 core::array<scene::SSkinMeshBuffer*> &buffersUsed=*SkinningBuffers;
\r
576 //Skin Vertices Positions and Normals...
\r
577 for (u32 i=0; i<joint->Weights.size(); ++i)
\r
579 SWeight& weight = joint->Weights[i];
\r
581 // Pull this vertex...
\r
582 jointVertexPull.transformVect(thisVertexMove, weight.StaticPos);
\r
584 if (AnimateNormals)
\r
585 jointVertexPull.rotateVect(thisNormalMove, weight.StaticNormal);
\r
587 if (! (*(weight.Moved)) )
\r
589 *(weight.Moved) = true;
\r
591 buffersUsed[weight.buffer_id]->getVertex(weight.vertex_id)->Pos = thisVertexMove * weight.strength;
\r
593 if (AnimateNormals)
\r
594 buffersUsed[weight.buffer_id]->getVertex(weight.vertex_id)->Normal = thisNormalMove * weight.strength;
\r
596 //*(weight._Pos) = thisVertexMove * weight.strength;
\r
600 buffersUsed[weight.buffer_id]->getVertex(weight.vertex_id)->Pos += thisVertexMove * weight.strength;
\r
602 if (AnimateNormals)
\r
603 buffersUsed[weight.buffer_id]->getVertex(weight.vertex_id)->Normal += thisNormalMove * weight.strength;
\r
605 //*(weight._Pos) += thisVertexMove * weight.strength;
\r
608 buffersUsed[weight.buffer_id]->boundingBoxNeedsRecalculated();
\r
612 //Skin all children
\r
613 for (u32 j=0; j<joint->Children.size(); ++j)
\r
614 skinJoint(joint->Children[j], joint);
\r
618 E_ANIMATED_MESH_TYPE CSkinnedMesh::getMeshType() const
\r
620 return EAMT_SKINNED;
\r
624 //! Gets joint count.
\r
625 u32 CSkinnedMesh::getJointCount() const
\r
627 return AllJoints.size();
\r
631 //! Gets the name of a joint.
\r
632 const c8* CSkinnedMesh::getJointName(u32 number) const
\r
634 if (number >= AllJoints.size())
\r
636 return AllJoints[number]->Name.c_str();
\r
640 //! Gets a joint number from its name
\r
641 s32 CSkinnedMesh::getJointNumber(const c8* name) const
\r
643 for (u32 i=0; i<AllJoints.size(); ++i)
\r
645 if (AllJoints[i]->Name == name)
\r
653 //! returns amount of mesh buffers.
\r
654 u32 CSkinnedMesh::getMeshBufferCount() const
\r
656 return LocalBuffers.size();
\r
660 //! returns pointer to a mesh buffer
\r
661 IMeshBuffer* CSkinnedMesh::getMeshBuffer(u32 nr) const
\r
663 if (nr < LocalBuffers.size())
\r
664 return LocalBuffers[nr];
\r
670 //! Returns pointer to a mesh buffer which fits a material
\r
671 IMeshBuffer* CSkinnedMesh::getMeshBuffer(const video::SMaterial &material) const
\r
673 for (u32 i=0; i<LocalBuffers.size(); ++i)
\r
675 if (LocalBuffers[i]->getMaterial() == material)
\r
676 return LocalBuffers[i];
\r
682 //! returns an axis aligned bounding box
\r
683 const core::aabbox3d<f32>& CSkinnedMesh::getBoundingBox() const
\r
685 return BoundingBox;
\r
689 //! set user axis aligned bounding box
\r
690 void CSkinnedMesh::setBoundingBox( const core::aabbox3df& box)
\r
696 //! sets a flag of all contained materials to a new value
\r
697 void CSkinnedMesh::setMaterialFlag(video::E_MATERIAL_FLAG flag, bool newvalue)
\r
699 for (u32 i=0; i<LocalBuffers.size(); ++i)
\r
700 LocalBuffers[i]->Material.setFlag(flag,newvalue);
\r
704 //! set the hardware mapping hint, for driver
\r
705 void CSkinnedMesh::setHardwareMappingHint(E_HARDWARE_MAPPING newMappingHint,
\r
706 E_BUFFER_TYPE buffer)
\r
708 for (u32 i=0; i<LocalBuffers.size(); ++i)
\r
709 LocalBuffers[i]->setHardwareMappingHint(newMappingHint, buffer);
\r
713 //! flags the meshbuffer as changed, reloads hardware buffers
\r
714 void CSkinnedMesh::setDirty(E_BUFFER_TYPE buffer)
\r
716 for (u32 i=0; i<LocalBuffers.size(); ++i)
\r
717 LocalBuffers[i]->setDirty(buffer);
\r
721 //! uses animation from another mesh
\r
722 bool CSkinnedMesh::useAnimationFrom(const ISkinnedMesh *mesh)
\r
724 bool unmatched=false;
\r
726 for(u32 i=0;i<AllJoints.size();++i)
\r
728 SJoint *joint=AllJoints[i];
\r
729 joint->UseAnimationFrom=0;
\r
731 if (joint->Name=="")
\r
735 for(u32 j=0;j<mesh->getAllJoints().size();++j)
\r
737 SJoint *otherJoint=mesh->getAllJoints()[j];
\r
738 if (joint->Name==otherJoint->Name)
\r
740 joint->UseAnimationFrom=otherJoint;
\r
743 if (!joint->UseAnimationFrom)
\r
748 checkForAnimation();
\r
754 //!Update Normals when Animating
\r
755 //!False= Don't animate them, faster
\r
756 //!True= Update normals (default)
\r
757 void CSkinnedMesh::updateNormalsWhenAnimating(bool on)
\r
759 AnimateNormals = on;
\r
763 //!Sets Interpolation Mode
\r
764 void CSkinnedMesh::setInterpolationMode(E_INTERPOLATION_MODE mode)
\r
766 InterpolationMode = mode;
\r
770 core::array<scene::SSkinMeshBuffer*> &CSkinnedMesh::getMeshBuffers()
\r
772 return LocalBuffers;
\r
776 core::array<CSkinnedMesh::SJoint*> &CSkinnedMesh::getAllJoints()
\r
782 const core::array<CSkinnedMesh::SJoint*> &CSkinnedMesh::getAllJoints() const
\r
788 //! (This feature is not implemented in irrlicht yet)
\r
789 bool CSkinnedMesh::setHardwareSkinning(bool on)
\r
791 if (HardwareSkinning!=on)
\r
796 //set mesh to static pose...
\r
797 for (u32 i=0; i<AllJoints.size(); ++i)
\r
799 SJoint *joint=AllJoints[i];
\r
800 for (u32 j=0; j<joint->Weights.size(); ++j)
\r
802 const u16 buffer_id=joint->Weights[j].buffer_id;
\r
803 const u32 vertex_id=joint->Weights[j].vertex_id;
\r
804 LocalBuffers[buffer_id]->getVertex(vertex_id)->Pos = joint->Weights[j].StaticPos;
\r
805 LocalBuffers[buffer_id]->getVertex(vertex_id)->Normal = joint->Weights[j].StaticNormal;
\r
806 LocalBuffers[buffer_id]->boundingBoxNeedsRecalculated();
\r
811 HardwareSkinning=on;
\r
813 return HardwareSkinning;
\r
816 void CSkinnedMesh::refreshJointCache()
\r
818 //copy cache from the mesh...
\r
819 for (u32 i=0; i<AllJoints.size(); ++i)
\r
821 SJoint *joint=AllJoints[i];
\r
822 for (u32 j=0; j<joint->Weights.size(); ++j)
\r
824 const u16 buffer_id=joint->Weights[j].buffer_id;
\r
825 const u32 vertex_id=joint->Weights[j].vertex_id;
\r
826 joint->Weights[j].StaticPos = LocalBuffers[buffer_id]->getVertex(vertex_id)->Pos;
\r
827 joint->Weights[j].StaticNormal = LocalBuffers[buffer_id]->getVertex(vertex_id)->Normal;
\r
832 void CSkinnedMesh::calculateGlobalMatrices(SJoint *joint,SJoint *parentJoint)
\r
834 if (!joint && parentJoint) // bit of protection from endless loops
\r
837 //Go through the root bones
\r
840 for (u32 i=0; i<RootJoints.size(); ++i)
\r
841 calculateGlobalMatrices(RootJoints[i],0);
\r
846 joint->GlobalMatrix = joint->LocalMatrix;
\r
848 joint->GlobalMatrix = parentJoint->GlobalMatrix * joint->LocalMatrix;
\r
850 joint->LocalAnimatedMatrix=joint->LocalMatrix;
\r
851 joint->GlobalAnimatedMatrix=joint->GlobalMatrix;
\r
853 if (joint->GlobalInversedMatrix.isIdentity())//might be pre calculated
\r
855 joint->GlobalInversedMatrix = joint->GlobalMatrix;
\r
856 joint->GlobalInversedMatrix.makeInverse(); // slow
\r
859 for (u32 j=0; j<joint->Children.size(); ++j)
\r
860 calculateGlobalMatrices(joint->Children[j],joint);
\r
861 SkinnedLastFrame=false;
\r
865 void CSkinnedMesh::checkForAnimation()
\r
868 //Check for animation...
\r
869 HasAnimation = false;
\r
870 for(i=0;i<AllJoints.size();++i)
\r
872 if (AllJoints[i]->UseAnimationFrom)
\r
874 if (AllJoints[i]->UseAnimationFrom->PositionKeys.size() ||
\r
875 AllJoints[i]->UseAnimationFrom->ScaleKeys.size() ||
\r
876 AllJoints[i]->UseAnimationFrom->RotationKeys.size() )
\r
878 HasAnimation = true;
\r
883 //meshes with weights, are still counted as animated for ragdolls, etc
\r
886 for(i=0;i<AllJoints.size();++i)
\r
888 if (AllJoints[i]->Weights.size())
\r
889 HasAnimation = true;
\r
895 //--- Find the length of the animation ---
\r
897 for(i=0;i<AllJoints.size();++i)
\r
899 if (AllJoints[i]->UseAnimationFrom)
\r
901 if (AllJoints[i]->UseAnimationFrom->PositionKeys.size())
\r
902 if (AllJoints[i]->UseAnimationFrom->PositionKeys.getLast().frame > EndFrame)
\r
903 EndFrame=AllJoints[i]->UseAnimationFrom->PositionKeys.getLast().frame;
\r
905 if (AllJoints[i]->UseAnimationFrom->ScaleKeys.size())
\r
906 if (AllJoints[i]->UseAnimationFrom->ScaleKeys.getLast().frame > EndFrame)
\r
907 EndFrame=AllJoints[i]->UseAnimationFrom->ScaleKeys.getLast().frame;
\r
909 if (AllJoints[i]->UseAnimationFrom->RotationKeys.size())
\r
910 if (AllJoints[i]->UseAnimationFrom->RotationKeys.getLast().frame > EndFrame)
\r
911 EndFrame=AllJoints[i]->UseAnimationFrom->RotationKeys.getLast().frame;
\r
916 if (HasAnimation && !PreparedForSkinning)
\r
918 PreparedForSkinning=true;
\r
921 for(i=0; i < AllJoints.size(); ++i)
\r
923 SJoint *joint = AllJoints[i];
\r
924 for (j=0; j<joint->Weights.size(); ++j)
\r
926 const u16 buffer_id=joint->Weights[j].buffer_id;
\r
927 const u32 vertex_id=joint->Weights[j].vertex_id;
\r
929 //check for invalid ids
\r
930 if (buffer_id>=LocalBuffers.size())
\r
932 os::Printer::log("Skinned Mesh: Weight buffer id too large", ELL_WARNING);
\r
933 joint->Weights[j].buffer_id = joint->Weights[j].vertex_id =0;
\r
935 else if (vertex_id>=LocalBuffers[buffer_id]->getVertexCount())
\r
937 os::Printer::log("Skinned Mesh: Weight vertex id too large", ELL_WARNING);
\r
938 joint->Weights[j].buffer_id = joint->Weights[j].vertex_id =0;
\r
943 //An array used in skinning
\r
945 for (i=0; i<Vertices_Moved.size(); ++i)
\r
946 for (j=0; j<Vertices_Moved[i].size(); ++j)
\r
947 Vertices_Moved[i][j] = false;
\r
949 // For skinning: cache weight values for speed
\r
951 for (i=0; i<AllJoints.size(); ++i)
\r
953 SJoint *joint = AllJoints[i];
\r
954 for (j=0; j<joint->Weights.size(); ++j)
\r
956 const u16 buffer_id=joint->Weights[j].buffer_id;
\r
957 const u32 vertex_id=joint->Weights[j].vertex_id;
\r
959 joint->Weights[j].Moved = &Vertices_Moved[buffer_id] [vertex_id];
\r
960 joint->Weights[j].StaticPos = LocalBuffers[buffer_id]->getVertex(vertex_id)->Pos;
\r
961 joint->Weights[j].StaticNormal = LocalBuffers[buffer_id]->getVertex(vertex_id)->Normal;
\r
963 //joint->Weights[j]._Pos=&Buffers[buffer_id]->getVertex(vertex_id)->Pos;
\r
967 // normalize weights
\r
968 normalizeWeights();
\r
970 SkinnedLastFrame=false;
\r
973 //! called by loader after populating with mesh and bone data
\r
974 void CSkinnedMesh::finalize()
\r
976 os::Printer::log("Skinned Mesh - finalize", ELL_DEBUG);
\r
979 // Make sure we recalc the next frame
\r
980 LastAnimatedFrame=-1;
\r
981 SkinnedLastFrame=false;
\r
983 //calculate bounding box
\r
984 for (i=0; i<LocalBuffers.size(); ++i)
\r
986 LocalBuffers[i]->recalculateBoundingBox();
\r
989 if (AllJoints.size() || RootJoints.size())
\r
991 // populate AllJoints or RootJoints, depending on which is empty
\r
992 if (!RootJoints.size())
\r
995 for(u32 CheckingIdx=0; CheckingIdx < AllJoints.size(); ++CheckingIdx)
\r
998 bool foundParent=false;
\r
999 for(i=0; i < AllJoints.size(); ++i)
\r
1001 for(u32 n=0; n < AllJoints[i]->Children.size(); ++n)
\r
1003 if (AllJoints[i]->Children[n] == AllJoints[CheckingIdx])
\r
1009 RootJoints.push_back(AllJoints[CheckingIdx]);
\r
1014 AllJoints=RootJoints;
\r
1018 for(i=0; i < AllJoints.size(); ++i)
\r
1020 AllJoints[i]->UseAnimationFrom=AllJoints[i];
\r
1023 //Set array sizes...
\r
1025 for (i=0; i<LocalBuffers.size(); ++i)
\r
1027 Vertices_Moved.push_back( core::array<bool>() );
\r
1028 Vertices_Moved[i].set_used(LocalBuffers[i]->getVertexCount());
\r
1031 checkForAnimation();
\r
1035 irr::u32 redundantPosKeys = 0;
\r
1036 irr::u32 unorderedPosKeys = 0;
\r
1037 irr::u32 redundantScaleKeys = 0;
\r
1038 irr::u32 unorderedScaleKeys = 0;
\r
1039 irr::u32 redundantRotationKeys = 0;
\r
1040 irr::u32 unorderedRotationKeys = 0;
\r
1042 //--- optimize and check keyframes ---
\r
1043 for(i=0;i<AllJoints.size();++i)
\r
1045 core::array<SPositionKey> &PositionKeys =AllJoints[i]->PositionKeys;
\r
1046 core::array<SScaleKey> &ScaleKeys = AllJoints[i]->ScaleKeys;
\r
1047 core::array<SRotationKey> &RotationKeys = AllJoints[i]->RotationKeys;
\r
1049 // redundant = identical middle keys - we only need the first and last frame
\r
1050 // unordered = frames which are out of order - we can't handle those
\r
1051 redundantPosKeys += dropMiddleKeys<SPositionKey>(PositionKeys, identicalPos);
\r
1052 unorderedPosKeys += dropBadKeys<SPositionKey>(PositionKeys);
\r
1053 redundantScaleKeys += dropMiddleKeys<SScaleKey>(ScaleKeys, identicalScale);
\r
1054 unorderedScaleKeys += dropBadKeys<SScaleKey>(ScaleKeys);
\r
1055 redundantRotationKeys += dropMiddleKeys<SRotationKey>(RotationKeys, identicalRotation);
\r
1056 unorderedRotationKeys += dropBadKeys<SRotationKey>(RotationKeys);
\r
1058 //Fill empty keyframe areas
\r
1059 if (PositionKeys.size())
\r
1061 SPositionKey *Key;
\r
1062 Key=&PositionKeys[0];//getFirst
\r
1063 if (Key->frame!=0)
\r
1065 PositionKeys.push_front(*Key);
\r
1066 Key=&PositionKeys[0];//getFirst
\r
1070 Key=&PositionKeys.getLast();
\r
1071 if (Key->frame!=EndFrame)
\r
1073 PositionKeys.push_back(*Key);
\r
1074 Key=&PositionKeys.getLast();
\r
1075 Key->frame=EndFrame;
\r
1079 if (ScaleKeys.size())
\r
1082 Key=&ScaleKeys[0];//getFirst
\r
1083 if (Key->frame!=0)
\r
1085 ScaleKeys.push_front(*Key);
\r
1086 Key=&ScaleKeys[0];//getFirst
\r
1090 Key=&ScaleKeys.getLast();
\r
1091 if (Key->frame!=EndFrame)
\r
1093 ScaleKeys.push_back(*Key);
\r
1094 Key=&ScaleKeys.getLast();
\r
1095 Key->frame=EndFrame;
\r
1099 if (RotationKeys.size())
\r
1101 SRotationKey *Key;
\r
1102 Key=&RotationKeys[0];//getFirst
\r
1103 if (Key->frame!=0)
\r
1105 RotationKeys.push_front(*Key);
\r
1106 Key=&RotationKeys[0];//getFirst
\r
1110 Key=&RotationKeys.getLast();
\r
1111 if (Key->frame!=EndFrame)
\r
1113 RotationKeys.push_back(*Key);
\r
1114 Key=&RotationKeys.getLast();
\r
1115 Key->frame=EndFrame;
\r
1120 if ( redundantPosKeys > 0 )
\r
1122 os::Printer::log("Skinned Mesh - redundant position frames kicked:", core::stringc(redundantPosKeys).c_str(), ELL_DEBUG);
\r
1124 if ( unorderedPosKeys > 0 )
\r
1126 irr::os::Printer::log("Skinned Mesh - unsorted position frames kicked:", irr::core::stringc(unorderedPosKeys).c_str(), irr::ELL_DEBUG);
\r
1128 if ( redundantScaleKeys > 0 )
\r
1130 os::Printer::log("Skinned Mesh - redundant scale frames kicked:", core::stringc(redundantScaleKeys).c_str(), ELL_DEBUG);
\r
1132 if ( unorderedScaleKeys > 0 )
\r
1134 irr::os::Printer::log("Skinned Mesh - unsorted scale frames kicked:", irr::core::stringc(unorderedScaleKeys).c_str(), irr::ELL_DEBUG);
\r
1136 if ( redundantRotationKeys > 0 )
\r
1138 os::Printer::log("Skinned Mesh - redundant rotation frames kicked:", core::stringc(redundantRotationKeys).c_str(), ELL_DEBUG);
\r
1140 if ( unorderedRotationKeys > 0 )
\r
1142 irr::os::Printer::log("Skinned Mesh - unsorted rotation frames kicked:", irr::core::stringc(unorderedRotationKeys).c_str(), irr::ELL_DEBUG);
\r
1146 //Needed for animation and skinning...
\r
1148 calculateGlobalMatrices(0,0);
\r
1150 //animateMesh(0, 1);
\r
1151 //buildAllLocalAnimatedMatrices();
\r
1152 //buildAllGlobalAnimatedMatrices();
\r
1154 //rigid animation for non animated meshes
\r
1155 for (i=0; i<AllJoints.size(); ++i)
\r
1157 for (u32 j=0; j<AllJoints[i]->AttachedMeshes.size(); ++j)
\r
1159 SSkinMeshBuffer* Buffer=(*SkinningBuffers)[ AllJoints[i]->AttachedMeshes[j] ];
\r
1160 Buffer->Transformation=AllJoints[i]->GlobalAnimatedMatrix;
\r
1164 //calculate bounding box
\r
1165 if (LocalBuffers.empty())
\r
1166 BoundingBox.reset(0,0,0);
\r
1169 irr::core::aabbox3df bb(LocalBuffers[0]->BoundingBox);
\r
1170 LocalBuffers[0]->Transformation.transformBoxEx(bb);
\r
1171 BoundingBox.reset(bb);
\r
1173 for (u32 j=1; j<LocalBuffers.size(); ++j)
\r
1175 bb = LocalBuffers[j]->BoundingBox;
\r
1176 LocalBuffers[j]->Transformation.transformBoxEx(bb);
\r
1178 BoundingBox.addInternalBox(bb);
\r
1184 void CSkinnedMesh::updateBoundingBox(void)
\r
1186 if(!SkinningBuffers)
\r
1189 core::array<SSkinMeshBuffer*> & buffer = *SkinningBuffers;
\r
1190 BoundingBox.reset(0,0,0);
\r
1192 if (!buffer.empty())
\r
1194 for (u32 j=0; j<buffer.size(); ++j)
\r
1196 buffer[j]->recalculateBoundingBox();
\r
1197 core::aabbox3df bb = buffer[j]->BoundingBox;
\r
1198 buffer[j]->Transformation.transformBoxEx(bb);
\r
1200 BoundingBox.addInternalBox(bb);
\r
1206 scene::SSkinMeshBuffer *CSkinnedMesh::addMeshBuffer()
\r
1208 scene::SSkinMeshBuffer *buffer=new scene::SSkinMeshBuffer();
\r
1209 LocalBuffers.push_back(buffer);
\r
1214 CSkinnedMesh::SJoint *CSkinnedMesh::addJoint(SJoint *parent)
\r
1216 SJoint *joint=new SJoint;
\r
1218 AllJoints.push_back(joint);
\r
1221 //Add root joints to array in finalize()
\r
1225 //Set parent (Be careful of the mesh loader also setting the parent)
\r
1226 parent->Children.push_back(joint);
\r
1233 CSkinnedMesh::SPositionKey *CSkinnedMesh::addPositionKey(SJoint *joint)
\r
1238 joint->PositionKeys.push_back(SPositionKey());
\r
1239 return &joint->PositionKeys.getLast();
\r
1243 CSkinnedMesh::SScaleKey *CSkinnedMesh::addScaleKey(SJoint *joint)
\r
1248 joint->ScaleKeys.push_back(SScaleKey());
\r
1249 return &joint->ScaleKeys.getLast();
\r
1253 CSkinnedMesh::SRotationKey *CSkinnedMesh::addRotationKey(SJoint *joint)
\r
1258 joint->RotationKeys.push_back(SRotationKey());
\r
1259 return &joint->RotationKeys.getLast();
\r
1263 CSkinnedMesh::SWeight *CSkinnedMesh::addWeight(SJoint *joint)
\r
1268 joint->Weights.push_back(SWeight());
\r
1269 return &joint->Weights.getLast();
\r
1273 bool CSkinnedMesh::isStatic()
\r
1275 return !HasAnimation;
\r
1279 void CSkinnedMesh::normalizeWeights()
\r
1281 // note: unsure if weights ids are going to be used.
\r
1283 // Normalise the weights on bones....
\r
1286 core::array< core::array<f32> > verticesTotalWeight;
\r
1288 verticesTotalWeight.reallocate(LocalBuffers.size());
\r
1289 for (i=0; i<LocalBuffers.size(); ++i)
\r
1291 verticesTotalWeight.push_back(core::array<f32>());
\r
1292 verticesTotalWeight[i].set_used(LocalBuffers[i]->getVertexCount());
\r
1295 for (i=0; i<verticesTotalWeight.size(); ++i)
\r
1296 for (j=0; j<verticesTotalWeight[i].size(); ++j)
\r
1297 verticesTotalWeight[i][j] = 0;
\r
1299 for (i=0; i<AllJoints.size(); ++i)
\r
1301 SJoint *joint=AllJoints[i];
\r
1302 for (j=0; j<joint->Weights.size(); ++j)
\r
1304 if (joint->Weights[j].strength<=0)//Check for invalid weights
\r
1306 joint->Weights.erase(j);
\r
1311 verticesTotalWeight[joint->Weights[j].buffer_id] [joint->Weights[j].vertex_id] += joint->Weights[j].strength;
\r
1316 for (i=0; i<AllJoints.size(); ++i)
\r
1318 SJoint *joint=AllJoints[i];
\r
1319 for (j=0; j< joint->Weights.size(); ++j)
\r
1321 const f32 total = verticesTotalWeight[joint->Weights[j].buffer_id] [joint->Weights[j].vertex_id];
\r
1322 if (total != 0 && total != 1)
\r
1323 joint->Weights[j].strength /= total;
\r
1329 void CSkinnedMesh::recoverJointsFromMesh(core::array<IBoneSceneNode*> &jointChildSceneNodes)
\r
1331 for (u32 i=0; i<AllJoints.size(); ++i)
\r
1333 IBoneSceneNode* node=jointChildSceneNodes[i];
\r
1334 SJoint *joint=AllJoints[i];
\r
1335 node->setPosition(joint->LocalAnimatedMatrix.getTranslation());
\r
1336 node->setRotation(joint->LocalAnimatedMatrix.getRotationDegrees());
\r
1337 node->setScale(joint->LocalAnimatedMatrix.getScale());
\r
1339 node->positionHint=joint->positionHint;
\r
1340 node->scaleHint=joint->scaleHint;
\r
1341 node->rotationHint=joint->rotationHint;
\r
1343 node->updateAbsolutePosition();
\r
1348 void CSkinnedMesh::transferJointsToMesh(const core::array<IBoneSceneNode*> &jointChildSceneNodes)
\r
1350 for (u32 i=0; i<AllJoints.size(); ++i)
\r
1352 const IBoneSceneNode* const node=jointChildSceneNodes[i];
\r
1353 SJoint *joint=AllJoints[i];
\r
1355 joint->LocalAnimatedMatrix.setRotationDegrees(node->getRotation());
\r
1356 joint->LocalAnimatedMatrix.setTranslation(node->getPosition());
\r
1357 joint->LocalAnimatedMatrix *= core::matrix4().setScale(node->getScale());
\r
1359 joint->positionHint=node->positionHint;
\r
1360 joint->scaleHint=node->scaleHint;
\r
1361 joint->rotationHint=node->rotationHint;
\r
1363 joint->GlobalSkinningSpace=(node->getSkinningSpace()==EBSS_GLOBAL);
\r
1365 // Make sure we recalc the next frame
\r
1366 LastAnimatedFrame=-1;
\r
1367 SkinnedLastFrame=false;
\r
1371 void CSkinnedMesh::transferOnlyJointsHintsToMesh(const core::array<IBoneSceneNode*> &jointChildSceneNodes)
\r
1373 for (u32 i=0; i<AllJoints.size(); ++i)
\r
1375 const IBoneSceneNode* const node=jointChildSceneNodes[i];
\r
1376 SJoint *joint=AllJoints[i];
\r
1378 joint->positionHint=node->positionHint;
\r
1379 joint->scaleHint=node->scaleHint;
\r
1380 joint->rotationHint=node->rotationHint;
\r
1382 SkinnedLastFrame=false;
\r
1386 void CSkinnedMesh::addJoints(core::array<IBoneSceneNode*> &jointChildSceneNodes,
\r
1387 IAnimatedMeshSceneNode* node, ISceneManager* smgr)
\r
1389 //Create new joints
\r
1390 for (u32 i=0; i<AllJoints.size(); ++i)
\r
1392 jointChildSceneNodes.push_back(new CBoneSceneNode(0, smgr, 0, i, AllJoints[i]->Name.c_str()));
\r
1395 //Match up parents
\r
1396 for (u32 i=0; i<jointChildSceneNodes.size(); ++i)
\r
1398 const SJoint* const joint=AllJoints[i]; //should be fine
\r
1402 for (u32 j=0;(parentID==-1)&&(j<AllJoints.size());++j)
\r
1406 const SJoint* const parentTest=AllJoints[j];
\r
1407 for (u32 n=0; n<parentTest->Children.size(); ++n)
\r
1409 if (parentTest->Children[n]==joint)
\r
1418 IBoneSceneNode* bone=jointChildSceneNodes[i];
\r
1420 bone->setParent(jointChildSceneNodes[parentID]);
\r
1422 bone->setParent(node);
\r
1426 SkinnedLastFrame=false;
\r
1430 void CSkinnedMesh::convertMeshToTangents()
\r
1432 // now calculate tangents
\r
1433 for (u32 b=0; b < LocalBuffers.size(); ++b)
\r
1435 if (LocalBuffers[b])
\r
1437 LocalBuffers[b]->convertToTangents();
\r
1439 const s32 idxCnt = LocalBuffers[b]->getIndexCount();
\r
1441 u16* idx = LocalBuffers[b]->getIndices();
\r
1442 video::S3DVertexTangents* v =
\r
1443 (video::S3DVertexTangents*)LocalBuffers[b]->getVertices();
\r
1445 for (s32 i=0; i<idxCnt; i+=3)
\r
1447 calculateTangents(
\r
1448 v[idx[i+0]].Normal,
\r
1449 v[idx[i+0]].Tangent,
\r
1450 v[idx[i+0]].Binormal,
\r
1454 v[idx[i+0]].TCoords,
\r
1455 v[idx[i+1]].TCoords,
\r
1456 v[idx[i+2]].TCoords);
\r
1458 calculateTangents(
\r
1459 v[idx[i+1]].Normal,
\r
1460 v[idx[i+1]].Tangent,
\r
1461 v[idx[i+1]].Binormal,
\r
1465 v[idx[i+1]].TCoords,
\r
1466 v[idx[i+2]].TCoords,
\r
1467 v[idx[i+0]].TCoords);
\r
1469 calculateTangents(
\r
1470 v[idx[i+2]].Normal,
\r
1471 v[idx[i+2]].Tangent,
\r
1472 v[idx[i+2]].Binormal,
\r
1476 v[idx[i+2]].TCoords,
\r
1477 v[idx[i+0]].TCoords,
\r
1478 v[idx[i+1]].TCoords);
\r
1485 void CSkinnedMesh::calculateTangents(
\r
1486 core::vector3df& normal,
\r
1487 core::vector3df& tangent,
\r
1488 core::vector3df& binormal,
\r
1489 const core::vector3df& vt1, const core::vector3df& vt2, const core::vector3df& vt3, // vertices
\r
1490 const core::vector2df& tc1, const core::vector2df& tc2, const core::vector2df& tc3) // texture coords
\r
1492 core::vector3df v1 = vt1 - vt2;
\r
1493 core::vector3df v2 = vt3 - vt1;
\r
1494 normal = v2.crossProduct(v1);
\r
1495 normal.normalize();
\r
1499 f32 deltaX1 = tc1.X - tc2.X;
\r
1500 f32 deltaX2 = tc3.X - tc1.X;
\r
1501 binormal = (v1 * deltaX2) - (v2 * deltaX1);
\r
1502 binormal.normalize();
\r
1506 f32 deltaY1 = tc1.Y - tc2.Y;
\r
1507 f32 deltaY2 = tc3.Y - tc1.Y;
\r
1508 tangent = (v1 * deltaY2) - (v2 * deltaY1);
\r
1509 tangent.normalize();
\r
1513 core::vector3df txb = tangent.crossProduct(binormal);
\r
1514 if (txb.dotProduct(normal) < 0.0f)
\r
1517 binormal *= -1.0f;
\r
1522 } // end namespace scene
\r
1523 } // end namespace irr
\r
1525 #endif // _IRR_COMPILE_WITH_SKINNED_MESH_SUPPORT_
\r