OpenSceneGraph 3.6.5
MorphGeometry
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>
21#include <osg/Geometry>
22#include <algorithm>
23
24namespace osgAnimation
25{
26
28 {
29
30 public:
31
37
39 {
40 protected:
42 float _weight;
43 public:
44 MorphTarget(osg::Geometry* geom, float w = 1.0) : _geom(geom), _weight(w) {}
45 void setWeight(float weight) { _weight = weight; }
46 float getWeight() const { return _weight; }
47 osg::Geometry* getGeometry() { return _geom.get(); }
48 const osg::Geometry* getGeometry() const { return _geom.get(); }
49 void setGeometry(osg::Geometry* geom) { _geom = geom; }
50 };
51
52 typedef std::vector<MorphTarget> MorphTargetList;
53
57
58 virtual osg::Object* cloneType() const { return new MorphGeometry(); }
59 virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new MorphGeometry(*this,copyop); }
60 virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const MorphGeometry*>(obj)!=NULL; }
61 virtual const char* libraryName() const { return "osgAnimation"; }
62 virtual const char* className() const { return "MorphGeometry"; }
63
64 // set implementation of rig method
68
70 inline void setMethod(Method method) { _method = method; }
72 inline Method getMethod() const { return _method; }
73
75 inline void setMorphNormals(bool morphNormals) { _morphNormals = morphNormals; }
77 inline bool getMorphNormals() const { return _morphNormals; }
78
80 inline const MorphTargetList& getMorphTargetList() const { return _morphTargets; }
81
84
86 inline const MorphTarget& getMorphTarget( unsigned int i ) const { return _morphTargets[i]; }
87
89 inline MorphTarget& getMorphTarget( unsigned int i ) { return _morphTargets[i]; }
90
93
95 inline osg::Vec3Array * getVertexSource() const { return _positionSource.get(); }
96
99
101 inline osg::Vec3Array * getNormalSource() const { return _normalSource.get(); }
102
112 virtual void addMorphTarget( osg::Geometry *morphTarget, float weight = 1.0 )
113 {
114 _morphTargets.push_back(MorphTarget(morphTarget, weight));
115 _dirty = true;
116 }
117
118 virtual void removeMorphTarget( osg::Geometry *morphTarget )
119 {
120 for(MorphTargetList::iterator iterator = _morphTargets.begin() ; iterator != _morphTargets.end() ; ++ iterator)
121 {
122 if(iterator->getGeometry() == morphTarget)
123 {
124 _morphTargets.erase(iterator);
125 break;
126 }
127 }
128 }
129
130 virtual void removeMorphTarget( const std::string& name )
131 {
132 for(MorphTargetList::iterator iterator = _morphTargets.begin() ; iterator != _morphTargets.end() ; ++ iterator)
133 {
134 if(iterator->getGeometry() && iterator->getGeometry()->getName() == name)
135 {
136 _morphTargets.erase(iterator);
137 break;
138 }
139 }
140
141 }
142
144 inline void setWeight(unsigned int index, float morphWeight)
145 {
146 if (index < _morphTargets.size())
147 {
148 _morphTargets[index].setWeight(morphWeight);
149 dirty();
150 }
151 }
152
154 inline void dirty(bool b=true) { _dirty = b; }
155 inline bool isDirty() const { return _dirty; }
156
159
160 protected:
163 bool _dirty;
164
167
170
173 };
174
176 {
177 public:
178 typedef std::vector<std::string> TargetNames;
179 typedef std::map< int, osg::ref_ptr<osgAnimation::FloatTarget> > WeightTargets;
180
182
183 UpdateMorph(const std::string& name = "");
184 UpdateMorph(const UpdateMorph& apc,const osg::CopyOp& copyop);
185
186 void addTarget(const std::string& name) { _targetNames.push_back(name); }
187 unsigned int getNumTarget() const { return _targetNames.size(); }
188 const std::string& getTargetName(unsigned int index) { return _targetNames[index]; }
189 void removeTarget(const std::string& name)
190 {
191 TargetNames::iterator found = std::find(_targetNames.begin(), _targetNames.end(), name);
192 if(found != _targetNames.end())
193 _targetNames.erase(found);
194 }
195
196 // for serialization
197 const std::vector<std::string>& getTargetNames() const { return _targetNames; }
198 std::vector<std::string>& getTargetNames() { return _targetNames; }
199
200 void setTargetNames(const TargetNames& targetNames) { _targetNames.assign(targetNames.begin(), targetNames.end()); }
201
203 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
204 bool needLink() const;
206 int link(Animation* animation);
207
208 protected:
211 };
212
214 {
216
218 osg::Object(org, copyop),
219 osg::Callback(org, copyop),
220 osg::DrawableUpdateCallback(org, copyop) {}
221
223
225 {
226 MorphGeometry* geom = dynamic_cast<MorphGeometry*>(drw);
227 if (!geom)
228 return;
229
231 {
233 }
234
235 MorphTransform& implementation = *geom->getMorphTransformImplementation();
236 (implementation)(*geom);
237 }
238 };
239}
240
241#endif
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
TemplateArray< Vec3, Array::Vec3ArrayType, 3, GL_FLOAT > Vec3Array
Definition Array:449
The osgAnimation library provides general purpose utility classes for animation.
Definition Action:34
Callback()
Definition Callback:38
Definition Callback:298
DrawableUpdateCallback()
Definition Callback:300
Copy Op(erator) used to control whether shallow or deep copy is used during copy construction and clo...
Definition CopyOp:41
@ SHALLOW_COPY
Definition CopyOp:47
Pure virtual base class for drawable geometry.
Definition Drawable:89
Definition Geometry:31
Base class for all internal nodes in the scene graph.
Definition Node:72
Visitor for type safe operations on osg::Nodes.
Definition NodeVisitor:82
Base class/standard interface for objects which require IO support, cloning and reference counting.
Definition Object:61
Object()
Construct an object.
Definition Object:69
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
Definition Animation:29
AnimationUpdateCallback()
Definition AnimationUpdateCallback:38
Definition Channel:33
Definition MorphGeometry:28
MorphTarget & getMorphTarget(unsigned int i)
Return the MorphTarget at position i.
Definition MorphGeometry:89
MorphTransform * getMorphTransformImplementation()
Definition MorphGeometry:66
osg::ref_ptr< MorphTransform > _morphTransformImplementation
Definition MorphGeometry:161
osg::Vec3Array * getNormalSource() const
Get source of normals for this morph geometry.
Definition MorphGeometry:101
Method
Definition MorphGeometry:33
@ RELATIVE
Definition MorphGeometry:35
@ NORMALIZED
Definition MorphGeometry:34
virtual osg::Object * cloneType() const
clone an object of the same type as the node.
Definition MorphGeometry:58
MorphGeometry(const MorphGeometry &b, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
osg::ref_ptr< osg::Vec3Array > _normalSource
Definition MorphGeometry:169
osg::ref_ptr< osg::Vec3Array > _positionSource
Definition MorphGeometry:168
Method getMethod() const
Get the morphing method.
Definition MorphGeometry:72
MorphGeometry(const osg::Geometry &b)
const MorphTarget & getMorphTarget(unsigned int i) const
Return the MorphTarget at position i.
Definition MorphGeometry:86
virtual bool isSameKindAs(const osg::Object *obj) const
return true if this and obj are of the same kind of object.
Definition MorphGeometry:60
void setMethod(Method method)
Set the morphing method.
Definition MorphGeometry:70
std::vector< MorphTarget > MorphTargetList
Definition MorphGeometry:52
virtual void removeMorphTarget(osg::Geometry *morphTarget)
Definition MorphGeometry:118
bool _dirty
Do we need to recalculate the morphed geometry?
Definition MorphGeometry:163
osg::Vec3Array * getVertexSource() const
Get source of vertices for this morph geometry.
Definition MorphGeometry:95
const MorphTransform * getMorphTransformImplementation() const
Definition MorphGeometry:67
void setMorphNormals(bool morphNormals)
Set flag for morphing normals.
Definition MorphGeometry:75
void transformSoftwareMethod()
for retrocompatibility
Definition MorphGeometry:158
MorphTargetList _morphTargets
Definition MorphGeometry:166
virtual void removeMorphTarget(const std::string &name)
Definition MorphGeometry:130
virtual const char * libraryName() const
return the name of the node's library.
Definition MorphGeometry:61
const MorphTargetList & getMorphTargetList() const
Get the list of MorphTargets.
Definition MorphGeometry:80
void setNormalSource(osg::Vec3Array *n)
Set source of normals for this morph geometry.
Definition MorphGeometry:98
Method _method
Definition MorphGeometry:165
void setWeight(unsigned int index, float morphWeight)
update a morph target at index setting its current weight to morphWeight
Definition MorphGeometry:144
void setMorphTransformImplementation(MorphTransform *mt)
Definition MorphGeometry:65
MorphTargetList & getMorphTargetList()
Get the list of MorphTargets.
Definition MorphGeometry:83
virtual const char * className() const
return the name of the node's class type.
Definition MorphGeometry:62
virtual osg::Object * clone(const osg::CopyOp &copyop) const
return a clone of a node, with Object* return type.
Definition MorphGeometry:59
virtual void addMorphTarget(osg::Geometry *morphTarget, float weight=1.0)
Add a MorphTarget to the MorphGeometry.
Definition MorphGeometry:112
bool _morphNormals
Do we also morph between normals?
Definition MorphGeometry:172
void dirty(bool b=true)
Set the MorphGeometry dirty.
Definition MorphGeometry:154
bool getMorphNormals() const
Get the flag for morphing normals.
Definition MorphGeometry:77
void setVertexSource(osg::Vec3Array *v)
Set source of vertices for this morph geometry.
Definition MorphGeometry:92
bool isDirty() const
Definition MorphGeometry:155
Definition MorphGeometry:39
void setGeometry(osg::Geometry *geom)
Definition MorphGeometry:49
osg::Geometry * getGeometry()
Definition MorphGeometry:47
const osg::Geometry * getGeometry() const
Definition MorphGeometry:48
float getWeight() const
Definition MorphGeometry:46
MorphTarget(osg::Geometry *geom, float w=1.0)
Definition MorphGeometry:44
float _weight
Definition MorphGeometry:42
void setWeight(float weight)
Definition MorphGeometry:45
osg::ref_ptr< osg::Geometry > _geom
Definition MorphGeometry:41
TargetNames _targetNames
Definition MorphGeometry:210
const std::string & getTargetName(unsigned int index)
Definition MorphGeometry:188
int link(Animation *animation)
META_Object(osgAnimation, UpdateMorph)
void removeTarget(const std::string &name)
Definition MorphGeometry:189
void setTargetNames(const TargetNames &targetNames)
Definition MorphGeometry:200
WeightTargets _weightTargets
Definition MorphGeometry:209
bool link(osgAnimation::Channel *channel)
unsigned int getNumTarget() const
Definition MorphGeometry:187
UpdateMorph(const UpdateMorph &apc, const osg::CopyOp &copyop)
const std::vector< std::string > & getTargetNames() const
Definition MorphGeometry:197
void addTarget(const std::string &name)
Definition MorphGeometry:186
std::vector< std::string > & getTargetNames()
Definition MorphGeometry:198
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
Callback method called by the NodeVisitor when visiting a node.
UpdateMorph(const std::string &name="")
std::map< int, osg::ref_ptr< osgAnimation::FloatTarget > > WeightTargets
Definition MorphGeometry:179
std::vector< std::string > TargetNames
Definition MorphGeometry:178
virtual void update(osg::NodeVisitor *, osg::Drawable *drw)
do customized update code.
Definition MorphGeometry:224
UpdateMorphGeometry()
Definition MorphGeometry:215
UpdateMorphGeometry(const UpdateMorphGeometry &org, const osg::CopyOp &copyop)
Definition MorphGeometry:217
META_Object(osgAnimation, UpdateMorphGeometry)
This class manage format for software morphing.
Definition MorphTransformSoftware:30
Definition RigTransform:47
#define NULL
Definition Export:55
#define OSGANIMATION_EXPORT
Definition Export:40

osg logo
Generated at Sun Jul 27 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.