Added new osg::TextureBase, osg::Texture1D, osg::Texture2D, and osg::Texture3D

classes, and changed osg::Texture and osg::TextureCubeMap so that they now derive
from osg::TextureBase.
This commit is contained in:
Robert Osfield
2002-08-24 19:39:39 +00:00
parent 0c383901a7
commit 239068f223
35 changed files with 2801 additions and 1140 deletions

View File

@@ -1,6 +1,7 @@
#include <osg/CopyOp>
#include <osg/Node>
#include <osg/StateSet>
#include <osg/TextureBase>
#include <osg/Texture>
#include <osg/Drawable>
#include <osg/Array>
@@ -48,26 +49,42 @@ StateAttribute* CopyOp::operator() (const StateAttribute* attr) const
{
if (attr && _flags&DEEP_COPY_STATEATTRIBUTES)
{
const Texture* text = dynamic_cast<const Texture*>(attr);
if (text)
const TextureBase* textbase = dynamic_cast<const TextureBase*>(attr);
if (textbase)
{
return operator()(text);
return operator()(textbase);
}
else
{
// const Texture* text = dynamic_cast<const Texture*>(attr);
// if (text)
// {
// return operator()(text);
// }
// else
return dynamic_cast<StateAttribute*>(attr->clone(*this));
}
else
return dynamic_cast<StateAttribute*>(attr->clone(*this));
}
else
return const_cast<StateAttribute*>(attr);
}
Texture* CopyOp::operator() (const Texture* text) const
TextureBase* CopyOp::operator() (const TextureBase* text) const
{
if (text && _flags&DEEP_COPY_TEXTURES)
return dynamic_cast<Texture*>(text->clone(*this));
return dynamic_cast<TextureBase*>(text->clone(*this));
else
return const_cast<Texture*>(text);
return const_cast<TextureBase*>(text);
}
// Texture* CopyOp::operator() (const Texture* text) const
// {
// if (text && _flags&DEEP_COPY_TEXTURES)
// return dynamic_cast<Texture*>(text->clone(*this));
// else
// return const_cast<Texture*>(text);
// }
Image* CopyOp::operator() (const Image* image) const
{
if (image && _flags&DEEP_COPY_IMAGES)

View File

@@ -7,7 +7,7 @@
#include <osg/Geode>
#include <osg/Geometry>
#include <osg/StateSet>
#include <osg/Texture>
#include <osg/Texture2D>
using namespace osg;
@@ -415,7 +415,7 @@ void Image::ensureValidSizeForTexturing()
int new_s = computeNearestPowerOfTwo(_s);
int new_t = computeNearestPowerOfTwo(_t);
static GLint max_size=Texture::getMaxTextureSize();
static GLint max_size=TextureBase::getMaxTextureSize();
if (new_s>max_size) new_s = max_size;
if (new_t>max_size) new_t = max_size;
@@ -454,7 +454,7 @@ Geode* osg::createGeodeForImage(osg::Image* image,const float s,const float t)
float x = y*(s/t);
// set up the texture.
osg::Texture* texture = osgNew osg::Texture;
osg::Texture2D* texture = osgNew osg::Texture2D;
texture->setImage(image);
// set up the drawstate.

View File

@@ -8,7 +8,7 @@
#include <osg/Geometry>
#include <osg/ImpostorSprite>
#include <osg/Texture>
#include <osg/Texture2D>
#include <osg/TexEnv>
#include <osg/AlphaFunc>
#include <osg/Notify>
@@ -107,7 +107,7 @@ const bool ImpostorSprite::computeBound() const
return true;
}
void ImpostorSprite::setTexture(Texture* tex,int s,int t)
void ImpostorSprite::setTexture(Texture2D* tex,int s,int t)
{
_texture = tex;
_s = s;
@@ -253,9 +253,9 @@ ImpostorSprite* ImpostorSpriteManager::createOrReuseImpostorSprite(int s,int t,i
stateset->setAttributeAndModes( _alphafunc.get(), StateAttribute::ON );
Texture* texture = osgNew Texture;
texture->setFilter(Texture::MIN_FILTER,Texture::LINEAR);
texture->setFilter(Texture::MAG_FILTER,Texture::LINEAR);
Texture2D* texture = osgNew Texture2D;
texture->setFilter(Texture2D::MIN_FILTER,Texture2D::LINEAR);
texture->setFilter(Texture2D::MAG_FILTER,Texture2D::LINEAR);
stateset->setTextureAttributeAndModes(0,texture,StateAttribute::ON);
stateset->setTextureAttribute(0,_texenv.get());

View File

@@ -75,6 +75,10 @@ CXXFILES =\
TexGen.cpp\
TexMat.cpp\
Texture.cpp\
TextureBase.cpp\
Texture1D.cpp\
Texture2D.cpp\
Texture3D.cpp\
TextureCubeMap.cpp\
Timer.cpp\
Transform.cpp\

View File

@@ -1,8 +1,8 @@
// Ideas and code borrowed from GLUT pointburst demo
// written by Mark J. Kilgard
#include <osg/GL>
#include <osg/GLExtensions>
#include <osg/GL>
#include <osg/Point>
#include <osg/Notify>

View File

@@ -1,6 +1,6 @@
#include <osg/GLExtensions>
#include <osg/State>
#include <osg/Notify>
#include <osg/GLExtensions>
#include <osg/GLU>
#include <algorithm>

View File

@@ -1,5 +1,5 @@
#include <osg/TexEnvCombine>
#include <osg/GLExtensions>
#include <osg/TexEnvCombine>
using namespace osg;

View File

@@ -1,36 +1,22 @@
#include <osg/GLExtensions>
#include <osg/Texture>
#ifdef TEXTURE_USE_DEPRECATED_API
#if defined(_MSC_VER)
#pragma warning( disable : 4786 )
#endif
#include <osg/ref_ptr>
#include <osg/Image>
#include <osg/Texture>
#include <osg/State>
#include <osg/Notify>
#include <osg/GLExtensions>
#include <osg/GLU>
typedef void (APIENTRY * MyCompressedTexImage2DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data);
using namespace osg;
Texture::DeletedTextureObjectCache Texture::s_deletedTextureObjectCache;
Texture::Texture():
_unrefImageAfterApply(false),
_target(GL_TEXTURE_2D),
_wrap_s(CLAMP),
_wrap_t(CLAMP),
_wrap_r(CLAMP),
_min_filter(LINEAR_MIPMAP_LINEAR), // trilinear
_mag_filter(LINEAR),
_maxAnisotropy(1.0f),
_texParamtersDirty(true),
_internalFormatMode(USE_IMAGE_DATA_FORMAT),
_internalFormatValue(0),
_borderColor(0.0, 0.0, 0.0, 0.0),
_textureWidth(0),
_textureHeight(0),
_numMimpmapLevels(0),
@@ -42,28 +28,11 @@ Texture::Texture():
_subloadImageWidth(0),
_subloadImageHeight(0)
{
_handleList.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
_modifiedTag.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
}
Texture::Texture(const Texture& text,const CopyOp& copyop):
StateAttribute(text,copyop),
_handleList(),
_modifiedTag(),
TextureBase(text,copyop),
_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),
_maxAnisotropy(text._maxAnisotropy),
_texParamtersDirty(false),
_internalFormatMode(text._internalFormatMode),
_internalFormatValue(text._internalFormatValue),
_borderColor(text._borderColor),
_textureWidth(text._textureWidth),
_textureHeight(text._textureHeight),
_numMimpmapLevels(text._numMimpmapLevels),
@@ -79,8 +48,6 @@ Texture::Texture(const Texture& text,const CopyOp& copyop):
Texture::~Texture()
{
// delete old texture objects.
dirtyTextureObject();
}
int Texture::compare(const StateAttribute& sa) const
@@ -109,14 +76,12 @@ int Texture::compare(const StateAttribute& sa) const
}
}
int result = compareTextureBase(rhs);
if (result!=0) return result;
// 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)
@@ -149,75 +114,6 @@ void Texture::setImage(Image* image)
_image = image;
}
void Texture::setWrap(const WrapParameter which, const WrapMode wrap)
{
switch( which )
{
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;
default : notify(WARN)<<"Error: invalid 'which' passed Texture::setWrap("<<(unsigned int)which<<","<<(unsigned int)wrap<<")"<<std::endl; break;
}
}
const Texture::WrapMode Texture::getWrap(const WrapParameter which) const
{
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;
}
}
void Texture::setFilter(const FilterParameter which, const FilterMode filter)
{
switch( which )
{
case MIN_FILTER : _min_filter = filter; _texParamtersDirty = true; break;
case MAG_FILTER : _mag_filter = filter; _texParamtersDirty = true; break;
default : notify(WARN)<<"Error: invalid 'which' passed Texture::setFilter("<<(unsigned int)which<<","<<(unsigned int)filter<<")"<<std::endl; break;
}
}
const Texture::FilterMode Texture::getFilter(const FilterParameter which) const
{
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;
}
}
void Texture::setMaxAnisotropy(float anis)
{
if (_maxAnisotropy!=anis)
{
_maxAnisotropy = anis;
_texParamtersDirty = true;
}
}
/** 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)
{
Texture::deleteTextureObject(i,_handleList[i]);
_handleList[i] = 0;
}
}
}
void Texture::apply(State& state) const
{
@@ -232,13 +128,13 @@ void Texture::apply(State& state) const
{
if (_subloadMode == OFF)
{
glBindTexture( _target, handle );
if (_texParamtersDirty) applyTexParameters(_target,state);
glBindTexture( GL_TEXTURE_2D, handle );
if (_texParamtersDirty) applyTexParameters(GL_TEXTURE_2D,state);
}
else if (_image.valid() && _image->data())
{
glBindTexture( _target, handle );
if (_texParamtersDirty) applyTexParameters(_target,state);
glBindTexture( GL_TEXTURE_2D, handle );
if (_texParamtersDirty) applyTexParameters(GL_TEXTURE_2D,state);
uint& modifiedTag = getModifiedTag(contextID);
if (_subloadMode == AUTO ||
@@ -246,7 +142,7 @@ void Texture::apply(State& state) const
{
glPixelStorei(GL_UNPACK_ROW_LENGTH,_image->s());
glTexSubImage2D(_target, 0,
glTexSubImage2D(GL_TEXTURE_2D, 0,
_subloadTextureOffsetX, _subloadTextureOffsetY,
(_subloadImageWidth>0)?_subloadImageWidth:_image->s(), (_subloadImageHeight>0)?_subloadImageHeight:_image->t(),
(GLenum) _image->getPixelFormat(), (GLenum) _image->getDataType(),
@@ -259,7 +155,7 @@ void Texture::apply(State& state) const
}
else if (_subloadMode == USE_CALLBACK)
{
_subloadCallback->subload(_target,*this,state);
_subloadCallback->subload(GL_TEXTURE_2D,*this,state);
}
}
@@ -268,392 +164,30 @@ void Texture::apply(State& state) const
{
glGenTextures( 1L, (GLuint *)&handle );
glBindTexture( _target, handle );
glBindTexture( GL_TEXTURE_2D, handle );
applyTexParameters(_target,state);
applyTexImage(_target,_image.get(),state);
applyTexParameters(GL_TEXTURE_2D,state);
applyTexImage2D(GL_TEXTURE_2D,_image.get(),state, _textureWidth, _textureHeight, _numMimpmapLevels);
// 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( _target, handle );
if (_unrefImageAfterApply)
_image = 0;
glBindTexture( GL_TEXTURE_2D, handle );
}
}
void Texture::compile(State& state) const
{
apply(state);
}
void Texture::applyTexParameters(GLenum target, State&) const
{
WrapMode ws = _wrap_s, wt = _wrap_t;
// 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;
}
// 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;
}
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;
}
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)
{
glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, _borderColor.ptr());
}
_texParamtersDirty=false;
}
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 || !image->data())
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.
getModifiedTag(contextID) = image->getModifiedTag();
if (_subloadMode == OFF)
image->ensureValidSizeForTexturing();
glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking());
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.
bool compressed = false;
GLint internalFormat = image->getInternalTextureFormat();
switch(_internalFormatMode)
{
case(USE_IMAGE_DATA_FORMAT):
internalFormat = image->getInternalTextureFormat();
break;
case(USE_ARB_COMPRESSION):
if (s_ARB_Compression)
{
compressed = true;
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;
case(USE_S3TC_DXT1_COMPRESSION):
if (s_S3TC_Compression)
{
compressed = true;
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;
case(USE_S3TC_DXT3_COMPRESSION):
if (s_S3TC_Compression)
{
compressed = true;
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;
}
}
else internalFormat = image->getInternalTextureFormat();
break;
case(USE_S3TC_DXT5_COMPRESSION):
if (s_S3TC_Compression)
{
compressed = true;
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;
}
}
else internalFormat = image->getInternalTextureFormat();
break;
case(USE_USER_DEFINED_FORMAT):
internalFormat = _internalFormatValue;
break;
}
_internalFormatValue = internalFormat;
// an experiment to look at the changes in performance
// when use 16 bit textures rather than 24/32bit textures.
// internalFormat = GL_RGBA4;
static MyCompressedTexImage2DArbProc glCompressedTexImage2D_ptr =
(MyCompressedTexImage2DArbProc)getGLExtensionFuncPtr("glCompressedTexImage2DARB");
if (_subloadMode == OFF) {
if( _min_filter == LINEAR || _min_filter == NEAREST )
{
if ( !compressed )
{
_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());
}
}
else
{
if(!image->isMipmap())
{
_numMimpmapLevels = 1;
gluBuild2DMipmaps( target, internalFormat,
image->s(),image->t(),
(GLenum)image->getPixelFormat(), (GLenum)image->getDataType(),
image->data() );
}
else
{
_numMimpmapLevels = image->getNumMipmapLevels();
int width = image->s();
int height = image->t();
if( !compressed )
{
for( size_t 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( size_t 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;
}
}
}
}
_textureWidth = image->s();
_textureHeight = image->t();
}
else if (_subloadMode == USE_CALLBACK)
{
_subloadCallback->load(target,*this,state);
}
else
{
static bool s_SGIS_GenMipmap = isGLExtensionSupported("GL_SGIS_generate_mipmap");
if (s_SGIS_GenMipmap && (_min_filter != LINEAR && _min_filter != NEAREST))
{
_numMimpmapLevels = 1; // will leave this at one, since the mipmap will be created internally by OpenGL.
glTexParameteri(target, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
}
else
{
_numMimpmapLevels = 1;
}
GLsizei width = (_subloadImageWidth>0)?_subloadImageWidth:image->s();
GLsizei height = (_subloadImageHeight>0)?_subloadImageHeight:image->t();
if (_textureWidth==0)
{
// need to calculate texture dimension
_textureWidth = 1;
for (; _textureWidth < (static_cast<GLsizei>(_subloadTextureOffsetX) + width); _textureWidth <<= 1) {}
}
if (_textureHeight==0)
{
// need to calculate texture dimension
_textureHeight = 1;
for (; _textureHeight < (static_cast<GLsizei>(_subloadTextureOffsetY) + height); _textureHeight <<= 1) {}
}
// reserve appropriate texture memory
glTexImage2D(target, 0, internalFormat,
_textureWidth, _textureHeight, 0,
(GLenum) image->getPixelFormat(), (GLenum) image->getDataType(),
NULL);
glPixelStorei(GL_UNPACK_ROW_LENGTH,image->s());
glTexSubImage2D(target, 0,
_subloadTextureOffsetX, _subloadTextureOffsetY,
width, height,
(GLenum) image->getPixelFormat(), (GLenum) image->getDataType(),
image->data(_subloadImageOffsetX,_subloadImageOffsetY));
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
glBindTexture( GL_TEXTURE_2D, 0 );
}
}
/** 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)
void Texture::computeInternalFormat() const
{
if (handle!=0)
{
// insert the handle into the cache for the appropriate context.
s_deletedTextureObjectCache[contextID].insert(handle);
}
if (_image.valid()) computeInternalFormatWithImage(*_image);
}
/** 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);
}
}
void Texture::copyTexImage2D(State& state, int x, int y, int width, int height )
{
const uint contextID = state.getContextID();
@@ -693,18 +227,13 @@ void Texture::copyTexImage2D(State& state, int x, int y, int width, int height )
// Get a new 2d texture handle.
glGenTextures( 1, &handle );
glBindTexture( _target, handle );
applyTexParameters(_target,state);
glCopyTexImage2D( _target, 0, GL_RGBA, x, y, width, height, 0 );
glBindTexture( GL_TEXTURE_2D, handle );
applyTexParameters(GL_TEXTURE_2D,state);
glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, x, y, width, height, 0 );
/* Redundant, delete later */
// glBindTexture( _target, handle );
_textureWidth = width;
_textureHeight = height;
// cout<<"copyTexImage2D x="<<x<<" y="<<y<<" w="<<width<<" h="<<height<< std::endl;
// inform state that this texture is the current one bound.
state.haveAppliedAttribute(this);
@@ -721,12 +250,12 @@ void Texture::copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, i
{
// we have a valid image
glBindTexture( _target, handle );
applyTexParameters(_target,state);
glCopyTexSubImage2D( _target, 0, xoffset,yoffset, x, y, width, height);
glBindTexture( GL_TEXTURE_2D, handle );
applyTexParameters(GL_TEXTURE_2D,state);
glCopyTexSubImage2D( GL_TEXTURE_2D, 0, xoffset,yoffset, x, y, width, height);
/* Redundant, delete later */
glBindTexture( _target, handle );
glBindTexture( GL_TEXTURE_2D, handle );
// inform state that this texture is the current one bound.
state.haveAppliedAttribute(this);
@@ -740,30 +269,4 @@ void Texture::copyTexSubImage2D(State& state, int xoffset, int yoffset, int x, i
}
}
GLint Texture::getMaxTextureSize()
{
static GLint s_maxTextureSize = 0;
if (s_maxTextureSize == 0)
{
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;
}
return s_maxTextureSize;
}
#endif // USE_DEPRECATED_API

