| 1 | /* -*-c++-*- |
|---|
| 2 | * Copyright (C) 2008 Cedric Pinson <cedric.pinson@plopbyte.net> |
|---|
| 3 | * |
|---|
| 4 | * This library is open source and may be redistributed and/or modified under |
|---|
| 5 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 6 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 7 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 8 | * |
|---|
| 9 | * This library is distributed in the hope that it will be useful, |
|---|
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 12 | * OpenSceneGraph Public License for more details. |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | #ifndef OSGANIMATION_VERTEX_INFLUENCE |
|---|
| 16 | #define OSGANIMATION_VERTEX_INFLUENCE 1 |
|---|
| 17 | |
|---|
| 18 | #include <osg/Object> |
|---|
| 19 | #include <osgAnimation/Export> |
|---|
| 20 | #include <map> |
|---|
| 21 | #include <vector> |
|---|
| 22 | #include <string> |
|---|
| 23 | |
|---|
| 24 | namespace osgAnimation |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | // first is vertex index, and second the weight, the |
|---|
| 28 | typedef std::pair<int, float> VertexIndexWeight; |
|---|
| 29 | typedef std::vector<VertexIndexWeight> VertexList; |
|---|
| 30 | class OSGANIMATION_EXPORT VertexInfluence : public VertexList |
|---|
| 31 | { |
|---|
| 32 | public: |
|---|
| 33 | const std::string& getName() const { return _name;} |
|---|
| 34 | void setName(const std::string& name) { _name = name;} |
|---|
| 35 | |
|---|
| 36 | protected: |
|---|
| 37 | // the name is the bone to link to |
|---|
| 38 | std::string _name; |
|---|
| 39 | }; |
|---|
| 40 | |
|---|
| 41 | class VertexInfluenceMap : public std::map<std::string, VertexInfluence> , public osg::Object |
|---|
| 42 | { |
|---|
| 43 | public: |
|---|
| 44 | META_Object(osgAnimation, VertexInfluenceMap); |
|---|
| 45 | |
|---|
| 46 | VertexInfluenceMap() {} |
|---|
| 47 | VertexInfluenceMap(const osgAnimation::VertexInfluenceMap&, const osg::CopyOp&) {} |
|---|
| 48 | }; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | // this class manage VertexInfluence database by mesh |
|---|
| 52 | // reference bones per vertex ... |
|---|
| 53 | class OSGANIMATION_EXPORT VertexInfluenceSet |
|---|
| 54 | { |
|---|
| 55 | public: |
|---|
| 56 | typedef std::vector<VertexInfluence> BoneToVertexList; |
|---|
| 57 | |
|---|
| 58 | class BoneWeight |
|---|
| 59 | { |
|---|
| 60 | public: |
|---|
| 61 | BoneWeight(const std::string& name, float weight) : _boneName(name), _weight(weight) {} |
|---|
| 62 | const std::string& getBoneName() const { return _boneName; } |
|---|
| 63 | float getWeight() const { return _weight; } |
|---|
| 64 | void setWeight(float weight) { _weight = weight; } |
|---|
| 65 | bool operator==(const BoneWeight& b) const { return (_boneName == b.getBoneName() && _weight == b.getWeight()); } |
|---|
| 66 | protected: |
|---|
| 67 | std::string _boneName; |
|---|
| 68 | float _weight; |
|---|
| 69 | }; |
|---|
| 70 | |
|---|
| 71 | typedef std::vector<BoneWeight> BoneWeightList; |
|---|
| 72 | typedef std::map<int,BoneWeightList> VertexIndexToBoneWeightMap; |
|---|
| 73 | |
|---|
| 74 | class UniqVertexSetToBoneSet |
|---|
| 75 | { |
|---|
| 76 | public: |
|---|
| 77 | void setBones(BoneWeightList& bones) { _bones = bones;} |
|---|
| 78 | const BoneWeightList& getBones() const { return _bones;} |
|---|
| 79 | std::vector<int>& getVertexes() { return _vertexes;} |
|---|
| 80 | const std::vector<int>& getVertexes() const { return _vertexes;} |
|---|
| 81 | protected: |
|---|
| 82 | std::vector<int> _vertexes; |
|---|
| 83 | BoneWeightList _bones; // here we could limit matrix operation by caching (weight * matrix) |
|---|
| 84 | }; |
|---|
| 85 | |
|---|
| 86 | typedef std::vector<UniqVertexSetToBoneSet> UniqVertexSetToBoneSetList; |
|---|
| 87 | |
|---|
| 88 | const UniqVertexSetToBoneSetList& getUniqVertexSetToBoneSetList() const { return _uniqVertexSetToBoneSet;} |
|---|
| 89 | void addVertexInfluence(const VertexInfluence& v); |
|---|
| 90 | void buildVertex2BoneList(); |
|---|
| 91 | void buildUniqVertexSetToBoneSetList(); |
|---|
| 92 | void clear(); |
|---|
| 93 | |
|---|
| 94 | const VertexIndexToBoneWeightMap& getVertexToBoneList() const; |
|---|
| 95 | protected: |
|---|
| 96 | BoneToVertexList _bone2Vertexes; |
|---|
| 97 | VertexIndexToBoneWeightMap _vertex2Bones; |
|---|
| 98 | UniqVertexSetToBoneSetList _uniqVertexSetToBoneSet; |
|---|
| 99 | }; |
|---|
| 100 | |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | #endif |
|---|