- Timestamp:
- 01/27/10 13:24:55 (3 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
OpenSceneGraph/trunk/src/osgAnimation/RigTransformSoftware.cpp
r10693 r11009 14 14 15 15 16 #include <osgAnimation/VertexInfluence> 16 17 #include <osgAnimation/RigTransformSoftware> 18 #include <osgAnimation/BoneMapVisitor> 17 19 #include <osgAnimation/RigGeometry> 18 20 19 21 using namespace osgAnimation; 22 23 RigTransformSoftware::RigTransformSoftware() 24 { 25 _needInit = true; 26 } 20 27 21 28 bool RigTransformSoftware::init(RigGeometry& geom) … … 23 30 if (!geom.getSkeleton()) 24 31 return false; 25 Bone::BoneMap bm = geom.getSkeleton()->getBoneMap(); 32 33 BoneMapVisitor mapVisitor; 34 geom.getSkeleton()->accept(mapVisitor); 35 BoneMap bm = mapVisitor.getBoneMap(); 26 36 initVertexSetFromBones(bm, geom.getVertexInfluenceSet().getUniqVertexSetToBoneSetList()); 37 38 geom.copyFrom(*geom.getSourceGeometry()); 39 geom.setVertexArray(0); 40 geom.setNormalArray(0); 41 27 42 _needInit = false; 28 43 return true; 29 44 } 30 45 31 void RigTransformSoftware:: update(RigGeometry& geom)46 void RigTransformSoftware::operator()(RigGeometry& geom) 32 47 { 33 osg::Vec3Array* pos = dynamic_cast<osg::Vec3Array*>(geom.getVertexArray()); 34 if (pos && _positionSource.size() != pos->size()) 48 if (_needInit) 49 if (!init(geom)) 50 return; 51 52 osg::Geometry& source = *geom.getSourceGeometry(); 53 osg::Geometry& destination = geom; 54 55 osg::Vec3Array* positionSrc = dynamic_cast<osg::Vec3Array*>(source.getVertexArray()); 56 osg::Vec3Array* positionDst = dynamic_cast<osg::Vec3Array*>(destination.getVertexArray()); 57 if (positionSrc && (!positionDst || (positionDst->size() != positionSrc->size()) ) ) 35 58 { 36 _positionSource = std::vector<osg::Vec3>(pos->begin(),pos->end()); 37 geom.getVertexArray()->setDataVariance(osg::Object::DYNAMIC); 59 if (!positionDst) 60 { 61 positionDst = new osg::Vec3Array; 62 positionDst->setDataVariance(osg::Object::DYNAMIC); 63 destination.setVertexArray(positionDst); 64 } 65 *positionDst = *positionSrc; 38 66 } 39 osg::Vec3Array* normal = dynamic_cast<osg::Vec3Array*>(geom.getNormalArray()); 40 if (normal && _normalSource.size() != normal->size()) 67 68 osg::Vec3Array* normalSrc = dynamic_cast<osg::Vec3Array*>(source.getNormalArray()); 69 osg::Vec3Array* normalDst = dynamic_cast<osg::Vec3Array*>(destination.getNormalArray()); 70 if (normalSrc && (!normalDst || (normalDst->size() != normalSrc->size()) ) ) 41 71 { 42 _normalSource = std::vector<osg::Vec3>(normal->begin(),normal->end()); 43 geom.getNormalArray()->setDataVariance(osg::Object::DYNAMIC); 72 if (!normalDst) 73 { 74 normalDst = new osg::Vec3Array; 75 normalDst->setDataVariance(osg::Object::DYNAMIC); 76 destination.setNormalArray(normalDst); 77 destination.setNormalBinding(osg::Geometry::BIND_PER_VERTEX); 78 } 79 *normalDst = *normalSrc; 44 80 } 45 81 46 if ( !_positionSource.empty())82 if (positionDst && !positionDst->empty()) 47 83 { 48 compute<osg::Vec3>(geom.getMatrixFromSkeletonToGeometry(), geom.getInvMatrixFromSkeletonToGeometry(), &_positionSource.front(), &pos->front()); 49 pos->dirty(); 84 compute<osg::Vec3>(geom.getMatrixFromSkeletonToGeometry(), 85 geom.getInvMatrixFromSkeletonToGeometry(), 86 &positionSrc->front(), 87 &positionDst->front()); 88 positionDst->dirty(); 50 89 } 51 if (!_normalSource.empty()) 90 91 if (normalDst && !normalDst->empty()) 52 92 { 53 computeNormal<osg::Vec3>(geom.getMatrixFromSkeletonToGeometry(), geom.getInvMatrixFromSkeletonToGeometry(), &_normalSource.front(), &normal->front()); 54 normal->dirty(); 93 computeNormal<osg::Vec3>(geom.getMatrixFromSkeletonToGeometry(), 94 geom.getInvMatrixFromSkeletonToGeometry(), 95 &normalSrc->front(), 96 &normalDst->front()); 97 normalDst->dirty(); 55 98 } 56 99 } 57 100 58 void RigTransformSoftware::initVertexSetFromBones(const Bone ::BoneMap& map, const VertexInfluenceSet::UniqVertexSetToBoneSetList& influence)101 void RigTransformSoftware::initVertexSetFromBones(const BoneMap& map, const VertexInfluenceSet::UniqVertexSetToBoneSetList& influence) 59 102 { 60 103 _boneSetVertexSet.clear(); … … 73 116 const std::string& bname = inf.getBones()[b].getBoneName(); 74 117 float weight = inf.getBones()[b].getWeight(); 75 Bone ::BoneMap::const_iterator it = map.find(bname);118 BoneMap::const_iterator it = map.find(bname); 76 119 if (it == map.end()) 77 120 {
