2001-09-20 05:08:56 +08:00
|
|
|
#if defined(_MSC_VER)
|
2002-03-20 19:24:47 +08:00
|
|
|
#pragma warning( disable : 4786 )
|
2001-09-20 05:08:56 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <osg/ref_ptr>
|
|
|
|
#include <osg/Image>
|
|
|
|
#include <osg/Texture>
|
|
|
|
#include <osg/State>
|
|
|
|
#include <osg/Notify>
|
|
|
|
#include <osg/GLExtensions>
|
|
|
|
|
2001-10-04 05:44:07 +08:00
|
|
|
#include <osg/GLU>
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-04-25 04:36:47 +08:00
|
|
|
typedef void (APIENTRY * MyCompressedTexImage2DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data);
|
2002-04-23 05:13:33 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
using namespace osg;
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
Texture::DeletedTextureObjectCache Texture::s_deletedTextureObjectCache;
|
|
|
|
|
2002-07-21 09:29:11 +08:00
|
|
|
Texture::Texture():
|
2002-07-29 07:28:27 +08:00
|
|
|
_unrefImageAfterApply(false),
|
2002-07-21 09:29:11 +08:00
|
|
|
_target(GL_TEXTURE_2D),
|
|
|
|
_wrap_s(CLAMP),
|
|
|
|
_wrap_t(CLAMP),
|
|
|
|
_wrap_r(CLAMP),
|
|
|
|
_min_filter(LINEAR_MIPMAP_LINEAR), // trilinear
|
|
|
|
_mag_filter(LINEAR),
|
|
|
|
_texParamtersDirty(true),
|
|
|
|
_internalFormatMode(USE_IMAGE_DATA_FORMAT),
|
|
|
|
_internalFormatValue(0),
|
|
|
|
_borderColor(0.0, 0.0, 0.0, 0.0),
|
|
|
|
_textureWidth(0),
|
|
|
|
_textureHeight(0),
|
|
|
|
_subloadMode(OFF),
|
2002-07-29 07:28:27 +08:00
|
|
|
_subloadTextureOffsetX(0),
|
|
|
|
_subloadTextureOffsetY(0),
|
|
|
|
_subloadImageOffsetX(0),
|
|
|
|
_subloadImageOffsetY(0),
|
|
|
|
_subloadImageWidth(0),
|
|
|
|
_subloadImageHeight(0)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2002-02-28 08:11:31 +08:00
|
|
|
_handleList.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
|
|
|
|
_modifiedTag.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
|
|
|
|
|
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-07-29 07:28:27 +08:00
|
|
|
StateAttribute(text,copyop),
|
|
|
|
_handleList(),
|
|
|
|
_modifiedTag(),
|
|
|
|
_image(copyop(text._image.get())),
|
|
|
|
_unrefImageAfterApply(text._unrefImageAfterApply),
|
|
|
|
_target(text._target),
|
|
|
|
_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),
|
|
|
|
_texParamtersDirty(false),
|
|
|
|
_internalFormatMode(text._internalFormatMode),
|
|
|
|
_internalFormatValue(text._internalFormatValue),
|
|
|
|
_borderColor(text._borderColor),
|
|
|
|
_textureWidth(text._textureWidth),
|
|
|
|
_textureHeight(text._textureHeight),
|
|
|
|
_subloadMode(text._subloadMode),
|
|
|
|
_subloadTextureOffsetX(text._subloadTextureOffsetX),
|
|
|
|
_subloadTextureOffsetY(text._subloadTextureOffsetY),
|
|
|
|
_subloadImageOffsetX(text._subloadImageOffsetX),
|
|
|
|
_subloadImageOffsetY(text._subloadImageOffsetY),
|
|
|
|
_subloadImageWidth(text._subloadImageWidth),
|
|
|
|
_subloadImageHeight(text._subloadImageHeight)
|
|
|
|
{}
|
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
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
// delete old texture objects.
|
|
|
|
dirtyTextureObject();
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2001-09-22 10:42:08 +08:00
|
|
|
int Texture::compare(const StateAttribute& sa) const
|
|
|
|
{
|
|
|
|
// check the types are equal and then create the rhs variable
|
|
|
|
// used by the COMPARE_StateAttribute_Paramter macro's below.
|
|
|
|
COMPARE_StateAttribute_Types(Texture,sa)
|
|
|
|
|
|
|
|
if (_image!=rhs._image) // smart pointer comparison.
|
|
|
|
{
|
|
|
|
if (_image.valid())
|
|
|
|
{
|
|
|
|
if (rhs._image.valid())
|
|
|
|
{
|
|
|
|
if (_image->getFileName()<rhs._image->getFileName()) return -1;
|
|
|
|
else if (_image->getFileName()>rhs._image->getFileName()) return 1;;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1; // valid lhs._image is greater than null.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rhs._image.valid())
|
|
|
|
{
|
|
|
|
return -1; // valid rhs._image is greater than null.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// compare each paramter in turn against the rhs.
|
|
|
|
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(_internalFormatMode)
|
|
|
|
COMPARE_StateAttribute_Parameter(_internalFormatValue)
|
|
|
|
COMPARE_StateAttribute_Parameter(_textureWidth)
|
|
|
|
COMPARE_StateAttribute_Parameter(_textureHeight)
|
|
|
|
COMPARE_StateAttribute_Parameter(_subloadMode)
|
2002-07-29 07:28:27 +08:00
|
|
|
COMPARE_StateAttribute_Parameter(_subloadTextureOffsetX)
|
|
|
|
COMPARE_StateAttribute_Parameter(_subloadTextureOffsetY)
|
|
|
|
COMPARE_StateAttribute_Parameter(_subloadImageOffsetX)
|
|
|
|
COMPARE_StateAttribute_Parameter(_subloadImageOffsetY)
|
|
|
|
COMPARE_StateAttribute_Parameter(_subloadImageWidth)
|
|
|
|
COMPARE_StateAttribute_Parameter(_subloadImageHeight)
|
2001-09-22 10:42:08 +08:00
|
|
|
|
|
|
|
return 0; // passed all the above comparison macro's, must be equal.
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
void Texture::setImage(Image* image)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
// delete old texture objects.
|
|
|
|
for(TextureNameList::iterator itr=_handleList.begin();
|
|
|
|
itr!=_handleList.end();
|
|
|
|
++itr)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
if (*itr != 0)
|
|
|
|
{
|
|
|
|
// contact global texture object handler to delete texture objects
|
|
|
|
// in appropriate context.
|
|
|
|
// glDeleteTextures( 1L, (const GLuint *)itr );
|
|
|
|
*itr = 0;
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
_image = image;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
void Texture::setWrap(const WrapParameter which, const WrapMode wrap)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
switch( which )
|
|
|
|
{
|
2002-03-15 00:01:21 +08:00
|
|
|
case WRAP_S : _wrap_s = wrap; _texParamtersDirty = true; break;
|
|
|
|
case WRAP_T : _wrap_t = wrap; _texParamtersDirty = true; break;
|
|
|
|
case WRAP_R : _wrap_r = wrap; _texParamtersDirty = true; break;
|
2001-12-15 07:18:28 +08:00
|
|
|
default : notify(WARN)<<"Error: invalid 'which' passed Texture::setWrap("<<(unsigned int)which<<","<<(unsigned int)wrap<<")"<<std::endl; break;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
2002-03-15 00:01:21 +08:00
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
const Texture::WrapMode Texture::getWrap(const WrapParameter which) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
switch( which )
|
|
|
|
{
|
|
|
|
case WRAP_S : return _wrap_s;
|
|
|
|
case WRAP_T : return _wrap_t;
|
|
|
|
case WRAP_R : return _wrap_r;
|
2001-12-15 07:18:28 +08:00
|
|
|
default : notify(WARN)<<"Error: invalid 'which' passed Texture::getWrap(which)"<<std::endl; return _wrap_s;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
void Texture::setFilter(const FilterParameter which, const FilterMode filter)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
switch( which )
|
|
|
|
{
|
2002-03-15 00:01:21 +08:00
|
|
|
case MIN_FILTER : _min_filter = filter; _texParamtersDirty = true; break;
|
|
|
|
case MAG_FILTER : _mag_filter = filter; _texParamtersDirty = true; break;
|
2001-12-15 07:18:28 +08:00
|
|
|
default : notify(WARN)<<"Error: invalid 'which' passed Texture::setFilter("<<(unsigned int)which<<","<<(unsigned int)filter<<")"<<std::endl; break;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
const Texture::FilterMode Texture::getFilter(const FilterParameter which) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
switch( which )
|
|
|
|
{
|
|
|
|
case MIN_FILTER : return _min_filter;
|
|
|
|
case MAG_FILTER : return _mag_filter;
|
2001-12-15 07:18:28 +08:00
|
|
|
default : notify(WARN)<<"Error: invalid 'which' passed Texture::getFilter(which)"<< std::endl; return _min_filter;
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
/** Force a recompile on next apply() of associated OpenGL texture objects.*/
|
|
|
|
void Texture::dirtyTextureObject()
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
for(uint i=0;i<_handleList.size();++i)
|
|
|
|
{
|
|
|
|
if (_handleList[i] != 0)
|
|
|
|
{
|
|
|
|
Texture::deleteTextureObject(i,_handleList[i]);
|
|
|
|
_handleList[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
void Texture::apply(State& state) const
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
|
2002-08-04 02:11:21 +08:00
|
|
|
// Texture* texture = const_cast<Texture*>(this);
|
|
|
|
// texture->_min_filter = LINEAR_MIPMAP_LINEAR;
|
|
|
|
// texture->_mag_filter = ANISOTROPIC;
|
|
|
|
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
// get the contextID (user defined ID of 0 upwards) for the
|
|
|
|
// current OpenGL context.
|
|
|
|
const uint contextID = state.getContextID();
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
// get the globj for the current contextID.
|
2001-10-04 05:44:07 +08:00
|
|
|
GLuint& handle = getHandle(contextID);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
if (handle != 0)
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
2001-09-20 05:08:56 +08:00
|
|
|
if (_subloadMode == OFF)
|
|
|
|
{
|
2002-03-20 19:24:47 +08:00
|
|
|
glBindTexture( _target, handle );
|
|
|
|
if (_texParamtersDirty) applyTexParameters(_target,state);
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
else if (_image.valid() && _image->data())
|
|
|
|
{
|
2002-07-09 17:35:42 +08:00
|
|
|
glBindTexture( _target, handle );
|
2002-07-21 02:27:40 +08:00
|
|
|
if (_texParamtersDirty) applyTexParameters(_target,state);
|
|
|
|
|
2002-02-28 08:11:31 +08:00
|
|
|
uint& modifiedTag = getModifiedTag(contextID);
|
2001-09-20 05:08:56 +08:00
|
|
|
if (_subloadMode == AUTO ||
|
2002-02-28 08:11:31 +08:00
|
|
|
(_subloadMode == IF_DIRTY && modifiedTag != _image->getModifiedTag()))
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2002-07-29 08:04:07 +08:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH,_image->s());
|
2002-07-29 07:28:27 +08:00
|
|
|
|
2002-03-20 19:24:47 +08:00
|
|
|
glTexSubImage2D(_target, 0,
|
2002-07-29 07:28:27 +08:00
|
|
|
_subloadTextureOffsetX, _subloadTextureOffsetY,
|
|
|
|
(_subloadImageWidth>0)?_subloadImageWidth:_image->s(), (_subloadImageHeight>0)?_subloadImageHeight:_image->t(),
|
2002-04-16 22:57:39 +08:00
|
|
|
(GLenum) _image->getPixelFormat(), (GLenum) _image->getDataType(),
|
2002-07-29 07:28:27 +08:00
|
|
|
_image->data(_subloadImageOffsetX,_subloadImageOffsetY));
|
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
// update the modified flag to show that the image has been loaded.
|
2002-02-28 08:11:31 +08:00
|
|
|
modifiedTag = _image->getModifiedTag();
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
}
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
2001-12-04 20:31:10 +08:00
|
|
|
else if (_image.valid() && _image->data())
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
glGenTextures( 1L, (GLuint *)&handle );
|
2002-03-20 19:24:47 +08:00
|
|
|
glBindTexture( _target, handle );
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-03-21 06:39:51 +08:00
|
|
|
applyTexParameters(_target,state);
|
2002-03-20 19:24:47 +08:00
|
|
|
applyTexImage(_target,_image.get(),state);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
// in theory the following line is redundent, but in practice
|
|
|
|
// have found that the first frame drawn doesn't apply the textures
|
|
|
|
// unless a second bind is called?!!
|
|
|
|
// perhaps it is the first glBind which is not required...
|
2002-03-20 19:24:47 +08:00
|
|
|
glBindTexture( _target, handle );
|
2002-07-29 05:25:32 +08:00
|
|
|
|
|
|
|
if (_unrefImageAfterApply)
|
|
|
|
_image = 0;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::compile(State& state) const
|
|
|
|
{
|
|
|
|
apply(state);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-20 19:24:47 +08:00
|
|
|
void Texture::applyTexParameters(GLenum target, State&) const
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2001-12-04 20:31:10 +08:00
|
|
|
WrapMode ws = _wrap_s, wt = _wrap_t;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-12-04 20:31:10 +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
|
|
|
}
|
2001-12-04 20:31:10 +08:00
|
|
|
|
|
|
|
// GL_EXT_texture_edge_clamp, fall-back CLAMP
|
|
|
|
static bool s_edgeClampSupported = isGLExtensionSupported("GL_EXT_texture_edge_clamp");
|
|
|
|
if (!s_edgeClampSupported)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
2001-12-04 20:31:10 +08:00
|
|
|
if (ws == CLAMP_TO_EDGE)
|
|
|
|
ws = CLAMP;
|
|
|
|
if (wt == CLAMP_TO_EDGE)
|
|
|
|
wt = CLAMP;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
2002-02-28 06:00:47 +08:00
|
|
|
static bool s_borderClampSupported = isGLExtensionSupported("GL_ARB_texture_border_clamp");
|
|
|
|
if(!s_borderClampSupported)
|
|
|
|
{
|
2002-03-15 00:01:21 +08:00
|
|
|
if(ws == CLAMP_TO_BORDER)
|
2002-02-28 06:00:47 +08:00
|
|
|
ws = CLAMP;
|
2002-03-15 00:01:21 +08:00
|
|
|
if(wt == CLAMP_TO_BORDER)
|
2002-02-28 06:00:47 +08:00
|
|
|
wt = CLAMP;
|
|
|
|
}
|
|
|
|
|
2002-03-20 19:24:47 +08:00
|
|
|
glTexParameteri( target, GL_TEXTURE_WRAP_S, ws );
|
|
|
|
glTexParameteri( target, GL_TEXTURE_WRAP_T, wt );
|
2001-12-04 20:31:10 +08:00
|
|
|
|
2002-03-20 19:24:47 +08:00
|
|
|
glTexParameteri( target, GL_TEXTURE_MIN_FILTER, _min_filter);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-03-01 17:27:10 +08:00
|
|
|
if (s_borderClampSupported)
|
|
|
|
{
|
2002-03-20 19:24:47 +08:00
|
|
|
glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, _borderColor.ptr());
|
2002-03-01 17:27:10 +08:00
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
if (_mag_filter == ANISOTROPIC)
|
|
|
|
{
|
|
|
|
// 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.
|
2002-03-20 19:24:47 +08:00
|
|
|
glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2.f);
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-03-20 19:24:47 +08:00
|
|
|
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, LINEAR);
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-03-20 19:24:47 +08:00
|
|
|
glTexParameteri( _target, GL_TEXTURE_MAG_FILTER, _mag_filter);
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
2002-03-15 00:01:21 +08:00
|
|
|
_texParamtersDirty=false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-03-20 19:24:47 +08:00
|
|
|
void Texture::applyTexImage(GLenum target, Image* image, State& state) const
|
2002-03-15 00:01:21 +08:00
|
|
|
{
|
|
|
|
// if we don't have a valid image we can't create a texture!
|
2002-03-21 06:39:51 +08:00
|
|
|
if (!image || !image->data())
|
2002-03-15 00:01:21 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
// get the contextID (user defined ID of 0 upwards) for the
|
|
|
|
// current OpenGL context.
|
|
|
|
const uint contextID = state.getContextID();
|
|
|
|
|
|
|
|
// update the modified tag to show that it is upto date.
|
2002-03-20 19:24:47 +08:00
|
|
|
getModifiedTag(contextID) = image->getModifiedTag();
|
2002-03-15 00:01:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
if (_subloadMode == OFF)
|
2002-04-15 06:21:59 +08:00
|
|
|
image->ensureValidSizeForTexturing();
|
2002-03-15 00:01:21 +08:00
|
|
|
|
2002-04-16 22:57:39 +08:00
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking());
|
2002-03-15 00:01:21 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
static bool s_ARB_Compression = isGLExtensionSupported("GL_ARB_texture_compression");
|
|
|
|
static bool s_S3TC_Compression = isGLExtensionSupported("GL_EXT_texture_compression_s3tc");
|
|
|
|
|
|
|
|
// select the internalFormat required for the texture.
|
2002-04-23 05:13:33 +08:00
|
|
|
bool compressed = false;
|
2002-04-16 22:57:39 +08:00
|
|
|
GLint internalFormat = image->getInternalTextureFormat();
|
2001-09-20 05:08:56 +08:00
|
|
|
switch(_internalFormatMode)
|
|
|
|
{
|
|
|
|
case(USE_IMAGE_DATA_FORMAT):
|
2002-04-16 22:57:39 +08:00
|
|
|
internalFormat = image->getInternalTextureFormat();
|
2001-09-20 05:08:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case(USE_ARB_COMPRESSION):
|
|
|
|
if (s_ARB_Compression)
|
|
|
|
{
|
2002-04-23 05:13:33 +08:00
|
|
|
compressed = true;
|
2002-04-16 22:57:39 +08:00
|
|
|
switch(image->getPixelFormat())
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2002-04-16 22:57:39 +08:00
|
|
|
else internalFormat = image->getInternalTextureFormat();
|
2001-09-20 05:08:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case(USE_S3TC_DXT1_COMPRESSION):
|
|
|
|
if (s_S3TC_Compression)
|
|
|
|
{
|
2002-04-23 05:13:33 +08:00
|
|
|
compressed = true;
|
2002-04-16 22:57:39 +08:00
|
|
|
switch(image->getPixelFormat())
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
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;
|
2002-04-16 22:57:39 +08:00
|
|
|
default: internalFormat = image->getInternalTextureFormat(); break;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
}
|
2002-04-16 22:57:39 +08:00
|
|
|
else internalFormat = image->getInternalTextureFormat();
|
2001-09-20 05:08:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case(USE_S3TC_DXT3_COMPRESSION):
|
|
|
|
if (s_S3TC_Compression)
|
|
|
|
{
|
2002-04-23 05:13:33 +08:00
|
|
|
compressed = true;
|
2002-04-16 22:57:39 +08:00
|
|
|
switch(image->getPixelFormat())
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
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;
|
2002-04-16 22:57:39 +08:00
|
|
|
default: internalFormat = _image->getInternalTextureFormat(); break;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
}
|
2002-04-16 22:57:39 +08:00
|
|
|
else internalFormat = image->getInternalTextureFormat();
|
2001-09-20 05:08:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case(USE_S3TC_DXT5_COMPRESSION):
|
|
|
|
if (s_S3TC_Compression)
|
|
|
|
{
|
2002-04-23 05:13:33 +08:00
|
|
|
compressed = true;
|
2002-04-16 22:57:39 +08:00
|
|
|
switch(image->getPixelFormat())
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
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;
|
2002-04-16 22:57:39 +08:00
|
|
|
default: internalFormat = image->getInternalTextureFormat(); break;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
}
|
2002-04-16 22:57:39 +08:00
|
|
|
else internalFormat = image->getInternalTextureFormat();
|
2001-09-20 05:08:56 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case(USE_USER_DEFINED_FORMAT):
|
|
|
|
internalFormat = _internalFormatValue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
2002-04-15 04:30:43 +08:00
|
|
|
|
|
|
|
// an experiment to look at the changes in performance
|
|
|
|
// when use 16 bit textures rather than 24/32bit textures.
|
|
|
|
// internalFormat = GL_RGBA4;
|
|
|
|
|
2002-06-25 05:37:34 +08:00
|
|
|
|
2002-04-25 04:36:47 +08:00
|
|
|
static MyCompressedTexImage2DArbProc glCompressedTexImage2D_ptr =
|
2002-04-25 16:54:48 +08:00
|
|
|
(MyCompressedTexImage2DArbProc)getGLExtensionFuncPtr("glCompressedTexImage2DARB");
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
if (_subloadMode == OFF) {
|
2001-01-11 00:32:10 +08:00
|
|
|
if( _min_filter == LINEAR || _min_filter == NEAREST )
|
|
|
|
{
|
2002-04-23 05:13:33 +08:00
|
|
|
if ( !compressed )
|
|
|
|
{
|
|
|
|
glTexImage2D( target, 0, internalFormat,
|
|
|
|
image->s(), image->t(), 0,
|
|
|
|
(GLenum)image->getPixelFormat(),
|
|
|
|
(GLenum)image->getDataType(),
|
|
|
|
image->data() );
|
|
|
|
|
|
|
|
}
|
|
|
|
else if(glCompressedTexImage2D_ptr)
|
|
|
|
{
|
|
|
|
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-01-11 00:32:10 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-04-23 05:13:33 +08:00
|
|
|
if(!image->isMipmap())
|
|
|
|
{
|
|
|
|
gluBuild2DMipmaps( target, internalFormat,
|
2002-03-20 19:24:47 +08:00
|
|
|
image->s(),image->t(),
|
2002-04-16 22:57:39 +08:00
|
|
|
(GLenum)image->getPixelFormat(), (GLenum)image->getDataType(),
|
2002-03-20 19:24:47 +08:00
|
|
|
image->data() );
|
2002-06-03 23:39:41 +08:00
|
|
|
|
2002-04-23 05:13:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t no_mipmaps = image->getNumMipmaps();
|
|
|
|
int width = image->s();
|
|
|
|
int height = image->t();
|
|
|
|
|
|
|
|
if( !compressed )
|
|
|
|
{
|
|
|
|
for( size_t k = 0 ; k < no_mipmaps && (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( size_t k = 0 ; k < no_mipmaps && (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
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
|
2002-03-20 19:24:47 +08:00
|
|
|
_textureWidth = image->s();
|
|
|
|
_textureHeight = image->t();
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
static bool s_SGIS_GenMipmap = isGLExtensionSupported("GL_SGIS_generate_mipmap");
|
|
|
|
|
|
|
|
if (s_SGIS_GenMipmap && (_min_filter != LINEAR && _min_filter != NEAREST)) {
|
2002-03-20 19:24:47 +08:00
|
|
|
glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
2002-03-28 06:51:58 +08:00
|
|
|
|
2002-07-29 07:28:27 +08:00
|
|
|
GLsizei width = (_subloadImageWidth>0)?_subloadImageWidth:image->s();
|
|
|
|
GLsizei height = (_subloadImageHeight>0)?_subloadImageHeight:image->t();
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-07-29 07:28:27 +08:00
|
|
|
if (_textureWidth==0)
|
|
|
|
{
|
|
|
|
// need to calculate texture dimension
|
|
|
|
_textureWidth = 1;
|
|
|
|
for (; _textureWidth < (static_cast<GLsizei>(_subloadTextureOffsetX) + width); _textureWidth <<= 1) {}
|
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-07-29 07:28:27 +08:00
|
|
|
if (_textureHeight==0)
|
|
|
|
{
|
|
|
|
// need to calculate texture dimension
|
|
|
|
_textureHeight = 1;
|
|
|
|
for (; _textureHeight < (static_cast<GLsizei>(_subloadTextureOffsetY) + height); _textureHeight <<= 1) {}
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
// reserve appropriate texture memory
|
2002-03-20 19:24:47 +08:00
|
|
|
glTexImage2D(target, 0, internalFormat,
|
2001-09-20 05:08:56 +08:00
|
|
|
_textureWidth, _textureHeight, 0,
|
2002-04-16 22:57:39 +08:00
|
|
|
(GLenum) image->getPixelFormat(), (GLenum) image->getDataType(),
|
2001-09-20 05:08:56 +08:00
|
|
|
NULL);
|
|
|
|
|
2002-07-29 07:28:27 +08:00
|
|
|
|
2002-07-29 08:04:07 +08:00
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH,image->s());
|
2002-07-29 07:28:27 +08:00
|
|
|
|
2002-03-20 19:24:47 +08:00
|
|
|
glTexSubImage2D(target, 0,
|
2002-07-29 07:28:27 +08:00
|
|
|
_subloadTextureOffsetX, _subloadTextureOffsetY,
|
2002-03-28 06:51:58 +08:00
|
|
|
width, height,
|
2002-04-16 22:57:39 +08:00
|
|
|
(GLenum) image->getPixelFormat(), (GLenum) image->getDataType(),
|
2002-07-29 07:28:27 +08:00
|
|
|
image->data(_subloadImageOffsetX,_subloadImageOffsetY));
|
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
|
2001-09-20 05:08:56 +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.*/
|
2001-09-22 10:42:08 +08:00
|
|
|
void Texture::deleteTextureObject(uint contextID,GLuint handle)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
if (handle!=0)
|
|
|
|
{
|
|
|
|
// insert the handle into the cache for the appropriate context.
|
|
|
|
s_deletedTextureObjectCache[contextID].insert(handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 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-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
void Texture::copyTexImage2D(State& state, int x, int y, int width, int height )
|
|
|
|
{
|
|
|
|
const uint contextID = state.getContextID();
|
|
|
|
|
|
|
|
// get the globj for the current contextID.
|
2001-09-22 10:42:08 +08:00
|
|
|
GLuint& handle = getHandle(contextID);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
if (handle)
|
|
|
|
{
|
|
|
|
if (width==(int)_textureWidth && height==(int)_textureHeight)
|
|
|
|
{
|
|
|
|
// we have a valid texture object which is the right size
|
|
|
|
// so lets play clever and use copyTexSubImage2D instead.
|
|
|
|
// this allows use to reuse the texture object and avoid
|
|
|
|
// expensive memory allocations.
|
|
|
|
copyTexSubImage2D(state,0 ,0, x, y, width, height);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// the relevent texture object is not of the right size so
|
|
|
|
// needs to been deleted
|
|
|
|
// remove previously bound textures.
|
|
|
|
dirtyTextureObject();
|
|
|
|
// note, dirtyTextureObject() dirties all the texture objects for
|
|
|
|
// this texture, is this right? Perhaps we should dirty just the
|
|
|
|
// one for this context. Note sure yet will leave till later.
|
|
|
|
// RO July 2001.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// remove any previously assigned images as these are nolonger valid.
|
|
|
|
_image = NULL;
|
|
|
|
|
|
|
|
// switch off mip-mapping.
|
|
|
|
_min_filter = LINEAR;
|
|
|
|
_mag_filter = LINEAR;
|
|
|
|
|
|
|
|
// Get a new 2d texture handle.
|
|
|
|
glGenTextures( 1, &handle );
|
|
|
|
|
2002-03-20 19:24:47 +08:00
|
|
|
glBindTexture( _target, handle );
|
|
|
|
applyTexParameters(_target,state);
|
|
|
|
glCopyTexImage2D( _target, 0, GL_RGBA, x, y, width, height, 0 );
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Redundant, delete later */
|
2002-03-20 19:24:47 +08:00
|
|
|
// glBindTexture( _target, handle );
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
_textureWidth = width;
|
|
|
|
_textureHeight = height;
|
|
|
|
|
2001-12-15 07:18:28 +08:00
|
|
|
// cout<<"copyTexImage2D x="<<x<<" y="<<y<<" w="<<width<<" h="<<height<< std::endl;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
// inform state that this texture is the current one bound.
|
2002-03-21 20:36:05 +08:00
|
|
|
state.haveAppliedAttribute(this);
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Texture::copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, int y, int width, int height )
|
|
|
|
{
|
|
|
|
const uint contextID = state.getContextID();
|
|
|
|
|
|
|
|
// get the globj for the current contextID.
|
2001-10-04 05:44:07 +08:00
|
|
|
GLuint& handle = getHandle(contextID);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
if (handle)
|
|
|
|
{
|
2002-07-07 22:40:41 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
// we have a valid image
|
2002-03-20 19:24:47 +08:00
|
|
|
glBindTexture( _target, handle );
|
|
|
|
applyTexParameters(_target,state);
|
|
|
|
glCopyTexSubImage2D( _target, 0, xoffset,yoffset, x, y, width, height);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
/* Redundant, delete later */
|
2002-03-20 19:24:47 +08:00
|
|
|
glBindTexture( _target, handle );
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
// inform state that this texture is the current one bound.
|
2002-03-21 20:36:05 +08:00
|
|
|
state.haveAppliedAttribute(this);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// no texture object already exsits for this context so need to
|
|
|
|
// create it upfront - simply call copyTexImage2D.
|
|
|
|
copyTexImage2D(state,x,y,width,height);
|
2001-01-11 00:32:10 +08:00
|
|
|
}
|
|
|
|
}
|