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>
|
2002-06-26 04:27:51 +08:00
|
|
|
#include <osg/Geometry>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osg/StateSet>
|
2002-08-25 03:39:39 +08:00
|
|
|
#include <osg/Texture2D>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
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()
|
|
|
|
{
|
2002-04-12 01:15:07 +08:00
|
|
|
_fileName = "";
|
|
|
|
_s = _t = _r = 0;
|
|
|
|
_internalTextureFormat = 0;
|
|
|
|
_pixelFormat = (unsigned int)0;
|
|
|
|
_dataType = (unsigned int)0;
|
|
|
|
_packing = 4;
|
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),
|
2002-04-12 01:15:07 +08:00
|
|
|
_internalTextureFormat(image._internalTextureFormat),
|
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
|
|
|
_pixelFormat(image._pixelFormat),
|
|
|
|
_dataType(image._dataType),
|
|
|
|
_packing(image._packing),
|
|
|
|
_data(0L),
|
2002-04-23 22:58:33 +08:00
|
|
|
_modifiedTag(image._modifiedTag),
|
|
|
|
_mipmapData(image._mipmapData)
|
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
|
|
|
{
|
|
|
|
if (image._data)
|
|
|
|
{
|
2002-04-23 05:13:33 +08:00
|
|
|
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);
|
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
|
|
|
memcpy(_data,image._data,size);
|
|
|
|
}
|
2002-04-23 05:13:33 +08:00
|
|
|
|
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
|
|
|
}
|
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 06:10:07 +08:00
|
|
|
|
2002-05-14 17:34:11 +08:00
|
|
|
const bool Image::isPackedType(GLenum type)
|
2002-04-12 01:15:07 +08:00
|
|
|
{
|
2002-05-14 17:34:11 +08:00
|
|
|
switch(type)
|
2002-04-12 01:15:07 +08:00
|
|
|
{
|
|
|
|
case(GL_UNSIGNED_BYTE_3_3_2):
|
|
|
|
case(GL_UNSIGNED_BYTE_2_3_3_REV):
|
|
|
|
case(GL_UNSIGNED_SHORT_5_6_5):
|
|
|
|
case(GL_UNSIGNED_SHORT_5_6_5_REV):
|
|
|
|
case(GL_UNSIGNED_SHORT_4_4_4_4):
|
|
|
|
case(GL_UNSIGNED_SHORT_4_4_4_4_REV):
|
|
|
|
case(GL_UNSIGNED_SHORT_5_5_5_1):
|
|
|
|
case(GL_UNSIGNED_SHORT_1_5_5_5_REV):
|
|
|
|
case(GL_UNSIGNED_INT_8_8_8_8):
|
|
|
|
case(GL_UNSIGNED_INT_8_8_8_8_REV):
|
|
|
|
case(GL_UNSIGNED_INT_10_10_10_2):
|
|
|
|
case(GL_UNSIGNED_INT_2_10_10_10_REV): return true;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-14 17:34:11 +08:00
|
|
|
const unsigned int Image::computeNumComponents(GLenum format)
|
2002-04-12 01:15:07 +08:00
|
|
|
{
|
2002-05-14 17:34:11 +08:00
|
|
|
switch(format)
|
2002-04-12 01:15:07 +08:00
|
|
|
{
|
|
|
|
case(GL_COLOR_INDEX): return 1;
|
|
|
|
case(GL_STENCIL_INDEX): return 1;
|
|
|
|
case(GL_DEPTH_COMPONENT): return 1;
|
|
|
|
case(GL_RED): return 1;
|
|
|
|
case(GL_GREEN): return 1;
|
|
|
|
case(GL_BLUE): return 1;
|
|
|
|
case(GL_ALPHA): return 1;
|
|
|
|
case(GL_RGB): return 3;
|
|
|
|
case(GL_BGR): return 3;
|
|
|
|
case(GL_RGBA): return 4;
|
|
|
|
case(GL_BGRA): return 4;
|
|
|
|
case(GL_LUMINANCE): return 1;
|
2002-04-16 22:09:46 +08:00
|
|
|
case(GL_LUMINANCE_ALPHA): return 2;
|
2002-04-12 01:15:07 +08:00
|
|
|
default: return 0;
|
|
|
|
}
|
2002-04-11 05:51:34 +08:00
|
|
|
}
|
|
|
|
|
2002-04-12 01:15:07 +08:00
|
|
|
|
|
|
|
const unsigned int Image::computePixelSizeInBits(GLenum format,GLenum type)
|
|
|
|
{
|
2002-04-14 21:41:13 +08:00
|
|
|
switch(type)
|
2002-04-12 01:15:07 +08:00
|
|
|
{
|
2002-04-14 21:41:13 +08:00
|
|
|
case(GL_BITMAP): return computeNumComponents(format);
|
2002-04-12 01:15:07 +08:00
|
|
|
|
|
|
|
case(GL_BYTE):
|
2002-04-14 21:41:13 +08:00
|
|
|
case(GL_UNSIGNED_BYTE): return 8*computeNumComponents(format);
|
2002-04-12 01:15:07 +08:00
|
|
|
|
|
|
|
case(GL_SHORT):
|
2002-04-14 21:41:13 +08:00
|
|
|
case(GL_UNSIGNED_SHORT): return 16*computeNumComponents(format);
|
2002-04-12 01:15:07 +08:00
|
|
|
|
|
|
|
case(GL_INT):
|
|
|
|
case(GL_UNSIGNED_INT):
|
2002-04-14 21:41:13 +08:00
|
|
|
case(GL_FLOAT): return 32*computeNumComponents(format);
|
2002-04-12 01:15:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
case(GL_UNSIGNED_BYTE_3_3_2):
|
|
|
|
case(GL_UNSIGNED_BYTE_2_3_3_REV): return 8;
|
|
|
|
|
|
|
|
case(GL_UNSIGNED_SHORT_5_6_5):
|
|
|
|
case(GL_UNSIGNED_SHORT_5_6_5_REV):
|
|
|
|
case(GL_UNSIGNED_SHORT_4_4_4_4):
|
|
|
|
case(GL_UNSIGNED_SHORT_4_4_4_4_REV):
|
|
|
|
case(GL_UNSIGNED_SHORT_5_5_5_1):
|
|
|
|
case(GL_UNSIGNED_SHORT_1_5_5_5_REV): return 16;
|
|
|
|
|
|
|
|
case(GL_UNSIGNED_INT_8_8_8_8):
|
|
|
|
case(GL_UNSIGNED_INT_8_8_8_8_REV):
|
|
|
|
case(GL_UNSIGNED_INT_10_10_10_2):
|
|
|
|
case(GL_UNSIGNED_INT_2_10_10_10_REV): return 32;
|
2002-04-14 21:41:13 +08:00
|
|
|
default: return 0;
|
2002-04-12 01:15:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const unsigned int Image::computeRowWidthInBytes(int width,GLenum format,GLenum type,int packing)
|
|
|
|
{
|
|
|
|
unsigned int pixelSize = computePixelSizeInBits(format,type);
|
|
|
|
int widthInBits = width*pixelSize;
|
|
|
|
int packingInBits = packing*8;
|
2002-04-14 21:41:13 +08:00
|
|
|
return (widthInBits/packingInBits + ((widthInBits%packingInBits)?1:0))*packing;
|
2002-04-12 01:15:07 +08:00
|
|
|
}
|
|
|
|
|
2002-08-16 21:33:32 +08:00
|
|
|
const unsigned int Image::computeNearestPowerOfTwo(unsigned int s,float bias)
|
|
|
|
{
|
|
|
|
if ((s & (s-1))!=0)
|
|
|
|
{
|
|
|
|
// it isn't so lets find the closest power of two.
|
|
|
|
// yes, logf and powf are slow, but this code should
|
|
|
|
// only be called during scene graph initilization,
|
|
|
|
// if at all, so not critical in the greater scheme.
|
|
|
|
float p2 = logf((float)s)/logf(2.0f);
|
|
|
|
float rounded_p2 = floorf(p2+bias);
|
|
|
|
s = (int)(powf(2.0f,rounded_p2));
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2002-04-16 22:57:39 +08:00
|
|
|
void Image::setInternalTextureFormat(GLint internalFormat)
|
|
|
|
{
|
|
|
|
// won't do any sanity checking right now, leave it to
|
|
|
|
// OpenGL to make the call.
|
|
|
|
_internalTextureFormat = internalFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Image::setPixelFormat(const GLenum format)
|
|
|
|
{
|
|
|
|
if (_pixelFormat==format) return; // do nothing if the same.
|
|
|
|
|
|
|
|
if (computeNumComponents(_pixelFormat)==computeNumComponents(format))
|
|
|
|
{
|
|
|
|
// if the two formats have the same number of componets then
|
|
|
|
// we can do a straight swap.
|
|
|
|
_pixelFormat = format;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
notify(WARN)<<"Image::setPixelFormat(..) - warning, attempt to reset the pixel format with a different number of components."<<std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-27 18:06:57 +08:00
|
|
|
void Image::allocateImage(int s,int t,int r,
|
2002-04-12 01:15:07 +08:00
|
|
|
GLenum format,GLenum type,
|
|
|
|
int packing)
|
|
|
|
{
|
2002-04-23 05:13:33 +08:00
|
|
|
_mipmapData.clear();
|
|
|
|
|
2002-04-12 01:15:07 +08:00
|
|
|
unsigned int previousTotalSize = computeRowWidthInBytes(_s,_pixelFormat,_dataType,_packing)*_t*_r;
|
|
|
|
|
|
|
|
unsigned int newTotalSize = computeRowWidthInBytes(s,format,type,packing)*t*r;
|
|
|
|
|
|
|
|
if (newTotalSize!=previousTotalSize)
|
|
|
|
{
|
|
|
|
if (_data) ::free(_data);
|
|
|
|
|
2002-04-14 21:41:13 +08:00
|
|
|
if (newTotalSize)
|
2002-05-14 18:20:55 +08:00
|
|
|
_data = (unsigned char *)malloc (newTotalSize);
|
2002-04-14 21:41:13 +08:00
|
|
|
else
|
|
|
|
_data = 0L;
|
2002-04-12 01:15:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_data)
|
|
|
|
{
|
|
|
|
_s = s;
|
|
|
|
_t = t;
|
|
|
|
_r = r;
|
|
|
|
_pixelFormat = format;
|
|
|
|
_dataType = type;
|
|
|
|
_packing = packing;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// throw exception?? not for now, will simply set values to 0.
|
|
|
|
_s = 0;
|
|
|
|
_t = 0;
|
|
|
|
_r = 0;
|
|
|
|
_pixelFormat = 0;
|
|
|
|
_dataType = 0;
|
|
|
|
_packing = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
++_modifiedTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Image::setImage(int s,int t,int r,
|
|
|
|
GLint internalTextureFormat,
|
|
|
|
GLenum format,GLenum type,
|
2001-01-11 00:32:10 +08:00
|
|
|
unsigned char *data,
|
2002-04-12 01:15:07 +08:00
|
|
|
int packing)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
if (_data) ::free(_data);
|
2002-04-23 05:13:33 +08:00
|
|
|
_mipmapData.clear();
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
_s = s;
|
|
|
|
_t = t;
|
|
|
|
_r = r;
|
|
|
|
|
2002-04-12 01:15:07 +08:00
|
|
|
_internalTextureFormat = internalTextureFormat;
|
|
|
|
_pixelFormat = format;
|
|
|
|
_dataType = type;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
_data = data;
|
2002-04-12 01:15:07 +08:00
|
|
|
_packing = packing;
|
2002-04-11 05:51:34 +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,
|
2002-04-12 01:15:07 +08:00
|
|
|
GLenum format,GLenum type)
|
2002-04-11 05:51:34 +08:00
|
|
|
{
|
2002-08-27 18:06:57 +08:00
|
|
|
allocateImage(width,height,1,format,type);
|
2002-04-11 05:51:34 +08:00
|
|
|
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT,_packing);
|
|
|
|
|
2002-04-12 01:15:07 +08:00
|
|
|
glReadPixels(x,y,width,height,format,type,_data);
|
2002-04-11 05:51:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-04-12 01:15:07 +08:00
|
|
|
void Image::scaleImage(const int s,const int t,const int r)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-08-27 18:06:57 +08:00
|
|
|
if (_s==s && _t==t && _r==r) return;
|
|
|
|
|
2002-04-12 01:15:07 +08:00
|
|
|
if (_data==NULL)
|
|
|
|
{
|
|
|
|
notify(WARN) << "Error Image::scaleImage() do not succeed : cannot scale NULL image."<<std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_r!=1 || r!=1)
|
|
|
|
{
|
|
|
|
notify(WARN) << "Error Image::scaleImage() do not succeed : scaling of volumes not implemented."<<std::endl;
|
|
|
|
return;
|
|
|
|
}
|
2002-08-27 18:06:57 +08:00
|
|
|
|
2002-04-12 01:15:07 +08:00
|
|
|
|
|
|
|
|
|
|
|
unsigned int newTotalSize = computeRowWidthInBytes(s,_pixelFormat,_dataType,_packing)*t;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-04-11 05:51:34 +08:00
|
|
|
// need to sort out what size to really use...
|
2002-05-14 18:20:55 +08:00
|
|
|
unsigned char* newData = (unsigned char *)malloc(newTotalSize);
|
2002-04-14 21:41:13 +08:00
|
|
|
if (!newData)
|
2002-04-12 01:15:07 +08:00
|
|
|
{
|
|
|
|
// should we throw an exception??? Just return for time being.
|
2002-05-28 19:40:37 +08:00
|
|
|
notify(FATAL) << "Error Image::scaleImage() do not succeed : out of memory."<<newTotalSize<<std::endl;
|
2002-04-12 01:15:07 +08:00
|
|
|
return;
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
glPixelStorei(GL_PACK_ALIGNMENT,_packing);
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT,_packing);
|
|
|
|
|
2002-04-12 01:15:07 +08:00
|
|
|
GLint status = gluScaleImage(_pixelFormat,
|
2001-09-20 05:08:56 +08:00
|
|
|
_s,
|
|
|
|
_t,
|
2002-04-12 01:15:07 +08:00
|
|
|
_dataType,
|
2001-09-20 05:08:56 +08:00
|
|
|
_data,
|
|
|
|
s,
|
|
|
|
t,
|
2002-04-12 01:15:07 +08:00
|
|
|
_dataType,
|
2001-09-20 05:08:56 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2002-08-27 18:06:57 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-14 17:34:11 +08:00
|
|
|
void Image::flipHorizontal(int image)
|
|
|
|
{
|
|
|
|
if (_data==NULL)
|
|
|
|
{
|
2002-05-14 18:20:55 +08:00
|
|
|
notify(WARN) << "Error Image::flipHorizontal() do not succeed : cannot flip NULL image."<<std::endl;
|
2002-05-14 17:34:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int elemSize = getPixelSizeInBits()/8;
|
|
|
|
|
|
|
|
for (int t=0; t<_t; ++t)
|
|
|
|
{
|
|
|
|
unsigned char* rowData = _data+t*getRowSizeInBytes()+image*getImageSizeInBytes();
|
|
|
|
unsigned char* left = rowData ;
|
|
|
|
unsigned char* right = rowData + ((_s-1)*getPixelSizeInBits())/8;
|
|
|
|
|
|
|
|
while (left < right)
|
|
|
|
{
|
|
|
|
char tmp[32]; // max elem size is four floats
|
|
|
|
memcpy(tmp, left, elemSize);
|
|
|
|
memcpy(left, right, elemSize);
|
|
|
|
memcpy(right, tmp, elemSize);
|
|
|
|
left += elemSize;
|
|
|
|
right -= elemSize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++_modifiedTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Image::flipVertical(int image)
|
|
|
|
{
|
|
|
|
if (_data==NULL)
|
|
|
|
{
|
|
|
|
notify(WARN) << "Error Image::flipVertical() do not succeed : cannot flip NULL image."<<std::endl;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int rowSizeInBytes = getRowSizeInBytes();
|
|
|
|
unsigned int imageSizeInBytes = getImageSizeInBytes();
|
|
|
|
unsigned char* imageData = _data+image*imageSizeInBytes;
|
|
|
|
|
|
|
|
// make temp. buffer for one image
|
|
|
|
unsigned char *tmpData = (unsigned char*) osgMalloc(imageSizeInBytes);
|
|
|
|
|
|
|
|
for (int t=0; t<_t; ++t)
|
|
|
|
{
|
|
|
|
unsigned char* srcRowData = imageData+t*rowSizeInBytes;
|
|
|
|
unsigned char* dstRowData = tmpData+(_t-1-t)*rowSizeInBytes;
|
|
|
|
memcpy(dstRowData, srcRowData, rowSizeInBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert fliped image
|
|
|
|
memcpy(imageData, tmpData, imageSizeInBytes);
|
|
|
|
|
2002-05-14 18:20:55 +08:00
|
|
|
osgFree(tmpData);
|
2002-05-14 17:34:11 +08:00
|
|
|
++_modifiedTag;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-04-15 06:21:59 +08:00
|
|
|
void Image::ensureValidSizeForTexturing()
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-08-16 21:33:32 +08:00
|
|
|
int new_s = computeNearestPowerOfTwo(_s);
|
|
|
|
int new_t = computeNearestPowerOfTwo(_t);
|
2001-10-05 18:38:16 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
static GLint max_size=Texture::getMaxTextureSize();
|
2002-03-18 02:44:47 +08:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-16 21:33:32 +08:00
|
|
|
void Image::computeMipMaps()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
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-08-25 03:39:39 +08:00
|
|
|
osg::Texture2D* texture = osgNew osg::Texture2D;
|
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);
|
2002-07-10 19:22:24 +08:00
|
|
|
dstate->setTextureAttributeAndModes(0, texture,osg::StateAttribute::ON);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
// set up the geoset.
|
2002-06-26 04:27:51 +08:00
|
|
|
Geometry* geom = osgNew Geometry;
|
|
|
|
geom->setStateSet(dstate);
|
|
|
|
|
|
|
|
Vec3Array* coords = osgNew Vec3Array(4);
|
|
|
|
(*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);
|
|
|
|
geom->setVertexArray(coords);
|
|
|
|
|
|
|
|
Vec2Array* tcoords = osgNew Vec2Array(4);
|
|
|
|
(*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);
|
|
|
|
geom->setTexCoordArray(0,tcoords);
|
|
|
|
|
|
|
|
osg::Vec4Array* colours = osgNew osg::Vec4Array(1);
|
|
|
|
(*colours)[0].set(1.0f,1.0f,1.0,1.0f);
|
|
|
|
geom->setColorArray(colours);
|
|
|
|
geom->setColorBinding(Geometry::BIND_OVERALL);
|
|
|
|
|
|
|
|
geom->addPrimitive(osgNew DrawArrays(Primitive::QUADS,0,4));
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
// set up the geode.
|
2002-03-27 07:52:52 +08:00
|
|
|
osg::Geode* geode = osgNew osg::Geode;
|
2002-06-26 04:27:51 +08:00
|
|
|
geode->addDrawable(geom);
|
2001-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
return geode;
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2002-08-16 21:33:32 +08:00
|
|
|
|