root/OpenSceneGraph/trunk/include/osgAnimation/MorphGeometry @ 13041

Revision 13041, 5.7 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
22namespace 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          * @param weight The weight to be added to the \c MorphGeometry.
92          * @return  \c true for success; \c false otherwise.
93        */
94        virtual void addMorphTarget( osg::Geometry *morphTarget, float weight = 1.0 ) { _morphTargets.push_back(MorphTarget(morphTarget, weight)); _dirty = true; }
95
96        void setWeight(unsigned int index, float morphWeight)
97        {
98            if (index < _morphTargets.size())
99            {
100                _morphTargets[index].setWeight(morphWeight);
101                dirty();
102            }
103        }
104
105        /** Set the MorphGeometry dirty.*/
106        void dirty() { _dirty = true; }
107
108        /** Get the list of MorphTargets.*/
109        const MorphTargetList& getMorphTargetList() const { return _morphTargets; }
110
111        /** Get the list of MorphTargets. Warning if you modify this array you will have to call dirty() */
112        MorphTargetList& getMorphTargetList() { return _morphTargets; }
113
114        /** Return the \c MorphTarget at position \c i.*/
115        inline const MorphTarget& getMorphTarget( unsigned int i ) const { return _morphTargets[i]; }
116
117        /** Return the \c MorphTarget at position \c i.*/
118        inline MorphTarget& getMorphTarget( unsigned int i ) { return _morphTargets[i]; }
119
120    protected:
121        /// Do we need to recalculate the morphed geometry?
122        bool _dirty;
123
124        Method          _method;
125        MorphTargetList _morphTargets;
126
127        std::vector<osg::Vec3> _positionSource;
128        std::vector<osg::Vec3> _normalSource;
129
130        /// Do we also morph between normals?
131        bool _morphNormals;
132    };
133
134    class OSGANIMATION_EXPORT UpdateMorph : public AnimationUpdateCallback<osg::NodeCallback>
135    {
136    protected:
137        std::map<int, osg::ref_ptr<osgAnimation::FloatTarget> > _weightTargets;
138
139    public:
140
141        META_Object(osgAnimation, UpdateMorph);
142
143        UpdateMorph(const std::string& name = "");
144        UpdateMorph(const UpdateMorph& apc,const osg::CopyOp& copyop);
145
146        /** Callback method called by the NodeVisitor when visiting a node.*/
147        virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
148        bool needLink() const;
149        bool link(osgAnimation::Channel* channel);
150    };
151
152}
153
154#endif
Note: See TracBrowser for help on using the browser.