| [6941] | 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| [1697] | 19 | #include <osg/Node> |
|---|
| 20 | #include <osg/Geometry> |
|---|
| 21 | #include <osg/Notify> |
|---|
| 22 | #include <osg/Texture3D> |
|---|
| 23 | #include <osg/TexGen> |
|---|
| 24 | #include <osg/Geode> |
|---|
| 25 | |
|---|
| 26 | #include <osgDB/Registry> |
|---|
| 27 | #include <osgDB/ReadFile> |
|---|
| 28 | |
|---|
| [5962] | 29 | #include <osgViewer/Viewer> |
|---|
| [1697] | 30 | |
|---|
| [5962] | 31 | #include <iostream> |
|---|
| [1697] | 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | typedef std::vector< osg::ref_ptr<osg::Image> > ImageList; |
|---|
| 40 | |
|---|
| [5962] | 41 | osg::StateSet* createState() |
|---|
| 42 | { |
|---|
| 43 | |
|---|
| 44 | osg::ref_ptr<osg::Image> image_0 = osgDB::readImageFile("Images/lz.rgb"); |
|---|
| 45 | osg::ref_ptr<osg::Image> image_1 = osgDB::readImageFile("Images/reflect.rgb"); |
|---|
| 46 | osg::ref_ptr<osg::Image> image_2 = osgDB::readImageFile("Images/tank.rgb"); |
|---|
| 47 | osg::ref_ptr<osg::Image> image_3 = osgDB::readImageFile("Images/skymap.jpg"); |
|---|
| [1697] | 48 | |
|---|
| [5962] | 49 | if (!image_0 || !image_1 || !image_2 || !image_3) |
|---|
| 50 | { |
|---|
| 51 | std::cout << "Warning: could not open files."<<std::endl; |
|---|
| 52 | return new osg::StateSet; |
|---|
| 53 | } |
|---|
| [1697] | 54 | |
|---|
| [5962] | 55 | if (image_0->getPixelFormat()!=image_1->getPixelFormat() || image_0->getPixelFormat()!=image_2->getPixelFormat() || image_0->getPixelFormat()!=image_3->getPixelFormat()) |
|---|
| 56 | { |
|---|
| 57 | std::cout << "Warning: image pixel formats not compatible."<<std::endl; |
|---|
| 58 | return new osg::StateSet; |
|---|
| 59 | } |
|---|
| [1697] | 60 | |
|---|
| [11829] | 61 | GLint textureSize = 256; |
|---|
| [1697] | 62 | |
|---|
| [5962] | 63 | |
|---|
| 64 | image_0->scaleImage(textureSize,textureSize,1); |
|---|
| 65 | image_1->scaleImage(textureSize,textureSize,1); |
|---|
| 66 | image_2->scaleImage(textureSize,textureSize,1); |
|---|
| 67 | image_3->scaleImage(textureSize,textureSize,1); |
|---|
| [1697] | 68 | |
|---|
| 69 | |
|---|
| [5962] | 70 | |
|---|
| 71 | osg::Image* image_3d = new osg::Image; |
|---|
| 72 | image_3d->allocateImage(textureSize,textureSize,4, |
|---|
| 73 | image_0->getPixelFormat(),image_0->getDataType()); |
|---|
| [1697] | 74 | |
|---|
| [5962] | 75 | |
|---|
| 76 | image_3d->copySubImage(0,0,0,image_0.get()); |
|---|
| 77 | image_3d->copySubImage(0,0,1,image_1.get()); |
|---|
| 78 | image_3d->copySubImage(0,0,2,image_2.get()); |
|---|
| 79 | image_3d->copySubImage(0,0,3,image_3.get()); |
|---|
| [1697] | 80 | |
|---|
| [5962] | 81 | image_3d->setInternalTextureFormat(image_0->getInternalTextureFormat()); |
|---|
| [1697] | 82 | |
|---|
| [5962] | 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | osg::Texture3D* texture3D = new osg::Texture3D; |
|---|
| 88 | texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR); |
|---|
| 89 | texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR); |
|---|
| 90 | texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::REPEAT); |
|---|
| 91 | texture3D->setImage(image_3d); |
|---|
| [1717] | 92 | |
|---|
| [5340] | 93 | |
|---|
| [5962] | 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | osg::TexGen* texgen = new osg::TexGen; |
|---|
| 100 | texgen->setMode(osg::TexGen::OBJECT_LINEAR); |
|---|
| 101 | texgen->setPlane(osg::TexGen::R, osg::Plane(0.0f,0.0f,0.0f,0.2f)); |
|---|
| [5340] | 102 | |
|---|
| [5962] | 103 | |
|---|
| 104 | osg::StateSet* stateset = new osg::StateSet; |
|---|
| 105 | stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON); |
|---|
| 106 | stateset->setTextureAttribute(0,texgen); |
|---|
| 107 | stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON); |
|---|
| [5340] | 108 | |
|---|
| [5962] | 109 | return stateset; |
|---|
| 110 | } |
|---|
| [1717] | 111 | |
|---|
| [5962] | 112 | |
|---|
| [1717] | 113 | class UpdateStateCallback : public osg::NodeCallback |
|---|
| 114 | { |
|---|
| 115 | public: |
|---|
| 116 | UpdateStateCallback() {} |
|---|
| 117 | |
|---|
| [1697] | 118 | void animateState(osg::StateSet* stateset) |
|---|
| 119 | { |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | osg::StateAttribute* attribute = stateset->getTextureAttribute(0,osg::StateAttribute::TEXGEN); |
|---|
| 123 | osg::TexGen* texgen = dynamic_cast<osg::TexGen*>(attribute); |
|---|
| 124 | if (texgen) |
|---|
| 125 | { |
|---|
| 126 | texgen->getPlane(osg::TexGen::R)[3] += 0.001f; |
|---|
| 127 | } |
|---|
| [1717] | 128 | |
|---|
| [1697] | 129 | } |
|---|
| 130 | |
|---|
| 131 | virtual void operator()(osg::Node* node, osg::NodeVisitor* nv) |
|---|
| 132 | { |
|---|
| 133 | |
|---|
| 134 | osg::StateSet* stateset = node->getStateSet(); |
|---|
| 135 | if (stateset) |
|---|
| 136 | { |
|---|
| 137 | |
|---|
| 138 | animateState(stateset); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | |
|---|
| 144 | traverse(node,nv); |
|---|
| 145 | } |
|---|
| 146 | }; |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | osg::Drawable* createSquare(float textureCoordMax=1.0f) |
|---|
| 150 | { |
|---|
| 151 | |
|---|
| 152 | osg::Geometry* geom = new osg::Geometry; |
|---|
| 153 | |
|---|
| 154 | osg::Vec3Array* coords = new osg::Vec3Array(4); |
|---|
| 155 | (*coords)[0].set(-1.0f,0.0f,1.0f); |
|---|
| 156 | (*coords)[1].set(-1.0f,0.0f,-1.0f); |
|---|
| 157 | (*coords)[2].set(1.0f,0.0f,-1.0f); |
|---|
| 158 | (*coords)[3].set(1.0f,0.0f,1.0f); |
|---|
| 159 | geom->setVertexArray(coords); |
|---|
| 160 | |
|---|
| 161 | osg::Vec3Array* norms = new osg::Vec3Array(1); |
|---|
| 162 | (*norms)[0].set(0.0f,-1.0f,0.0f); |
|---|
| 163 | geom->setNormalArray(norms); |
|---|
| 164 | geom->setNormalBinding(osg::Geometry::BIND_OVERALL); |
|---|
| 165 | |
|---|
| 166 | osg::Vec2Array* tcoords = new osg::Vec2Array(4); |
|---|
| 167 | (*tcoords)[0].set(0.0f,textureCoordMax); |
|---|
| 168 | (*tcoords)[1].set(0.0f,0.0f); |
|---|
| 169 | (*tcoords)[2].set(textureCoordMax,0.0f); |
|---|
| 170 | (*tcoords)[3].set(textureCoordMax,textureCoordMax); |
|---|
| 171 | geom->setTexCoordArray(0,tcoords); |
|---|
| 172 | |
|---|
| 173 | geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4)); |
|---|
| 174 | |
|---|
| 175 | return geom; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | osg::Node* createModel() |
|---|
| 179 | { |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | osg::Geode* geode = new osg::Geode; |
|---|
| 183 | geode->addDrawable(createSquare()); |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | |
|---|
| [1717] | 196 | geode->setUpdateCallback(new UpdateStateCallback()); |
|---|
| [5962] | 197 | geode->setStateSet(createState()); |
|---|
| [1697] | 198 | |
|---|
| 199 | return geode; |
|---|
| 200 | |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | |
|---|
| [5962] | 204 | int main(int , char **) |
|---|
| [1697] | 205 | { |
|---|
| 206 | |
|---|
| [5962] | 207 | osgViewer::Viewer viewer; |
|---|
| [1697] | 208 | |
|---|
| [5962] | 209 | |
|---|
| 210 | viewer.setSceneData(createModel()); |
|---|
| [1697] | 211 | |
|---|
| [5962] | 212 | return viewer.run(); |
|---|
| [1697] | 213 | } |
|---|