ImageSequence 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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_IMAGESEQUENCE
  14. #define OSG_IMAGESEQUENCE 1
  15. #include <OpenThreads/Mutex>
  16. #include <osg/ImageStream>
  17. #include <list>
  18. #include <set>
  19. namespace osg {
  20. /**
  21. * Image Buffer class.
  22. */
  23. class OSG_EXPORT ImageSequence : public ImageStream
  24. {
  25. public:
  26. ImageSequence();
  27. /** Copy constructor using CopyOp to manage deep vs shallow copy. */
  28. ImageSequence(const ImageSequence& ImageSequence, const CopyOp& copyop=CopyOp::SHALLOW_COPY);
  29. virtual Object* cloneType() const { return new ImageSequence(); }
  30. virtual Object* clone(const CopyOp& copyop) const { return new ImageSequence(*this,copyop); }
  31. virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const ImageSequence*>(obj)!=0; }
  32. virtual const char* libraryName() const { return "osg"; }
  33. virtual const char* className() const { return "ImageSequence"; }
  34. /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
  35. virtual int compare(const Image& rhs) const;
  36. virtual void setReferenceTime(double t) { _referenceTime = t; }
  37. virtual double getReferenceTime() const { return _referenceTime; }
  38. virtual void setTimeMultiplier(double tm) { _timeMultiplier = tm; }
  39. virtual double getTimeMultiplier() const { return _timeMultiplier; }
  40. struct OSG_EXPORT ImageData
  41. {
  42. ImageData();
  43. ImageData(const ImageData& id);
  44. ImageData& operator = (const ImageData& id);
  45. std::string _filename;
  46. osg::ref_ptr<osg::Image> _image;
  47. osg::ref_ptr<osg::Referenced> _imageRequest;
  48. };
  49. typedef std::vector<ImageData> ImageDataList;
  50. virtual void seek(double time);
  51. virtual void play();
  52. virtual void pause();
  53. virtual void rewind();
  54. enum Mode
  55. {
  56. PRE_LOAD_ALL_IMAGES,
  57. PAGE_AND_RETAIN_IMAGES,
  58. PAGE_AND_DISCARD_USED_IMAGES,
  59. LOAD_AND_RETAIN_IN_UPDATE_TRAVERSAL,
  60. LOAD_AND_DISCARD_IN_UPDATE_TRAVERSAL
  61. };
  62. void setMode(Mode mode);
  63. Mode getMode() const { return _mode; }
  64. void setLength(double length);
  65. virtual double getLength() const { return _length; }
  66. void addImageFile(const std::string& fileName);
  67. void setImageFile(unsigned int pos, const std::string& fileName);
  68. std::string getImageFile(unsigned int pos) const;
  69. void addImage(osg::Image* image);
  70. template<class T> void addImage(const osg::ref_ptr<T>& image) { addImage(image.get()); }
  71. void setImage(int s,int t,int r,
  72. GLint internalTextureformat,
  73. GLenum pixelFormat,GLenum type,
  74. unsigned char* data,
  75. AllocationMode mode,
  76. int packing=1) { Image::setImage(s,t,r,internalTextureformat, pixelFormat, type, data, mode, packing); }
  77. void setImage(unsigned int pos, osg::Image* image);
  78. template<class T> void setImage(unsigned int pos, const osg::ref_ptr<T>& image) { setImage(pos, image.get()); }
  79. Image* getImage(unsigned int pos);
  80. const Image* getImage(unsigned int pos) const;
  81. unsigned int getNumImageData() const { return _imageDataList.size(); }
  82. ImageDataList& getImageDataList() { return _imageDataList; }
  83. const ImageDataList& getImageDataList() const { return _imageDataList; }
  84. /** ImageSequence requires a call to update(NodeVisitor*) during the update traversal so return true.*/
  85. virtual bool requiresUpdateCall() const { return true; }
  86. /** update method for osg::Image subclasses that update themselves during the update traversal.*/
  87. virtual void update(NodeVisitor* nv);
  88. /** Set the optional osgDB::Options object to use when reading images.*/
  89. void setReadOptions(osg::Referenced* options) { _readOptions = options; }
  90. /** Get the optional osgDB::Options object used when reading images.*/
  91. osg::Referenced* getReadOptions() { return _readOptions.get(); }
  92. /** Get the optional osgDB::Options object used when reading images.*/
  93. const osg::Referenced* getReadOptions() const { return _readOptions.get(); }
  94. protected:
  95. virtual ~ImageSequence() {}
  96. virtual void applyLoopingMode();
  97. void setImageToChild(int pos);
  98. void computeTimePerImage();
  99. int imageIndex(double time);
  100. // setImage without acquiring mutex.
  101. void _setImage(unsigned int pos, osg::Image* image);
  102. double _referenceTime;
  103. double _timeMultiplier;
  104. Mode _mode;
  105. double _length;
  106. double _timePerImage;
  107. mutable OpenThreads::Mutex _mutex;
  108. ImageDataList _imageDataList;
  109. int _previousAppliedImageIndex;
  110. bool _seekTimeSet;
  111. double _seekTime;
  112. osg::ref_ptr<osg::Referenced> _readOptions;
  113. };
  114. } // namespace
  115. #endif