OpenSceneGraph/include/osgAnimation/VertexInfluence

129 lines
4.8 KiB
C++

/* -*-c++-*-
* Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net>
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGANIMATION_VERTEX_INFLUENCE
#define OSGANIMATION_VERTEX_INFLUENCE 1
#include <osg/Object>
#include <osgAnimation/Export>
#include <map>
#include <vector>
#include <string>
namespace osgAnimation
{
// first is bonename, and second the weight
struct BoneWeight: public std::pair<std::string, float>
{
BoneWeight( std::string f,float s):
std::pair<std::string,float>(f,s){}
inline const std::string& getBoneName()const{return first;}
inline void setBoneName(const std::string&s){first=s;}
inline const float &getWeight()const{return second;}
inline void setWeight(float i){second=i;}
};
// first is vertex index, and second the weight
struct IndexWeight: public std::pair<unsigned int, float>
{
IndexWeight( unsigned int f = 0xffffffff,float s = 0.0f): std::pair<unsigned int,float>(f,s){}
inline const unsigned int& getIndex()const{return first;}
inline void setIndex(unsigned int i){first=i;}
inline const float &getWeight()const{return second;}
inline void setWeight(float i){second=i;}
};
typedef std::vector<IndexWeight> VertexList;
class OSGANIMATION_EXPORT BoneInfluenceList : public VertexList
{
public:
const std::string& getBoneName() const { return _name;}
void setBoneName(const std::string& name) { _name = name;}
protected:
// the name is the bone to link to
std::string _name;
};
class VertexInfluenceMap : public std::map<std::string, BoneInfluenceList> , public osg::Object
{
public:
META_Object(osgAnimation, VertexInfluenceMap);
VertexInfluenceMap() {}
VertexInfluenceMap(const osgAnimation::VertexInfluenceMap& org, const osg::CopyOp& copyop):
std::map<std::string, BoneInfluenceList>(org),
osg::Object(org, copyop)
{}
};
// this class manage VertexInfluence database by mesh
// reference bones per vertex ...
class OSGANIMATION_EXPORT VertexInfluenceSet
{
public:
typedef std::vector<BoneInfluenceList> BoneToVertexList;
class BoneWeight
{
public:
BoneWeight(const std::string& name, float weight) : _boneName(name), _weight(weight) {}
BoneWeight(const BoneWeight &bw2) : _boneName(bw2._boneName), _weight(bw2._weight) {}
const std::string& getBoneName() const { return _boneName; }
float getWeight() const { return _weight; }
void setWeight(float weight) { _weight = weight; }
bool operator==(const BoneWeight& b) const { return (_boneName == b.getBoneName() && _weight == b.getWeight()); }
protected:
std::string _boneName;
float _weight;
};
typedef std::vector<BoneWeight> BoneWeightList;
typedef std::vector<BoneWeightList> VertIDToBoneWeightList;
class VertexGroup
{
public:
// set Influences of the VertexGroup
void setBones(BoneWeightList& bones) { _bones = bones;}
const BoneWeightList& getBones() const { return _bones;}
// set Vertex Indices of the VertexGroup
std::vector<unsigned int>& getVertexes() { return _vertexes;}
const std::vector<unsigned int>& getVertexes() const { return _vertexes;}
protected:
std::vector<unsigned int> _vertexes;
BoneWeightList _bones; // here we could limit matrix operation by caching (weight * matrix)
};
typedef std::vector<VertexGroup> UniqVertexGroupList;
/** construct a vector of unique VertexGroups and their influences**/
void buildUniqVertexGroupList();
/** return a list of unique VertexGroups and their influences**/
const UniqVertexGroupList& getUniqVertexGroupList() const { return _uniqVertexSetToBoneSet;}
void addVertexInfluence(const BoneInfluenceList& v);
void buildVertex2BoneList(unsigned int numvertices);
void clear();
const VertIDToBoneWeightList& getVertexToBoneList() const;
protected:
BoneToVertexList _bone2Vertexes;
VertIDToBoneWeightList _vertex2Bones;
UniqVertexGroupList _uniqVertexSetToBoneSet;
};
}
#endif