MorphGeometry 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /* -*-c++-*-
  2. * Copyright (C) 2008 Cedric Pinson <mornifle@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. #ifndef OSGANIMATION_MORPHGEOMETRY_H
  15. #define OSGANIMATION_MORPHGEOMETRY_H
  16. #include <osgAnimation/Export>
  17. #include <osgAnimation/AnimationUpdateCallback>
  18. #include <osgAnimation/MorphTransformSoftware>
  19. #include <osg/Geometry>
  20. #include <algorithm>
  21. namespace osgAnimation
  22. {
  23. class OSGANIMATION_EXPORT MorphGeometry : public osg::Geometry
  24. {
  25. public:
  26. enum Method
  27. {
  28. NORMALIZED,
  29. RELATIVE
  30. };
  31. class MorphTarget
  32. {
  33. protected:
  34. osg::ref_ptr<osg::Geometry> _geom;
  35. float _weight;
  36. public:
  37. MorphTarget(osg::Geometry* geom, float w = 1.0) : _geom(geom), _weight(w) {}
  38. void setWeight(float weight) { _weight = weight; }
  39. float getWeight() const { return _weight; }
  40. osg::Geometry* getGeometry() { return _geom.get(); }
  41. const osg::Geometry* getGeometry() const { return _geom.get(); }
  42. void setGeometry(osg::Geometry* geom) { _geom = geom; }
  43. };
  44. typedef std::vector<MorphTarget> MorphTargetList;
  45. MorphGeometry();
  46. MorphGeometry(const osg::Geometry& b);
  47. MorphGeometry(const MorphGeometry& b, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
  48. virtual osg::Object* cloneType() const { return new MorphGeometry(); }
  49. virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new MorphGeometry(*this,copyop); }
  50. virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const MorphGeometry*>(obj)!=NULL; }
  51. virtual const char* libraryName() const { return "osgAnimation"; }
  52. virtual const char* className() const { return "MorphGeometry"; }
  53. // set implementation of rig method
  54. inline void setMorphTransformImplementation(MorphTransform*mt) { _morphTransformImplementation=mt; }
  55. inline MorphTransform* getMorphTransformImplementation() { return _morphTransformImplementation.get(); }
  56. inline const MorphTransform* getMorphTransformImplementation() const { return _morphTransformImplementation.get(); }
  57. /** Set the morphing method. */
  58. inline void setMethod(Method method) { _method = method; }
  59. /** Get the morphing method. */
  60. inline Method getMethod() const { return _method; }
  61. /** Set flag for morphing normals. */
  62. inline void setMorphNormals(bool morphNormals) { _morphNormals = morphNormals; }
  63. /** Get the flag for morphing normals. */
  64. inline bool getMorphNormals() const { return _morphNormals; }
  65. /** Get the list of MorphTargets.*/
  66. inline const MorphTargetList& getMorphTargetList() const { return _morphTargets; }
  67. /** Get the list of MorphTargets. Warning if you modify this array you will have to call dirty() */
  68. inline MorphTargetList& getMorphTargetList() { return _morphTargets; }
  69. /** Return the \c MorphTarget at position \c i.*/
  70. inline const MorphTarget& getMorphTarget( unsigned int i ) const { return _morphTargets[i]; }
  71. /** Return the \c MorphTarget at position \c i.*/
  72. inline MorphTarget& getMorphTarget( unsigned int i ) { return _morphTargets[i]; }
  73. /** Set source of vertices for this morph geometry */
  74. inline void setVertexSource(osg::Vec3Array *v) { _positionSource=v; }
  75. /** Get source of vertices for this morph geometry */
  76. inline osg::Vec3Array * getVertexSource() const { return _positionSource.get(); }
  77. /** Set source of normals for this morph geometry */
  78. inline void setNormalSource(osg::Vec3Array *n) { _normalSource=n; }
  79. /** Get source of normals for this morph geometry */
  80. inline osg::Vec3Array * getNormalSource() const { return _normalSource.get(); }
  81. /** Add a \c MorphTarget to the \c MorphGeometry.
  82. * If \c MorphTarget is not \c NULL and is not contained in the \c MorphGeometry
  83. * then increment its reference count, add it to the MorphTargets list and
  84. * dirty the bounding sphere to force it to be recomputed on the next
  85. * call to \c getBound().
  86. * @param morphTarget The \c MorphTarget to be added to the \c MorphGeometry.
  87. * @param weight The weight to be added to the \c MorphGeometry.
  88. * @return \c true for success; \c false otherwise.
  89. */
  90. virtual void addMorphTarget( osg::Geometry *morphTarget, float weight = 1.0 )
  91. {
  92. _morphTargets.push_back(MorphTarget(morphTarget, weight));
  93. _dirty = true;
  94. }
  95. virtual void removeMorphTarget( osg::Geometry *morphTarget )
  96. {
  97. for(MorphTargetList::iterator iterator = _morphTargets.begin() ; iterator != _morphTargets.end() ; ++ iterator)
  98. {
  99. if(iterator->getGeometry() == morphTarget)
  100. {
  101. _morphTargets.erase(iterator);
  102. break;
  103. }
  104. }
  105. }
  106. virtual void removeMorphTarget( const std::string& name )
  107. {
  108. for(MorphTargetList::iterator iterator = _morphTargets.begin() ; iterator != _morphTargets.end() ; ++ iterator)
  109. {
  110. if(iterator->getGeometry() && iterator->getGeometry()->getName() == name)
  111. {
  112. _morphTargets.erase(iterator);
  113. break;
  114. }
  115. }
  116. }
  117. /** update a morph target at index setting its current weight to morphWeight */
  118. inline void setWeight(unsigned int index, float morphWeight)
  119. {
  120. if (index < _morphTargets.size())
  121. {
  122. _morphTargets[index].setWeight(morphWeight);
  123. dirty();
  124. }
  125. }
  126. /** Set the MorphGeometry dirty.*/
  127. inline void dirty(bool b=true) { _dirty = b; }
  128. inline bool isDirty() const { return _dirty; }
  129. /** for retrocompatibility */
  130. void transformSoftwareMethod() { (*_morphTransformImplementation.get())(*this); }
  131. protected:
  132. osg::ref_ptr<MorphTransform> _morphTransformImplementation;
  133. /// Do we need to recalculate the morphed geometry?
  134. bool _dirty;
  135. Method _method;
  136. MorphTargetList _morphTargets;
  137. osg::ref_ptr<osg::Vec3Array> _positionSource;
  138. osg::ref_ptr<osg::Vec3Array> _normalSource;
  139. /// Do we also morph between normals?
  140. bool _morphNormals;
  141. };
  142. class OSGANIMATION_EXPORT UpdateMorph : public AnimationUpdateCallback<osg::NodeCallback>
  143. {
  144. public:
  145. typedef std::vector<std::string> TargetNames;
  146. typedef std::map< int, osg::ref_ptr<osgAnimation::FloatTarget> > WeightTargets;
  147. META_Object(osgAnimation, UpdateMorph);
  148. UpdateMorph(const std::string& name = "");
  149. UpdateMorph(const UpdateMorph& apc,const osg::CopyOp& copyop);
  150. void addTarget(const std::string& name) { _targetNames.push_back(name); }
  151. unsigned int getNumTarget() const { return _targetNames.size(); }
  152. const std::string& getTargetName(unsigned int index) { return _targetNames[index]; }
  153. void removeTarget(const std::string& name)
  154. {
  155. TargetNames::iterator found = std::find(_targetNames.begin(), _targetNames.end(), name);
  156. if(found != _targetNames.end())
  157. _targetNames.erase(found);
  158. }
  159. // for serialization
  160. const std::vector<std::string>& getTargetNames() const { return _targetNames; }
  161. std::vector<std::string>& getTargetNames() { return _targetNames; }
  162. void setTargetNames(const TargetNames& targetNames) { _targetNames.assign(targetNames.begin(), targetNames.end()); }
  163. /** Callback method called by the NodeVisitor when visiting a node.*/
  164. virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
  165. bool needLink() const;
  166. bool link(osgAnimation::Channel* channel);
  167. int link(Animation* animation);
  168. protected:
  169. WeightTargets _weightTargets;
  170. TargetNames _targetNames;
  171. };
  172. struct UpdateMorphGeometry : public osg::DrawableUpdateCallback
  173. {
  174. UpdateMorphGeometry() {}
  175. UpdateMorphGeometry(const UpdateMorphGeometry& org, const osg::CopyOp& copyop):
  176. osg::Object(org, copyop),
  177. osg::Callback(org, copyop),
  178. osg::DrawableUpdateCallback(org, copyop) {}
  179. META_Object(osgAnimation, UpdateMorphGeometry);
  180. virtual void update(osg::NodeVisitor*, osg::Drawable* drw)
  181. {
  182. MorphGeometry* geom = dynamic_cast<MorphGeometry*>(drw);
  183. if (!geom)
  184. return;
  185. if (!geom->getMorphTransformImplementation())
  186. {
  187. geom->setMorphTransformImplementation( new MorphTransformSoftware);
  188. }
  189. MorphTransform& implementation = *geom->getMorphTransformImplementation();
  190. (implementation)(*geom);
  191. }
  192. };
  193. }
  194. #endif