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