| 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 | |
|---|
| 14 | #ifndef OSG_IMAGESEQUENCE |
|---|
| 15 | #define OSG_IMAGESEQUENCE 1 |
|---|
| 16 | |
|---|
| 17 | #include <OpenThreads/Mutex> |
|---|
| 18 | #include <osg/ImageStream> |
|---|
| 19 | #include <osg/StateAttribute> |
|---|
| 20 | |
|---|
| 21 | #include <list> |
|---|
| 22 | #include <set> |
|---|
| 23 | |
|---|
| 24 | namespace osg { |
|---|
| 25 | |
|---|
| 26 | /** |
|---|
| 27 | * Image Buffer class. |
|---|
| 28 | */ |
|---|
| 29 | class OSG_EXPORT ImageSequence : public ImageStream |
|---|
| 30 | { |
|---|
| 31 | public: |
|---|
| 32 | ImageSequence(); |
|---|
| 33 | |
|---|
| 34 | /** Copy constructor using CopyOp to manage deep vs shallow copy. */ |
|---|
| 35 | ImageSequence(const ImageSequence& ImageSequence, const CopyOp& copyop=CopyOp::SHALLOW_COPY); |
|---|
| 36 | |
|---|
| 37 | virtual Object* cloneType() const { return new ImageSequence(); } |
|---|
| 38 | virtual Object* clone(const CopyOp& copyop) const { return new ImageSequence(*this,copyop); } |
|---|
| 39 | virtual bool isSameKindAs(const Object* obj) const { return dynamic_cast<const ImageSequence*>(obj)!=0; } |
|---|
| 40 | virtual const char* libraryName() const { return "osg"; } |
|---|
| 41 | virtual const char* className() const { return "ImageSequence"; } |
|---|
| 42 | |
|---|
| 43 | /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */ |
|---|
| 44 | virtual int compare(const Image& rhs) const; |
|---|
| 45 | |
|---|
| 46 | virtual void setReferenceTime(double t) { _referenceTime = t; } |
|---|
| 47 | virtual double getReferenceTime() const { return _referenceTime; } |
|---|
| 48 | |
|---|
| 49 | virtual void setTimeMultiplier(double tm) { _timeMultiplier = tm; } |
|---|
| 50 | virtual double getTimeMultiplier() const { return _timeMultiplier; } |
|---|
| 51 | |
|---|
| 52 | typedef std::vector< osg::ref_ptr<osg::Image> > Images; |
|---|
| 53 | typedef std::vector< std::string > FileNames; |
|---|
| 54 | |
|---|
| 55 | virtual void seek(double time); |
|---|
| 56 | |
|---|
| 57 | virtual void play(); |
|---|
| 58 | |
|---|
| 59 | virtual void pause(); |
|---|
| 60 | |
|---|
| 61 | virtual void rewind(); |
|---|
| 62 | |
|---|
| 63 | enum Mode |
|---|
| 64 | { |
|---|
| 65 | PRE_LOAD_ALL_IMAGES, |
|---|
| 66 | PAGE_AND_RETAIN_IMAGES, |
|---|
| 67 | PAGE_AND_DISCARD_USED_IMAGES |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | void setMode(Mode mode); |
|---|
| 71 | Mode getMode() const { return _mode; } |
|---|
| 72 | |
|---|
| 73 | void setLength(double length); |
|---|
| 74 | virtual double getLength() const { return _length; } |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | void addImageFile(const std::string& fileName); |
|---|
| 78 | |
|---|
| 79 | void setImageFile(unsigned int pos, const std::string& fileName); |
|---|
| 80 | std::string getImageFile(unsigned int pos) const; |
|---|
| 81 | |
|---|
| 82 | unsigned int getNumImageFiles() const { return _fileNames.size(); } |
|---|
| 83 | |
|---|
| 84 | FileNames& getFileNames() { return _fileNames; } |
|---|
| 85 | const FileNames& getFileNames() const { return _fileNames; } |
|---|
| 86 | |
|---|
| 87 | void addImage(osg::Image* image); |
|---|
| 88 | |
|---|
| 89 | void setImage(int s,int t,int r, |
|---|
| 90 | GLint internalTextureformat, |
|---|
| 91 | GLenum pixelFormat,GLenum type, |
|---|
| 92 | unsigned char* data, |
|---|
| 93 | AllocationMode mode, |
|---|
| 94 | int packing=1) { Image::setImage(s,t,r,internalTextureformat, pixelFormat, type, data, mode, packing); } |
|---|
| 95 | |
|---|
| 96 | void setImage(unsigned int pos, osg::Image* image); |
|---|
| 97 | Image* getImage(unsigned int pos); |
|---|
| 98 | const Image* getImage(unsigned int pos) const; |
|---|
| 99 | |
|---|
| 100 | unsigned int getNumImages() const { return _images.size(); } |
|---|
| 101 | |
|---|
| 102 | Images& getImages() { return _images; } |
|---|
| 103 | const Images& getImages() const { return _images; } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | virtual void update(NodeVisitor* nv); |
|---|
| 107 | |
|---|
| 108 | struct OSG_EXPORT UpdateCallback : public osg::StateAttributeCallback |
|---|
| 109 | { |
|---|
| 110 | virtual void operator () (osg::StateAttribute* attr, osg::NodeVisitor* nv); |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | protected: |
|---|
| 114 | |
|---|
| 115 | virtual ~ImageSequence() {} |
|---|
| 116 | |
|---|
| 117 | virtual void applyLoopingMode(); |
|---|
| 118 | |
|---|
| 119 | void setImageToChild(const osg::Image* image); |
|---|
| 120 | |
|---|
| 121 | void computeTimePerImage(); |
|---|
| 122 | |
|---|
| 123 | int imageIndex(double time); |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | double _referenceTime; |
|---|
| 127 | double _timeMultiplier; |
|---|
| 128 | |
|---|
| 129 | Mode _mode; |
|---|
| 130 | double _length; |
|---|
| 131 | |
|---|
| 132 | double _timePerImage; |
|---|
| 133 | |
|---|
| 134 | mutable OpenThreads::Mutex _mutex; |
|---|
| 135 | FileNames _fileNames; |
|---|
| 136 | |
|---|
| 137 | Images _images; |
|---|
| 138 | |
|---|
| 139 | typedef std::set< std::string > FilesRequested; |
|---|
| 140 | FilesRequested _filesRequested; |
|---|
| 141 | |
|---|
| 142 | int _previousAppliedImageIndex; |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | bool _seekTimeSet; |
|---|
| 146 | double _seekTime; |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | } // namespace |
|---|
| 153 | |
|---|
| 154 | #endif |
|---|