| 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 | |
|---|
| 39 | typedef std::vector< osg::ref_ptr<osg::Image> > ImageList; |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | class MyGraphicsContext { |
|---|
| 43 | public: |
|---|
| 44 | MyGraphicsContext() |
|---|
| 45 | { |
|---|
| 46 | osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; |
|---|
| 47 | traits->x = 0; |
|---|
| 48 | traits->y = 0; |
|---|
| 49 | traits->width = 1; |
|---|
| 50 | traits->height = 1; |
|---|
| 51 | traits->windowDecoration = false; |
|---|
| 52 | traits->doubleBuffer = false; |
|---|
| 53 | traits->sharedContext = 0; |
|---|
| 54 | traits->pbuffer = true; |
|---|
| 55 | |
|---|
| 56 | _gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 57 | |
|---|
| 58 | if (!_gc) |
|---|
| 59 | { |
|---|
| 60 | traits->pbuffer = false; |
|---|
| 61 | _gc = osg::GraphicsContext::createGraphicsContext(traits.get()); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | if (_gc.valid()) |
|---|
| 65 | { |
|---|
| 66 | _gc->realize(); |
|---|
| 67 | _gc->makeCurrent(); |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | bool valid() const { return _gc.valid() && _gc->isRealized(); } |
|---|
| 72 | |
|---|
| 73 | private: |
|---|
| 74 | osg::ref_ptr<osg::GraphicsContext> _gc; |
|---|
| 75 | }; |
|---|
| 76 | |
|---|
| 77 | |
|---|
| 78 | osg::StateSet* createState() |
|---|
| 79 | { |
|---|
| 80 | |
|---|
| 81 | osg::ref_ptr<osg::Image> image_0 = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 82 | osg::ref_ptr<osg::Image> image_1 = osgDB::readImageFile("Images/reflect.rgb"); |
|---|
| 83 | osg::ref_ptr<osg::Image> image_2 = osgDB::readImageFile("Images/tank.rgb"); |
|---|
| 84 | osg::ref_ptr<osg::Image> image_3 = osgDB::readImageFile("Images/skymap.jpg"); |
|---|
| 85 | |
|---|
| 86 | osg::ref_ptr<osg::ImageSequence> imageSequence = new osg::ImageSequence; |
|---|
| 87 | imageSequence->addImage(image_0.get(), 0.25); |
|---|
| 88 | imageSequence->addImage(image_1.get(), 0.25); |
|---|
| 89 | imageSequence->addImage(image_2.get(), 0.25); |
|---|
| 90 | imageSequence->addImage(image_3.get(), 0.25); |
|---|
| 91 | |
|---|
| 92 | imageSequence->setImage(image_0->s(),image_0->t(),image_0->r(), |
|---|
| 93 | image_0->getInternalTextureFormat(), |
|---|
| 94 | image_0->getPixelFormat(),image_0->getDataType(), |
|---|
| 95 | image_0->data(), |
|---|
| 96 | osg::Image::NO_DELETE, |
|---|
| 97 | image_0->getPacking()); |
|---|
| 98 | |
|---|
| 99 | osg::Texture2D* texture = new osg::Texture2D; |
|---|
| 100 | texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR); |
|---|
| 101 | texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR); |
|---|
| 102 | texture->setWrap(osg::Texture2D::WRAP_R,osg::Texture2D::REPEAT); |
|---|
| 103 | texture->setResizeNonPowerOfTwoHint(false); |
|---|
| 104 | texture->setImage(imageSequence.get()); |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 108 | stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON); |
|---|
| 109 | |
|---|
| 110 | return stateset; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | osg::Node* createModel() |
|---|
| 114 | { |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | osg::Geode* geode = new osg::Geode; |
|---|
| 118 | 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))); |
|---|
| 119 | |
|---|
| 120 | geode->setStateSet(createState()); |
|---|
| 121 | |
|---|
| 122 | return geode; |
|---|
| 123 | |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | int main(int , char **) |
|---|
| 128 | { |
|---|
| 129 | |
|---|
| 130 | osgViewer::Viewer viewer; |
|---|
| 131 | |
|---|
| 132 | |
|---|
| 133 | viewer.setSceneData(createModel()); |
|---|
| 134 | |
|---|
| 135 | return viewer.run(); |
|---|
| 136 | } |
|---|