AnimationMaterial 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2018 Robert Osfield
  2. *
  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.
  7. *
  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.
  12. */
  13. #ifndef OSG_ANIMATIONMATERIAL
  14. #define OSG_ANIMATIONMATERIAL 1
  15. #include <osg/Material>
  16. #include <osg/Callback>
  17. #include <osgPresentation/Export>
  18. #include <iosfwd>
  19. #include <map>
  20. #include <float.h>
  21. namespace osgPresentation {
  22. /** AnimationMaterial for specify the time varying transformation pathway to use when update camera and model objects.
  23. * Subclassed from Transform::ComputeTransformCallback allows AnimationMaterial to
  24. * be attached directly to Transform nodes to move subgraphs around the scene.
  25. */
  26. class OSGPRESENTATION_EXPORT AnimationMaterial : public virtual osg::Object
  27. {
  28. public:
  29. AnimationMaterial():_loopMode(LOOP) {}
  30. AnimationMaterial(const AnimationMaterial& ap, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
  31. Object(ap,copyop),
  32. _timeControlPointMap(ap._timeControlPointMap),
  33. _loopMode(ap._loopMode) {}
  34. META_Object(osg,AnimationMaterial);
  35. /** get the transformation matrix for a point in time.*/
  36. bool getMaterial(double time,osg::Material& material) const;
  37. void insert(double time,osg::Material* material);
  38. double getFirstTime() const { if (!_timeControlPointMap.empty()) return _timeControlPointMap.begin()->first; else return 0.0;}
  39. double getLastTime() const { if (!_timeControlPointMap.empty()) return _timeControlPointMap.rbegin()->first; else return 0.0;}
  40. double getPeriod() const { return getLastTime()-getFirstTime();}
  41. enum LoopMode
  42. {
  43. SWING,
  44. LOOP,
  45. NO_LOOPING
  46. };
  47. void setLoopMode(LoopMode lm) { _loopMode = lm; }
  48. LoopMode getLoopMode() const { return _loopMode; }
  49. typedef std::map<double, osg::ref_ptr<osg::Material> > TimeControlPointMap;
  50. TimeControlPointMap& getTimeControlPointMap() { return _timeControlPointMap; }
  51. const TimeControlPointMap& getTimeControlPointMap() const { return _timeControlPointMap; }
  52. /** read the anumation path from a flat ascii file stream.*/
  53. void read(std::istream& in);
  54. /** write the anumation path to a flat ascii file stream.*/
  55. void write(std::ostream& out) const;
  56. bool requiresBlending() const;
  57. protected:
  58. virtual ~AnimationMaterial() {}
  59. void interpolate(osg::Material& material, float r, const osg::Material& lhs,const osg::Material& rhs) const;
  60. TimeControlPointMap _timeControlPointMap;
  61. LoopMode _loopMode;
  62. };
  63. class OSGPRESENTATION_EXPORT AnimationMaterialCallback : public osg::NodeCallback
  64. {
  65. public:
  66. AnimationMaterialCallback():
  67. _useInverseMatrix(false),
  68. _timeOffset(0.0),
  69. _timeMultiplier(1.0),
  70. _firstTime(DBL_MAX),
  71. _latestTime(0.0),
  72. _pause(false),
  73. _pauseTime(0.0) {}
  74. AnimationMaterialCallback(const AnimationMaterialCallback& apc,const osg::CopyOp& copyop):
  75. osg::Object(apc, copyop),
  76. osg::Callback(apc, copyop),
  77. osg::NodeCallback(apc,copyop),
  78. _animationMaterial(apc._animationMaterial),
  79. _useInverseMatrix(apc._useInverseMatrix),
  80. _timeOffset(apc._timeOffset),
  81. _timeMultiplier(apc._timeMultiplier),
  82. _firstTime(apc._firstTime),
  83. _latestTime(apc._latestTime),
  84. _pause(apc._pause),
  85. _pauseTime(apc._pauseTime) {}
  86. META_Object(osg,AnimationMaterialCallback);
  87. AnimationMaterialCallback(AnimationMaterial* ap,double timeOffset=0.0f,double timeMultiplier=1.0f):
  88. _animationMaterial(ap),
  89. _useInverseMatrix(false),
  90. _timeOffset(timeOffset),
  91. _timeMultiplier(timeMultiplier),
  92. _firstTime(DBL_MAX),
  93. _latestTime(0.0),
  94. _pause(false),
  95. _pauseTime(0.0) {}
  96. void setAnimationMaterial(AnimationMaterial* path) { _animationMaterial = path; }
  97. AnimationMaterial* getAnimationMaterial() { return _animationMaterial.get(); }
  98. const AnimationMaterial* getAnimationMaterial() const { return _animationMaterial.get(); }
  99. void setTimeOffset(double offset) { _timeOffset = offset; }
  100. double getTimeOffset() const { return _timeOffset; }
  101. void setTimeMultiplier(double multiplier) { _timeMultiplier = multiplier; }
  102. double getTimeMultiplier() const { return _timeMultiplier; }
  103. void reset();
  104. void setPause(bool pause);
  105. /** get the animation time that is used to specify the position along the AnimationMaterial.
  106. * Animation time is computed from the formula ((_latestTime-_firstTime)-_timeOffset)*_timeMultiplier.*/
  107. double getAnimationTime() const;
  108. /** implements the callback*/
  109. virtual void operator()(osg::Node* node, osg::NodeVisitor* nv);
  110. void update(osg::Node& node);
  111. public:
  112. osg::ref_ptr<AnimationMaterial> _animationMaterial;
  113. bool _useInverseMatrix;
  114. double _timeOffset;
  115. double _timeMultiplier;
  116. double _firstTime;
  117. double _latestTime;
  118. bool _pause;
  119. double _pauseTime;
  120. protected:
  121. ~AnimationMaterialCallback(){}
  122. };
  123. }
  124. #endif