OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MorphGeometry.h
Go to the documentation of this file.
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 
15 #ifndef OSGANIMATION_MORPHGEOMETRY_H
16 #define OSGANIMATION_MORPHGEOMETRY_H
17 
18 #include <osgAnimation/Export>
19 #include <osgAnimation/AnimationUpdateCallback>
20 #include <osg/Geometry>
21 
22 namespace osgAnimation
23 {
24 
26  {
27 
28  public:
29 
30  enum Method {
32  RELATIVE
33  };
34 
36  {
37  protected:
39  float _weight;
40  public:
41  MorphTarget(osg::Geometry* geom, float w = 1.0) : _geom(geom), _weight(w) {}
42  void setWeight(float weight) { _weight = weight; }
43  float getWeight() const { return _weight; }
44  osg::Geometry* getGeometry() { return _geom.get(); }
45  const osg::Geometry* getGeometry() const { return _geom.get(); }
46  void setGeometry(osg::Geometry* geom) { _geom = geom; }
47  };
48 
49  typedef std::vector<MorphTarget> MorphTargetList;
50 
51  MorphGeometry();
52  MorphGeometry(const osg::Geometry& b);
54 
55  virtual osg::Object* cloneType() const { return new MorphGeometry(); }
56  virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new MorphGeometry(*this,copyop); }
57  virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const MorphGeometry*>(obj)!=NULL; }
58  virtual const char* libraryName() const { return "osgAnimation"; }
59  virtual const char* className() const { return "MorphGeometry"; }
60 
61  virtual void transformSoftwareMethod();
62 
64  void setMethod(Method method) { _method = method; }
66  inline Method getMethod() const { return _method; }
67 
69  void setMorphNormals(bool morphNormals) { _morphNormals = morphNormals; }
71  inline bool getMorphNormals() const { return _morphNormals; }
72 
82  virtual void addMorphTarget( osg::Geometry *morphTarget, float weight = 1.0 ) { _morphTargets.push_back(MorphTarget(morphTarget, weight)); _dirty = true; }
83 
84  void setWeight(unsigned int index, float morphWeight)
85  {
86  if (index < _morphTargets.size())
87  {
88  _morphTargets[index].setWeight(morphWeight);
89  dirty();
90  }
91  }
92 
94  void dirty() { _dirty = true; }
95 
97  const MorphTargetList& getMorphTargetList() const { return _morphTargets; }
98 
100  MorphTargetList& getMorphTargetList() { return _morphTargets; }
101 
103  inline const MorphTarget& getMorphTarget( unsigned int i ) const { return _morphTargets[i]; }
104 
106  inline MorphTarget& getMorphTarget( unsigned int i ) { return _morphTargets[i]; }
107 
108  protected:
110  bool _dirty;
111 
113  MorphTargetList _morphTargets;
114 
115  std::vector<osg::Vec3> _positionSource;
116  std::vector<osg::Vec3> _normalSource;
117 
120  };
121 
122  class OSGANIMATION_EXPORT UpdateMorph : public AnimationUpdateCallback<osg::NodeCallback>
123  {
124  protected:
125  std::map<int, osg::ref_ptr<osgAnimation::FloatTarget> > _weightTargets;
126 
127  public:
128 
130 
131  UpdateMorph(const std::string& name = "");
132  UpdateMorph(const UpdateMorph& apc,const osg::CopyOp& copyop);
133 
135  virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
136  bool needLink() const;
137  bool link(osgAnimation::Channel* channel);
138  };
139 
141  {
143 
145 
147 
148  virtual void update(osg::NodeVisitor*, osg::Drawable* drw)
149  {
150  MorphGeometry* geom = dynamic_cast<MorphGeometry*>(drw);
151  if (!geom)
152  return;
153 
154  geom->transformSoftwareMethod();
155  }
156  };
157 
158 
159 }
160 
161 #endif
#define OSGANIMATION_EXPORT
Definition: Export.h:40
bool _morphNormals
Do we also morph between normals?
#define NULL
Definition: Export.h:59
UpdateMorphGeometry(const UpdateMorphGeometry &, const osg::CopyOp &)
virtual bool isSameKindAs(const osg::Object *obj) const
Definition: MorphGeometry.h:57
META_Object(osgAnimation, UpdateMorphGeometry)
virtual void addMorphTarget(osg::Geometry *morphTarget, float weight=1.0)
Definition: MorphGeometry.h:82
MorphTargetList & getMorphTargetList()
MorphTarget(osg::Geometry *geom, float w=1.0)
Definition: MorphGeometry.h:41
virtual void update(osg::NodeVisitor *, osg::Drawable *drw)
void setGeometry(osg::Geometry *geom)
Definition: MorphGeometry.h:46
std::vector< osg::Vec3 > _normalSource
virtual const char * libraryName() const
Definition: MorphGeometry.h:58
#define META_Object(library, name)
Definition: Object.h:42
bool _dirty
Do we need to recalculate the morphed geometry?
T * get() const
Definition: ref_ptr.h:92
std::vector< MorphTarget > MorphTargetList
Definition: MorphGeometry.h:49
virtual osg::Object * cloneType() const
Definition: MorphGeometry.h:55
void setMorphNormals(bool morphNormals)
Definition: MorphGeometry.h:69
const osg::Geometry * getGeometry() const
Definition: MorphGeometry.h:45
virtual void transformSoftwareMethod()
Definition: Node.h:71
void setMethod(Method method)
Definition: MorphGeometry.h:64
void setWeight(unsigned int index, float morphWeight)
Definition: MorphGeometry.h:84
virtual const char * className() const
Definition: MorphGeometry.h:59
MorphTarget & getMorphTarget(unsigned int i)
std::vector< osg::Vec3 > _positionSource
MorphTargetList _morphTargets
const MorphTarget & getMorphTarget(unsigned int i) const
const MorphTargetList & getMorphTargetList() const
Definition: MorphGeometry.h:97
osg::ref_ptr< osg::Geometry > _geom
Definition: MorphGeometry.h:38
virtual osg::Object * clone(const osg::CopyOp &copyop) const
Definition: MorphGeometry.h:56
std::map< int, osg::ref_ptr< osgAnimation::FloatTarget > > _weightTargets