2002-08-25 03:39:39 +08:00
|
|
|
#include <osg/GLExtensions>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osg/Image>
|
2002-08-28 23:28:11 +08:00
|
|
|
#include <osg/Texture>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osg/State>
|
|
|
|
#include <osg/Notify>
|
2001-10-04 05:44:07 +08:00
|
|
|
#include <osg/GLU>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
typedef void (APIENTRY * MyCompressedTexImage2DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data);
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
using namespace osg;
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
Texture::DeletedTextureObjectCache Texture::s_deletedTextureObjectCache;
|
|
|
|
|
2002-07-21 09:29:11 +08:00
|
|
|
Texture::Texture():
|
2002-08-28 23:28:11 +08:00
|
|
|
_wrap_s(CLAMP),
|
|
|
|
_wrap_t(CLAMP),
|
|
|
|
_wrap_r(CLAMP),
|
|
|
|
_min_filter(LINEAR_MIPMAP_LINEAR), // trilinear
|
|
|
|
_mag_filter(LINEAR),
|
|
|
|
_maxAnisotropy(1.0f),
|
|
|
|
_borderColor(0.0, 0.0, 0.0, 0.0),
|
|
|
|
_internalFormatMode(USE_IMAGE_DATA_FORMAT),
|
|
|
|
_internalFormat(0)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
_handleList.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
|
|
|
|
_modifiedTag.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
|
2002-09-04 16:14:04 +08:00
|
|
|
_texParametersDirtyList.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),true);
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2002-07-29 19:02:14 +08:00
|
|
|
Texture::Texture(const Texture& text,const CopyOp& copyop):
|
2002-08-28 23:28:11 +08:00
|
|
|
StateAttribute(text,copyop),
|
|
|
|
_wrap_s(text._wrap_s),
|
|
|
|
_wrap_t(text._wrap_t),
|
|
|
|
_wrap_r(text._wrap_r),
|
|
|
|
_min_filter(text._min_filter),
|
|
|
|
_mag_filter(text._mag_filter),
|
|
|
|
_maxAnisotropy(text._maxAnisotropy),
|
|
|
|
_borderColor(text._borderColor),
|
|
|
|
_internalFormatMode(text._internalFormatMode),
|
|
|
|
_internalFormat(text._internalFormat)
|
|
|
|
{
|
|
|
|
_handleList.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
|
|
|
|
_modifiedTag.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
|
2002-09-04 16:14:04 +08:00
|
|
|
_texParametersDirtyList.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),true);
|
2002-08-28 23:28:11 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
Texture::~Texture()
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
// delete old texture objects.
|
|
|
|
dirtyTextureObject();
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
int Texture::compareTexture(const Texture& rhs) const
|
2001-09-22 10:42:08 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
COMPARE_StateAttribute_Parameter(_wrap_s)
|
|
|
|
COMPARE_StateAttribute_Parameter(_wrap_t)
|
|
|
|
COMPARE_StateAttribute_Parameter(_wrap_r)
|
|
|
|
COMPARE_StateAttribute_Parameter(_min_filter)
|
|
|
|
COMPARE_StateAttribute_Parameter(_mag_filter)
|
|
|
|
COMPARE_StateAttribute_Parameter(_maxAnisotropy)
|
|
|
|
COMPARE_StateAttribute_Parameter(_internalFormatMode)
|
|
|
|
COMPARE_StateAttribute_Parameter(_internalFormat)
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
void Texture::setWrap(WrapParameter which, WrapMode wrap)
|
2002-08-28 23:28:11 +08:00
|
|
|
{
|
|
|
|
switch( which )
|
2001-09-22 10:42:08 +08:00
|
|
|
{
|
2002-09-04 16:14:04 +08:00
|
|
|
case WRAP_S : _wrap_s = wrap; dirtyTextureParameters(); break;
|
|
|
|
case WRAP_T : _wrap_t = wrap; dirtyTextureParameters(); break;
|
|
|
|
case WRAP_R : _wrap_r = wrap; dirtyTextureParameters(); break;
|
2002-08-28 23:28:11 +08:00
|
|
|
default : notify(WARN)<<"Error: invalid 'which' passed Texture::setWrap("<<(unsigned int)which<<","<<(unsigned int)wrap<<")"<<std::endl; break;
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
Texture::WrapMode Texture::getWrap(WrapParameter which) const
|
2002-08-28 23:28:11 +08:00
|
|
|
{
|
|
|
|
switch( which )
|
|
|
|
{
|
|
|
|
case WRAP_S : return _wrap_s;
|
|
|
|
case WRAP_T : return _wrap_t;
|
|
|
|
case WRAP_R : return _wrap_r;
|
|
|
|
default : notify(WARN)<<"Error: invalid 'which' passed Texture::getWrap(which)"<<std::endl; return _wrap_s;
|
|
|
|
}
|
|
|
|
}
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
void Texture::setFilter(FilterParameter which, FilterMode filter)
|
2002-08-28 23:28:11 +08:00
|
|
|
{
|
|
|
|
switch( which )
|
|
|
|
{
|
2002-09-04 16:14:04 +08:00
|
|
|
case MIN_FILTER : _min_filter = filter; dirtyTextureParameters(); break;
|
|
|
|
case MAG_FILTER : _mag_filter = filter; dirtyTextureParameters(); break;
|
2002-08-28 23:28:11 +08:00
|
|
|
default : notify(WARN)<<"Error: invalid 'which' passed Texture::setFilter("<<(unsigned int)which<<","<<(unsigned int)filter<<")"<<std::endl; break;
|
|
|
|
}
|
|
|
|
}
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
Texture::FilterMode Texture::getFilter(FilterParameter which) const
|
2002-08-28 23:28:11 +08:00
|
|
|
{
|
|
|
|
switch( which )
|
|
|
|
{
|
|
|
|
case MIN_FILTER : return _min_filter;
|
|
|
|
case MAG_FILTER : return _mag_filter;
|
|
|
|
default : notify(WARN)<<"Error: invalid 'which' passed Texture::getFilter(which)"<< std::endl; return _min_filter;
|
|
|
|
}
|
2001-09-22 10:42:08 +08:00
|
|
|
}
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
void Texture::setMaxAnisotropy(float anis)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
if (_maxAnisotropy!=anis)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
_maxAnisotropy = anis;
|
2002-09-04 16:14:04 +08:00
|
|
|
dirtyTextureParameters();
|
2002-08-28 23:28:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Force a recompile on next apply() of associated OpenGL texture objects.*/
|
|
|
|
void Texture::dirtyTextureObject()
|
|
|
|
{
|
|
|
|
for(uint i=0;i<_handleList.size();++i)
|
|
|
|
{
|
|
|
|
if (_handleList[i] != 0)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
Texture::deleteTextureObject(i,_handleList[i]);
|
|
|
|
_handleList[i] = 0;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-04 16:14:04 +08:00
|
|
|
void Texture::dirtyTextureParameters()
|
|
|
|
{
|
|
|
|
for(TexParameterDirtyList::iterator itr=_texParametersDirtyList.begin();
|
|
|
|
itr!=_texParametersDirtyList.end();
|
|
|
|
++itr)
|
|
|
|
{
|
|
|
|
*itr = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
void Texture::computeInternalFormatWithImage(osg::Image& image) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
static bool s_ARB_Compression = isGLExtensionSupported("GL_ARB_texture_compression");
|
|
|
|
static bool s_S3TC_Compression = isGLExtensionSupported("GL_EXT_texture_compression_s3tc");
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
GLint internalFormat = image.getInternalTextureFormat();
|
|
|
|
switch(_internalFormatMode)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
case(USE_IMAGE_DATA_FORMAT):
|
|
|
|
internalFormat = image.getInternalTextureFormat();
|
|
|
|
break;
|
2002-07-21 02:27:40 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
case(USE_ARB_COMPRESSION):
|
|
|
|
if (s_ARB_Compression)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
switch(image.getPixelFormat())
|
|
|
|
{
|
|
|
|
case(1): internalFormat = GL_COMPRESSED_ALPHA_ARB; break;
|
|
|
|
case(2): internalFormat = GL_COMPRESSED_LUMINANCE_ALPHA_ARB; break;
|
|
|
|
case(3): internalFormat = GL_COMPRESSED_RGB_ARB; break;
|
|
|
|
case(4): internalFormat = GL_COMPRESSED_RGBA_ARB; break;
|
|
|
|
case(GL_RGB): internalFormat = GL_COMPRESSED_RGB_ARB; break;
|
|
|
|
case(GL_RGBA): internalFormat = GL_COMPRESSED_RGBA_ARB; break;
|
|
|
|
case(GL_ALPHA): internalFormat = GL_COMPRESSED_ALPHA_ARB; break;
|
|
|
|
case(GL_LUMINANCE): internalFormat = GL_COMPRESSED_LUMINANCE_ARB; break;
|
|
|
|
case(GL_LUMINANCE_ALPHA): internalFormat = GL_COMPRESSED_LUMINANCE_ALPHA_ARB; break;
|
|
|
|
case(GL_INTENSITY): internalFormat = GL_COMPRESSED_INTENSITY_ARB; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else internalFormat = image.getInternalTextureFormat();
|
|
|
|
break;
|
2002-07-29 07:28:27 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
case(USE_S3TC_DXT1_COMPRESSION):
|
|
|
|
if (s_S3TC_Compression)
|
|
|
|
{
|
|
|
|
switch(image.getPixelFormat())
|
|
|
|
{
|
|
|
|
case(3): internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; break;
|
|
|
|
case(4): internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break;
|
|
|
|
case(GL_RGB): internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; break;
|
|
|
|
case(GL_RGBA): internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break;
|
|
|
|
default: internalFormat = image.getInternalTextureFormat(); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else internalFormat = image.getInternalTextureFormat();
|
|
|
|
break;
|
2002-07-29 07:28:27 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
case(USE_S3TC_DXT3_COMPRESSION):
|
|
|
|
if (s_S3TC_Compression)
|
|
|
|
{
|
|
|
|
switch(image.getPixelFormat())
|
|
|
|
{
|
|
|
|
case(3):
|
|
|
|
case(GL_RGB): internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; break;
|
|
|
|
case(4):
|
|
|
|
case(GL_RGBA): internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break;
|
|
|
|
default: internalFormat = image.getInternalTextureFormat(); break;
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
2002-08-28 23:28:11 +08:00
|
|
|
else internalFormat = image.getInternalTextureFormat();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case(USE_S3TC_DXT5_COMPRESSION):
|
|
|
|
if (s_S3TC_Compression)
|
2002-08-16 21:33:32 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
switch(image.getPixelFormat())
|
|
|
|
{
|
|
|
|
case(3):
|
|
|
|
case(GL_RGB): internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; break;
|
|
|
|
case(4):
|
|
|
|
case(GL_RGBA): internalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break;
|
|
|
|
default: internalFormat = image.getInternalTextureFormat(); break;
|
|
|
|
}
|
2002-08-16 21:33:32 +08:00
|
|
|
}
|
2002-08-28 23:28:11 +08:00
|
|
|
else internalFormat = image.getInternalTextureFormat();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case(USE_USER_DEFINED_FORMAT):
|
|
|
|
internalFormat = _internalFormat;
|
|
|
|
break;
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
_internalFormat = internalFormat;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Texture::isCompressedInternalFormat(GLint internalFormat) const
|
|
|
|
{
|
|
|
|
switch(internalFormat)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
case(GL_COMPRESSED_ALPHA_ARB):
|
|
|
|
case(GL_COMPRESSED_INTENSITY_ARB):
|
|
|
|
case(GL_COMPRESSED_LUMINANCE_ALPHA_ARB):
|
|
|
|
case(GL_COMPRESSED_LUMINANCE_ARB):
|
|
|
|
case(GL_COMPRESSED_RGBA_ARB):
|
|
|
|
case(GL_COMPRESSED_RGB_ARB):
|
|
|
|
case(GL_COMPRESSED_RGBA_S3TC_DXT1_EXT):
|
|
|
|
case(GL_COMPRESSED_RGBA_S3TC_DXT3_EXT):
|
|
|
|
case(GL_COMPRESSED_RGBA_S3TC_DXT5_EXT):
|
|
|
|
case(GL_COMPRESSED_RGB_S3TC_DXT1_EXT):
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-09-04 16:14:04 +08:00
|
|
|
void Texture::applyTexParameters(GLenum target, State& state) const
|
2002-08-28 23:28:11 +08:00
|
|
|
{
|
|
|
|
WrapMode ws = _wrap_s, wt = _wrap_t;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
// GL_IBM_texture_mirrored_repeat, fall-back REPEAT
|
|
|
|
static bool s_mirroredSupported = isGLExtensionSupported("GL_IBM_texture_mirrored_repeat");
|
|
|
|
if (!s_mirroredSupported)
|
|
|
|
{
|
|
|
|
if (ws == MIRROR)
|
|
|
|
ws = REPEAT;
|
|
|
|
if (wt == MIRROR)
|
|
|
|
wt = REPEAT;
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
// GL_EXT_texture_edge_clamp, fall-back CLAMP
|
|
|
|
static bool s_edgeClampSupported = isGLExtensionSupported("GL_EXT_texture_edge_clamp");
|
|
|
|
if (!s_edgeClampSupported)
|
|
|
|
{
|
|
|
|
if (ws == CLAMP_TO_EDGE)
|
|
|
|
ws = CLAMP;
|
|
|
|
if (wt == CLAMP_TO_EDGE)
|
|
|
|
wt = CLAMP;
|
|
|
|
}
|
2001-12-04 20:31:10 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
static bool s_borderClampSupported = isGLExtensionSupported("GL_ARB_texture_border_clamp");
|
|
|
|
if(!s_borderClampSupported)
|
|
|
|
{
|
|
|
|
if(ws == CLAMP_TO_BORDER)
|
|
|
|
ws = CLAMP;
|
|
|
|
if(wt == CLAMP_TO_BORDER)
|
|
|
|
wt = CLAMP;
|
2002-08-16 21:33:32 +08:00
|
|
|
}
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
glTexParameteri( target, GL_TEXTURE_WRAP_S, ws );
|
|
|
|
glTexParameteri( target, GL_TEXTURE_WRAP_T, wt );
|
|
|
|
|
|
|
|
glTexParameteri( target, GL_TEXTURE_MIN_FILTER, _min_filter);
|
|
|
|
glTexParameteri( target, GL_TEXTURE_MAG_FILTER, _mag_filter);
|
|
|
|
|
|
|
|
if (_maxAnisotropy>1.0f)
|
|
|
|
{
|
|
|
|
// check for support for anisotropic filter,
|
|
|
|
// note since this is static varible it is intialised
|
|
|
|
// only on the first time entering this code block,
|
|
|
|
// is then never reevaluated on subsequent calls.
|
|
|
|
static bool s_anisotropicSupported =
|
|
|
|
isGLExtensionSupported("GL_EXT_texture_filter_anisotropic");
|
|
|
|
|
|
|
|
if (s_anisotropicSupported)
|
|
|
|
{
|
|
|
|
// note, GL_TEXTURE_MAX_ANISOTROPY_EXT will either be defined
|
|
|
|
// by gl.h (or via glext.h) or by include/osg/Texture.
|
|
|
|
glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, _maxAnisotropy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s_borderClampSupported)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, _borderColor.ptr());
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
2002-09-04 16:14:04 +08:00
|
|
|
getTextureParameterDity(state.getContextID()) = false;
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
void Texture::applyTexImage2D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight,GLsizei& numMimpmapLevels) const
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
// if we don't have a valid image we can't create a texture!
|
|
|
|
if (!image || !image->data())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// get the contextID (user defined ID of 0 upwards) for the
|
|
|
|
// current OpenGL context.
|
2001-09-20 05:08:56 +08:00
|
|
|
const uint contextID = state.getContextID();
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
// update the modified tag to show that it is upto date.
|
|
|
|
getModifiedTag(contextID) = image->getModifiedTag();
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
// compute the internal texture format, this set the _internalFormat to an appropriate value.
|
|
|
|
computeInternalFormat();
|
|
|
|
|
|
|
|
// select the internalFormat required for the texture.
|
|
|
|
bool compressed = isCompressedInternalFormat(_internalFormat);
|
|
|
|
|
|
|
|
image->ensureValidSizeForTexturing();
|
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking());
|
|
|
|
|
|
|
|
static MyCompressedTexImage2DArbProc glCompressedTexImage2D_ptr =
|
|
|
|
(MyCompressedTexImage2DArbProc)getGLExtensionFuncPtr("glCompressedTexImage2DARB");
|
|
|
|
|
|
|
|
if( _min_filter == LINEAR || _min_filter == NEAREST )
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
if ( !compressed )
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
numMimpmapLevels = 1;
|
|
|
|
glTexImage2D( target, 0, _internalFormat,
|
|
|
|
image->s(), image->t(), 0,
|
|
|
|
(GLenum)image->getPixelFormat(),
|
|
|
|
(GLenum)image->getDataType(),
|
|
|
|
image->data() );
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(glCompressedTexImage2D_ptr)
|
|
|
|
{
|
|
|
|
numMimpmapLevels = 1;
|
|
|
|
GLint blockSize = ( _internalFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ? 8 : 16 );
|
|
|
|
GLint size = ((image->s()+3)/4)*((image->t()+3)/4)*blockSize;
|
|
|
|
glCompressedTexImage2D_ptr(target, 0, _internalFormat,
|
|
|
|
image->s(), image->t(),0,
|
|
|
|
size,
|
|
|
|
image->data());
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
2002-08-28 23:28:11 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
2002-08-28 23:28:11 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!image->isMipmap())
|
|
|
|
{
|
|
|
|
|
|
|
|
numMimpmapLevels = 1;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
gluBuild2DMipmaps( target, _internalFormat,
|
|
|
|
image->s(),image->t(),
|
|
|
|
(GLenum)image->getPixelFormat(), (GLenum)image->getDataType(),
|
|
|
|
image->data() );
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
numMimpmapLevels = image->getNumMipmapLevels();
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
int width = image->s();
|
|
|
|
int height = image->t();
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
if( !compressed )
|
|
|
|
{
|
|
|
|
for( GLsizei k = 0 ; k < numMimpmapLevels && (width || height) ;k++)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (width == 0)
|
|
|
|
width = 1;
|
|
|
|
if (height == 0)
|
|
|
|
height = 1;
|
|
|
|
|
|
|
|
glTexImage2D( target, k, _internalFormat,
|
|
|
|
width, height, 0,
|
|
|
|
(GLenum)image->getPixelFormat(),
|
|
|
|
(GLenum)image->getDataType(),
|
|
|
|
image->getMipmapData(k));
|
|
|
|
|
|
|
|
width >>= 1;
|
|
|
|
height >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(glCompressedTexImage2D_ptr)
|
|
|
|
{
|
|
|
|
GLint blockSize = ( _internalFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ? 8 : 16 );
|
|
|
|
GLint size = 0;
|
|
|
|
for( GLsizei k = 0 ; k < numMimpmapLevels && (width || height) ;k++)
|
|
|
|
{
|
|
|
|
if (width == 0)
|
|
|
|
width = 1;
|
|
|
|
if (height == 0)
|
|
|
|
height = 1;
|
|
|
|
|
|
|
|
size = ((width+3)/4)*((height+3)/4)*blockSize;
|
|
|
|
glCompressedTexImage2D_ptr(target, k, _internalFormat,
|
|
|
|
width, height, 0, size, image->getMipmapData(k));
|
|
|
|
|
|
|
|
width >>= 1;
|
|
|
|
height >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
inwidth = image->s();
|
|
|
|
inheight = image->t();
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** use deleteTextureObject instead of glDeleteTextures to allow
|
|
|
|
* OpenGL texture objects to cached until they can be deleted
|
|
|
|
* by the OpenGL context in which they were created, specified
|
|
|
|
* by contextID.*/
|
|
|
|
void Texture::deleteTextureObject(uint contextID,GLuint handle)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
if (handle!=0)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
// insert the handle into the cache for the appropriate context.
|
|
|
|
s_deletedTextureObjectCache[contextID].insert(handle);
|
|
|
|
}
|
|
|
|
}
|
2002-07-07 22:40:41 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** flush all the cached display list which need to be deleted
|
|
|
|
* in the OpenGL context related to contextID.*/
|
|
|
|
void Texture::flushDeletedTextureObjects(uint contextID)
|
|
|
|
{
|
|
|
|
DeletedTextureObjectCache::iterator citr = s_deletedTextureObjectCache.find(contextID);
|
|
|
|
if (citr!=s_deletedTextureObjectCache.end())
|
|
|
|
{
|
|
|
|
std::set<uint>& textureObjectSet = citr->second;
|
|
|
|
for(std::set<uint>::iterator titr=textureObjectSet.begin();
|
|
|
|
titr!=textureObjectSet.end();
|
|
|
|
++titr)
|
|
|
|
{
|
|
|
|
glDeleteTextures( 1L, (const GLuint *)&(*titr ));
|
|
|
|
}
|
|
|
|
s_deletedTextureObjectCache.erase(citr);
|
|
|
|
}
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
GLint Texture::getMaxTextureSize()
|
|
|
|
{
|
|
|
|
static GLint s_maxTextureSize = 0;
|
|
|
|
if (s_maxTextureSize == 0)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE,&s_maxTextureSize);
|
|
|
|
notify(INFO) << "GL_MAX_TEXTURE_SIZE "<<s_maxTextureSize<<std::endl;
|
|
|
|
|
|
|
|
char *ptr;
|
|
|
|
if( (ptr = getenv("OSG_MAX_TEXTURE_SIZE")) != 0)
|
|
|
|
{
|
|
|
|
GLint osg_max_size = atoi(ptr);
|
|
|
|
|
|
|
|
notify(INFO) << "OSG_MAX_TEXTURE_SIZE "<<osg_max_size<<std::endl;
|
|
|
|
|
|
|
|
if (osg_max_size<s_maxTextureSize)
|
|
|
|
{
|
|
|
|
|
|
|
|
s_maxTextureSize = osg_max_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
notify(INFO) << "Selected max texture size "<<s_maxTextureSize<<std::endl;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
2002-08-28 23:28:11 +08:00
|
|
|
return s_maxTextureSize;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
2002-08-16 21:33:32 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
void Texture::compile(State& state) const
|
|
|
|
{
|
|
|
|
apply(state);
|
|
|
|
}
|