335
src/osg/Texture1D.cpp Normal file
View File

@@ -0,0 +1,335 @@
#include <osg/GLExtensions>
#include <osg/Texture1D>
#include <osg/State>
#include <osg/GLU>
typedef void (APIENTRY * MyCompressedTexImage1DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLint border, GLsizei imageSize, const GLvoid *data);
using namespace osg;
Texture1D::Texture1D():
_textureWidth(0),
_numMimpmapLevels(0)
{
}
Texture1D::Texture1D(const Texture1D& text,const CopyOp& copyop):
TextureBase(text,copyop),
_image(copyop(text._image.get())),
_textureWidth(text._textureWidth),
_numMimpmapLevels(text._numMimpmapLevels),
_subloadCallback(text._subloadCallback)
{
}
Texture1D::~Texture1D()
{
}
int Texture1D::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(Texture1D,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.
}
}
int result = compareTextureBase(rhs);
if (result!=0) return result;
// compare each paramter in turn against the rhs.
COMPARE_StateAttribute_Parameter(_textureWidth)
COMPARE_StateAttribute_Parameter(_subloadCallback)
return 0; // passed all the above comparison macro's, must be equal.
}
void Texture1D::setImage(Image* image)
{
// delete old texture objects.
for(TextureNameList::iterator itr=_handleList.begin();
itr!=_handleList.end();
++itr)
{
if (*itr != 0)
{
// contact global texture object handler to delete texture objects
// in appropriate context.
// glDeleteTextures( 1L, (const GLuint *)itr );
*itr = 0;
}
}
_image = image;
}
void Texture1D::apply(State& state) const
{
// get the contextID (user defined ID of 0 upwards) for the
// current OpenGL context.
const uint contextID = state.getContextID();
// get the globj for the current contextID.
GLuint& handle = getTextureObject(contextID);
if (handle != 0)
{
glBindTexture( GL_TEXTURE_1D, handle );
if (_texParamtersDirty) applyTexParameters(GL_TEXTURE_1D,state);
if (_subloadCallback.valid())
{
_subloadCallback->subload(*this,state);
}
}
else if (_subloadCallback.valid())
{
glGenTextures( 1L, (GLuint *)&handle );
glBindTexture( GL_TEXTURE_1D, handle );
applyTexParameters(GL_TEXTURE_1D,state);
_subloadCallback->load(*this,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_1D, handle );
}
else if (_image.valid() && _image->data())
{
glGenTextures( 1L, (GLuint *)&handle );
glBindTexture( GL_TEXTURE_1D, handle );
applyTexParameters(GL_TEXTURE_1D,state);
applyTexImage1D(GL_TEXTURE_1D,_image.get(),state, _textureWidth, _numMimpmapLevels);
// 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_1D, handle );
}
else
{
glBindTexture( GL_TEXTURE_1D, 0 );
}
}
void Texture1D::computeInternalFormat() const
{
if (_image.valid()) computeInternalFormatWithImage(*_image);
}
void Texture1D::applyTexImage1D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& numMimpmapLevels) const
{
// 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.
const uint contextID = state.getContextID();
// update the modified tag to show that it is upto date.
getModifiedTag(contextID) = image->getModifiedTag();
// 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 MyCompressedTexImage1DArbProc glCompressedTexImage1D_ptr =
(MyCompressedTexImage1DArbProc)getGLExtensionFuncPtr("glCompressedTexImage1DARB");
if( _min_filter == LINEAR || _min_filter == NEAREST )
{
if ( !compressed )
{
numMimpmapLevels = 1;
glTexImage1D( target, 0, _internalFormat,
image->s(), 0,
(GLenum)image->getPixelFormat(),
(GLenum)image->getDataType(),
image->data() );
}
else if(glCompressedTexImage1D_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;
glCompressedTexImage1D_ptr(target, 0, _internalFormat,
image->s(), 0,
size,
image->data());
}
}
else
{
if(!image->isMipmap())
{
numMimpmapLevels = 1;
gluBuild1DMipmaps( target, _internalFormat,
image->s(),
(GLenum)image->getPixelFormat(), (GLenum)image->getDataType(),
image->data() );
}
else
{
numMimpmapLevels = image->getNumMipmapLevels();
int width = image->s();
if( !compressed )
{
for( GLsizei k = 0 ; k < numMimpmapLevels && width ;k++)
{
glTexImage1D( target, k, _internalFormat,
width,0,
(GLenum)image->getPixelFormat(),
(GLenum)image->getDataType(),
image->getMipmapData(k));
width >>= 1;
}
}
else if(glCompressedTexImage1D_ptr)
{
GLint blockSize = ( _internalFormat == GL_COMPRESSED_RGB_S3TC_DXT1_EXT ? 8 : 16 );
GLint size = 0;
for( GLsizei k = 0 ; k < numMimpmapLevels && width ;k++)
{
size = ((width+3)/4)*blockSize;
glCompressedTexImage1D_ptr(target, k, _internalFormat,
width, 0, size, image->getMipmapData(k));
width >>= 1;
}
}
}
}
inwidth = image->s();
}
void Texture1D::copyTexImage1D(State& state, int x, int y, int width)
{
const uint contextID = state.getContextID();
// get the globj for the current contextID.
GLuint& handle = getTextureObject(contextID);
if (handle)
{
if (width==(int)_textureWidth)
{
// 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.
copyTexSubImage1D(state,0 ,x, y, width);
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 );
glBindTexture( GL_TEXTURE_1D, handle );
applyTexParameters(GL_TEXTURE_1D,state);
glCopyTexImage1D( GL_TEXTURE_1D, 0, GL_RGBA, x, y, width, 0 );
_textureWidth = width;
// inform state that this texture is the current one bound.
state.haveAppliedAttribute(this);
}
void Texture1D::copyTexSubImage1D(State& state, int xoffset, int x, int y, int width)
{
const uint contextID = state.getContextID();
// get the globj for the current contextID.
GLuint& handle = getTextureObject(contextID);
if (handle)
{
// we have a valid image
glBindTexture( GL_TEXTURE_1D, handle );
applyTexParameters(GL_TEXTURE_1D,state);
glCopyTexSubImage1D( GL_TEXTURE_1D, 0, xoffset, x, y, width);
/* Redundant, delete later */
glBindTexture( GL_TEXTURE_1D, handle );
// inform state that this texture is the current one bound.
state.haveAppliedAttribute(this);
}
else
{
// no texture object already exsits for this context so need to
// create it upfront - simply call copyTexImage1D.
copyTexImage1D(state,x,y,width);
}
}

234
src/osg/Texture2D.cpp Normal file
View File

@@ -0,0 +1,234 @@
#include <osg/GLExtensions>
#include <osg/Texture2D>
#include <osg/State>
#include <osg/GLU>
typedef void (APIENTRY * MyCompressedTexImage2DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data);
using namespace osg;
Texture2D::Texture2D():
_textureWidth(0),
_textureHeight(0),
_numMimpmapLevels(0)
{
}
Texture2D::Texture2D(const Texture2D& text,const CopyOp& copyop):
TextureBase(text,copyop),
_image(copyop(text._image.get())),
_textureWidth(text._textureWidth),
_textureHeight(text._textureHeight),
_numMimpmapLevels(text._numMimpmapLevels),
_subloadCallback(text._subloadCallback)
{
}
Texture2D::~Texture2D()
{
}
int Texture2D::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(Texture2D,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.
}
}
int result = compareTextureBase(rhs);
if (result!=0) return result;
// compare each paramter in turn against the rhs.
COMPARE_StateAttribute_Parameter(_textureWidth)
COMPARE_StateAttribute_Parameter(_textureHeight)
COMPARE_StateAttribute_Parameter(_subloadCallback)
return 0; // passed all the above comparison macro's, must be equal.
}
void Texture2D::setImage(Image* image)
{
// delete old texture objects.
for(TextureNameList::iterator itr=_handleList.begin();
itr!=_handleList.end();
++itr)
{
if (*itr != 0)
{
// contact global texture object handler to delete texture objects
// in appropriate context.
// glDeleteTextures( 1L, (const GLuint *)itr );
*itr = 0;
}
}
_image = image;
}
void Texture2D::apply(State& state) const
{
// get the contextID (user defined ID of 0 upwards) for the
// current OpenGL context.
const uint contextID = state.getContextID();
// get the globj for the current contextID.
GLuint& handle = getTextureObject(contextID);
if (handle != 0)
{
glBindTexture( GL_TEXTURE_2D, handle );
if (_texParamtersDirty) applyTexParameters(GL_TEXTURE_2D,state);
if (_subloadCallback.valid())
{
_subloadCallback->subload(*this,state);
}
}
else if (_subloadCallback.valid())
{
glGenTextures( 1L, (GLuint *)&handle );
glBindTexture( GL_TEXTURE_2D, handle );
applyTexParameters(GL_TEXTURE_2D,state);
_subloadCallback->load(*this,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 );
}
else if (_image.valid() && _image->data())
{
glGenTextures( 1L, (GLuint *)&handle );
glBindTexture( GL_TEXTURE_2D, handle );
applyTexParameters(GL_TEXTURE_2D,state);
applyTexImage2D(GL_TEXTURE_2D,_image.get(),state, _textureWidth, _textureHeight, _numMimpmapLevels);
// 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 );
}
else
{
glBindTexture( GL_TEXTURE_2D, 0 );
}
}
void Texture2D::computeInternalFormat() const
{
if (_image.valid()) computeInternalFormatWithImage(*_image);
}
void Texture2D::copyTexImage2D(State& state, int x, int y, int width, int height )
{
const uint contextID = state.getContextID();
// get the globj for the current contextID.
GLuint& handle = getTextureObject(contextID);
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 );
glBindTexture( GL_TEXTURE_2D, handle );
applyTexParameters(GL_TEXTURE_2D,state);
glCopyTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, x, y, width, height, 0 );
_textureWidth = width;
_textureHeight = height;
// inform state that this texture is the current one bound.
state.haveAppliedAttribute(this);
}
void Texture2D::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.
GLuint& handle = getTextureObject(contextID);
if (handle)
{
// we have a valid image
glBindTexture( GL_TEXTURE_2D, handle );
applyTexParameters(GL_TEXTURE_2D,state);
glCopyTexSubImage2D( GL_TEXTURE_2D, 0, xoffset,yoffset, x, y, width, height);
/* Redundant, delete later */
glBindTexture( GL_TEXTURE_2D, handle );
// inform state that this texture is the current one bound.
state.haveAppliedAttribute(this);
}
else
{
// no texture object already exsits for this context so need to
// create it upfront - simply call copyTexImage2D.
copyTexImage2D(state,x,y,width,height);
}
}

