| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <osg/Node> |
|---|
| 20 | #include <osg/Geometry> |
|---|
| 21 | #include <osg/Notify> |
|---|
| 22 | #include <osg/Texture1D> |
|---|
| 23 | #include <osg/Texture2D> |
|---|
| 24 | #include <osg/Texture3D> |
|---|
| 25 | #include <osg/TextureRectangle> |
|---|
| 26 | #include <osg/ImageSequence> |
|---|
| 27 | #include <osg/Geode> |
|---|
| 28 | |
|---|
| 29 | #include <osgDB/Registry> |
|---|
| 30 | #include <osgDB/ReadFile> |
|---|
| 31 | #include <osgDB/WriteFile> |
|---|
| 32 | |
|---|
| 33 | #include <osgViewer/Viewer> |
|---|
| 34 | |
|---|
| 35 | #include <iostream> |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | osg::StateSet* createState() |
|---|
| 45 | { |
|---|
| 46 | osg::ref_ptr<osg::ImageSequence> imageSequence = new osg::ImageSequence; |
|---|
| 47 | |
|---|
| 48 | imageSequence->setDuration(2.0); |
|---|
| 49 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posx.png")); |
|---|
| 50 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negx.png")); |
|---|
| 51 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posy.png")); |
|---|
| 52 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negy.png")); |
|---|
| 53 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posz.png")); |
|---|
| 54 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negz.png")); |
|---|
| 55 | |
|---|
| 56 | #if 1 |
|---|
| 57 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 58 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
|---|
| 59 | texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); |
|---|
| 60 | texture->setWrap(osg::Texture::WRAP_R,osg::Texture::REPEAT); |
|---|
| 61 | texture->setResizeNonPowerOfTwoHint(false); |
|---|
| 62 | texture->setImage(imageSequence.get()); |
|---|
| 63 | |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | #else |
|---|
| 67 | osg::TextureRectangle* texture = new osg::TextureRectangle; |
|---|
| 68 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
|---|
| 69 | texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); |
|---|
| 70 | texture->setWrap(osg::Texture::WRAP_R,osg::Texture::REPEAT); |
|---|
| 71 | |
|---|
| 72 | texture->setImage(imageSequence.get()); |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | #endif |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 80 | |
|---|
| 81 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 82 | |
|---|
| 83 | return stateset; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | osg::Node* createModel() |
|---|
| 87 | { |
|---|
| 88 | |
|---|
| 89 | |
|---|
| 90 | osg::Geode* geode = new osg::Geode; |
|---|
| 91 | geode->addDrawable(osg::createTexturedQuadGeometry(osg::Vec3(0.0f,0.0f,0.0), osg::Vec3(1.0f,0.0f,0.0), osg::Vec3(0.0f,0.0f,1.0f))); |
|---|
| 92 | |
|---|
| 93 | geode->setStateSet(createState()); |
|---|
| 94 | |
|---|
| 95 | return geode; |
|---|
| 96 | |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | |
|---|
| 100 | osg::ImageStream* s_imageStream = 0; |
|---|
| 101 | class MovieEventHandler : public osgGA::GUIEventHandler |
|---|
| 102 | { |
|---|
| 103 | public: |
|---|
| 104 | |
|---|
| 105 | MovieEventHandler():_trackMouse(false),_playToggle(true) {} |
|---|
| 106 | |
|---|
| 107 | void setMouseTracking(bool track) { _trackMouse = track; } |
|---|
| 108 | bool getMouseTracking() const { return _trackMouse; } |
|---|
| 109 | |
|---|
| 110 | void set(osg::Node* node); |
|---|
| 111 | |
|---|
| 112 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv); |
|---|
| 113 | |
|---|
| 114 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| 115 | |
|---|
| 116 | typedef std::vector< osg::observer_ptr<osg::ImageStream> > ImageStreamList; |
|---|
| 117 | |
|---|
| 118 | protected: |
|---|
| 119 | |
|---|
| 120 | virtual ~MovieEventHandler() {} |
|---|
| 121 | |
|---|
| 122 | class FindImageStreamsVisitor : public osg::NodeVisitor |
|---|
| 123 | { |
|---|
| 124 | public: |
|---|
| 125 | FindImageStreamsVisitor(ImageStreamList& imageStreamList): |
|---|
| 126 | _imageStreamList(imageStreamList) {} |
|---|
| 127 | |
|---|
| 128 | virtual void apply(osg::Geode& geode) |
|---|
| 129 | { |
|---|
| 130 | apply(geode.getStateSet()); |
|---|
| 131 | |
|---|
| 132 | for(unsigned int i=0;i<geode.getNumDrawables();++i) |
|---|
| 133 | { |
|---|
| 134 | apply(geode.getDrawable(i)->getStateSet()); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | traverse(geode); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | virtual void apply(osg::Node& node) |
|---|
| 141 | { |
|---|
| 142 | apply(node.getStateSet()); |
|---|
| 143 | traverse(node); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | inline void apply(osg::StateSet* stateset) |
|---|
| 147 | { |
|---|
| 148 | if (!stateset) return; |
|---|
| 149 | |
|---|
| 150 | osg::StateAttribute* attr = stateset->getTextureAttribute(0,osg::StateAttribute::TEXTURE); |
|---|
| 151 | if (attr) |
|---|
| 152 | { |
|---|
| 153 | osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(attr); |
|---|
| 154 | if (texture2D) apply(dynamic_cast<osg::ImageStream*>(texture2D->getImage())); |
|---|
| 155 | |
|---|
| 156 | osg::TextureRectangle* textureRec = dynamic_cast<osg::TextureRectangle*>(attr); |
|---|
| 157 | if (textureRec) apply(dynamic_cast<osg::ImageStream*>(textureRec->getImage())); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | inline void apply(osg::ImageStream* imagestream) |
|---|
| 162 | { |
|---|
| 163 | if (imagestream) |
|---|
| 164 | { |
|---|
| 165 | _imageStreamList.push_back(imagestream); |
|---|
| 166 | s_imageStream = imagestream; |
|---|
| 167 | } |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | ImageStreamList& _imageStreamList; |
|---|
| 171 | }; |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | bool _playToggle; |
|---|
| 175 | bool _trackMouse; |
|---|
| 176 | ImageStreamList _imageStreamList; |
|---|
| 177 | |
|---|
| 178 | }; |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | void MovieEventHandler::set(osg::Node* node) |
|---|
| 183 | { |
|---|
| 184 | _imageStreamList.clear(); |
|---|
| 185 | if (node) |
|---|
| 186 | { |
|---|
| 187 | FindImageStreamsVisitor fisv(_imageStreamList); |
|---|
| 188 | node->accept(fisv); |
|---|
| 189 | } |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | bool MovieEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv) |
|---|
| 194 | { |
|---|
| 195 | switch(ea.getEventType()) |
|---|
| 196 | { |
|---|
| 197 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 198 | { |
|---|
| 199 | if (ea.getKey()=='p') |
|---|
| 200 | { |
|---|
| 201 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 202 | itr!=_imageStreamList.end(); |
|---|
| 203 | ++itr) |
|---|
| 204 | { |
|---|
| 205 | if ((*itr)->getStatus()==osg::ImageStream::PLAYING) |
|---|
| 206 | { |
|---|
| 207 | |
|---|
| 208 | std::cout<<"Pause"<<std::endl; |
|---|
| 209 | (*itr)->pause(); |
|---|
| 210 | } |
|---|
| 211 | else |
|---|
| 212 | { |
|---|
| 213 | |
|---|
| 214 | std::cout<<"Play"<<std::endl; |
|---|
| 215 | (*itr)->play(); |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | return true; |
|---|
| 219 | } |
|---|
| 220 | else if (ea.getKey()=='r') |
|---|
| 221 | { |
|---|
| 222 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 223 | itr!=_imageStreamList.end(); |
|---|
| 224 | ++itr) |
|---|
| 225 | { |
|---|
| 226 | std::cout<<"Restart"<<std::endl; |
|---|
| 227 | (*itr)->rewind(); |
|---|
| 228 | } |
|---|
| 229 | return true; |
|---|
| 230 | } |
|---|
| 231 | else if (ea.getKey()=='L') |
|---|
| 232 | { |
|---|
| 233 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 234 | itr!=_imageStreamList.end(); |
|---|
| 235 | ++itr) |
|---|
| 236 | { |
|---|
| 237 | if ( (*itr)->getLoopingMode() == osg::ImageStream::LOOPING) |
|---|
| 238 | { |
|---|
| 239 | std::cout<<"Toggle Looping Off"<<std::endl; |
|---|
| 240 | (*itr)->setLoopingMode( osg::ImageStream::NO_LOOPING ); |
|---|
| 241 | } |
|---|
| 242 | else |
|---|
| 243 | { |
|---|
| 244 | std::cout<<"Toggle Looping On"<<std::endl; |
|---|
| 245 | (*itr)->setLoopingMode( osg::ImageStream::LOOPING ); |
|---|
| 246 | } |
|---|
| 247 | } |
|---|
| 248 | return true; |
|---|
| 249 | } |
|---|
| 250 | return false; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | default: |
|---|
| 254 | return false; |
|---|
| 255 | } |
|---|
| 256 | return false; |
|---|
| 257 | } |
|---|
| 258 | |
|---|
| 259 | void MovieEventHandler::getUsage(osg::ApplicationUsage& usage) const |
|---|
| 260 | { |
|---|
| 261 | usage.addKeyboardMouseBinding("p","Play/Pause movie"); |
|---|
| 262 | usage.addKeyboardMouseBinding("r","Restart movie"); |
|---|
| 263 | usage.addKeyboardMouseBinding("l","Toggle looping of movie"); |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | |
|---|
| 267 | |
|---|
| 268 | |
|---|
| 269 | int main(int argc, char **argv) |
|---|
| 270 | { |
|---|
| 271 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | osgViewer::Viewer viewer(arguments); |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | viewer.setSceneData(createModel()); |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | MovieEventHandler* meh = new MovieEventHandler(); |
|---|
| 281 | meh->set( viewer.getSceneData() ); |
|---|
| 282 | viewer.addEventHandler( meh ); |
|---|
| 283 | |
|---|
| 284 | |
|---|
| 285 | std::string filename; |
|---|
| 286 | if (arguments.read("-o",filename)) |
|---|
| 287 | { |
|---|
| 288 | osgDB::writeNodeFile(*viewer.getSceneData(),filename); |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | return viewer.run(); |
|---|
| 292 | } |
|---|