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