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