Updates to Texture and TextureCubeMap to clean up the code so they share
much more of the core texture setup code. This largely invloved paramterizing the applyImmediateMode, which has also been rename applyTexImage to reflect its functionality better.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#if defined(_MSC_VER)
|
||||
#pragma warning( disable : 4786 )
|
||||
#pragma warning( disable : 4786 )
|
||||
#endif
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
@@ -21,6 +21,8 @@ Texture::Texture()
|
||||
_handleList.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
|
||||
_modifiedTag.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
|
||||
|
||||
_target = GL_TEXTURE_2D;
|
||||
|
||||
_textureUnit = 0;
|
||||
|
||||
_wrap_s = CLAMP;
|
||||
@@ -193,8 +195,8 @@ void Texture::apply(State& state) const
|
||||
{
|
||||
if (_subloadMode == OFF)
|
||||
{
|
||||
glBindTexture( GL_TEXTURE_2D, handle );
|
||||
if (_texParamtersDirty) applyTexParameters(state);
|
||||
glBindTexture( _target, handle );
|
||||
if (_texParamtersDirty) applyTexParameters(_target,state);
|
||||
}
|
||||
else if (_image.valid() && _image->data())
|
||||
{
|
||||
@@ -203,9 +205,9 @@ void Texture::apply(State& state) const
|
||||
if (_subloadMode == AUTO ||
|
||||
(_subloadMode == IF_DIRTY && modifiedTag != _image->getModifiedTag()))
|
||||
{
|
||||
glBindTexture( GL_TEXTURE_2D, handle );
|
||||
if (_texParamtersDirty) applyTexParameters(state);
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0,
|
||||
glBindTexture( _target, handle );
|
||||
if (_texParamtersDirty) applyTexParameters(_target,state);
|
||||
glTexSubImage2D(_target, 0,
|
||||
_subloadOffsX, _subloadOffsY,
|
||||
_image->s(), _image->t(),
|
||||
(GLenum) _image->pixelFormat(), (GLenum) _image->dataType(),
|
||||
@@ -219,15 +221,15 @@ void Texture::apply(State& state) const
|
||||
{
|
||||
|
||||
glGenTextures( 1L, (GLuint *)&handle );
|
||||
glBindTexture( GL_TEXTURE_2D, handle );
|
||||
glBindTexture( _target, handle );
|
||||
|
||||
applyImmediateMode(state);
|
||||
applyTexImage(_target,_image.get(),state);
|
||||
|
||||
// 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...
|
||||
glBindTexture( GL_TEXTURE_2D, handle );
|
||||
glBindTexture( _target, handle );
|
||||
|
||||
}
|
||||
}
|
||||
@@ -238,7 +240,7 @@ void Texture::compile(State& state) const
|
||||
}
|
||||
|
||||
|
||||
void Texture::applyTexParameters(State&) const
|
||||
void Texture::applyTexParameters(GLenum target, State&) const
|
||||
{
|
||||
WrapMode ws = _wrap_s, wt = _wrap_t;
|
||||
|
||||
@@ -271,14 +273,14 @@ void Texture::applyTexParameters(State&) const
|
||||
wt = CLAMP;
|
||||
}
|
||||
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, ws );
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wt );
|
||||
glTexParameteri( target, GL_TEXTURE_WRAP_S, ws );
|
||||
glTexParameteri( target, GL_TEXTURE_WRAP_T, wt );
|
||||
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, _min_filter);
|
||||
glTexParameteri( target, GL_TEXTURE_MIN_FILTER, _min_filter);
|
||||
|
||||
if (s_borderClampSupported)
|
||||
{
|
||||
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, _borderColor.ptr());
|
||||
glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, _borderColor.ptr());
|
||||
}
|
||||
|
||||
if (_mag_filter == ANISOTROPIC)
|
||||
@@ -294,26 +296,26 @@ void Texture::applyTexParameters(State&) const
|
||||
{
|
||||
// note, GL_TEXTURE_MAX_ANISOTROPY_EXT will either be defined
|
||||
// by gl.h (or via glext.h) or by include/osg/Texture.
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2.f);
|
||||
glTexParameterf(target, GL_TEXTURE_MAX_ANISOTROPY_EXT, 2.f);
|
||||
}
|
||||
else
|
||||
{
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, LINEAR);
|
||||
glTexParameteri(target, GL_TEXTURE_MAG_FILTER, LINEAR);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, _mag_filter);
|
||||
glTexParameteri( _target, GL_TEXTURE_MAG_FILTER, _mag_filter);
|
||||
}
|
||||
|
||||
_texParamtersDirty=false;
|
||||
|
||||
}
|
||||
|
||||
void Texture::applyImmediateMode(State& state) const
|
||||
void Texture::applyTexImage(GLenum target, Image* image, State& state) const
|
||||
{
|
||||
// if we don't have a valid image we can't create a texture!
|
||||
if (!_image.valid() || !_image->data())
|
||||
if (!_image || !_image->data())
|
||||
return;
|
||||
|
||||
// get the contextID (user defined ID of 0 upwards) for the
|
||||
@@ -321,31 +323,31 @@ void Texture::applyImmediateMode(State& state) const
|
||||
const uint contextID = state.getContextID();
|
||||
|
||||
// update the modified tag to show that it is upto date.
|
||||
getModifiedTag(contextID) = _image->getModifiedTag();
|
||||
getModifiedTag(contextID) = image->getModifiedTag();
|
||||
|
||||
|
||||
if (_subloadMode == OFF)
|
||||
_image->ensureDimensionsArePowerOfTwo();
|
||||
image->ensureDimensionsArePowerOfTwo();
|
||||
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT,_image->packing());
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT,image->packing());
|
||||
|
||||
applyTexParameters(state);
|
||||
applyTexParameters(target,state);
|
||||
|
||||
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.
|
||||
int internalFormat = _image->internalFormat();
|
||||
int internalFormat = image->internalFormat();
|
||||
switch(_internalFormatMode)
|
||||
{
|
||||
case(USE_IMAGE_DATA_FORMAT):
|
||||
internalFormat = _image->internalFormat();
|
||||
internalFormat = image->internalFormat();
|
||||
break;
|
||||
|
||||
case(USE_ARB_COMPRESSION):
|
||||
if (s_ARB_Compression)
|
||||
{
|
||||
switch(_image->pixelFormat())
|
||||
switch(image->pixelFormat())
|
||||
{
|
||||
case(1): internalFormat = GL_COMPRESSED_ALPHA_ARB; break;
|
||||
case(2): internalFormat = GL_COMPRESSED_LUMINANCE_ALPHA_ARB; break;
|
||||
@@ -359,28 +361,28 @@ void Texture::applyImmediateMode(State& state) const
|
||||
case(GL_INTENSITY): internalFormat = GL_COMPRESSED_INTENSITY_ARB; break;
|
||||
}
|
||||
}
|
||||
else internalFormat = _image->internalFormat();
|
||||
else internalFormat = image->internalFormat();
|
||||
break;
|
||||
|
||||
case(USE_S3TC_DXT1_COMPRESSION):
|
||||
if (s_S3TC_Compression)
|
||||
{
|
||||
switch(_image->pixelFormat())
|
||||
switch(image->pixelFormat())
|
||||
{
|
||||
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->internalFormat(); break;
|
||||
default: internalFormat = image->internalFormat(); break;
|
||||
}
|
||||
}
|
||||
else internalFormat = _image->internalFormat();
|
||||
else internalFormat = image->internalFormat();
|
||||
break;
|
||||
|
||||
case(USE_S3TC_DXT3_COMPRESSION):
|
||||
if (s_S3TC_Compression)
|
||||
{
|
||||
switch(_image->pixelFormat())
|
||||
switch(image->pixelFormat())
|
||||
{
|
||||
case(3):
|
||||
case(GL_RGB): internalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; break;
|
||||
@@ -389,22 +391,22 @@ void Texture::applyImmediateMode(State& state) const
|
||||
default: internalFormat = _image->internalFormat(); break;
|
||||
}
|
||||
}
|
||||
else internalFormat = _image->internalFormat();
|
||||
else internalFormat = image->internalFormat();
|
||||
break;
|
||||
|
||||
case(USE_S3TC_DXT5_COMPRESSION):
|
||||
if (s_S3TC_Compression)
|
||||
{
|
||||
switch(_image->pixelFormat())
|
||||
switch(image->pixelFormat())
|
||||
{
|
||||
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->internalFormat(); break;
|
||||
default: internalFormat = image->internalFormat(); break;
|
||||
}
|
||||
}
|
||||
else internalFormat = _image->internalFormat();
|
||||
else internalFormat = image->internalFormat();
|
||||
break;
|
||||
|
||||
case(USE_USER_DEFINED_FORMAT):
|
||||
@@ -416,63 +418,63 @@ void Texture::applyImmediateMode(State& state) const
|
||||
if (_subloadMode == OFF) {
|
||||
if( _min_filter == LINEAR || _min_filter == NEAREST )
|
||||
{
|
||||
glTexImage2D( GL_TEXTURE_2D, 0, internalFormat,
|
||||
_image->s(), _image->t(), 0,
|
||||
(GLenum)_image->pixelFormat(),
|
||||
(GLenum)_image->dataType(),
|
||||
_image->data() );
|
||||
glTexImage2D( target, 0, internalFormat,
|
||||
image->s(), image->t(), 0,
|
||||
(GLenum)image->pixelFormat(),
|
||||
(GLenum)image->dataType(),
|
||||
image->data() );
|
||||
|
||||
// just estimate estimate it right now..
|
||||
// note, ignores texture compression..
|
||||
_textureObjectSize = _image->s()*_image->t()*4;
|
||||
_textureObjectSize = image->s()*image->t()*4;
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
gluBuild2DMipmaps( GL_TEXTURE_2D, internalFormat,
|
||||
_image->s(),_image->t(),
|
||||
(GLenum)_image->pixelFormat(), (GLenum)_image->dataType(),
|
||||
_image->data() );
|
||||
gluBuild2DMipmaps( target, internalFormat,
|
||||
image->s(),image->t(),
|
||||
(GLenum)image->pixelFormat(), (GLenum)image->dataType(),
|
||||
image->data() );
|
||||
|
||||
// just estimate size it right now..
|
||||
// crude x2 multiplier to account for minmap storage.
|
||||
// note, ignores texture compression..
|
||||
_textureObjectSize = _image->s()*_image->t()*4;
|
||||
_textureObjectSize = image->s()*image->t()*4;
|
||||
|
||||
}
|
||||
|
||||
_textureWidth = _image->s();
|
||||
_textureHeight = _image->t();
|
||||
_textureWidth = image->s();
|
||||
_textureHeight = image->t();
|
||||
}
|
||||
else
|
||||
{
|
||||
static bool s_SGIS_GenMipmap = isGLExtensionSupported("GL_SGIS_generate_mipmap");
|
||||
|
||||
if (s_SGIS_GenMipmap && (_min_filter != LINEAR && _min_filter != NEAREST)) {
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
|
||||
glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
|
||||
}
|
||||
|
||||
// calculate texture dimension
|
||||
_textureWidth = 1;
|
||||
for (; _textureWidth < (_subloadOffsX + _image->s()); _textureWidth <<= 1)
|
||||
for (; _textureWidth < (_subloadOffsX + image->s()); _textureWidth <<= 1)
|
||||
;
|
||||
|
||||
_textureHeight = 1;
|
||||
for (; _textureHeight < (_subloadOffsY + _image->t()); _textureHeight <<= 1)
|
||||
for (; _textureHeight < (_subloadOffsY + image->t()); _textureHeight <<= 1)
|
||||
;
|
||||
|
||||
// reserve appropriate texture memory
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, internalFormat,
|
||||
glTexImage2D(target, 0, internalFormat,
|
||||
_textureWidth, _textureHeight, 0,
|
||||
(GLenum) _image->pixelFormat(), (GLenum) _image->dataType(),
|
||||
(GLenum) image->pixelFormat(), (GLenum) image->dataType(),
|
||||
NULL);
|
||||
|
||||
glTexSubImage2D(GL_TEXTURE_2D, 0,
|
||||
glTexSubImage2D(target, 0,
|
||||
_subloadOffsX, _subloadOffsY,
|
||||
_image->s(), _image->t(),
|
||||
(GLenum) _image->pixelFormat(), (GLenum) _image->dataType(),
|
||||
_image->data());
|
||||
image->s(), image->t(),
|
||||
(GLenum) image->pixelFormat(), (GLenum) image->dataType(),
|
||||
image->data());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -550,13 +552,13 @@ void Texture::copyTexImage2D(State& state, int x, int y, int width, int height )
|
||||
// Get a new 2d texture handle.
|
||||
glGenTextures( 1, &handle );
|
||||
|
||||
glBindTexture( GL_TEXTURE_2D, handle );
|
||||
applyTexParameters(state);
|
||||
glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, x, y, width, height, 0 );
|
||||
glBindTexture( _target, handle );
|
||||
applyTexParameters(_target,state);
|
||||
glCopyTexImage2D( _target, 0, GL_RGBA, x, y, width, height, 0 );
|
||||
|
||||
|
||||
/* Redundant, delete later */
|
||||
// glBindTexture( GL_TEXTURE_2D, handle );
|
||||
// glBindTexture( _target, handle );
|
||||
|
||||
_textureWidth = width;
|
||||
_textureHeight = height;
|
||||
@@ -577,12 +579,12 @@ void Texture::copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, i
|
||||
if (handle)
|
||||
{
|
||||
// we have a valid image
|
||||
glBindTexture( GL_TEXTURE_2D, handle );
|
||||
applyTexParameters(state);
|
||||
glCopyTexSubImage2D( GL_TEXTURE_2D, 0, xoffset,yoffset, x, y, width, height);
|
||||
glBindTexture( _target, handle );
|
||||
applyTexParameters(_target,state);
|
||||
glCopyTexSubImage2D( _target, 0, xoffset,yoffset, x, y, width, height);
|
||||
|
||||
/* Redundant, delete later */
|
||||
glBindTexture( GL_TEXTURE_2D, handle );
|
||||
glBindTexture( _target, handle );
|
||||
|
||||
// inform state that this texture is the current one bound.
|
||||
state.have_applied(this);
|
||||
|
||||
Reference in New Issue
Block a user