2003-01-22 00:45:36 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2003-04-07 21:20:53 +08:00
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
#include <osg/GLExtensions>
|
|
|
|
#include <osg/Texture2D>
|
|
|
|
#include <osg/State>
|
2004-10-19 02:31:04 +08:00
|
|
|
#include <osg/Notify>
|
2002-08-25 03:39:39 +08:00
|
|
|
#include <osg/GLU>
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
|
|
|
Texture2D::Texture2D():
|
|
|
|
_textureWidth(0),
|
|
|
|
_textureHeight(0),
|
2003-04-30 19:40:17 +08:00
|
|
|
_numMipmapLevels(0)
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
2003-04-01 05:41:17 +08:00
|
|
|
setUseHardwareMipMapGeneration(true);
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
|
2004-03-09 22:59:33 +08:00
|
|
|
Texture2D::Texture2D(Image* image):
|
2003-06-25 05:57:13 +08:00
|
|
|
_textureWidth(0),
|
|
|
|
_textureHeight(0),
|
|
|
|
_numMipmapLevels(0)
|
|
|
|
{
|
|
|
|
setUseHardwareMipMapGeneration(true);
|
|
|
|
setImage(image);
|
|
|
|
}
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
Texture2D::Texture2D(const Texture2D& text,const CopyOp& copyop):
|
2002-08-28 23:28:11 +08:00
|
|
|
Texture(text,copyop),
|
2002-08-25 03:39:39 +08:00
|
|
|
_image(copyop(text._image.get())),
|
|
|
|
_textureWidth(text._textureWidth),
|
|
|
|
_textureHeight(text._textureHeight),
|
2003-04-30 19:40:17 +08:00
|
|
|
_numMipmapLevels(text._numMipmapLevels),
|
2002-08-25 03:39:39 +08:00
|
|
|
_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())
|
|
|
|
{
|
2003-03-11 21:30:03 +08:00
|
|
|
int result = _image->compare(*rhs._image);
|
|
|
|
if (result!=0) return result;
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1; // valid lhs._image is greater than null.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rhs._image.valid())
|
|
|
|
{
|
|
|
|
return -1; // valid rhs._image is greater than null.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
int result = compareTexture(rhs);
|
2002-08-25 03:39:39 +08:00
|
|
|
if (result!=0) return result;
|
|
|
|
|
|
|
|
// compare each paramter in turn against the rhs.
|
2004-01-11 01:13:20 +08:00
|
|
|
#if 1
|
2002-08-25 03:39:39 +08:00
|
|
|
COMPARE_StateAttribute_Parameter(_textureWidth)
|
|
|
|
COMPARE_StateAttribute_Parameter(_textureHeight)
|
2004-01-11 01:13:20 +08:00
|
|
|
#endif
|
2002-08-25 03:39:39 +08:00
|
|
|
COMPARE_StateAttribute_Parameter(_subloadCallback)
|
|
|
|
|
|
|
|
return 0; // passed all the above comparison macro's, must be equal.
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture2D::setImage(Image* image)
|
|
|
|
{
|
|
|
|
_image = image;
|
2005-02-09 18:39:45 +08:00
|
|
|
_modifiedCount.setAllElementsTo(0);
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Texture2D::apply(State& state) const
|
|
|
|
{
|
|
|
|
|
2003-07-16 05:19:03 +08:00
|
|
|
//state.setReportGLErrors(true);
|
2003-07-14 22:42:10 +08:00
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
// get the contextID (user defined ID of 0 upwards) for the
|
|
|
|
// current OpenGL context.
|
2003-02-15 04:27:23 +08:00
|
|
|
const unsigned int contextID = state.getContextID();
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
// get the texture object for the current contextID.
|
|
|
|
TextureObject* textureObject = getTextureObject(contextID);
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
if (textureObject != 0)
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
2003-07-14 22:42:10 +08:00
|
|
|
textureObject->bind();
|
|
|
|
|
2002-11-19 20:48:58 +08:00
|
|
|
if (getTextureParameterDirty(state.getContextID()))
|
|
|
|
applyTexParameters(GL_TEXTURE_2D,state);
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
if (_subloadCallback.valid())
|
|
|
|
{
|
|
|
|
_subloadCallback->subload(*this,state);
|
|
|
|
}
|
2005-02-09 18:39:45 +08:00
|
|
|
else if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount())
|
2002-11-19 20:48:58 +08:00
|
|
|
{
|
2003-07-14 22:42:10 +08:00
|
|
|
applyTexImage2D_subload(state,GL_TEXTURE_2D,_image.get(),
|
2004-01-23 21:25:45 +08:00
|
|
|
_textureWidth, _textureHeight, _internalFormat, _numMipmapLevels);
|
2003-04-07 17:46:06 +08:00
|
|
|
|
|
|
|
// update the modified tag to show that it is upto date.
|
2005-02-09 18:39:45 +08:00
|
|
|
getModifiedCount(contextID) = _image->getModifiedCount();
|
2003-04-07 17:46:06 +08:00
|
|
|
|
2002-11-19 20:48:58 +08:00
|
|
|
}
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else if (_subloadCallback.valid())
|
|
|
|
{
|
|
|
|
|
2004-09-22 05:33:52 +08:00
|
|
|
_textureObjectBuffer[contextID] = textureObject = generateTextureObject(contextID,GL_TEXTURE_2D);
|
2003-07-14 22:42:10 +08:00
|
|
|
|
|
|
|
textureObject->bind();
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
applyTexParameters(GL_TEXTURE_2D,state);
|
|
|
|
|
|
|
|
_subloadCallback->load(*this,state);
|
2003-07-14 22:42:10 +08:00
|
|
|
|
|
|
|
textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0);
|
2002-08-25 03:39:39 +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...
|
2003-07-14 22:42:10 +08:00
|
|
|
//glBindTexture( GL_TEXTURE_2D, handle );
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else if (_image.valid() && _image->data())
|
|
|
|
{
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
// compute the internal texture format, this set the _internalFormat to an appropriate value.
|
|
|
|
computeInternalFormat();
|
|
|
|
|
|
|
|
// compute the dimensions of the texture.
|
|
|
|
computeRequiredTextureDimensions(state,*_image,_textureWidth, _textureHeight, _numMipmapLevels);
|
|
|
|
|
|
|
|
|
2004-09-22 05:33:52 +08:00
|
|
|
_textureObjectBuffer[contextID] = textureObject = generateTextureObject(
|
2003-07-14 22:42:10 +08:00
|
|
|
contextID,GL_TEXTURE_2D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0);
|
|
|
|
|
|
|
|
textureObject->bind();
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
applyTexParameters(GL_TEXTURE_2D,state);
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
if (textureObject->isAllocated())
|
|
|
|
{
|
|
|
|
//std::cout<<"Reusing texture object"<<std::endl;
|
|
|
|
applyTexImage2D_subload(state,GL_TEXTURE_2D,_image.get(),
|
2004-01-23 21:25:45 +08:00
|
|
|
_textureWidth, _textureHeight, _internalFormat, _numMipmapLevels);
|
2003-07-14 22:42:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//std::cout<<"Creating new texture object"<<std::endl;
|
|
|
|
applyTexImage2D_load(state,GL_TEXTURE_2D,_image.get(),
|
|
|
|
_textureWidth, _textureHeight, _numMipmapLevels);
|
|
|
|
|
|
|
|
textureObject->setAllocated(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-04-07 17:46:06 +08:00
|
|
|
// update the modified tag to show that it is upto date.
|
2005-02-09 18:39:45 +08:00
|
|
|
getModifiedCount(contextID) = _image->getModifiedCount();
|
2003-04-07 17:46:06 +08:00
|
|
|
|
2003-04-07 20:51:00 +08:00
|
|
|
|
2004-07-27 21:24:35 +08:00
|
|
|
if (_unrefImageDataAfterApply && areAllTextureObjectsLoaded() && _image->getDataVariance()==STATIC)
|
2003-04-07 20:51:00 +08:00
|
|
|
{
|
2003-07-14 22:42:10 +08:00
|
|
|
Texture2D* non_const_this = const_cast<Texture2D*>(this);
|
|
|
|
non_const_this->_image = 0;
|
2003-04-07 20:51:00 +08:00
|
|
|
}
|
|
|
|
|
2002-08-25 03:39:39 +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...
|
2003-07-14 22:42:10 +08:00
|
|
|
//glBindTexture( GL_TEXTURE_2D, handle );
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
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 )
|
|
|
|
{
|
2003-02-15 04:27:23 +08:00
|
|
|
const unsigned int contextID = state.getContextID();
|
2003-11-21 21:45:40 +08:00
|
|
|
|
|
|
|
if (_internalFormat==0) _internalFormat=GL_RGBA;
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
// get the globj for the current contextID.
|
2003-07-14 22:42:10 +08:00
|
|
|
TextureObject* textureObject = getTextureObject(contextID);
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
if (textureObject)
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
|
|
|
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.
|
2004-10-19 02:31:04 +08:00
|
|
|
//
|
2004-09-22 05:33:52 +08:00
|
|
|
_textureObjectBuffer[contextID] = textureObject = generateTextureObject(contextID,GL_TEXTURE_2D);
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
textureObject->bind();
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
applyTexParameters(GL_TEXTURE_2D,state);
|
2004-10-19 02:31:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
bool needHardwareMipMap = (_min_filter != LINEAR && _min_filter != NEAREST);
|
|
|
|
bool hardwareMipMapOn = false;
|
|
|
|
if (needHardwareMipMap)
|
|
|
|
{
|
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
|
|
|
bool generateMipMapSupported = extensions->isGenerateMipMapSupported();
|
|
|
|
|
|
|
|
hardwareMipMapOn = _useHardwareMipMapGeneration && generateMipMapSupported;
|
|
|
|
|
|
|
|
if (!hardwareMipMapOn)
|
|
|
|
{
|
|
|
|
// have to swtich off mip mapping
|
|
|
|
notify(NOTICE)<<"Warning: Texture2D::copyTexImage2D(,,,,) switch of mip mapping as hardware support not available."<<std::endl;
|
|
|
|
_min_filter = LINEAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE);
|
|
|
|
|
2003-11-21 21:45:40 +08:00
|
|
|
glCopyTexImage2D( GL_TEXTURE_2D, 0, _internalFormat, x, y, width, height, 0 );
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2004-10-19 02:31:04 +08:00
|
|
|
if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE);
|
|
|
|
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
_textureWidth = width;
|
|
|
|
_textureHeight = height;
|
2003-07-14 22:42:10 +08:00
|
|
|
_numMipmapLevels = 1;
|
|
|
|
|
|
|
|
textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0);
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
// 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 )
|
|
|
|
{
|
2003-02-15 04:27:23 +08:00
|
|
|
const unsigned int contextID = state.getContextID();
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2003-11-21 21:45:40 +08:00
|
|
|
if (_internalFormat==0) _internalFormat=GL_RGBA;
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
// get the texture object for the current contextID.
|
|
|
|
TextureObject* textureObject = getTextureObject(contextID);
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
if (textureObject)
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
|
|
|
// we have a valid image
|
2003-07-14 22:42:10 +08:00
|
|
|
textureObject->bind();
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
applyTexParameters(GL_TEXTURE_2D,state);
|
|
|
|
|
2004-10-19 02:31:04 +08:00
|
|
|
bool needHardwareMipMap = (_min_filter != LINEAR && _min_filter != NEAREST);
|
|
|
|
bool hardwareMipMapOn = false;
|
|
|
|
if (needHardwareMipMap)
|
|
|
|
{
|
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
|
|
|
bool generateMipMapSupported = extensions->isGenerateMipMapSupported();
|
|
|
|
|
|
|
|
hardwareMipMapOn = _useHardwareMipMapGeneration && generateMipMapSupported;
|
|
|
|
|
|
|
|
if (!hardwareMipMapOn)
|
|
|
|
{
|
|
|
|
// have to swtich off mip mapping
|
|
|
|
notify(NOTICE)<<"Warning: Texture2D::copyTexImage2D(,,,,) switch of mip mapping as hardware support not available."<<std::endl;
|
|
|
|
_min_filter = LINEAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE);
|
|
|
|
|
|
|
|
glCopyTexSubImage2D( GL_TEXTURE_2D, 0, xoffset, yoffset, x, y, width, height);
|
|
|
|
|
|
|
|
if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE);
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
}
|