2001-10-04 05:44:07 +08:00
|
|
|
#include <osg/GL>
|
|
|
|
#include <osg/GLU>
|
|
|
|
|
|
|
|
#include <osg/Image>
|
|
|
|
#include <osg/Notify>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/GeoSet>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osg/StateSet>
|
2001-01-11 00:32:10 +08:00
|
|
|
#include <osg/Texture>
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
using namespace osg;
|
2002-02-23 01:12:10 +08:00
|
|
|
using namespace std;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
Image::Image()
|
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
_fileName = "";
|
2001-01-11 00:32:10 +08:00
|
|
|
_s = _t = _r = 0;
|
|
|
|
_internalFormat = 0;
|
|
|
|
_pixelFormat = (unsigned int)0;
|
|
|
|
_dataType = (unsigned int)0;
|
|
|
|
_packing = 4;
|
2002-04-11 05:51:34 +08:00
|
|
|
_pixelSizeInBits = 0;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
_data = (unsigned char *)0L;
|
2001-11-19 05:31:16 +08:00
|
|
|
|
|
|
|
_modifiedTag = 0;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2002-01-29 22:04:06 +08:00
|
|
|
Image::Image(const Image& image,const CopyOp& copyop):
|
|
|
|
Object(image,copyop),
|
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
|
|
|
_fileName(image._fileName),
|
|
|
|
_s(image._s), _t(image._t), _r(image._r),
|
|
|
|
_internalFormat(image._internalFormat),
|
|
|
|
_pixelFormat(image._pixelFormat),
|
|
|
|
_dataType(image._dataType),
|
|
|
|
_packing(image._packing),
|
2002-04-11 05:51:34 +08:00
|
|
|
_pixelSizeInBits(image._pixelSizeInBits),
|
Added support for shallow and deep copy of nodes, drawables and state, via a
copy constructor which takes an optional Cloner object, and the old
osg::Object::clone() has changed so that it now requires a Cloner as paramter.
This is passed on to the copy constructor to help control the shallow vs
deep copying. The old functionality of clone() which was clone of type has
been renamed to cloneType().
Updated all of the OSG to work with these new conventions, implemention all
the required copy constructors etc. A couple of areas will do shallow
copies by design, a couple of other still need to be updated to do either
shallow or deep.
Neither of the shallow or deep copy operations have been tested yet, only
the old functionality of the OSG has been checked so far, such running the
viewer on various demo datasets.
Also fixed a problem in osg::Optimize::RemoveRendundentNodesVisitor which
was not checking that Group didn't have have any attached StateSet's, Callbacks
or UserData. These checks have now been added, which fixes a bug which was
revealled by the new osgscribe demo, this related to removal of group acting
as state decorator.
method
2002-01-29 05:17:01 +08:00
|
|
|
_data(0L),
|
|
|
|
_modifiedTag(image._modifiedTag)
|
|
|
|
{
|
|
|
|
if (image._data)
|
|
|
|
{
|
|
|
|
int num_components =
|
|
|
|
_pixelFormat == GL_LUMINANCE ? 1 :
|
|
|
|
_pixelFormat == GL_LUMINANCE_ALPHA ? 2 :
|
|
|
|
_pixelFormat == GL_RGB ? 3 :
|
|
|
|
_pixelFormat == GL_RGBA ? 4 : 4;
|
|
|
|
|
|
|
|
int size = _s*_t*_r*num_components;
|
|
|
|
_data = (unsigned char*) malloc(size);
|
|
|
|
memcpy(_data,image._data,size);
|
|
|
|
}
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
Image::~Image()
|
|
|
|
{
|
|
|
|
if (_data) ::free(_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
void Image::setFileName(const std::string& fileName)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
_fileName = fileName;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-11 05:51:34 +08:00
|
|
|
void Image::computePixelSize()
|
|
|
|
{
|
|
|
|
// temporary code.. to be filled in properly ASAP. RO.
|
|
|
|
_pixelSizeInBits=32;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
void Image::setImage(const int s,const int t,const int r,
|
|
|
|
const int internalFormat,
|
|
|
|
const unsigned int pixelFormat,
|
|
|
|
const unsigned int dataType,
|
2001-01-11 00:32:10 +08:00
|
|
|
unsigned char *data,
|
2001-09-20 05:08:56 +08:00
|
|
|
const int packing)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
if (_data) ::free(_data);
|
|
|
|
|
|
|
|
_s = s;
|
|
|
|
_t = t;
|
|
|
|
_r = r;
|
|
|
|
|
|
|
|
_internalFormat = internalFormat;
|
|
|
|
_pixelFormat = pixelFormat;
|
|
|
|
_dataType = dataType;
|
|
|
|
|
|
|
|
_data = data;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
if (packing<0)
|
|
|
|
{
|
|
|
|
if (_s%4==0)
|
|
|
|
_packing = 4;
|
|
|
|
else
|
2001-09-20 05:08:56 +08:00
|
|
|
_packing = 1;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
_packing = packing;
|
2002-04-11 05:51:34 +08:00
|
|
|
|
|
|
|
computePixelSize();
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
// test scaling...
|
|
|
|
// scaleImageTo(16,16,_r);
|
2001-11-19 05:31:16 +08:00
|
|
|
|
|
|
|
++_modifiedTag;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-04-11 05:51:34 +08:00
|
|
|
void Image::readPixels(int x,int y,int width,int height,
|
|
|
|
unsigned int pixelFormat,unsigned int dataType,
|
|
|
|
const int packing)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (width!=_s || height!=_s || _t != 1 ||
|
|
|
|
_pixelFormat!=pixelFormat ||
|
|
|
|
_dataType!=dataType)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (_data) ::free(_data);
|
|
|
|
|
|
|
|
_s = width;
|
|
|
|
_t = height;
|
|
|
|
_r = 1;
|
|
|
|
_pixelFormat = pixelFormat;
|
|
|
|
_dataType = dataType;
|
|
|
|
|
|
|
|
if (packing<0)
|
|
|
|
{
|
|
|
|
if (_s%4==0)
|
|
|
|
_packing = 4;
|
|
|
|
else
|
|
|
|
_packing = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
_packing = packing;
|
|
|
|
|
|
|
|
computePixelSize();
|
|
|
|
|
|
|
|
// need to sort out what size to really use...
|
|
|
|
_data = (unsigned char *)malloc(2 * (_s+1)*(_t+1)*4);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT,_packing);
|
|
|
|
|
|
|
|
glReadPixels(x,y,width,height,pixelFormat,dataType,_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
void Image::scaleImage(const int s,const int t,const int /*r*/)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
if (_data==NULL) return;
|
|
|
|
|
2002-04-11 05:51:34 +08:00
|
|
|
// need to sort out what size to really use...
|
2001-01-11 00:32:10 +08:00
|
|
|
unsigned char* newData = (unsigned char *)malloc(2 * (s+1)*(t+1)*4);
|
|
|
|
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT,_packing);
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT,_packing);
|
|
|
|
|
|
|
|
GLint status = gluScaleImage((GLenum)_pixelFormat,
|
2001-09-20 05:08:56 +08:00
|
|
|
_s,
|
|
|
|
_t,
|
|
|
|
(GLenum)_dataType,
|
|
|
|
_data,
|
|
|
|
s,
|
|
|
|
t,
|
|
|
|
(GLenum)_dataType,
|
|
|
|
newData);
|
|
|
|
|
|
|
|
if (status==0)
|
|
|
|
{
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
// free old image.
|
|
|
|
::free(_data);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
_s = s;
|
|
|
|
_t = t;
|
|
|
|
_data = newData;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
::free(newData);
|
|
|
|
|
2001-12-15 07:18:28 +08:00
|
|
|
notify(WARN) << "Error Image::scaleImage() do not succeed : errorString = "<<gluErrorString((GLenum)status)<<std::endl;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
2001-11-19 05:31:16 +08:00
|
|
|
|
|
|
|
++_modifiedTag;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
void Image::ensureDimensionsArePowerOfTwo()
|
|
|
|
{
|
|
|
|
float sp2 = logf((float)_s)/logf(2.0f);
|
|
|
|
float rounded_sp2 = floorf(sp2+0.5f);
|
|
|
|
int new_s = (int)(powf(2.0f,rounded_sp2));
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
float tp2 = logf((float)_t)/logf(2.0f);
|
|
|
|
float rounded_tp2 = floorf(tp2+0.5f);
|
|
|
|
int new_t = (int)(powf(2.0f,rounded_tp2));
|
2001-10-05 18:38:16 +08:00
|
|
|
|
2002-03-26 03:04:30 +08:00
|
|
|
static GLint max_size=256;
|
2002-03-15 01:34:08 +08:00
|
|
|
|
|
|
|
static bool init = true;
|
|
|
|
if (init)
|
|
|
|
{
|
|
|
|
init = false;
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE,&max_size);
|
2002-03-18 02:44:47 +08:00
|
|
|
notify(INFO) << "Max texture size "<<max_size<<std::endl;
|
2002-03-15 01:34:08 +08:00
|
|
|
}
|
2002-03-18 02:44:47 +08:00
|
|
|
|
|
|
|
//max_size = 64;
|
|
|
|
|
2002-03-15 01:34:08 +08:00
|
|
|
if (new_s>max_size) new_s = max_size;
|
|
|
|
if (new_t>max_size) new_t = max_size;
|
2001-10-05 18:38:16 +08:00
|
|
|
|
|
|
|
if (new_s!=_s || new_t!=_t)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-12-15 07:18:28 +08:00
|
|
|
if (!_fileName.empty()) notify(NOTICE) << "Scaling image '"<<_fileName<<"' from ("<<_s<<","<<_t<<") to ("<<new_s<<","<<new_t<<")"<<std::endl;
|
|
|
|
else notify(NOTICE) << "Scaling image from ("<<_s<<","<<_t<<") to ("<<new_s<<","<<new_t<<")"<<std::endl;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
scaleImage(new_s,new_t,_r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
Geode* osg::createGeodeForImage(osg::Image* image)
|
|
|
|
{
|
|
|
|
return createGeodeForImage(image,image->s(),image->t());
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
Geode* osg::createGeodeForImage(osg::Image* image,const float s,const float t)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
if (s>0 && t>0)
|
|
|
|
{
|
|
|
|
|
|
|
|
float y = 1.0;
|
|
|
|
float x = y*(s/t);
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
// set up the texture.
|
2002-03-27 07:52:52 +08:00
|
|
|
osg::Texture* texture = osgNew osg::Texture;
|
2001-01-11 00:32:10 +08:00
|
|
|
texture->setImage(image);
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
// set up the drawstate.
|
2002-03-27 07:52:52 +08:00
|
|
|
osg::StateSet* dstate = osgNew osg::StateSet;
|
2001-09-20 05:08:56 +08:00
|
|
|
dstate->setMode(GL_CULL_FACE,osg::StateAttribute::OFF);
|
|
|
|
dstate->setMode(GL_LIGHTING,osg::StateAttribute::OFF);
|
|
|
|
dstate->setAttributeAndModes(texture,osg::StateAttribute::ON);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
// set up the geoset.
|
2002-03-27 07:52:52 +08:00
|
|
|
osg::GeoSet* gset = osgNew osg::GeoSet;
|
2001-09-20 05:08:56 +08:00
|
|
|
gset->setStateSet(dstate);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-03-27 07:52:52 +08:00
|
|
|
osg::Vec3* coords = osgNew Vec3[4];
|
2001-01-11 00:32:10 +08:00
|
|
|
coords[0].set(-x,0.0f,y);
|
|
|
|
coords[1].set(-x,0.0f,-y);
|
|
|
|
coords[2].set(x,0.0f,-y);
|
|
|
|
coords[3].set(x,0.0f,y);
|
|
|
|
gset->setCoords(coords);
|
|
|
|
|
2002-03-27 07:52:52 +08:00
|
|
|
osg::Vec2* tcoords = osgNew Vec2[4];
|
2001-01-11 00:32:10 +08:00
|
|
|
tcoords[0].set(0.0f,1.0f);
|
|
|
|
tcoords[1].set(0.0f,0.0f);
|
|
|
|
tcoords[2].set(1.0f,0.0f);
|
|
|
|
tcoords[3].set(1.0f,1.0f);
|
|
|
|
gset->setTextureCoords(tcoords);
|
|
|
|
gset->setTextureBinding(osg::GeoSet::BIND_PERVERTEX);
|
|
|
|
|
2002-03-27 07:52:52 +08:00
|
|
|
osg::Vec4* colours = osgNew Vec4[1];
|
2001-09-20 05:08:56 +08:00
|
|
|
colours->set(1.0f,1.0f,1.0,1.0f);
|
2001-01-11 00:32:10 +08:00
|
|
|
gset->setColors(colours);
|
|
|
|
gset->setColorBinding(osg::GeoSet::BIND_OVERALL);
|
|
|
|
|
|
|
|
gset->setNumPrims(1);
|
|
|
|
gset->setPrimType(osg::GeoSet::QUADS);
|
|
|
|
|
|
|
|
// set up the geode.
|
2002-03-27 07:52:52 +08:00
|
|
|
osg::Geode* geode = osgNew osg::Geode;
|
2001-09-20 05:08:56 +08:00
|
|
|
geode->addDrawable(gset);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
return geode;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|