2 * Copyright (C) 2009 Cedric Pinson <cedric.pinson@plopbyte.net>
3 * Copyright (C) 2017 Julien Valentin <mp3butcher@hotmail.com>
5 * This library is open source and may be redistributed and/or modified under
6 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
7 * (at your option) any later version. The full license is in LICENSE file
8 * included with this distribution, and on the openscenegraph.org website.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * OpenSceneGraph Public License for more details.
16#ifndef OSGANIMATION_RIGTRANSFORM_SOFTWARE
17#define OSGANIMATION_RIGTRANSFORM_SOFTWARE 1
19#include <osgAnimation/Export>
20#include <osgAnimation/RigTransform>
21#include <osgAnimation/Bone>
22#include <osgAnimation/VertexInfluence>
23#include <osg/observer_ptr>
30 /// This class manage format for software skinning
31 class OSGANIMATION_EXPORT RigTransformSoftware : public RigTransform
34 RigTransformSoftware();
35 RigTransformSoftware(const RigTransformSoftware& rts,const osg::CopyOp& copyop);
37 META_Object(osgAnimation,RigTransformSoftware)
39 virtual void operator()(RigGeometry&);
40 //to call when a skeleton is reacheable from the rig to prepare technic data
41 virtual bool prepareData(RigGeometry&);
43 typedef std::pair<unsigned int, float> LocalBoneIDWeight;
44 class BonePtrWeight: LocalBoneIDWeight
47 BonePtrWeight(unsigned int id,float weight, Bone*bone=0 ): LocalBoneIDWeight(id,weight), _boneptr(bone) {}
48 BonePtrWeight(const BonePtrWeight &bw2): LocalBoneIDWeight(bw2.getBoneID(),bw2.getWeight()), _boneptr(bw2._boneptr.get()) {}
49 inline const float & getWeight() const { return second; }
50 inline void setWeight(float b) { second=b; }
51 inline const unsigned int & getBoneID() const { return first; }
52 inline void setBoneID(unsigned int b) { first=b; }
53 inline bool operator< (const BonePtrWeight &b1) const {
54 if (second > b1.second) return true;
55 if (second < b1.second) return false;
56 return (first > b1.first);
59 inline const Bone * getBonePtr() const { return _boneptr.get(); }
60 inline void setBonePtr(Bone*b) { _boneptr=b; }
62 osg::observer_ptr< Bone > _boneptr;
65 typedef std::vector<BonePtrWeight> BonePtrWeightList;
67 /// map a set of boneinfluence to a list of vertex indices sharing this set
71 inline BonePtrWeightList& getBoneWeights() { return _boneweights; }
73 inline IndexList& getVertices() { return _vertexes; }
75 inline void resetMatrix()
77 _result.set(0, 0, 0, 0,
82 inline void accummulateMatrix(const osg::Matrix& invBindMatrix, const osg::Matrix& matrix, osg::Matrix::value_type weight)
84 osg::Matrix m = invBindMatrix * matrix;
85 osg::Matrix::value_type* ptr = m.ptr();
86 osg::Matrix::value_type* ptrresult = _result.ptr();
87 ptrresult[0] += ptr[0] * weight;
88 ptrresult[1] += ptr[1] * weight;
89 ptrresult[2] += ptr[2] * weight;
91 ptrresult[4] += ptr[4] * weight;
92 ptrresult[5] += ptr[5] * weight;
93 ptrresult[6] += ptr[6] * weight;
95 ptrresult[8] += ptr[8] * weight;
96 ptrresult[9] += ptr[9] * weight;
97 ptrresult[10] += ptr[10] * weight;
99 ptrresult[12] += ptr[12] * weight;
100 ptrresult[13] += ptr[13] * weight;
101 ptrresult[14] += ptr[14] * weight;
103 inline void computeMatrixForVertexSet()
105 if (_boneweights.empty())
107 osg::notify(osg::WARN) << this << " RigTransformSoftware::VertexGroup no bones found" << std::endl;
108 _result = osg::Matrix::identity();
113 for(BonePtrWeightList::iterator bwit=_boneweights.begin(); bwit!=_boneweights.end(); ++bwit )
115 const Bone* bone = bwit->getBonePtr();
118 osg::notify(osg::WARN) << this << " RigTransformSoftware::computeMatrixForVertexSet Warning a bone is null, skip it" << std::endl;
121 const osg::Matrix& invBindMatrix = bone->getInvBindMatrixInSkeletonSpace();
122 const osg::Matrix& matrix = bone->getMatrixInSkeletonSpace();
123 osg::Matrix::value_type w = bwit->getWeight();
124 accummulateMatrix(invBindMatrix, matrix, w);
128 inline const osg::Matrix& getMatrix() const { return _result; }
130 BonePtrWeightList _boneweights;
136 inline void compute(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst)
138 // the result of matrix mult should be cached to be used for vertices transform and normal transform and maybe other computation
139 for(VertexGroupList::iterator itvg=_uniqVertexGroupList.begin(); itvg!=_uniqVertexGroupList.end(); ++itvg)
141 VertexGroup& uniq = *itvg;
142 uniq.computeMatrixForVertexSet();
143 osg::Matrix matrix = transform * uniq.getMatrix() * invTransform;
145 const IndexList& vertices = uniq.getVertices();
146 for(IndexList::const_iterator vertIDit=vertices.begin(); vertIDit!=vertices.end(); ++vertIDit)
148 dst[*vertIDit] = src[*vertIDit] * matrix;
155 inline void computeNormal(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst)
157 for(VertexGroupList::iterator itvg=_uniqVertexGroupList.begin(); itvg!=_uniqVertexGroupList.end(); ++itvg)
159 VertexGroup& uniq = *itvg;
160 uniq.computeMatrixForVertexSet();
161 osg::Matrix matrix = transform * uniq.getMatrix() * invTransform;
163 const IndexList& vertices = uniq.getVertices();
164 for(IndexList::const_iterator vertIDit=vertices.begin(); vertIDit!=vertices.end(); ++vertIDit)
166 dst[*vertIDit] = osg::Matrix::transform3x3(src[*vertIDit],matrix);
175 virtual bool init(RigGeometry&);
177 std::map<std::string,bool> _invalidInfluence;
179 typedef std::vector<VertexGroup> VertexGroupList;
180 VertexGroupList _uniqVertexGroupList;
182 void buildMinimumUpdateSet(const RigGeometry&rig );