1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2018 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
14#ifndef OSG_ANIMATIONMATERIAL
15#define OSG_ANIMATIONMATERIAL 1
17#include <osg/Material>
18#include <osg/Callback>
20#include <osgPresentation/Export>
26namespace osgPresentation {
28/** AnimationMaterial for specify the time varying transformation pathway to use when update camera and model objects.
29 * Subclassed from Transform::ComputeTransformCallback allows AnimationMaterial to
30 * be attached directly to Transform nodes to move subgraphs around the scene.
32class OSGPRESENTATION_EXPORT AnimationMaterial : public virtual osg::Object
36 AnimationMaterial():_loopMode(LOOP) {}
38 AnimationMaterial(const AnimationMaterial& ap, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
40 _timeControlPointMap(ap._timeControlPointMap),
41 _loopMode(ap._loopMode) {}
43 META_Object(osg,AnimationMaterial);
46 /** get the transformation matrix for a point in time.*/
47 bool getMaterial(double time,osg::Material& material) const;
49 void insert(double time,osg::Material* material);
51 double getFirstTime() const { if (!_timeControlPointMap.empty()) return _timeControlPointMap.begin()->first; else return 0.0;}
52 double getLastTime() const { if (!_timeControlPointMap.empty()) return _timeControlPointMap.rbegin()->first; else return 0.0;}
53 double getPeriod() const { return getLastTime()-getFirstTime();}
62 void setLoopMode(LoopMode lm) { _loopMode = lm; }
64 LoopMode getLoopMode() const { return _loopMode; }
67 typedef std::map<double, osg::ref_ptr<osg::Material> > TimeControlPointMap;
69 TimeControlPointMap& getTimeControlPointMap() { return _timeControlPointMap; }
71 const TimeControlPointMap& getTimeControlPointMap() const { return _timeControlPointMap; }
73 /** read the anumation path from a flat ascii file stream.*/
74 void read(std::istream& in);
76 /** write the anumation path to a flat ascii file stream.*/
77 void write(std::ostream& out) const;
79 bool requiresBlending() const;
83 virtual ~AnimationMaterial() {}
85 void interpolate(osg::Material& material, float r, const osg::Material& lhs,const osg::Material& rhs) const;
87 TimeControlPointMap _timeControlPointMap;
93class OSGPRESENTATION_EXPORT AnimationMaterialCallback : public osg::NodeCallback
97 AnimationMaterialCallback():
98 _useInverseMatrix(false),
100 _timeMultiplier(1.0),
107 AnimationMaterialCallback(const AnimationMaterialCallback& apc,const osg::CopyOp& copyop):
108 osg::Object(apc, copyop),
109 osg::Callback(apc, copyop),
110 osg::NodeCallback(apc,copyop),
111 _animationMaterial(apc._animationMaterial),
112 _useInverseMatrix(apc._useInverseMatrix),
113 _timeOffset(apc._timeOffset),
114 _timeMultiplier(apc._timeMultiplier),
115 _firstTime(apc._firstTime),
116 _latestTime(apc._latestTime),
118 _pauseTime(apc._pauseTime) {}
121 META_Object(osg,AnimationMaterialCallback);
123 AnimationMaterialCallback(AnimationMaterial* ap,double timeOffset=0.0f,double timeMultiplier=1.0f):
124 _animationMaterial(ap),
125 _useInverseMatrix(false),
126 _timeOffset(timeOffset),
127 _timeMultiplier(timeMultiplier),
133 void setAnimationMaterial(AnimationMaterial* path) { _animationMaterial = path; }
135 AnimationMaterial* getAnimationMaterial() { return _animationMaterial.get(); }
137 const AnimationMaterial* getAnimationMaterial() const { return _animationMaterial.get(); }
139 void setTimeOffset(double offset) { _timeOffset = offset; }
140 double getTimeOffset() const { return _timeOffset; }
142 void setTimeMultiplier(double multiplier) { _timeMultiplier = multiplier; }
143 double getTimeMultiplier() const { return _timeMultiplier; }
147 void setPause(bool pause);
149 /** get the animation time that is used to specify the position along the AnimationMaterial.
150 * Animation time is computed from the formula ((_latestTime-_firstTime)-_timeOffset)*_timeMultiplier.*/
151 double getAnimationTime() const;
153 /** implements the callback*/
154 virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
156 void update(osg::Node& node);
160 osg::ref_ptr<AnimationMaterial> _animationMaterial;
161 bool _useInverseMatrix;
163 double _timeMultiplier;
171 ~AnimationMaterialCallback(){}