2007-06-12 22:20:16 +08:00
|
|
|
/* OpenSceneGraph example, osgtexture3D.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
#include <osg/Node>
|
|
|
|
#include <osg/Geometry>
|
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/Texture3D>
|
|
|
|
#include <osg/TexGen>
|
|
|
|
#include <osg/Geode>
|
|
|
|
|
|
|
|
#include <osgDB/Registry>
|
|
|
|
#include <osgDB/ReadFile>
|
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
#include <osgViewer/Viewer>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
#include <iostream>
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
//
|
2015-04-13 18:43:56 +08:00
|
|
|
// A simple demo demonstrating use osg::Texture3D to create a blended animation between four separate images packed together into a 3d texture
|
2003-03-15 04:35:45 +08:00
|
|
|
//
|
|
|
|
|
|
|
|
typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
|
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
osg::StateSet* createState()
|
|
|
|
{
|
|
|
|
// read 4 2d images
|
2015-10-22 21:42:19 +08:00
|
|
|
osg::ref_ptr<osg::Image> image_0 = osgDB::readRefImageFile("Images/lz.rgb");
|
|
|
|
osg::ref_ptr<osg::Image> image_1 = osgDB::readRefImageFile("Images/reflect.rgb");
|
|
|
|
osg::ref_ptr<osg::Image> image_2 = osgDB::readRefImageFile("Images/tank.rgb");
|
|
|
|
osg::ref_ptr<osg::Image> image_3 = osgDB::readRefImageFile("Images/skymap.jpg");
|
2003-03-19 20:06:29 +08:00
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
if (!image_0 || !image_1 || !image_2 || !image_3)
|
|
|
|
{
|
|
|
|
std::cout << "Warning: could not open files."<<std::endl;
|
|
|
|
return new osg::StateSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (image_0->getPixelFormat()!=image_1->getPixelFormat() || image_0->getPixelFormat()!=image_2->getPixelFormat() || image_0->getPixelFormat()!=image_3->getPixelFormat())
|
|
|
|
{
|
|
|
|
std::cout << "Warning: image pixel formats not compatible."<<std::endl;
|
|
|
|
return new osg::StateSet;
|
|
|
|
}
|
|
|
|
|
2010-10-07 19:53:28 +08:00
|
|
|
GLint textureSize = 256;
|
2007-01-11 23:19:59 +08:00
|
|
|
|
|
|
|
// scale them all to the same size.
|
|
|
|
image_0->scaleImage(textureSize,textureSize,1);
|
|
|
|
image_1->scaleImage(textureSize,textureSize,1);
|
|
|
|
image_2->scaleImage(textureSize,textureSize,1);
|
|
|
|
image_3->scaleImage(textureSize,textureSize,1);
|
|
|
|
|
|
|
|
|
|
|
|
// then allocated a 3d image to use for texturing.
|
|
|
|
osg::Image* image_3d = new osg::Image;
|
|
|
|
image_3d->allocateImage(textureSize,textureSize,4,
|
|
|
|
image_0->getPixelFormat(),image_0->getDataType());
|
|
|
|
|
|
|
|
// copy the 2d images into the 3d image.
|
|
|
|
image_3d->copySubImage(0,0,0,image_0.get());
|
|
|
|
image_3d->copySubImage(0,0,1,image_1.get());
|
|
|
|
image_3d->copySubImage(0,0,2,image_2.get());
|
|
|
|
image_3d->copySubImage(0,0,3,image_3.get());
|
|
|
|
|
2013-06-27 17:54:12 +08:00
|
|
|
image_3d->setInternalTextureFormat(image_0->getInternalTextureFormat());
|
2007-01-11 23:19:59 +08:00
|
|
|
|
|
|
|
// set up the 3d texture itself,
|
|
|
|
// note, well set the filtering up so that mip mapping is disabled,
|
|
|
|
// gluBuild3DMipsmaps doesn't do a very good job of handled the
|
|
|
|
// inbalanced dimensions of the 256x256x4 texture.
|
|
|
|
osg::Texture3D* texture3D = new osg::Texture3D;
|
|
|
|
texture3D->setFilter(osg::Texture3D::MIN_FILTER,osg::Texture3D::LINEAR);
|
|
|
|
texture3D->setFilter(osg::Texture3D::MAG_FILTER,osg::Texture3D::LINEAR);
|
|
|
|
texture3D->setWrap(osg::Texture3D::WRAP_R,osg::Texture3D::REPEAT);
|
|
|
|
texture3D->setImage(image_3d);
|
|
|
|
|
|
|
|
|
|
|
|
// create a texgen to generate a R texture coordinate, the geometry
|
|
|
|
// itself will supply the S & T texture coordinates.
|
|
|
|
// in the animateStateSet callback well alter this R value to
|
|
|
|
// move the texture through the 3d texture, 3d texture filtering
|
|
|
|
// will do the blending for us.
|
|
|
|
osg::TexGen* texgen = new osg::TexGen;
|
|
|
|
texgen->setMode(osg::TexGen::OBJECT_LINEAR);
|
|
|
|
texgen->setPlane(osg::TexGen::R, osg::Plane(0.0f,0.0f,0.0f,0.2f));
|
|
|
|
|
|
|
|
// create the StateSet to store the texture data
|
|
|
|
osg::StateSet* stateset = new osg::StateSet;
|
|
|
|
stateset->setTextureMode(0,GL_TEXTURE_GEN_R,osg::StateAttribute::ON);
|
|
|
|
stateset->setTextureAttribute(0,texgen);
|
|
|
|
stateset->setTextureAttributeAndModes(0,texture3D,osg::StateAttribute::ON);
|
|
|
|
|
|
|
|
return stateset;
|
|
|
|
}
|
2006-07-19 21:02:35 +08:00
|
|
|
|
2003-03-19 20:06:29 +08:00
|
|
|
|
|
|
|
class UpdateStateCallback : public osg::NodeCallback
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UpdateStateCallback() {}
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
void animateState(osg::StateSet* stateset)
|
|
|
|
{
|
|
|
|
// here we simply get any existing texgen, and then increment its
|
|
|
|
// plane, pushing the R coordinate through the texture.
|
|
|
|
osg::StateAttribute* attribute = stateset->getTextureAttribute(0,osg::StateAttribute::TEXGEN);
|
|
|
|
osg::TexGen* texgen = dynamic_cast<osg::TexGen*>(attribute);
|
|
|
|
if (texgen)
|
|
|
|
{
|
|
|
|
texgen->getPlane(osg::TexGen::R)[3] += 0.001f;
|
|
|
|
}
|
2003-03-19 20:06:29 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
|
2013-06-27 17:54:12 +08:00
|
|
|
{
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
osg::StateSet* stateset = node->getStateSet();
|
|
|
|
if (stateset)
|
|
|
|
{
|
2015-04-08 02:01:12 +08:00
|
|
|
// we have an existing stateset, so lets animate it.
|
2003-03-15 04:35:45 +08:00
|
|
|
animateState(stateset);
|
|
|
|
}
|
|
|
|
|
2015-04-08 02:01:12 +08:00
|
|
|
// note, callback is responsible for scenegraph traversal so
|
2013-06-27 17:54:12 +08:00
|
|
|
// should always include call the traverse(node,nv) to ensure
|
2015-04-08 02:01:12 +08:00
|
|
|
// that the rest of callbacks and the scene graph are traversed.
|
2003-03-15 04:35:45 +08:00
|
|
|
traverse(node,nv);
|
2013-06-27 17:54:12 +08:00
|
|
|
}
|
2003-03-15 04:35:45 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/** create 2,2 square with center at 0,0,0 and aligned along the XZ plan */
|
|
|
|
osg::Drawable* createSquare(float textureCoordMax=1.0f)
|
|
|
|
{
|
|
|
|
// set up the Geometry.
|
|
|
|
osg::Geometry* geom = new osg::Geometry;
|
|
|
|
|
|
|
|
osg::Vec3Array* coords = new osg::Vec3Array(4);
|
|
|
|
(*coords)[0].set(-1.0f,0.0f,1.0f);
|
|
|
|
(*coords)[1].set(-1.0f,0.0f,-1.0f);
|
|
|
|
(*coords)[2].set(1.0f,0.0f,-1.0f);
|
|
|
|
(*coords)[3].set(1.0f,0.0f,1.0f);
|
|
|
|
geom->setVertexArray(coords);
|
|
|
|
|
|
|
|
osg::Vec3Array* norms = new osg::Vec3Array(1);
|
|
|
|
(*norms)[0].set(0.0f,-1.0f,0.0f);
|
2013-06-27 17:54:12 +08:00
|
|
|
geom->setNormalArray(norms, osg::Array::BIND_OVERALL);
|
2003-03-15 04:35:45 +08:00
|
|
|
|
|
|
|
osg::Vec2Array* tcoords = new osg::Vec2Array(4);
|
|
|
|
(*tcoords)[0].set(0.0f,textureCoordMax);
|
|
|
|
(*tcoords)[1].set(0.0f,0.0f);
|
|
|
|
(*tcoords)[2].set(textureCoordMax,0.0f);
|
|
|
|
(*tcoords)[3].set(textureCoordMax,textureCoordMax);
|
|
|
|
geom->setTexCoordArray(0,tcoords);
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::QUADS,0,4));
|
|
|
|
|
|
|
|
return geom;
|
|
|
|
}
|
|
|
|
|
|
|
|
osg::Node* createModel()
|
|
|
|
{
|
2013-06-27 17:54:12 +08:00
|
|
|
// create the geometry of the model, just a simple 2d quad right now.
|
2003-03-15 04:35:45 +08:00
|
|
|
osg::Geode* geode = new osg::Geode;
|
2015-03-11 23:44:23 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
geode->addDrawable(createSquare());
|
|
|
|
|
2003-03-19 20:06:29 +08:00
|
|
|
geode->setUpdateCallback(new UpdateStateCallback());
|
2015-03-11 23:44:23 +08:00
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
geode->setStateSet(createState());
|
2013-06-27 17:54:12 +08:00
|
|
|
|
2003-03-15 04:35:45 +08:00
|
|
|
return geode;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
int main(int , char **)
|
2003-03-15 04:35:45 +08:00
|
|
|
{
|
|
|
|
// construct the viewer.
|
2007-01-11 23:19:59 +08:00
|
|
|
osgViewer::Viewer viewer;
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
// create a model from the images and pass it to the viewer.
|
|
|
|
viewer.setSceneData(createModel());
|
2003-03-15 04:35:45 +08:00
|
|
|
|
2007-01-11 23:19:59 +08:00
|
|
|
return viewer.run();
|
2003-03-15 04:35:45 +08:00
|
|
|
}
|