280
src/osg/Texture3D.cpp Normal file
View File

@@ -0,0 +1,280 @@
#include <osg/GLExtensions>
#include <osg/Texture3D>
#include <osg/State>
#include <osg/GLU>
#include <osg/Notify>
#define TEMPORARY_COMMENT_OUT_WHILE_ESTABLISHING_X_PLATFORM_SUPPORT_FOR_3D_TEXTURES
using namespace osg;
Texture3D::Texture3D():
_textureWidth(0),
_textureHeight(0),
_textureDepth(0),
_numMimpmapLevels(0)
{
}
Texture3D::Texture3D(const Texture3D& text,const CopyOp& copyop):
TextureBase(text,copyop),
_image(copyop(text._image.get())),
_textureWidth(text._textureWidth),
_textureHeight(text._textureHeight),
_textureDepth(text._textureDepth),
_numMimpmapLevels(text._numMimpmapLevels),
_subloadCallback(text._subloadCallback)
{
}
Texture3D::~Texture3D()
{
}
int Texture3D::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(Texture3D,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.
}
}
int result = compareTextureBase(rhs);
if (result!=0) return result;
// compare each paramter in turn against the rhs.
COMPARE_StateAttribute_Parameter(_textureWidth)
COMPARE_StateAttribute_Parameter(_textureHeight)
COMPARE_StateAttribute_Parameter(_textureDepth)
COMPARE_StateAttribute_Parameter(_subloadCallback)
return 0; // passed all the above comparison macro's, must be equal.
}
void Texture3D::setImage(Image* image)
{
// delete old texture objects.
for(TextureNameList::iterator itr=_handleList.begin();
itr!=_handleList.end();
++itr)
{
if (*itr != 0)
{
// contact global texture object handler to delete texture objects
// in appropriate context.
// glDeleteTextures( 1L, (const GLuint *)itr );
*itr = 0;
}
}
_image = image;
}
void Texture3D::apply(State& state) const
{
// get the contextID (user defined ID of 0 upwards) for the
// current OpenGL context.
const uint contextID = state.getContextID();
// get the globj for the current contextID.
GLuint& handle = getTextureObject(contextID);
if (handle != 0)
{
glBindTexture( GL_TEXTURE_3D, handle );
if (_texParamtersDirty) applyTexParameters(GL_TEXTURE_3D,state);
if (_subloadCallback.valid())
{
_subloadCallback->subload(*this,state);
}
}
else if (_subloadCallback.valid())
{
glGenTextures( 1L, (GLuint *)&handle );
glBindTexture( GL_TEXTURE_3D, handle );
applyTexParameters(GL_TEXTURE_3D,state);
_subloadCallback->load(*this,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_3D, handle );
}
else if (_image.valid() && _image->data())
{
glGenTextures( 1L, (GLuint *)&handle );
glBindTexture( GL_TEXTURE_3D, handle );
applyTexParameters(GL_TEXTURE_3D,state);
applyTexImage3D(GL_TEXTURE_3D,_image.get(),state, _textureWidth, _textureHeight, _textureDepth,_numMimpmapLevels);
// 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_3D, handle );
}
else
{
glBindTexture( GL_TEXTURE_3D, 0 );
}
}
void Texture3D::computeInternalFormat() const
{
if (_image.valid()) computeInternalFormatWithImage(*_image);
}
void Texture3D::applyTexImage3D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight, GLsizei& indepth, GLsizei& numMimpmapLevels) const
{
#ifndef TEMPORARY_COMMENT_OUT_WHILE_ESTABLISHING_X_PLATFORM_SUPPORT_FOR_3D_TEXTURES
// 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.
const uint contextID = state.getContextID();
// update the modified tag to show that it is upto date.
getModifiedTag(contextID) = image->getModifiedTag();
// 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);
if (compressed)
{
notify(WARN)<<"Warning::cannot currently use compressed format with 3D textures."<<std::endl;
return;
}
image->ensureValidSizeForTexturing();
glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking());
if( _min_filter == LINEAR || _min_filter == NEAREST )
{
numMimpmapLevels = 1;
glTexImage3D( target, 0, _internalFormat,
image->s(), image->t(), image->r(), 0,
(GLenum)image->getPixelFormat(),
(GLenum)image->getDataType(),
image->data() );
}
else
{
if(!image->isMipmap())
{
numMimpmapLevels = 1;
gluBuild3DMipmaps( target, _internalFormat,
image->s(),image->t(),image->r(),
(GLenum)image->getPixelFormat(), (GLenum)image->getDataType(),
image->data() );
}
else
{
numMimpmapLevels = image->getNumMipmapLevels();
int width = image->s();
int height = image->t();
int depth = image->r();
for( GLsizei k = 0 ; k < numMimpmapLevels && (width || height || depth) ;k++)
{
if (width == 0)
width = 1;
if (height == 0)
height = 1;
if (depth == 0)
depth = 1;
glTexImage3D( target, k, _internalFormat,
width, height, depth, 0,
(GLenum)image->getPixelFormat(),
(GLenum)image->getDataType(),
image->getMipmapData(k));
width >>= 1;
height >>= 1;
depth >>= 1;
}
}
}
inwidth = image->s();
inheight = image->t();
indepth = image->r();
#endif // TEMPORARY_COMMENT_OUT_WHILE_ESTABLISHING_X_PLATFORM_SUPPORT_FOR_3D_TEXTURES
}
void Texture3D::copyTexSubImage3D(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height )
{
#ifndef TEMPORARY_COMMENT_OUT_WHILE_ESTABLISHING_X_PLATFORM_SUPPORT_FOR_3D_TEXTURES
const uint contextID = state.getContextID();
// get the globj for the current contextID.
GLuint& handle = getTextureObject(contextID);
if (handle)
{
// we have a valid image
glBindTexture( GL_TEXTURE_3D, handle );
applyTexParameters(GL_TEXTURE_3D,state);
glCopyTexSubImage3D( GL_TEXTURE_3D, 0, xoffset,yoffset,zoffset, x, y, width, height);
/* Redundant, delete later */
glBindTexture( GL_TEXTURE_3D, handle );
// inform state that this texture is the current one bound.
state.haveAppliedAttribute(this);
}
else
{
notify(WARN)<<"Warning: Texture3D::copyTexSubImage3D(..) failed, cannot not copy to a non existant texture."<<std::endl;
}
#endif // TEMPORARY_COMMENT_OUT_WHILE_ESTABLISHING_X_PLATFORM_SUPPORT_FOR_3D_TEXTURES
}

