MorphGeometry
Go to the documentation of this file.
/* -*-c++-*-
* Copyright (C) 2008 Cedric Pinson <mornifle@plopbyte.net>
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGANIMATION_MORPHGEOMETRY_H
#define OSGANIMATION_MORPHGEOMETRY_H
#include <osgAnimation/Export>
#include <osgAnimation/AnimationUpdateCallback>
#include <osgAnimation/MorphTransformSoftware>
#include <osg/Geometry>
#include <algorithm>
namespace osgAnimation
{
class OSGANIMATION_EXPORT MorphGeometry : public osg::Geometry
{
public:
enum Method
{
NORMALIZED,
RELATIVE
};
class MorphTarget
{
protected:
osg::ref_ptr<osg::Geometry> _geom;
float _weight;
public:
MorphTarget(osg::Geometry* geom, float w = 1.0) : _geom(geom), _weight(w) {}
void setWeight(float weight) { _weight = weight; }
float getWeight() const { return _weight; }
osg::Geometry* getGeometry() { return _geom.get(); }
const osg::Geometry* getGeometry() const { return _geom.get(); }
void setGeometry(osg::Geometry* geom) { _geom = geom; }
};
typedef std::vector<MorphTarget> MorphTargetList;
MorphGeometry();
MorphGeometry(const osg::Geometry& b);
MorphGeometry(const MorphGeometry& b, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY);
virtual osg::Object* cloneType() const { return new MorphGeometry(); }
virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new MorphGeometry(*this,copyop); }
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const MorphGeometry*>(obj)!=NULL; }
virtual const char* libraryName() const { return "osgAnimation"; }
virtual const char* className() const { return "MorphGeometry"; }
// set implementation of rig method
inline void setMorphTransformImplementation(MorphTransform*mt) { _morphTransformImplementation=mt; }
inline MorphTransform* getMorphTransformImplementation() { return _morphTransformImplementation.get(); }
inline const MorphTransform* getMorphTransformImplementation() const { return _morphTransformImplementation.get(); }
inline void setMethod(Method method) { _method = method; }
inline Method getMethod() const { return _method; }
inline void setMorphNormals(bool morphNormals) { _morphNormals = morphNormals; }
inline bool getMorphNormals() const { return _morphNormals; }
inline const MorphTargetList& getMorphTargetList() const { return _morphTargets; }
inline MorphTargetList& getMorphTargetList() { return _morphTargets; }
inline const MorphTarget& getMorphTarget( unsigned int i ) const { return _morphTargets[i]; }
inline MorphTarget& getMorphTarget( unsigned int i ) { return _morphTargets[i]; }
inline void setVertexSource(osg::Vec3Array *v) { _positionSource=v; }
inline osg::Vec3Array * getVertexSource() const { return _positionSource.get(); }
inline void setNormalSource(osg::Vec3Array *n) { _normalSource=n; }
inline osg::Vec3Array * getNormalSource() const { return _normalSource.get(); }
virtual void addMorphTarget( osg::Geometry *morphTarget, float weight = 1.0 )
{
_morphTargets.push_back(MorphTarget(morphTarget, weight));
_dirty = true;
}
virtual void removeMorphTarget( osg::Geometry *morphTarget )
{
for(MorphTargetList::iterator iterator = _morphTargets.begin() ; iterator != _morphTargets.end() ; ++ iterator)
{
if(iterator->getGeometry() == morphTarget)
{
_morphTargets.erase(iterator);
break;
}
}
}
virtual void removeMorphTarget( const std::string& name )
{
for(MorphTargetList::iterator iterator = _morphTargets.begin() ; iterator != _morphTargets.end() ; ++ iterator)
{
if(iterator->getGeometry() && iterator->getGeometry()->getName() == name)
{
_morphTargets.erase(iterator);
break;
}
}
}
inline void setWeight(unsigned int index, float morphWeight)
{
if (index < _morphTargets.size())
{
_morphTargets[index].setWeight(morphWeight);
dirty();
}
}
inline void dirty(bool b=true) { _dirty = b; }
inline bool isDirty() const { return _dirty; }
void transformSoftwareMethod() { (*_morphTransformImplementation.get())(*this); }
protected:
osg::ref_ptr<MorphTransform> _morphTransformImplementation;
bool _dirty;
Method _method;
MorphTargetList _morphTargets;
osg::ref_ptr<osg::Vec3Array> _positionSource;
osg::ref_ptr<osg::Vec3Array> _normalSource;
bool _morphNormals;
};
class OSGANIMATION_EXPORT UpdateMorph : public AnimationUpdateCallback<osg::NodeCallback>
{
public:
typedef std::vector<std::string> TargetNames;
typedef std::map< int, osg::ref_ptr<osgAnimation::FloatTarget> > WeightTargets;
META_Object(osgAnimation, UpdateMorph);
UpdateMorph(const std::string& name = "");
UpdateMorph(const UpdateMorph& apc,const osg::CopyOp& copyop);
void addTarget(const std::string& name) { _targetNames.push_back(name); }
unsigned int getNumTarget() const { return _targetNames.size(); }
const std::string& getTargetName(unsigned int index) { return _targetNames[index]; }
void removeTarget(const std::string& name)
{
TargetNames::iterator found = std::find(_targetNames.begin(), _targetNames.end(), name);
if(found != _targetNames.end())
_targetNames.erase(found);
}
// for serialization
const std::vector<std::string>& getTargetNames() const { return _targetNames; }
std::vector<std::string>& getTargetNames() { return _targetNames; }
void setTargetNames(const TargetNames& targetNames) { _targetNames.assign(targetNames.begin(), targetNames.end()); }
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
bool needLink() const;
bool link(osgAnimation::Channel* channel);
int link(Animation* animation);
protected:
WeightTargets _weightTargets;
TargetNames _targetNames;
};
struct UpdateMorphGeometry : public osg::DrawableUpdateCallback
{
UpdateMorphGeometry() {}
UpdateMorphGeometry(const UpdateMorphGeometry& org, const osg::CopyOp& copyop):
osg::Object(org, copyop),
osg::Callback(org, copyop),
osg::DrawableUpdateCallback(org, copyop) {}
META_Object(osgAnimation, UpdateMorphGeometry);
virtual void update(osg::NodeVisitor*, osg::Drawable* drw)
{
MorphGeometry* geom = dynamic_cast<MorphGeometry*>(drw);
if (!geom)
return;
if (!geom->getMorphTransformImplementation())
{
geom->setMorphTransformImplementation( new MorphTransformSoftware);
}
MorphTransform& implementation = *geom->getMorphTransformImplementation();
(implementation)(*geom);
}
};
}
#endif
AnimationUpdateCallback
Geometry
MorphTransformSoftware
osgAnimation::AnimationUpdateCallback< osg::NodeCallback >::AnimationUpdateCallback
AnimationUpdateCallback()
Definition AnimationUpdateCallback:38
osgAnimation::Animation
Definition Animation:29
osgAnimation::Channel
Definition Channel:33
osgAnimation::MorphGeometry::MorphTarget
Definition MorphGeometry:39
osgAnimation::MorphGeometry::MorphTarget::setGeometry
void setGeometry(osg::Geometry *geom)
Definition MorphGeometry:49
osgAnimation::MorphGeometry::MorphTarget::getGeometry
osg::Geometry * getGeometry()
Definition MorphGeometry:47
osgAnimation::MorphGeometry::MorphTarget::getGeometry
const osg::Geometry * getGeometry() const
Definition MorphGeometry:48
osgAnimation::MorphGeometry::MorphTarget::getWeight
float getWeight() const
Definition MorphGeometry:46
osgAnimation::MorphGeometry::MorphTarget::MorphTarget
MorphTarget(osg::Geometry *geom, float w=1.0)
Definition MorphGeometry:44
osgAnimation::MorphGeometry::MorphTarget::_weight
float _weight
Definition MorphGeometry:42
osgAnimation::MorphGeometry::MorphTarget::setWeight
void setWeight(float weight)
Definition MorphGeometry:45
osgAnimation::MorphGeometry::MorphTarget::_geom
osg::ref_ptr< osg::Geometry > _geom
Definition MorphGeometry:41
osgAnimation::MorphGeometry
Definition MorphGeometry:28
osgAnimation::MorphGeometry::getMorphTarget
MorphTarget & getMorphTarget(unsigned int i)
Definition MorphGeometry:89
osgAnimation::MorphGeometry::getMorphTransformImplementation
MorphTransform * getMorphTransformImplementation()
Definition MorphGeometry:66
osgAnimation::MorphGeometry::_morphTransformImplementation
osg::ref_ptr< MorphTransform > _morphTransformImplementation
Definition MorphGeometry:161
osgAnimation::MorphGeometry::getNormalSource
osg::Vec3Array * getNormalSource() const
Definition MorphGeometry:101
osgAnimation::MorphGeometry::Method
Method
Definition MorphGeometry:33
osgAnimation::MorphGeometry::RELATIVE
@ RELATIVE
Definition MorphGeometry:35
osgAnimation::MorphGeometry::NORMALIZED
@ NORMALIZED
Definition MorphGeometry:34
osgAnimation::MorphGeometry::cloneType
virtual osg::Object * cloneType() const
Definition MorphGeometry:58
osgAnimation::MorphGeometry::MorphGeometry
MorphGeometry(const MorphGeometry &b, const osg::CopyOp &copyop=osg::CopyOp::SHALLOW_COPY)
osgAnimation::MorphGeometry::_normalSource
osg::ref_ptr< osg::Vec3Array > _normalSource
Definition MorphGeometry:169
osgAnimation::MorphGeometry::_positionSource
osg::ref_ptr< osg::Vec3Array > _positionSource
Definition MorphGeometry:168
osgAnimation::MorphGeometry::getMethod
Method getMethod() const
Definition MorphGeometry:72
osgAnimation::MorphGeometry::MorphGeometry
MorphGeometry(const osg::Geometry &b)
osgAnimation::MorphGeometry::getMorphTarget
const MorphTarget & getMorphTarget(unsigned int i) const
Definition MorphGeometry:86
osgAnimation::MorphGeometry::isSameKindAs
virtual bool isSameKindAs(const osg::Object *obj) const
Definition MorphGeometry:60
osgAnimation::MorphGeometry::setMethod
void setMethod(Method method)
Definition MorphGeometry:70
osgAnimation::MorphGeometry::MorphTargetList
std::vector< MorphTarget > MorphTargetList
Definition MorphGeometry:52
osgAnimation::MorphGeometry::removeMorphTarget
virtual void removeMorphTarget(osg::Geometry *morphTarget)
Definition MorphGeometry:118
osgAnimation::MorphGeometry::_dirty
bool _dirty
Do we need to recalculate the morphed geometry?
Definition MorphGeometry:163
osgAnimation::MorphGeometry::getVertexSource
osg::Vec3Array * getVertexSource() const
Definition MorphGeometry:95
osgAnimation::MorphGeometry::getMorphTransformImplementation
const MorphTransform * getMorphTransformImplementation() const
Definition MorphGeometry:67
osgAnimation::MorphGeometry::MorphGeometry
MorphGeometry()
osgAnimation::MorphGeometry::setMorphNormals
void setMorphNormals(bool morphNormals)
Definition MorphGeometry:75
osgAnimation::MorphGeometry::transformSoftwareMethod
void transformSoftwareMethod()
Definition MorphGeometry:158
osgAnimation::MorphGeometry::_morphTargets
MorphTargetList _morphTargets
Definition MorphGeometry:166
osgAnimation::MorphGeometry::removeMorphTarget
virtual void removeMorphTarget(const std::string &name)
Definition MorphGeometry:130
osgAnimation::MorphGeometry::libraryName
virtual const char * libraryName() const
Definition MorphGeometry:61
osgAnimation::MorphGeometry::getMorphTargetList
const MorphTargetList & getMorphTargetList() const
Definition MorphGeometry:80
osgAnimation::MorphGeometry::setNormalSource
void setNormalSource(osg::Vec3Array *n)
Definition MorphGeometry:98
osgAnimation::MorphGeometry::_method
Method _method
Definition MorphGeometry:165
osgAnimation::MorphGeometry::setWeight
void setWeight(unsigned int index, float morphWeight)
Definition MorphGeometry:144
osgAnimation::MorphGeometry::setMorphTransformImplementation
void setMorphTransformImplementation(MorphTransform *mt)
Definition MorphGeometry:65
osgAnimation::MorphGeometry::getMorphTargetList
MorphTargetList & getMorphTargetList()
Definition MorphGeometry:83
osgAnimation::MorphGeometry::className
virtual const char * className() const
Definition MorphGeometry:62
osgAnimation::MorphGeometry::clone
virtual osg::Object * clone(const osg::CopyOp &copyop) const
Definition MorphGeometry:59
osgAnimation::MorphGeometry::addMorphTarget
virtual void addMorphTarget(osg::Geometry *morphTarget, float weight=1.0)
Definition MorphGeometry:112
osgAnimation::MorphGeometry::_morphNormals
bool _morphNormals
Do we also morph between normals?
Definition MorphGeometry:172
osgAnimation::MorphGeometry::dirty
void dirty(bool b=true)
Definition MorphGeometry:154
osgAnimation::MorphGeometry::getMorphNormals
bool getMorphNormals() const
Definition MorphGeometry:77
osgAnimation::MorphGeometry::setVertexSource
void setVertexSource(osg::Vec3Array *v)
Definition MorphGeometry:92
osgAnimation::MorphGeometry::isDirty
bool isDirty() const
Definition MorphGeometry:155
osgAnimation::MorphTransformSoftware
This class manage format for software morphing.
Definition MorphTransformSoftware:30
osgAnimation::MorphTransform
Definition RigTransform:47
osgAnimation::UpdateMorph::_targetNames
TargetNames _targetNames
Definition MorphGeometry:210
osgAnimation::UpdateMorph::getTargetName
const std::string & getTargetName(unsigned int index)
Definition MorphGeometry:188
osgAnimation::UpdateMorph::link
int link(Animation *animation)
osgAnimation::UpdateMorph::META_Object
META_Object(osgAnimation, UpdateMorph)
osgAnimation::UpdateMorph::removeTarget
void removeTarget(const std::string &name)
Definition MorphGeometry:189
osgAnimation::UpdateMorph::setTargetNames
void setTargetNames(const TargetNames &targetNames)
Definition MorphGeometry:200
osgAnimation::UpdateMorph::needLink
bool needLink() const
osgAnimation::UpdateMorph::_weightTargets
WeightTargets _weightTargets
Definition MorphGeometry:209
osgAnimation::UpdateMorph::link
bool link(osgAnimation::Channel *channel)
osgAnimation::UpdateMorph::getNumTarget
unsigned int getNumTarget() const
Definition MorphGeometry:187
osgAnimation::UpdateMorph::UpdateMorph
UpdateMorph(const UpdateMorph &apc, const osg::CopyOp &copyop)
osgAnimation::UpdateMorph::getTargetNames
const std::vector< std::string > & getTargetNames() const
Definition MorphGeometry:197
osgAnimation::UpdateMorph::addTarget
void addTarget(const std::string &name)
Definition MorphGeometry:186
osgAnimation::UpdateMorph::getTargetNames
std::vector< std::string > & getTargetNames()
Definition MorphGeometry:198
osgAnimation::UpdateMorph::operator()
virtual void operator()(osg::Node *node, osg::NodeVisitor *nv)
osgAnimation::UpdateMorph::UpdateMorph
UpdateMorph(const std::string &name="")
osgAnimation::UpdateMorph::WeightTargets
std::map< int, osg::ref_ptr< osgAnimation::FloatTarget > > WeightTargets
Definition MorphGeometry:179
osgAnimation::UpdateMorph::TargetNames
std::vector< std::string > TargetNames
Definition MorphGeometry:178
osg::Callback::Callback
Callback()
Definition Callback:38
osg::CopyOp
Definition CopyOp:41
osg::CopyOp::SHALLOW_COPY
@ SHALLOW_COPY
Definition CopyOp:47
osg::DrawableUpdateCallback
Definition Callback:298
osg::DrawableUpdateCallback::DrawableUpdateCallback
DrawableUpdateCallback()
Definition Callback:300
osg::Drawable
Definition Drawable:89
osg::Geometry
Definition Geometry:31
osg::NodeVisitor
Definition NodeVisitor:82
osg::Node
Definition Node:72
osg::Object
Definition Object:61
osg::Object::Object
Object()
Definition Object:69
osg::ref_ptr
Definition ref_ptr:32
osgAnimation
Definition Action:34
osg
Definition AlphaFunc:19
osg::Vec3Array
TemplateArray< Vec3, Array::Vec3ArrayType, 3, GL_FLOAT > Vec3Array
Definition Array:449
Export
OSGANIMATION_EXPORT
#define OSGANIMATION_EXPORT
Definition osgAnimation/Export:40
NULL
#define NULL
Definition osg/Export:55
osgAnimation::UpdateMorphGeometry::update
virtual void update(osg::NodeVisitor *, osg::Drawable *drw)
Definition MorphGeometry:224
osgAnimation::UpdateMorphGeometry::UpdateMorphGeometry
UpdateMorphGeometry()
Definition MorphGeometry:215
osgAnimation::UpdateMorphGeometry::UpdateMorphGeometry
UpdateMorphGeometry(const UpdateMorphGeometry &org, const osg::CopyOp &copyop)
Definition MorphGeometry:217
osgAnimation::UpdateMorphGeometry::META_Object
META_Object(osgAnimation, UpdateMorphGeometry)