Added osgtexture3D demo, renamed osgtexture demo to osgtexture2D, and have
added osgtexture1D demo which currently is simply copy of osgtexture2D. I will be modifying it to do 1D texturing next. Fixed a bug in osg::Texture3D relating to checking of existance of texturing. Merged some fixes to the pfb loader from Ulrich Hertlein.
This commit is contained in:
@@ -2,7 +2,7 @@ TOPDIR = ../../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
osgtexture.cpp\
|
||||
osgtexture1D.cpp\
|
||||
|
||||
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||
|
||||
@@ -10,6 +10,6 @@ INSTFILES = \
|
||||
$(CXXFILES)\
|
||||
Makefile.inst=Makefile
|
||||
|
||||
EXEC = osgtexture
|
||||
EXEC = osgtexture1D
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
@@ -2,10 +2,10 @@ TOPDIR = ../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
osgtexture.cpp\
|
||||
osgtexture1D.cpp\
|
||||
|
||||
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||
|
||||
EXEC = osgtexture
|
||||
EXEC = osgtexture1D
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
15
src/Demos/osgtexture2D/Makefile
Normal file
15
src/Demos/osgtexture2D/Makefile
Normal file
@@ -0,0 +1,15 @@
|
||||
TOPDIR = ../../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
osgtexture2D.cpp\
|
||||
|
||||
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||
|
||||
INSTFILES = \
|
||||
$(CXXFILES)\
|
||||
Makefile.inst=Makefile
|
||||
|
||||
EXEC = osgtexture2D
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
11
src/Demos/osgtexture2D/Makefile.inst
Normal file
11
src/Demos/osgtexture2D/Makefile.inst
Normal file
@@ -0,0 +1,11 @@
|
||||
TOPDIR = ../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
osgtexture2D.cpp\
|
||||
|
||||
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||
|
||||
EXEC = osgtexture2D
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
485
src/Demos/osgtexture2D/osgtexture2D.cpp
Normal file
485
src/Demos/osgtexture2D/osgtexture2D.cpp
Normal file
@@ -0,0 +1,485 @@
|
||||
#include <osg/Node>
|
||||
#include <osg/Geometry>
|
||||
#include <osg/Notify>
|
||||
#include <osg/MatrixTransform>
|
||||
#include <osg/Texture2D>
|
||||
#include <osg/DrawPixels>
|
||||
|
||||
#include <osgGA/TrackballManipulator>
|
||||
#include <osgGA/FlightManipulator>
|
||||
#include <osgGA/DriveManipulator>
|
||||
|
||||
#include <osgDB/Registry>
|
||||
#include <osgDB/ReadFile>
|
||||
|
||||
#include <osgGLUT/glut>
|
||||
#include <osgGLUT/Viewer>
|
||||
|
||||
|
||||
//
|
||||
// A simple demo demonstrating different texturing modes,
|
||||
// including using of texture extensions.
|
||||
//
|
||||
|
||||
|
||||
typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
|
||||
|
||||
class Texture2DCallback : public osg::NodeCallback
|
||||
{
|
||||
public:
|
||||
Texture2DCallback(osg::Texture2D* texture):_texture(texture)
|
||||
{
|
||||
_filterRange.push_back(osg::Texture2D::LINEAR);
|
||||
_filterRange.push_back(osg::Texture2D::LINEAR_MIPMAP_LINEAR);
|
||||
_filterRange.push_back(osg::Texture2D::LINEAR_MIPMAP_NEAREST);
|
||||
_filterRange.push_back(osg::Texture2D::NEAREST);
|
||||
_filterRange.push_back(osg::Texture2D::NEAREST_MIPMAP_LINEAR);
|
||||
_filterRange.push_back(osg::Texture2D::NEAREST_MIPMAP_NEAREST);
|
||||
_currPos = 0;
|
||||
_prevTime = 0.0;
|
||||
}
|
||||
|
||||
virtual ~Texture2DCallback() {}
|
||||
|
||||
virtual void operator()(osg::Node*, osg::NodeVisitor* nv)
|
||||
{
|
||||
if (nv->getFrameStamp())
|
||||
{
|
||||
double currTime = nv->getFrameStamp()->getReferenceTime();
|
||||
if (currTime-_prevTime>1.0)
|
||||
{
|
||||
std::cout<<"Updating texturing filter to "<<std::hex<<_filterRange[_currPos]<<std::dec<<std::endl;
|
||||
_texture->setFilter(osg::Texture2D::MAG_FILTER,_filterRange[_currPos]);
|
||||
_currPos++;
|
||||
if (_currPos>=_filterRange.size()) _currPos=0;
|
||||
_prevTime = currTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
osg::ref_ptr<osg::Texture2D> _texture;
|
||||
std::vector<osg::Texture2D::FilterMode> _filterRange;
|
||||
osg::uint _currPos;
|
||||
double _prevTime;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Function to read several images files (typically one) as specified
|
||||
* on the command line, and return them in an ImageList
|
||||
*/
|
||||
ImageList getImagesFromFiles(std::vector<std::string>& commandLine)
|
||||
{
|
||||
|
||||
ImageList imageList;
|
||||
|
||||
for(std::vector<std::string>::iterator itr=commandLine.begin();
|
||||
itr!=commandLine.end();
|
||||
++itr)
|
||||
{
|
||||
if ((*itr)[0]!='-')
|
||||
{
|
||||
// not an option so assume string is a filename.
|
||||
osg::Image *image = osgDB::readImageFile( *itr );
|
||||
if (image)
|
||||
{
|
||||
imageList.push_back(image);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if (imageList.size()==0)
|
||||
{
|
||||
osg::notify(osg::WARN) << "No image data loaded."<<std::endl;
|
||||
}
|
||||
|
||||
return imageList;
|
||||
}
|
||||
|
||||
/** 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);
|
||||
geom->setNormalArray(norms);
|
||||
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
|
||||
|
||||
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);
|
||||
|
||||
geom->addPrimitive(osgNew osg::DrawArrays(osg::Primitive::QUADS,0,4));
|
||||
|
||||
return geom;
|
||||
}
|
||||
|
||||
osg::Node* createTexturedItem(const osg::Vec3& offset,osg::Texture2D* texture,osg::Node* geometry)
|
||||
{
|
||||
// create a tranform node to position each square in appropriate
|
||||
// place and also to add individual texture set to it, so that
|
||||
// that state is inherited down to its children.
|
||||
osg::MatrixTransform* local_transform = new osg::MatrixTransform;
|
||||
local_transform->postMult(osg::Matrix::translate(offset));
|
||||
|
||||
// create the StateSet to store the texture data
|
||||
osg::StateSet* stateset = new osg::StateSet;
|
||||
|
||||
stateset->setTextureAttributeAndModes(0,texture,osg::StateAttribute::ON);
|
||||
|
||||
// turn the face culling off so you can see the texture from
|
||||
// all angles.
|
||||
stateset->setMode(GL_CULL_FACE,osg::StateAttribute::OFF);
|
||||
|
||||
// attach the setset to tranform node.
|
||||
local_transform->setStateSet(stateset);
|
||||
|
||||
// add the geode to the transform.
|
||||
local_transform->addChild(geometry);
|
||||
|
||||
return local_transform;
|
||||
}
|
||||
|
||||
osg::Node* createLayer(const osg::Vec3& offset,osg::Image* image,osg::Node* geometry,osg::Node* geometryRep)
|
||||
{
|
||||
if (image==NULL) return NULL;
|
||||
|
||||
osg::MatrixTransform* top_transform = new osg::MatrixTransform;
|
||||
top_transform->postMult(osg::Matrix::translate(offset));
|
||||
|
||||
osg::Vec3 local_offset(0.0f,0.0f,0.0f);
|
||||
osg::Vec3 local_delta(3.0f,0.0f,0.0f);
|
||||
|
||||
// // use DrawPixels drawable to draw a pixel image.
|
||||
// {
|
||||
//
|
||||
// osg::DrawPixels* drawimage = osgNew osg::DrawPixels;
|
||||
// drawimage->setPosition(local_offset);
|
||||
// drawimage->setImage(image);
|
||||
//
|
||||
// osg::Geode* geode = osgNew osg::Geode;
|
||||
// geode->addDrawable(drawimage);
|
||||
//
|
||||
// // add the transform node to root group node.
|
||||
// top_transform->addChild(geode);
|
||||
//
|
||||
// local_offset += local_delta;
|
||||
// }
|
||||
|
||||
|
||||
// defaults mipmapped texturing.
|
||||
{
|
||||
// create the texture attribute
|
||||
osg::Texture2D* texture = new osg::Texture2D;
|
||||
texture->setImage(image);
|
||||
|
||||
// add the transform node to root group node.
|
||||
top_transform->addChild(createTexturedItem(local_offset,texture,geometry));
|
||||
|
||||
local_offset += local_delta;
|
||||
|
||||
// top_transform->setAppCallback(new TextureCallback(texture));
|
||||
|
||||
}
|
||||
|
||||
|
||||
// bilinear
|
||||
{
|
||||
// create the texture attribute
|
||||
osg::Texture2D* texture = new osg::Texture2D;
|
||||
texture->setImage(image);
|
||||
|
||||
// set up bilinear filtering.
|
||||
texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR_MIPMAP_NEAREST);
|
||||
texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
|
||||
|
||||
// add the transform node to root group node.
|
||||
top_transform->addChild(createTexturedItem(local_offset,texture,geometry));
|
||||
|
||||
local_offset += local_delta;
|
||||
|
||||
}
|
||||
|
||||
// trilinear
|
||||
{
|
||||
// create the texture attribute
|
||||
osg::Texture2D* texture = new osg::Texture2D;
|
||||
texture->setImage(image);
|
||||
|
||||
// set up trilinear filtering.
|
||||
texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR_MIPMAP_LINEAR);
|
||||
texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
|
||||
|
||||
// add the transform node to root group node.
|
||||
top_transform->addChild(createTexturedItem(local_offset,texture,geometry));
|
||||
|
||||
local_offset += local_delta;
|
||||
|
||||
}
|
||||
|
||||
|
||||
// anisotropic
|
||||
{
|
||||
// create the texture attribute
|
||||
osg::Texture2D* texture = new osg::Texture2D;
|
||||
texture->setImage(image);
|
||||
|
||||
// set up anistropic filtering.
|
||||
texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR_MIPMAP_LINEAR);
|
||||
texture->setFilter(osg::Texture2D::MAG_FILTER,osg::Texture2D::LINEAR);
|
||||
texture->setMaxAnisotropy(2.0f);
|
||||
|
||||
// add the transform node to root group node.
|
||||
top_transform->addChild(createTexturedItem(local_offset,texture,geometry));
|
||||
|
||||
local_offset += local_delta;
|
||||
|
||||
}
|
||||
|
||||
// arb compression
|
||||
{
|
||||
// create the texture attribute
|
||||
osg::Texture2D* texture = new osg::Texture2D;
|
||||
texture->setImage(image);
|
||||
|
||||
texture->setInternalFormatMode(osg::Texture2D::USE_ARB_COMPRESSION);
|
||||
|
||||
// add the transform node to root group node.
|
||||
top_transform->addChild(createTexturedItem(local_offset,texture,geometry));
|
||||
|
||||
local_offset += local_delta;
|
||||
|
||||
}
|
||||
|
||||
// s3tc_dxt1 compression
|
||||
{
|
||||
// create the texture attribute
|
||||
osg::Texture2D* texture = new osg::Texture2D;
|
||||
texture->setImage(image);
|
||||
|
||||
texture->setInternalFormatMode(osg::Texture2D::USE_S3TC_DXT1_COMPRESSION);
|
||||
|
||||
// add the transform node to root group node.
|
||||
top_transform->addChild(createTexturedItem(local_offset,texture,geometry));
|
||||
|
||||
local_offset += local_delta;
|
||||
|
||||
}
|
||||
|
||||
// default wrap mode. (osg::Texture2D::CLAMP)
|
||||
{
|
||||
// create the texture attribute
|
||||
osg::Texture2D* texture = new osg::Texture2D;
|
||||
texture->setImage(image);
|
||||
|
||||
// add the transform node to root group node.
|
||||
top_transform->addChild(createTexturedItem(local_offset,texture,geometryRep));
|
||||
|
||||
local_offset += local_delta;
|
||||
|
||||
}
|
||||
|
||||
// clamp-to-edge mode.
|
||||
{
|
||||
// create the texture attribute
|
||||
osg::Texture2D* texture = new osg::Texture2D;
|
||||
texture->setImage(image);
|
||||
|
||||
texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP_TO_EDGE);
|
||||
texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP_TO_EDGE);
|
||||
|
||||
// add the transform node to root group node.
|
||||
top_transform->addChild(createTexturedItem(local_offset,texture,geometryRep));
|
||||
|
||||
local_offset += local_delta;
|
||||
|
||||
}
|
||||
|
||||
// repeat wrap mode.
|
||||
{
|
||||
// create the texture attribute
|
||||
osg::Texture2D* texture = new osg::Texture2D;
|
||||
texture->setImage(image);
|
||||
|
||||
texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT);
|
||||
texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT);
|
||||
|
||||
// add the transform node to root group node.
|
||||
top_transform->addChild(createTexturedItem(local_offset,texture,geometryRep));
|
||||
|
||||
local_offset += local_delta;
|
||||
|
||||
}
|
||||
|
||||
// mirror wrap mode.
|
||||
{
|
||||
// create the texture attribute
|
||||
osg::Texture2D* texture = new osg::Texture2D;
|
||||
texture->setImage(image);
|
||||
|
||||
texture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::MIRROR);
|
||||
texture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::MIRROR);
|
||||
|
||||
// add the transform node to root group node.
|
||||
top_transform->addChild(createTexturedItem(local_offset,texture,geometryRep));
|
||||
|
||||
local_offset += local_delta;
|
||||
|
||||
}
|
||||
|
||||
return top_transform;
|
||||
}
|
||||
|
||||
osg::Node* createModelFromImages(ImageList& imageList)
|
||||
{
|
||||
|
||||
if (imageList.empty()) return NULL;
|
||||
|
||||
// create the root node which will hold the model.
|
||||
osg::Group* root = new osg::Group();
|
||||
|
||||
// create a single drawable to be shared by each texture instance.
|
||||
osg::Drawable* drawable_noTexCoodRep = createSquare(1.0f);
|
||||
|
||||
// add the drawable into a single goede to be shared...
|
||||
osg::Geode* geode_noTexCoodRep = new osg::Geode();
|
||||
geode_noTexCoodRep->addDrawable(drawable_noTexCoodRep);
|
||||
|
||||
|
||||
// create a single drawable to be shared by each texture instance.
|
||||
osg::Drawable* drawable_texCoodRep = createSquare(2.0f);
|
||||
|
||||
// add the drawable into a single goede to be shared...
|
||||
osg::Geode* geode_texCoodRep = new osg::Geode();
|
||||
geode_texCoodRep->addDrawable(drawable_texCoodRep);
|
||||
|
||||
osg::Vec3 offset(0.0f,0.0f,0.0f);
|
||||
osg::Vec3 delta(0.0f,0.0f,3.0f);
|
||||
|
||||
// step through the image list processing each image in turn.
|
||||
for(ImageList::iterator itr=imageList.begin();
|
||||
itr!=imageList.end();
|
||||
++itr)
|
||||
{
|
||||
|
||||
// add the transform node to root group node.
|
||||
root->addChild(createLayer(offset,itr->get(),geode_noTexCoodRep,geode_texCoodRep));
|
||||
|
||||
offset += delta;
|
||||
|
||||
}
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
|
||||
void write_usage(std::ostream& out,const std::string& name)
|
||||
{
|
||||
out << std::endl;
|
||||
out <<"usage:"<< std::endl;
|
||||
out <<" "<<name<<" [options] image_infile1 [image_infile2 ...]"<< std::endl;
|
||||
out << std::endl;
|
||||
out <<"options:"<< std::endl;
|
||||
out <<" -l libraryName - load plugin of name libraryName"<< std::endl;
|
||||
out <<" i.e. -l osgdb_pfb"<< std::endl;
|
||||
out <<" Useful for loading reader/writers which can load"<< std::endl;
|
||||
out <<" other file formats in addition to its extension."<< std::endl;
|
||||
out <<" -e extensionName - load reader/wrter plugin for file extension"<< std::endl;
|
||||
out <<" i.e. -e pfb"<< std::endl;
|
||||
out <<" Useful short hand for specifying full library name as"<< std::endl;
|
||||
out <<" done with -l above, as it automatically expands to"<< std::endl;
|
||||
out <<" the full library name appropriate for each platform."<< std::endl;
|
||||
out <<std::endl;
|
||||
out <<" -stereo - switch on stereo rendering, using the default of,"<< std::endl;
|
||||
out <<" ANAGLYPHIC or the value set in the OSG_STEREO_MODE "<< std::endl;
|
||||
out <<" environmental variable. See doc/stereo.html for "<< std::endl;
|
||||
out <<" further details on setting up accurate stereo "<< std::endl;
|
||||
out <<" for your system. "<< std::endl;
|
||||
out <<" -stereo ANAGLYPHIC - switch on anaglyphic(red/cyan) stereo rendering."<< std::endl;
|
||||
out <<" -stereo QUAD_BUFFER - switch on quad buffered stereo rendering."<< std::endl;
|
||||
out <<std::endl;
|
||||
out <<" -stencil - use a visual with stencil buffer enabled, this "<< std::endl;
|
||||
out <<" also allows the depth complexity statistics mode"<< std::endl;
|
||||
out <<" to be used (press 'p' three times to cycle to it)."<< std::endl;
|
||||
out << std::endl;
|
||||
out<<"example:"<<std::endl;
|
||||
out<<" osgtexture lz.rgb"<<std::endl;
|
||||
out<<std::endl;
|
||||
}
|
||||
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
|
||||
// initialize the GLUT
|
||||
glutInit( &argc, argv );
|
||||
|
||||
if (argc<2)
|
||||
{
|
||||
write_usage(osg::notify(osg::NOTICE),argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// create the commandline args.
|
||||
std::vector<std::string> commandLine;
|
||||
for(int i=1;i<argc;++i) commandLine.push_back(argv[i]);
|
||||
|
||||
|
||||
// initialize the viewer.
|
||||
osgGLUT::Viewer viewer;
|
||||
viewer.setWindowTitle(argv[0]);
|
||||
|
||||
// configure the viewer from the commandline arguments, and eat any
|
||||
// parameters that have been matched.
|
||||
viewer.readCommandLine(commandLine);
|
||||
|
||||
// configure the plugin registry from the commandline arguments, and
|
||||
// eat any parameters that have been matched.
|
||||
osgDB::readCommandLine(commandLine);
|
||||
|
||||
// load the images specified on command line
|
||||
ImageList imageList = getImagesFromFiles(commandLine);
|
||||
|
||||
|
||||
if (!imageList.empty())
|
||||
{
|
||||
|
||||
// create a model from the images.
|
||||
osg::Node* rootNode = createModelFromImages(imageList);
|
||||
|
||||
imageList.clear();
|
||||
|
||||
// add model to viewer.
|
||||
viewer.addViewport( rootNode );
|
||||
|
||||
// register trackball, flight and drive.
|
||||
viewer.registerCameraManipulator(new osgGA::TrackballManipulator);
|
||||
viewer.registerCameraManipulator(new osgGA::FlightManipulator);
|
||||
viewer.registerCameraManipulator(new osgGA::DriveManipulator);
|
||||
|
||||
viewer.open();
|
||||
|
||||
viewer.run();
|
||||
}
|
||||
else
|
||||
{
|
||||
write_usage(osg::notify(osg::NOTICE),argv[0]);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
15
src/Demos/osgtexture3D/Makefile
Normal file
15
src/Demos/osgtexture3D/Makefile
Normal file
@@ -0,0 +1,15 @@
|
||||
TOPDIR = ../../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
osgtexture3D.cpp\
|
||||
|
||||
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||
|
||||
INSTFILES = \
|
||||
$(CXXFILES)\
|
||||
Makefile.inst=Makefile
|
||||
|
||||
EXEC = osgtexture3D
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
11
src/Demos/osgtexture3D/Makefile.inst
Normal file
11
src/Demos/osgtexture3D/Makefile.inst
Normal file
@@ -0,0 +1,11 @@
|
||||
TOPDIR = ../..
|
||||
include $(TOPDIR)/Make/makedefs
|
||||
|
||||
CXXFILES =\
|
||||
osgtexture3D.cpp\
|
||||
|
||||
LIBS += $(OSG_LIBS) $(GLUT_LIB) $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
|
||||
|
||||
EXEC = osgtexture3D
|
||||
|
||||
include $(TOPDIR)/Make/makerules
|
||||
237
src/Demos/osgtexture3D/osgtexture3D.cpp
Normal file
237
src/Demos/osgtexture3D/osgtexture3D.cpp
Normal file
@@ -0,0 +1,237 @@
|
||||
#include <osg/Node>
|
||||
#include <osg/Geometry>
|
||||
#include <osg/Notify>
|
||||
#include <osg/Texture3D>
|
||||
#include <osg/TexGen>
|
||||
|
||||
#include <osgGA/TrackballManipulator>
|
||||
#include <osgGA/FlightManipulator>
|
||||
#include <osgGA/DriveManipulator>
|
||||
|
||||
#include <osgDB/Registry>
|
||||
#include <osgDB/ReadFile>
|
||||
|
||||
#include <osgGLUT/glut>
|
||||
#include <osgGLUT/Viewer>
|
||||
|
||||
|
||||
//
|
||||
// A simple demo demonstrating different texturing modes,
|
||||
// including using of texture extensions.
|
||||
//
|
||||
|
||||
|
||||
typedef std::vector< osg::ref_ptr<osg::Image> > ImageList;
|
||||
|
||||
|
||||
class ConstructStateCallback : public osg::NodeCallback
|
||||
{
|
||||
public:
|
||||
ConstructStateCallback() {}
|
||||
|
||||
osg::StateSet* constructState()
|
||||
{
|
||||
|
||||
// read 4 2d images
|
||||
osg::ref_ptr<osg::Image> image_0 = osgDB::readImageFile("lz.rgb");
|
||||
osg::ref_ptr<osg::Image> image_1 = osgDB::readImageFile("reflect.rgb");
|
||||
osg::ref_ptr<osg::Image> image_2 = osgDB::readImageFile("tank.rgb");
|
||||
osg::ref_ptr<osg::Image> image_3 = osgDB::readImageFile("skymap.jpg");
|
||||
|
||||
if (!image_0 || !image_1 || !image_2 || !image_3)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (image_0->getPixelFormat()!=image_1->getPixelFormat() || image_0->getPixelFormat()!=image_2->getPixelFormat() || image_0->getPixelFormat()!=image_3->getPixelFormat())
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// scale them all to the same size.
|
||||
image_0->scaleImage(256,256,1);
|
||||
image_1->scaleImage(256,256,1);
|
||||
image_2->scaleImage(256,256,1);
|
||||
image_3->scaleImage(256,256,1);
|
||||
|
||||
|
||||
// then allocated a 3d image to use for texturing.
|
||||
osg::Image* image_3d = new osg::Image;
|
||||
image_3d->allocateImage(256,256,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());
|
||||
|
||||
image_3d->setInternalTextureFormat(image_0->getInternalTextureFormat());
|
||||
|
||||
// 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->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::Vec4(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;
|
||||
}
|
||||
|
||||
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->setPlane(osg::TexGen::R, texgen->getPlane(osg::TexGen::R)+osg::Vec4(0.0f,0.0f,0.0f,0.001f));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
|
||||
{
|
||||
|
||||
osg::StateSet* stateset = node->getStateSet();
|
||||
if (stateset)
|
||||
{
|
||||
// we have an exisitng stateset, so lets animate it.
|
||||
animateState(stateset);
|
||||
}
|
||||
else
|
||||
{
|
||||
// no state exist yet, so we must be in the first
|
||||
// pass, so lets create our stateset with all the
|
||||
// textures in it.
|
||||
stateset = constructState();
|
||||
if (stateset) node->setStateSet(stateset);
|
||||
}
|
||||
|
||||
// note, callback is repsonsible for scenegraph traversal so
|
||||
// should always include call the traverse(node,nv) to ensure
|
||||
// that the rest of cullbacks and the scene graph are traversed.
|
||||
traverse(node,nv);
|
||||
}
|
||||
};
|
||||
|
||||
/** 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);
|
||||
geom->setNormalArray(norms);
|
||||
geom->setNormalBinding(osg::Geometry::BIND_OVERALL);
|
||||
|
||||
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);
|
||||
|
||||
geom->addPrimitive(osgNew osg::DrawArrays(osg::Primitive::QUADS,0,4));
|
||||
|
||||
return geom;
|
||||
}
|
||||
|
||||
osg::Node* createModel()
|
||||
{
|
||||
|
||||
// create the geometry of the model, just a simple 2d quad right now.
|
||||
osg::Geode* geode = new osg::Geode;
|
||||
geode->addDrawable(createSquare());
|
||||
|
||||
// normally we'd create the stateset's to contain all the textures
|
||||
// etc here, but, the above technique uses osg::Image::scaleImage and
|
||||
// osg::Image::copySubImage() which are implemented with OpenGL utility
|
||||
// library, which unfortunately can't be used until we have a valid
|
||||
// OpenGL context, and at this point in initilialization we don't have
|
||||
// a valid OpenGL context, so we have to delay creation of state until
|
||||
// there is a valid OpenGL context. I'll manage this by using an
|
||||
// app callback which will create the state during the first traversal.
|
||||
// A bit hacky, and my plan is to reimplement the osg::scaleImage and
|
||||
// osg::Image::copySubImage() without using GLU which will get round
|
||||
// this current limitation.
|
||||
geode->setAppCallback(new ConstructStateCallback());
|
||||
|
||||
return geode;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main( int argc, char **argv )
|
||||
{
|
||||
|
||||
// initialize the GLUT
|
||||
glutInit( &argc, argv );
|
||||
|
||||
// create the commandline args.
|
||||
std::vector<std::string> commandLine;
|
||||
for(int i=1;i<argc;++i) commandLine.push_back(argv[i]);
|
||||
|
||||
|
||||
// initialize the viewer.
|
||||
osgGLUT::Viewer viewer;
|
||||
viewer.setWindowTitle(argv[0]);
|
||||
|
||||
// configure the viewer from the commandline arguments, and eat any
|
||||
// parameters that have been matched.
|
||||
viewer.readCommandLine(commandLine);
|
||||
|
||||
// configure the plugin registry from the commandline arguments, and
|
||||
// eat any parameters that have been matched.
|
||||
osgDB::readCommandLine(commandLine);
|
||||
|
||||
// create a model from the images.
|
||||
osg::Node* rootNode = createModel();
|
||||
|
||||
if (rootNode)
|
||||
{
|
||||
|
||||
// add model to viewer.
|
||||
viewer.addViewport( rootNode );
|
||||
|
||||
// register trackball, flight and drive.
|
||||
viewer.registerCameraManipulator(new osgGA::TrackballManipulator);
|
||||
viewer.registerCameraManipulator(new osgGA::FlightManipulator);
|
||||
viewer.registerCameraManipulator(new osgGA::DriveManipulator);
|
||||
|
||||
viewer.open();
|
||||
|
||||
viewer.run();
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -190,7 +190,7 @@ void Image::setPixelFormat(const GLenum format)
|
||||
}
|
||||
}
|
||||
|
||||
void Image::createImage(int s,int t,int r,
|
||||
void Image::allocateImage(int s,int t,int r,
|
||||
GLenum format,GLenum type,
|
||||
int packing)
|
||||
{
|
||||
@@ -264,7 +264,7 @@ void Image::setImage(int s,int t,int r,
|
||||
void Image::readPixels(int x,int y,int width,int height,
|
||||
GLenum format,GLenum type)
|
||||
{
|
||||
createImage(width,height,1,format,type);
|
||||
allocateImage(width,height,1,format,type);
|
||||
|
||||
glPixelStorei(GL_PACK_ALIGNMENT,_packing);
|
||||
|
||||
@@ -274,6 +274,8 @@ void Image::readPixels(int x,int y,int width,int height,
|
||||
|
||||
void Image::scaleImage(const int s,const int t,const int r)
|
||||
{
|
||||
if (_s==s && _t==t && _r==r) return;
|
||||
|
||||
if (_data==NULL)
|
||||
{
|
||||
notify(WARN) << "Error Image::scaleImage() do not succeed : cannot scale NULL image."<<std::endl;
|
||||
@@ -285,6 +287,7 @@ void Image::scaleImage(const int s,const int t,const int r)
|
||||
notify(WARN) << "Error Image::scaleImage() do not succeed : scaling of volumes not implemented."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
unsigned int newTotalSize = computeRowWidthInBytes(s,_pixelFormat,_dataType,_packing)*t;
|
||||
@@ -331,6 +334,63 @@ void Image::scaleImage(const int s,const int t,const int r)
|
||||
++_modifiedTag;
|
||||
}
|
||||
|
||||
void Image::copySubImage(int s_offset,int t_offset,int r_offset,osg::Image* source)
|
||||
{
|
||||
if (!source) return;
|
||||
if (s_offset<0 || t_offset<0 || r_offset<0)
|
||||
{
|
||||
notify(WARN)<<"Warning: negative offsets passed to Image::copySubImage(..) not supported, operation ignored."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_data)
|
||||
{
|
||||
cout<<"allocating image"<<endl;
|
||||
allocateImage(s_offset+source->r(),t_offset+source->t(),r_offset+source->t(),
|
||||
source->getPixelFormat(),source->getDataType(),
|
||||
source->getPacking());
|
||||
}
|
||||
|
||||
if (s_offset>=_s || t_offset>=_t || r_offset>=_r)
|
||||
{
|
||||
notify(WARN)<<"Warning: offsets passed to Image::copySubImage(..) outside destination image, operation ignored."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (_pixelFormat != source->getPixelFormat())
|
||||
{
|
||||
notify(WARN)<<"Warning: image with an incompatible pixel formats passed to Image::copySubImage(..), operation ignored."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
void* data_destination = data(s_offset,t_offset,r_offset);
|
||||
|
||||
glPixelStorei(GL_PACK_ALIGNMENT,source->getPacking());
|
||||
glPixelStorei(GL_PACK_ROW_LENGTH,_s);
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT,_packing);
|
||||
|
||||
GLint status = gluScaleImage(_pixelFormat,
|
||||
source->s(),
|
||||
source->t(),
|
||||
source->getDataType(),
|
||||
source->data(),
|
||||
source->s(),
|
||||
source->t(),
|
||||
_dataType,
|
||||
data_destination);
|
||||
|
||||
glPixelStorei(GL_PACK_ROW_LENGTH,0);
|
||||
|
||||
if (status!=0)
|
||||
{
|
||||
notify(WARN) << "Error Image::scaleImage() do not succeed : errorString = "<<gluErrorString((GLenum)status)<<std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void Image::flipHorizontal(int image)
|
||||
{
|
||||
if (_data==NULL)
|
||||
|
||||
@@ -97,13 +97,13 @@ void Texture3D::setImage(Image* image)
|
||||
|
||||
void Texture3D::apply(State& state) const
|
||||
{
|
||||
|
||||
static bool s_texturing_supported = strncmp((const char*)glGetString(GL_VERSION),"1.2",3)>=0 ||
|
||||
isGLExtensionSupported("GL_EXT_texture3D");
|
||||
|
||||
if (s_texturing_supported)
|
||||
if (!s_texturing_supported)
|
||||
{
|
||||
notify(WARN)<<"Warning: Texture3D::apply(..) failed, 3D texturing is not support by your OpenGL drivers."<<std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
// get the contextID (user defined ID of 0 upwards) for the
|
||||
@@ -205,8 +205,6 @@ void Texture3D::applyTexImage3D(GLenum target, Image* image, State& state, GLsiz
|
||||
image->ensureValidSizeForTexturing();
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking());
|
||||
|
||||
|
||||
|
||||
if( _min_filter == LINEAR || _min_filter == NEAREST )
|
||||
{
|
||||
|
||||
@@ -1125,19 +1125,19 @@ osg::Material* ConvertFromPerformer::visitMaterial(osg::StateSet* osgStateSet,pf
|
||||
}
|
||||
|
||||
|
||||
static osg:::Texture2D::FilterMode getTexfilter(int filter, int pftype)
|
||||
static osg::Texture2D::FilterMode getTexfilter(int filter, int pftype)
|
||||
{
|
||||
if (filter == PFTEX_MINFILTER)
|
||||
{
|
||||
|
||||
if (pftype & PFTEX_LINEAR)
|
||||
return osg:::Texture2D::NEAREST_MIPMAP_LINEAR;
|
||||
return osg::Texture2D::NEAREST_MIPMAP_LINEAR;
|
||||
else if (pftype & PFTEX_BILINEAR)
|
||||
return osg:::Texture2D::LINEAR_MIPMAP_NEAREST;
|
||||
return osg::Texture2D::LINEAR_MIPMAP_NEAREST;
|
||||
else if (pftype & PFTEX_TRILINEAR)
|
||||
return osg:::Texture2D::LINEAR_MIPMAP_LINEAR;
|
||||
return osg::Texture2D::LINEAR_MIPMAP_LINEAR;
|
||||
|
||||
return osg:::Texture2D::NEAREST_MIPMAP_LINEAR;
|
||||
return osg::Texture2D::NEAREST_MIPMAP_LINEAR;
|
||||
|
||||
}
|
||||
else
|
||||
@@ -1147,7 +1147,7 @@ static osg:::Texture2D::FilterMode getTexfilter(int filter, int pftype)
|
||||
// not quite sure what is supposed to be interpret the Peformer
|
||||
// filter modes here so will simple go with OpenGL default.
|
||||
|
||||
return osg:::Texture2D::LINEAR;
|
||||
return osg::Texture2D::LINEAR;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1173,26 +1173,26 @@ osg::Texture2D* ConvertFromPerformer::visitTexture(osg::StateSet* osgStateSet,pf
|
||||
int repeat_t = tex->getRepeat(PFTEX_WRAP_T);
|
||||
|
||||
if (repeat_r==PFTEX_CLAMP)
|
||||
osgTexture->setWrap(osg:::Texture2D::WRAP_R,osg:::Texture2D::CLAMP);
|
||||
osgTexture->setWrap(osg::Texture2D::WRAP_R,osg::Texture2D::CLAMP);
|
||||
else
|
||||
osgTexture->setWrap(osg:::Texture2D::WRAP_R,osg:::Texture2D::REPEAT);
|
||||
osgTexture->setWrap(osg::Texture2D::WRAP_R,osg::Texture2D::REPEAT);
|
||||
|
||||
if (repeat_s==PFTEX_CLAMP)
|
||||
osgTexture->setWrap(osg:::Texture2D::WRAP_S,osg:::Texture2D::CLAMP);
|
||||
osgTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP);
|
||||
else
|
||||
osgTexture->setWrap(osg:::Texture2D::WRAP_S,osg:::Texture2D::REPEAT);
|
||||
osgTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT);
|
||||
|
||||
if (repeat_t==PFTEX_CLAMP)
|
||||
osgTexture->setWrap(osg:::Texture2D::WRAP_T,osg:::Texture2D::CLAMP);
|
||||
osgTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP);
|
||||
else
|
||||
osgTexture->setWrap(osg:::Texture2D::WRAP_T,osg:::Texture2D::REPEAT);
|
||||
osgTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT);
|
||||
|
||||
// filter
|
||||
#if 1
|
||||
osgTexture->setFilter(osg:::Texture2D::MIN_FILTER,
|
||||
osgTexture->setFilter(osg::Texture2D::MIN_FILTER,
|
||||
getTexfilter(PFTEX_MINFILTER,
|
||||
tex->getFilter(PFTEX_MINFILTER)));
|
||||
osgTexture->setFilter(osg:::Texture2D::MAG_FILTER,
|
||||
osgTexture->setFilter(osg::Texture2D::MAG_FILTER,
|
||||
getTexfilter(PFTEX_MAGFILTER,
|
||||
tex->getFilter(PFTEX_MAGFILTER)));
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user