493
src/osg/TextureBase.cpp Normal file
View File

@@ -0,0 +1,493 @@
#include <osg/GLExtensions>
#include <osg/Image>
#include <osg/TextureBase>
#include <osg/State>
#include <osg/Notify>
#include <osg/GLU>
typedef void (APIENTRY * MyCompressedTexImage2DArbProc) (GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data);
using namespace osg;
TextureBase::DeletedTextureObjectCache TextureBase::s_deletedTextureObjectCache;
TextureBase::TextureBase():
_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),
_texParamtersDirty(true),
_internalFormatMode(USE_IMAGE_DATA_FORMAT),
_internalFormat(0)
{
_handleList.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
_modifiedTag.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
}
TextureBase::TextureBase(const TextureBase& text,const CopyOp& copyop):
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),
_texParamtersDirty(false),
_internalFormatMode(text._internalFormatMode),
_internalFormat(text._internalFormat)
{
_handleList.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
_modifiedTag.resize(DisplaySettings::instance()->getMaxNumberOfGraphicsContexts(),0);
}
TextureBase::~TextureBase()
{
// delete old texture objects.
dirtyTextureObject();
}
int TextureBase::compareTextureBase(const TextureBase& rhs) const
{
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;
}
void TextureBase::setWrap(const WrapParameter which, const WrapMode wrap)
{
switch( which )
{
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;
default : notify(WARN)<<"Error: invalid 'which' passed TextureBase::setWrap("<<(unsigned int)which<<","<<(unsigned int)wrap<<")"<<std::endl; break;
}
}
const TextureBase::WrapMode TextureBase::getWrap(const WrapParameter which) const
{
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 TextureBase::getWrap(which)"<<std::endl; return _wrap_s;
}
}
void TextureBase::setFilter(const FilterParameter which, const FilterMode filter)
{
switch( which )
{
case MIN_FILTER : _min_filter = filter; _texParamtersDirty = true; break;
case MAG_FILTER : _mag_filter = filter; _texParamtersDirty = true; break;
default : notify(WARN)<<"Error: invalid 'which' passed TextureBase::setFilter("<<(unsigned int)which<<","<<(unsigned int)filter<<")"<<std::endl; break;
}
}
const TextureBase::FilterMode TextureBase::getFilter(const FilterParameter which) const
{
switch( which )
{
case MIN_FILTER : return _min_filter;
case MAG_FILTER : return _mag_filter;
default : notify(WARN)<<"Error: invalid 'which' passed TextureBase::getFilter(which)"<< std::endl; return _min_filter;
}
}
void TextureBase::setMaxAnisotropy(float anis)
{
if (_maxAnisotropy!=anis)
{
_maxAnisotropy = anis;
_texParamtersDirty = true;
}
}
/** Force a recompile on next apply() of associated OpenGL texture objects.*/
void TextureBase::dirtyTextureObject()
{
for(uint i=0;i<_handleList.size();++i)
{
if (_handleList[i] != 0)
{
TextureBase::deleteTextureObject(i,_handleList[i]);
_handleList[i] = 0;
}
}
}
void TextureBase::computeInternalFormatWithImage(osg::Image& image) const
{
static bool s_ARB_Compression = isGLExtensionSupported("GL_ARB_texture_compression");
static bool s_S3TC_Compression = isGLExtensionSupported("GL_EXT_texture_compression_s3tc");
GLint internalFormat = image.getInternalTextureFormat();
switch(_internalFormatMode)
{
case(USE_IMAGE_DATA_FORMAT):
internalFormat = image.getInternalTextureFormat();
break;
case(USE_ARB_COMPRESSION):
if (s_ARB_Compression)
{
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;
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;
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;
}
}
else internalFormat = image.getInternalTextureFormat();
break;
case(USE_S3TC_DXT5_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_DXT5_EXT; break;
default: internalFormat = image.getInternalTextureFormat(); break;
}
}
else internalFormat = image.getInternalTextureFormat();
break;
case(USE_USER_DEFINED_FORMAT):
internalFormat = _internalFormat;
break;
}
_internalFormat = internalFormat;
}
bool TextureBase::isCompressedInternalFormat(GLint internalFormat) const
{
switch(internalFormat)
{
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;
}
}
void TextureBase::applyTexParameters(GLenum target, State&) const
{
WrapMode ws = _wrap_s, wt = _wrap_t;
// 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;
}
// 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;
}
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;
}
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)
{
glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, _borderColor.ptr());
}
_texParamtersDirty=false;
}
void TextureBase::applyTexImage2D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight,GLsizei& numMimpmapLevels) const
{
// 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.
const uint contextID = state.getContextID();
// update the modified tag to show that it is upto date.
getModifiedTag(contextID) = image->getModifiedTag();
// 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 )
{
if ( !compressed )
{
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());
}
}
else
{
if(!image->isMipmap())
{
numMimpmapLevels = 1;
gluBuild2DMipmaps( target, _internalFormat,
image->s(),image->t(),
(GLenum)image->getPixelFormat(), (GLenum)image->getDataType(),
image->data() );
}
else
{
numMimpmapLevels = image->getNumMipmapLevels();
int width = image->s();
int height = image->t();
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;
}
}
}
}
inwidth = image->s();
inheight = image->t();
}
/** 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 TextureBase::deleteTextureObject(uint contextID,GLuint handle)
{
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 TextureBase::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);
}
}
GLint TextureBase::getMaxTextureSize()
{
static GLint s_maxTextureSize = 0;
if (s_maxTextureSize == 0)
{
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;
}
return s_maxTextureSize;
}
void TextureBase::compile(State& state) const
{
apply(state);
}

View File

@@ -2,12 +2,12 @@
#pragma warning( disable : 4786 )
#endif
#include <osg/GLExtensions>
#include <osg/ref_ptr>
#include <osg/Image>
#include <osg/State>
#include <osg/Texture>
#include <osg/TextureCubeMap>
#include <osg/GLExtensions>
#include <osg/GLU>
@@ -79,11 +79,29 @@ static GLenum faceTarget[6] =
#endif
TextureCubeMap::TextureCubeMap():Texture()
TextureCubeMap::TextureCubeMap():
_textureWidth(0),
_textureHeight(0),
_numMimpmapLevels(0)
{
_target = GL_TEXTURE_CUBE_MAP; // default to ARB extension
}
TextureCubeMap::TextureCubeMap(const TextureCubeMap& text,const CopyOp& copyop):
TextureBase(text,copyop),
_textureWidth(text._textureWidth),
_textureHeight(text._textureHeight),
_numMimpmapLevels(text._numMimpmapLevels),
_subloadCallback(text._subloadCallback)
{
_images[0] = copyop(text._images[0].get());
_images[1] = copyop(text._images[1].get());
_images[2] = copyop(text._images[2].get());
_images[3] = copyop(text._images[3].get());
_images[4] = copyop(text._images[4].get());
_images[5] = copyop(text._images[5].get());
}
TextureCubeMap::~TextureCubeMap()
{
@@ -119,6 +137,14 @@ int TextureCubeMap::compare(const StateAttribute& sa) const
}
}
int result = compareTextureBase(rhs);
if (result!=0) return result;
// compare each paramter in turn against the rhs.
COMPARE_StateAttribute_Parameter(_textureWidth)
COMPARE_StateAttribute_Parameter(_textureHeight)
COMPARE_StateAttribute_Parameter(_subloadCallback)
return 0; // passed all the above comparison macro's, must be equal.
}
@@ -165,6 +191,10 @@ bool TextureCubeMap::imagesValid() const
return true;
}
void TextureCubeMap::computeInternalFormat() const
{
if (imagesValid()) computeInternalFormatWithImage(*_images[0]);
}
void TextureCubeMap::apply(State& state) const
{
@@ -183,65 +213,55 @@ void TextureCubeMap::apply(State& state) const
if (handle != 0)
{
if (_subloadMode == OFF)
glBindTexture( GL_TEXTURE_CUBE_MAP, handle );
if (_texParamtersDirty) applyTexParameters(GL_TEXTURE_CUBE_MAP,state);
if (_subloadCallback.valid())
{
glBindTexture( _target, handle );
if (_texParamtersDirty) applyTexParameters(_target,state);
_subloadCallback->subload(*this,state);
}
else if (imagesValid())
{
uint& modifiedTag = getModifiedTag(contextID);
modifiedTag = 0;
glBindTexture( _target, handle );
if (_texParamtersDirty) applyTexParameters(_target,state);
int rowwidth = 0;
for (int n=0; n<6; n++)
{
if ((_subloadMode == AUTO) ||
(_subloadMode == IF_DIRTY && modifiedTag != _images[n]->getModifiedTag()))
{
if (rowwidth != _images[n]->s())
{
rowwidth = _images[n]->s();
glPixelStorei(GL_UNPACK_ROW_LENGTH,rowwidth);
}
glTexSubImage2D(faceTarget[n], 0,
_subloadTextureOffsetX, _subloadTextureOffsetX,
(_subloadImageWidth>0)?_subloadImageWidth:_images[n]->s(), (_subloadImageHeight>0)?_subloadImageHeight:_images[n]->t(),
(GLenum) _images[n]->getPixelFormat(), (GLenum) _images[n]->getDataType(),
_images[n]->data(_subloadImageOffsetX,_subloadImageOffsetY));
// update the modified flag to show that the image has been loaded.
modifiedTag += _images[n]->getModifiedTag();
}
else if (_subloadMode == USE_CALLBACK)
{
_subloadCallback->subload(faceTarget[n],*this,state);
}
}
glPixelStorei(GL_UNPACK_ROW_LENGTH,0);
}
}
else if (imagesValid())
else if (_subloadCallback.valid())
{
glGenTextures( 1L, (GLuint *)&handle );
glBindTexture( _target, handle );
glBindTexture( GL_TEXTURE_CUBE_MAP, handle );
applyTexParameters(_target,state);
applyTexParameters(GL_TEXTURE_CUBE_MAP,state);
for (int n=0; n<6; n++)
{
applyTexImage( faceTarget[n], _images[n].get(), state);
}
_subloadCallback->load(*this,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( _target, handle );
glBindTexture( GL_TEXTURE_CUBE_MAP, handle );
}
else if (imagesValid())
{
glGenTextures( 1L, (GLuint *)&handle );
glBindTexture( GL_TEXTURE_CUBE_MAP, handle );
applyTexParameters(GL_TEXTURE_CUBE_MAP,state);
for (int n=0; n<6; n++)
{
applyTexImage2D( faceTarget[n], _images[n].get(), state, _textureWidth, _textureHeight, _numMimpmapLevels);
}
// 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_CUBE_MAP, handle );
}
else
{
glBindTexture( GL_TEXTURE_CUBE_MAP, 0 );
}
}