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>
21#include <osgAnimation/Bone>
23#include <osg/observer_ptr>
24
25namespace osgAnimation
26{
27
28 class RigGeometry;
29
32 {
33 public:
36
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;
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 }
59 inline const Bone * getBonePtr() const { return _boneptr.get(); }
60 inline void setBonePtr(Bone*b) { _boneptr=b; }
61 protected:
63 };
64
65 typedef std::vector<BonePtrWeight> BonePtrWeightList;
66
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;
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 }
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:
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;
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;
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
174
175 virtual bool init(RigGeometry&);
176
177 std::map<std::string,bool> _invalidInfluence;
178
179 typedef std::vector<VertexGroup> VertexGroupList;
181
183
184 };
185}
186
187#endif
Definition Bone:31
const osg::Matrix & getInvBindMatrixInSkeletonSpace() const
Definition Bone:46
const osg::Matrix & getMatrixInSkeletonSpace() const
Definition Bone:45
Definition RigGeometry:50
Definition RigTransformSoftware:45
void setBoneID(unsigned int b)
Definition RigTransformSoftware:52
osg::observer_ptr< Bone > _boneptr
Definition RigTransformSoftware:62
BonePtrWeight(const BonePtrWeight &bw2)
Definition RigTransformSoftware:48
BonePtrWeight(unsigned int id, float weight, Bone *bone=0)
Definition RigTransformSoftware:47
void setBonePtr(Bone *b)
Definition RigTransformSoftware:60
const float & getWeight() const
Definition RigTransformSoftware:49
const Bone * getBonePtr() const
set Bone pointer
Definition RigTransformSoftware:59
const unsigned int & getBoneID() const
Definition RigTransformSoftware:51
void setWeight(float b)
Definition RigTransformSoftware:50
map a set of boneinfluence to a list of vertex indices sharing this set
Definition RigTransformSoftware:69
BonePtrWeightList _boneweights
Definition RigTransformSoftware:130
IndexList & getVertices()
Definition RigTransformSoftware:73
osg::Matrix _result
Definition RigTransformSoftware:132
const osg::Matrix & getMatrix() const
Definition RigTransformSoftware:128
void accummulateMatrix(const osg::Matrix &invBindMatrix, const osg::Matrix &matrix, osg::Matrix::value_type weight)
Definition RigTransformSoftware:82
IndexList _vertexes
Definition RigTransformSoftware:131
BonePtrWeightList & getBoneWeights()
Definition RigTransformSoftware:71
void computeMatrixForVertexSet()
Definition RigTransformSoftware:103
void resetMatrix()
Definition RigTransformSoftware:75
This class manage format for software skinning.
Definition RigTransformSoftware:32
bool _needInit
Definition RigTransformSoftware:173
void compute(const osg::Matrix &transform, const osg::Matrix &invTransform, const V *src, V *dst)
Definition RigTransformSoftware:136
std::map< std::string, bool > _invalidInfluence
Definition RigTransformSoftware:177
void buildMinimumUpdateSet(const RigGeometry &rig)
std::pair< unsigned int, float > LocalBoneIDWeight
Definition RigTransformSoftware:43
virtual bool init(RigGeometry &)
virtual bool prepareData(RigGeometry &)
std::vector< BonePtrWeight > BonePtrWeightList
Definition RigTransformSoftware:65
META_Object(osgAnimation, RigTransformSoftware) virtual void operator()(RigGeometry &)
void computeNormal(const osg::Matrix &transform, const osg::Matrix &invTransform, const V *src, V *dst)
Definition RigTransformSoftware:155
std::vector< VertexGroup > VertexGroupList
Definition RigTransformSoftware:179
VertexGroupList _uniqVertexGroupList
Definition RigTransformSoftware:180
RigTransformSoftware(const RigTransformSoftware &rts, const osg::CopyOp &copyop)
Definition RigTransform:26
Definition CopyOp:41
Definition Matrixd:27
static Vec3f transform3x3(const Vec3f &v, const Matrixd &m)
Definition Matrixd:665
value_type * ptr()
Definition Matrixd:92
double value_type
Definition Matrixd:30
static Matrixd identity(void)
Definition Matrixd:444
Definition observer_ptr:39
Definition Action:34
std::vector< unsigned int > IndexList
Definition VertexInfluence:38
@ WARN
Definition Notify:33
std::ostream & notify(void)
Definition Notify:80
#define OSGANIMATION_EXPORT
Definition osgAnimation/Export:40