| 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/Texture2D> |
|---|
| 23 | #include <osg/ImageSequence> |
|---|
| 24 | #include <osg/Geode> |
|---|
| 25 | |
|---|
| 26 | #include <osgDB/Registry> |
|---|
| 27 | #include <osgDB/ReadFile> |
|---|
| 28 | |
|---|
| 29 | #include <osgViewer/Viewer> |
|---|
| 30 | |
|---|
| 31 | #include <iostream> |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | struct ImageUpdateCallback : public osg::StateAttribute::Callback |
|---|
| 39 | { |
|---|
| 40 | |
|---|
| 41 | virtual void operator () (osg::StateAttribute* attr, osg::NodeVisitor* nv) |
|---|
| 42 | { |
|---|
| 43 | const osg::FrameStamp* fs = nv!=0 ? nv->getFrameStamp() : 0; |
|---|
| 44 | osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(attr); |
|---|
| 45 | if (texture2D && texture2D->getImage() && fs) |
|---|
| 46 | { |
|---|
| 47 | texture2D->getImage()->update(fs); |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | }; |
|---|
| 51 | |
|---|
| 52 | osg::StateSet* createState() |
|---|
| 53 | { |
|---|
| 54 | |
|---|
| 55 | osg::ref_ptr<osg::Image> image_0 = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 56 | osg::ref_ptr<osg::Image> image_1 = osgDB::readImageFile("Images/reflect.rgb"); |
|---|
| 57 | osg::ref_ptr<osg::Image> image_2 = osgDB::readImageFile("Images/tank.rgb"); |
|---|
| 58 | osg::ref_ptr<osg::Image> image_3 = osgDB::readImageFile("Images/skymap.jpg"); |
|---|
| 59 | |
|---|
| 60 | osg::ref_ptr<osg::ImageSequence> imageSequence = new osg::ImageSequence; |
|---|
| 61 | imageSequence->addImage(image_0.get(), 0.25); |
|---|
| 62 | imageSequence->addImage(image_1.get(), 0.25); |
|---|
| 63 | imageSequence->addImage(image_2.get(), 0.25); |
|---|
| 64 | imageSequence->addImage(image_3.get(), 0.25); |
|---|
| 65 | |
|---|
| 66 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 67 | texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
|---|
| 68 | texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
|---|
| 69 | texture->setWrap(osg::Texture2D::WRAP_R,osg::Texture2D::REPEAT); |
|---|
| 70 | texture->setResizeNonPowerOfTwoHint(false); |
|---|
| 71 | texture->setImage(imageSequence.get()); |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | texture->setUpdateCallback(new ImageUpdateCallback); |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 78 | |
|---|
| 79 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 80 | |
|---|
| 81 | return stateset; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | osg::Node* createModel() |
|---|
| 85 | { |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | osg::Geode* geode = new osg::Geode; |
|---|
| 89 | 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))); |
|---|
| 90 | |
|---|
| 91 | geode->setStateSet(createState()); |
|---|
| 92 | |
|---|
| 93 | return geode; |
|---|
| 94 | |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | int main(int , char **) |
|---|
| 99 | { |
|---|
| 100 | |
|---|
| 101 | osgViewer::Viewer viewer; |
|---|
| 102 | |
|---|
| 103 | |
|---|
| 104 | viewer.setSceneData(createModel()); |
|---|
| 105 | |
|---|
| 106 | return viewer.run(); |
|---|
| 107 | } |
|---|