openscenegraph
RigTransformSoftware
Go to the documentation of this file.
1/* -*-c++-*-
2 * Copyright (C) 2009 Cedric Pinson <cedric.pinson@plopbyte.net>
3 * Copyright (C) 2017 Julien Valentin <mp3butcher@hotmail.com>
4 *
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.
9 *
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.
14 */
15
16#ifndef OSGANIMATION_RIGTRANSFORM_SOFTWARE
17#define OSGANIMATION_RIGTRANSFORM_SOFTWARE 1
18
19#include <osgAnimation/Export>
20#include <osgAnimation/RigTransform>
21#include <osgAnimation/Bone>
22#include <osgAnimation/VertexInfluence>
23#include <osg/observer_ptr>
24
25namespace osgAnimation
26{
27
28 class RigGeometry;
29
30 /// This class manage format for software skinning
31 class OSGANIMATION_EXPORT RigTransformSoftware : public RigTransform
32 {
33 public:
34 RigTransformSoftware();
35 RigTransformSoftware(const RigTransformSoftware& rts,const osg::CopyOp& copyop);
36
37 META_Object(osgAnimation,RigTransformSoftware)
38
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&);
42
43 typedef std::pair<unsigned int, float> LocalBoneIDWeight;
44 class BonePtrWeight: LocalBoneIDWeight
45 {
46 public:
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);
57 }
58 ///set Bone pointer
59 inline const Bone * getBonePtr() const { return _boneptr.get(); }
60 inline void setBonePtr(Bone*b) { _boneptr=b; }
61 protected:
62 osg::observer_ptr< Bone > _boneptr;
63 };
64
65 typedef std::vector<BonePtrWeight> BonePtrWeightList;
66
67 /// map a set of boneinfluence to a list of vertex indices sharing this set
68 class VertexGroup
69 {
70 public:
71 inline BonePtrWeightList& getBoneWeights() { return _boneweights; }
72
73 inline IndexList& getVertices() { return _vertexes; }
74
75 inline void resetMatrix()
76 {
77 _result.set(0, 0, 0, 0,
78 0, 0, 0, 0,
79 0, 0, 0, 0,
80 0, 0, 0, 1);
81 }
82 inline void accummulateMatrix(const osg::Matrix& invBindMatrix, const osg::Matrix& matrix, osg::Matrix::value_type weight)
83 {
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;
90
91 ptrresult[4] += ptr[4] * weight;
92 ptrresult[5] += ptr[5] * weight;
93 ptrresult[6] += ptr[6] * weight;
94
95 ptrresult[8] += ptr[8] * weight;
96 ptrresult[9] += ptr[9] * weight;
97 ptrresult[10] += ptr[10] * weight;
98
99 ptrresult[12] += ptr[12] * weight;
100 ptrresult[13] += ptr[13] * weight;
101 ptrresult[14] += ptr[14] * weight;
102 }
103 inline void computeMatrixForVertexSet()
104 {
105 if (_boneweights.empty())
106 {
107 osg::notify(osg::WARN) << this << " RigTransformSoftware::VertexGroup no bones found" << std::endl;
108 _result = osg::Matrix::identity();
109 return;
110 }
111 resetMatrix();
112
113 for(BonePtrWeightList::iterator bwit=_boneweights.begin(); bwit!=_boneweights.end(); ++bwit )
114 {
115 const Bone* bone = bwit->getBonePtr();
116 if (!bone)
117 {
118 osg::notify(osg::WARN) << this << " RigTransformSoftware::computeMatrixForVertexSet Warning a bone is null, skip it" << std::endl;
119 continue;
120 }
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);
125 }
126 }
127 void normalize();
128 inline const osg::Matrix& getMatrix() const { return _result; }
129 protected:
130 BonePtrWeightList _boneweights;
131 IndexList _vertexes;
132 osg::Matrix _result;
133 };
134
135 template <class V>
136 inline void compute(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst)
137 {
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)
140 {
141 VertexGroup& uniq = *itvg;
142 uniq.computeMatrixForVertexSet();
143 osg::Matrix matrix = transform * uniq.getMatrix() * invTransform;
144
145 const IndexList& vertices = uniq.getVertices();
146 for(IndexList::const_iterator vertIDit=vertices.begin(); vertIDit!=vertices.end(); ++vertIDit)
147 {
148 dst[*vertIDit] = src[*vertIDit] * matrix;
149 }
150
151 }
152 }
153
154 template <class V>
155 inline void computeNormal(const osg::Matrix& transform, const osg::Matrix& invTransform, const V* src, V* dst)
156 {
157 for(VertexGroupList::iterator itvg=_uniqVertexGroupList.begin(); itvg!=_uniqVertexGroupList.end(); ++itvg)
158 {
159 VertexGroup& uniq = *itvg;
160 uniq.computeMatrixForVertexSet();
161 osg::Matrix matrix = transform * uniq.getMatrix() * invTransform;
162
163 const IndexList& vertices = uniq.getVertices();
164 for(IndexList::const_iterator vertIDit=vertices.begin(); vertIDit!=vertices.end(); ++vertIDit)
165 {
166 dst[*vertIDit] = osg::Matrix::transform3x3(src[*vertIDit],matrix);
167 }
168 }
169 }
170
171 protected:
172
173 bool _needInit;
174
175 virtual bool init(RigGeometry&);
176
177 std::map<std::string,bool> _invalidInfluence;
178
179 typedef std::vector<VertexGroup> VertexGroupList;
180 VertexGroupList _uniqVertexGroupList;
181
182 void buildMinimumUpdateSet(const RigGeometry&rig );
183
184 };
185}
186
187#endif