| 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 <osg/Geometry> |
|---|
| 20 | #include <osgAnimation/UpdateCallback> |
|---|
| 21 | |
|---|
| 22 | namespace osgAnimation |
|---|
| 23 | { |
|---|
| 24 | |
|---|
| 25 | class OSGANIMATION_EXPORT MorphGeometry : public osg::Geometry |
|---|
| 26 | { |
|---|
| 27 | |
|---|
| 28 | public: |
|---|
| 29 | |
|---|
| 30 | enum Method { |
|---|
| 31 | NORMALIZED, |
|---|
| 32 | RELATIVE |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | class MorphTarget |
|---|
| 36 | { |
|---|
| 37 | protected: |
|---|
| 38 | osg::ref_ptr<osg::Geometry> _geom; |
|---|
| 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 | const 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 | struct UpdateVertex : public osg::Drawable::UpdateCallback |
|---|
| 52 | { |
|---|
| 53 | virtual void update(osg::NodeVisitor*, osg::Drawable* drw) |
|---|
| 54 | { |
|---|
| 55 | MorphGeometry* geom = dynamic_cast<MorphGeometry*>(drw); |
|---|
| 56 | if (!geom) |
|---|
| 57 | return; |
|---|
| 58 | |
|---|
| 59 | geom->transformSoftwareMethod(); |
|---|
| 60 | } |
|---|
| 61 | }; |
|---|
| 62 | |
|---|
| 63 | MorphGeometry(); |
|---|
| 64 | MorphGeometry(const osg::Geometry& b); |
|---|
| 65 | MorphGeometry(const MorphGeometry& b, const osg::CopyOp& copyop = osg::CopyOp::SHALLOW_COPY); |
|---|
| 66 | |
|---|
| 67 | virtual osg::Object* cloneType() const { return new MorphGeometry(); } |
|---|
| 68 | virtual osg::Object* clone(const osg::CopyOp& copyop) const { return new MorphGeometry(*this,copyop); } |
|---|
| 69 | virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const MorphGeometry*>(obj)!=NULL; } |
|---|
| 70 | virtual const char* libraryName() const { return "osgAnimation"; } |
|---|
| 71 | virtual const char* className() const { return "MorphGeometry"; } |
|---|
| 72 | |
|---|
| 73 | virtual void transformSoftwareMethod(); |
|---|
| 74 | |
|---|
| 75 | /** Set the morphing method. */ |
|---|
| 76 | void setMethod(Method method) { _method = method; } |
|---|
| 77 | /** Get the morphing method. */ |
|---|
| 78 | inline Method getMethod() const { return _method; } |
|---|
| 79 | |
|---|
| 80 | /** Set flag for morphing normals. */ |
|---|
| 81 | void setMorphNormals(bool morphNormals) { _morphNormals = morphNormals; } |
|---|
| 82 | /** Get the flag for morphing normals. */ |
|---|
| 83 | inline bool getMorphNormals() const { return _morphNormals; } |
|---|
| 84 | |
|---|
| 85 | /** Add a \c MorphTarget to the \c MorphGeometry. |
|---|
| 86 | * If \c MorphTarget is not \c NULL and is not contained in the \c MorphGeometry |
|---|
| 87 | * then increment its reference count, add it to the MorphTargets list and |
|---|
| 88 | * dirty the bounding sphere to force it to be recomputed on the next |
|---|
| 89 | * call to \c getBound(). |
|---|
| 90 | * @param morphTarget The \c MorphTarget to be added to the \c MorphGeometry. |
|---|
| 91 | * @return \c true for success; \c false otherwise. |
|---|
| 92 | */ |
|---|
| 93 | virtual void addMorphTarget( osg::Geometry *morphTarget, float weight = 1.0 ) { _morphTargets.push_back(MorphTarget(morphTarget, weight)); _dirty = true; } |
|---|
| 94 | |
|---|
| 95 | void setWeight(unsigned int index, float morphWeight) |
|---|
| 96 | { |
|---|
| 97 | if (index < _morphTargets.size()) |
|---|
| 98 | { |
|---|
| 99 | _morphTargets[index].setWeight(morphWeight); |
|---|
| 100 | dirty(); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | /** Set the MorphGeometry dirty.*/ |
|---|
| 105 | void dirty() { _dirty = true; } |
|---|
| 106 | |
|---|
| 107 | /** Get the list of MorphTargets.*/ |
|---|
| 108 | const MorphTargetList& getMorphTargetList() const { return _morphTargets; } |
|---|
| 109 | |
|---|
| 110 | /** Get the list of MorphTargets. Warning if you modify this array you will have to call dirty() */ |
|---|
| 111 | MorphTargetList& getMorphTargetList() { return _morphTargets; } |
|---|
| 112 | |
|---|
| 113 | /** Return the \c MorphTarget at position \c i.*/ |
|---|
| 114 | inline const MorphTarget& getMorphTarget( unsigned int i ) const { return _morphTargets[i]; } |
|---|
| 115 | |
|---|
| 116 | /** Return the \c MorphTarget at position \c i.*/ |
|---|
| 117 | inline MorphTarget& getMorphTarget( unsigned int i ) { return _morphTargets[i]; } |
|---|
| 118 | |
|---|
| 119 | protected: |
|---|
| 120 | /// Do we need to recalculate the morphed geometry? |
|---|
| 121 | bool _dirty; |
|---|
| 122 | |
|---|
| 123 | Method _method; |
|---|
| 124 | MorphTargetList _morphTargets; |
|---|
| 125 | |
|---|
| 126 | std::vector<osg::Vec3> _positionSource; |
|---|
| 127 | std::vector<osg::Vec3> _normalSource; |
|---|
| 128 | |
|---|
| 129 | /// Do we also morph between normals? |
|---|
| 130 | bool _morphNormals; |
|---|
| 131 | }; |
|---|
| 132 | |
|---|
| 133 | class OSGANIMATION_EXPORT UpdateMorph : public AnimationUpdateCallback |
|---|
| 134 | { |
|---|
| 135 | protected: |
|---|
| 136 | std::map<int, osg::ref_ptr<osgAnimation::FloatTarget> > _weightTargets; |
|---|
| 137 | |
|---|
| 138 | public: |
|---|
| 139 | |
|---|
| 140 | META_Object(osgAnimation, UpdateMorph); |
|---|
| 141 | |
|---|
| 142 | UpdateMorph(const std::string& name = ""); |
|---|
| 143 | UpdateMorph(const UpdateMorph& apc,const osg::CopyOp& copyop); |
|---|
| 144 | |
|---|
| 145 | /** Callback method called by the NodeVisitor when visiting a node.*/ |
|---|
| 146 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv); |
|---|
| 147 | bool needLink() const; |
|---|
| 148 | bool link(osgAnimation::Channel* channel); |
|---|
| 149 | }; |
|---|
| 150 | |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | #endif |
|---|