| 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 | #include <osgViewer/ViewerEventHandlers> |
|---|
| 35 | |
|---|
| 36 | #include <iostream> |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | osg::StateSet* createState(osg::ArgumentParser& arguments) |
|---|
| 46 | { |
|---|
| 47 | osg::ref_ptr<osg::ImageSequence> imageSequence = new osg::ImageSequence; |
|---|
| 48 | |
|---|
| 49 | bool preLoad = true; |
|---|
| 50 | |
|---|
| 51 | while (arguments.read("--page-and-discard")) |
|---|
| 52 | { |
|---|
| 53 | imageSequence->setMode(osg::ImageSequence::PAGE_AND_DISCARD_USED_IMAGES); |
|---|
| 54 | preLoad = false; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | while (arguments.read("--page-and-retain")) |
|---|
| 58 | { |
|---|
| 59 | imageSequence->setMode(osg::ImageSequence::PAGE_AND_RETAIN_IMAGES); |
|---|
| 60 | preLoad = false; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | while (arguments.read("--preload")) |
|---|
| 64 | { |
|---|
| 65 | imageSequence->setMode(osg::ImageSequence::PRE_LOAD_ALL_IMAGES); |
|---|
| 66 | preLoad = true; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | double length = -1.0; |
|---|
| 70 | while (arguments.read("--length",length)) {} |
|---|
| 71 | |
|---|
| 72 | if (arguments.argc()>1) |
|---|
| 73 | { |
|---|
| 74 | for(int i=1; i<arguments.argc(); ++i) |
|---|
| 75 | { |
|---|
| 76 | if (preLoad) |
|---|
| 77 | { |
|---|
| 78 | osg::ref_ptr<osg::Image> image = osgDB::readImageFile(arguments[i]); |
|---|
| 79 | if (image.valid()) |
|---|
| 80 | { |
|---|
| 81 | imageSequence->addImage(image.get()); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | else |
|---|
| 85 | { |
|---|
| 86 | imageSequence->addImageFile(arguments[i]); |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | if (length>0.0) |
|---|
| 91 | { |
|---|
| 92 | imageSequence->setLength(length); |
|---|
| 93 | } |
|---|
| 94 | else |
|---|
| 95 | { |
|---|
| 96 | unsigned int maxNum = osg::maximum(imageSequence->getFileNames().size(), |
|---|
| 97 | imageSequence->getImages().size()); |
|---|
| 98 | |
|---|
| 99 | imageSequence->setLength(float(maxNum)*0.1f); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | else |
|---|
| 103 | { |
|---|
| 104 | if (length>0.0) |
|---|
| 105 | { |
|---|
| 106 | imageSequence->setLength(length); |
|---|
| 107 | } |
|---|
| 108 | else |
|---|
| 109 | { |
|---|
| 110 | imageSequence->setLength(4.0); |
|---|
| 111 | } |
|---|
| 112 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posx.png")); |
|---|
| 113 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negx.png")); |
|---|
| 114 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posy.png")); |
|---|
| 115 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negy.png")); |
|---|
| 116 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/posz.png")); |
|---|
| 117 | imageSequence->addImage(osgDB::readImageFile("Cubemap_axis/negz.png")); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | imageSequence->play(); |
|---|
| 122 | |
|---|
| 123 | #if 1 |
|---|
| 124 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 125 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
|---|
| 126 | texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); |
|---|
| 127 | texture->setWrap(osg::Texture::WRAP_R,osg::Texture::REPEAT); |
|---|
| 128 | texture->setResizeNonPowerOfTwoHint(false); |
|---|
| 129 | texture->setImage(imageSequence.get()); |
|---|
| 130 | |
|---|
| 131 | #else |
|---|
| 132 | osg::TextureRectangle* texture = new osg::TextureRectangle; |
|---|
| 133 | texture->setFilter(osg::Texture::MIN_FILTER,osg::Texture::LINEAR); |
|---|
| 134 | texture->setFilter(osg::Texture::MAG_FILTER,osg::Texture::LINEAR); |
|---|
| 135 | texture->setWrap(osg::Texture::WRAP_R,osg::Texture::REPEAT); |
|---|
| 136 | texture->setImage(imageSequence.get()); |
|---|
| 137 | |
|---|
| 138 | #endif |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 142 | |
|---|
| 143 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 144 | |
|---|
| 145 | return stateset; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | osg::Node* createModel(osg::ArgumentParser& arguments) |
|---|
| 149 | { |
|---|
| 150 | |
|---|
| 151 | |
|---|
| 152 | osg::Geode* geode = new osg::Geode; |
|---|
| 153 | 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))); |
|---|
| 154 | |
|---|
| 155 | geode->setStateSet(createState(arguments)); |
|---|
| 156 | |
|---|
| 157 | return geode; |
|---|
| 158 | |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | osg::ImageStream* s_imageStream = 0; |
|---|
| 163 | class MovieEventHandler : public osgGA::GUIEventHandler |
|---|
| 164 | { |
|---|
| 165 | public: |
|---|
| 166 | |
|---|
| 167 | MovieEventHandler():_playToggle(true),_trackMouse(false) {} |
|---|
| 168 | |
|---|
| 169 | void setMouseTracking(bool track) { _trackMouse = track; } |
|---|
| 170 | bool getMouseTracking() const { return _trackMouse; } |
|---|
| 171 | |
|---|
| 172 | void set(osg::Node* node); |
|---|
| 173 | |
|---|
| 174 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv); |
|---|
| 175 | |
|---|
| 176 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
|---|
| 177 | |
|---|
| 178 | typedef std::vector< osg::observer_ptr<osg::ImageStream> > ImageStreamList; |
|---|
| 179 | |
|---|
| 180 | protected: |
|---|
| 181 | |
|---|
| 182 | virtual ~MovieEventHandler() {} |
|---|
| 183 | |
|---|
| 184 | class FindImageStreamsVisitor : public osg::NodeVisitor |
|---|
| 185 | { |
|---|
| 186 | public: |
|---|
| 187 | FindImageStreamsVisitor(ImageStreamList& imageStreamList): |
|---|
| 188 | _imageStreamList(imageStreamList) {} |
|---|
| 189 | |
|---|
| 190 | virtual void apply(osg::Geode& geode) |
|---|
| 191 | { |
|---|
| 192 | apply(geode.getStateSet()); |
|---|
| 193 | |
|---|
| 194 | for(unsigned int i=0;i<geode.getNumDrawables();++i) |
|---|
| 195 | { |
|---|
| 196 | apply(geode.getDrawable(i)->getStateSet()); |
|---|
| 197 | } |
|---|
| 198 | |
|---|
| 199 | traverse(geode); |
|---|
| 200 | } |
|---|
| 201 | |
|---|
| 202 | virtual void apply(osg::Node& node) |
|---|
| 203 | { |
|---|
| 204 | apply(node.getStateSet()); |
|---|
| 205 | traverse(node); |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | inline void apply(osg::StateSet* stateset) |
|---|
| 209 | { |
|---|
| 210 | if (!stateset) return; |
|---|
| 211 | |
|---|
| 212 | osg::StateAttribute* attr = stateset->getTextureAttribute(0,osg::StateAttribute::TEXTURE); |
|---|
| 213 | if (attr) |
|---|
| 214 | { |
|---|
| 215 | osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(attr); |
|---|
| 216 | if (texture2D) apply(dynamic_cast<osg::ImageStream*>(texture2D->getImage())); |
|---|
| 217 | |
|---|
| 218 | osg::TextureRectangle* textureRec = dynamic_cast<osg::TextureRectangle*>(attr); |
|---|
| 219 | if (textureRec) apply(dynamic_cast<osg::ImageStream*>(textureRec->getImage())); |
|---|
| 220 | } |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | inline void apply(osg::ImageStream* imagestream) |
|---|
| 224 | { |
|---|
| 225 | if (imagestream) |
|---|
| 226 | { |
|---|
| 227 | _imageStreamList.push_back(imagestream); |
|---|
| 228 | s_imageStream = imagestream; |
|---|
| 229 | } |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | ImageStreamList& _imageStreamList; |
|---|
| 233 | }; |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | bool _playToggle; |
|---|
| 237 | bool _trackMouse; |
|---|
| 238 | ImageStreamList _imageStreamList; |
|---|
| 239 | |
|---|
| 240 | }; |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | void MovieEventHandler::set(osg::Node* node) |
|---|
| 245 | { |
|---|
| 246 | _imageStreamList.clear(); |
|---|
| 247 | if (node) |
|---|
| 248 | { |
|---|
| 249 | FindImageStreamsVisitor fisv(_imageStreamList); |
|---|
| 250 | node->accept(fisv); |
|---|
| 251 | } |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | bool MovieEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa, osg::Object*, osg::NodeVisitor* nv) |
|---|
| 256 | { |
|---|
| 257 | switch(ea.getEventType()) |
|---|
| 258 | { |
|---|
| 259 | case(osgGA::GUIEventAdapter::KEYDOWN): |
|---|
| 260 | { |
|---|
| 261 | if (ea.getKey()=='p') |
|---|
| 262 | { |
|---|
| 263 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 264 | itr!=_imageStreamList.end(); |
|---|
| 265 | ++itr) |
|---|
| 266 | { |
|---|
| 267 | if ((*itr)->getStatus()==osg::ImageStream::PLAYING) |
|---|
| 268 | { |
|---|
| 269 | |
|---|
| 270 | std::cout<<"Pause"<<std::endl; |
|---|
| 271 | (*itr)->pause(); |
|---|
| 272 | } |
|---|
| 273 | else |
|---|
| 274 | { |
|---|
| 275 | |
|---|
| 276 | std::cout<<"Play"<<std::endl; |
|---|
| 277 | (*itr)->play(); |
|---|
| 278 | } |
|---|
| 279 | } |
|---|
| 280 | return true; |
|---|
| 281 | } |
|---|
| 282 | else if (ea.getKey()=='r') |
|---|
| 283 | { |
|---|
| 284 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 285 | itr!=_imageStreamList.end(); |
|---|
| 286 | ++itr) |
|---|
| 287 | { |
|---|
| 288 | std::cout<<"Restart"<<std::endl; |
|---|
| 289 | (*itr)->rewind(); |
|---|
| 290 | } |
|---|
| 291 | return true; |
|---|
| 292 | } |
|---|
| 293 | else if (ea.getKey()=='L') |
|---|
| 294 | { |
|---|
| 295 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
|---|
| 296 | itr!=_imageStreamList.end(); |
|---|
| 297 | ++itr) |
|---|
| 298 | { |
|---|
| 299 | if ( (*itr)->getLoopingMode() == osg::ImageStream::LOOPING) |
|---|
| 300 | { |
|---|
| 301 | std::cout<<"Toggle Looping Off"<<std::endl; |
|---|
| 302 | (*itr)->setLoopingMode( osg::ImageStream::NO_LOOPING ); |
|---|
| 303 | } |
|---|
| 304 | else |
|---|
| 305 | { |
|---|
| 306 | std::cout<<"Toggle Looping On"<<std::endl; |
|---|
| 307 | (*itr)->setLoopingMode( osg::ImageStream::LOOPING ); |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | return true; |
|---|
| 311 | } |
|---|
| 312 | return false; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | default: |
|---|
| 316 | return false; |
|---|
| 317 | } |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | void MovieEventHandler::getUsage(osg::ApplicationUsage& usage) const |
|---|
| 321 | { |
|---|
| 322 | usage.addKeyboardMouseBinding("p","Play/Pause movie"); |
|---|
| 323 | usage.addKeyboardMouseBinding("r","Restart movie"); |
|---|
| 324 | usage.addKeyboardMouseBinding("l","Toggle looping of movie"); |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | |
|---|
| 329 | |
|---|
| 330 | int main(int argc, char **argv) |
|---|
| 331 | { |
|---|
| 332 | osg::ArgumentParser arguments(&argc,argv); |
|---|
| 333 | |
|---|
| 334 | |
|---|
| 335 | osgViewer::Viewer viewer(arguments); |
|---|
| 336 | |
|---|
| 337 | std::string filename; |
|---|
| 338 | arguments.read("-o",filename); |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | viewer.setSceneData(createModel(arguments)); |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | MovieEventHandler* meh = new MovieEventHandler(); |
|---|
| 345 | meh->set( viewer.getSceneData() ); |
|---|
| 346 | viewer.addEventHandler( meh ); |
|---|
| 347 | |
|---|
| 348 | viewer.addEventHandler( new osgViewer::StatsHandler()); |
|---|
| 349 | |
|---|
| 350 | if (!filename.empty()) |
|---|
| 351 | { |
|---|
| 352 | osgDB::writeNodeFile(*viewer.getSceneData(),filename); |
|---|
| 353 | } |
|---|
| 354 | |
|---|
| 355 | return viewer.run(); |
|---|
| 356 | } |
|---|