2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2002-08-25 03:39:39 +08:00
|
|
|
#include <osg/GLExtensions>
|
|
|
|
#include <osg/Texture3D>
|
|
|
|
#include <osg/State>
|
|
|
|
#include <osg/GLU>
|
|
|
|
#include <osg/Notify>
|
|
|
|
|
2002-08-26 18:24:01 +08:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
|
|
|
Texture3D::Texture3D():
|
|
|
|
_textureWidth(0),
|
|
|
|
_textureHeight(0),
|
|
|
|
_textureDepth(0),
|
2003-07-14 22:42:10 +08:00
|
|
|
_numMipmapLevels(0)
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-10-08 19:30:18 +08:00
|
|
|
|
|
|
|
Texture3D::Texture3D(Image* image):
|
|
|
|
_textureWidth(0),
|
|
|
|
_textureHeight(0),
|
|
|
|
_textureDepth(0),
|
|
|
|
_numMipmapLevels(0)
|
|
|
|
{
|
|
|
|
setImage(image);
|
|
|
|
}
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
Texture3D::Texture3D(const Texture3D& 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),
|
|
|
|
_textureDepth(text._textureDepth),
|
2003-07-14 22:42:10 +08:00
|
|
|
_numMipmapLevels(text._numMipmapLevels),
|
2002-08-25 03:39:39 +08:00
|
|
|
_subloadCallback(text._subloadCallback)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Texture3D::~Texture3D()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
int Texture3D::compare(const StateAttribute& sa) const
|
|
|
|
{
|
|
|
|
// check the types are equal and then create the rhs variable
|
2010-10-01 00:57:02 +08:00
|
|
|
// used by the COMPARE_StateAttribute_Parameter macros below.
|
2002-08-25 03:39:39 +08:00
|
|
|
COMPARE_StateAttribute_Types(Texture3D,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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-15 03:42:22 +08:00
|
|
|
if (!_image && !rhs._image)
|
|
|
|
{
|
|
|
|
// no image attached to either Texture2D
|
|
|
|
// but could these textures already be downloaded?
|
|
|
|
// check the _textureObjectBuffer to see if they have been
|
|
|
|
// downloaded
|
|
|
|
|
|
|
|
int result = compareTextureObjects(rhs);
|
|
|
|
if (result!=0) return result;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2007-12-11 01:30:18 +08:00
|
|
|
// compare each parameter in turn against the rhs.
|
2002-08-25 03:39:39 +08:00
|
|
|
COMPARE_StateAttribute_Parameter(_textureWidth)
|
|
|
|
COMPARE_StateAttribute_Parameter(_textureHeight)
|
|
|
|
COMPARE_StateAttribute_Parameter(_textureDepth)
|
|
|
|
COMPARE_StateAttribute_Parameter(_subloadCallback)
|
|
|
|
|
2010-10-01 00:57:02 +08:00
|
|
|
return 0; // passed all the above comparison macros, must be equal.
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Texture3D::setImage(Image* image)
|
|
|
|
{
|
2008-07-22 20:28:46 +08:00
|
|
|
if (_image == image) return;
|
|
|
|
|
2010-01-07 20:14:47 +08:00
|
|
|
if (_image.valid() && _image->requiresUpdateCall())
|
2008-07-22 20:28:46 +08:00
|
|
|
{
|
|
|
|
setUpdateCallback(0);
|
|
|
|
setDataVariance(osg::Object::STATIC);
|
|
|
|
}
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
// delete old texture objects.
|
2002-09-05 19:42:55 +08:00
|
|
|
dirtyTextureObject();
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
_modifiedCount.setAllElementsTo(0);
|
2003-04-07 17:46:06 +08:00
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
_image = image;
|
2010-01-07 20:14:47 +08:00
|
|
|
|
|
|
|
if (_image.valid() && _image->requiresUpdateCall())
|
2008-07-22 20:28:46 +08:00
|
|
|
{
|
2010-01-07 20:14:47 +08:00
|
|
|
setUpdateCallback(new Image::UpdateCallback());
|
2008-07-22 20:28:46 +08:00
|
|
|
setDataVariance(osg::Object::DYNAMIC);
|
|
|
|
}
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
|
2004-09-30 17:10:29 +08:00
|
|
|
void Texture3D::computeRequiredTextureDimensions(State& state, const osg::Image& image,GLsizei& inwidth, GLsizei& inheight,GLsizei& indepth, GLsizei& numMipmapLevels) const
|
|
|
|
{
|
|
|
|
const unsigned int contextID = state.getContextID();
|
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
2005-05-06 17:04:41 +08:00
|
|
|
const Texture::Extensions* texExtensions = Texture::getExtensions(contextID,true);
|
2004-09-30 17:10:29 +08:00
|
|
|
|
2005-05-06 17:04:41 +08:00
|
|
|
int width,height,depth;
|
|
|
|
|
2005-05-17 19:00:13 +08:00
|
|
|
if( !_resizeNonPowerOfTwoHint && texExtensions->isNonPowerOfTwoTextureSupported(_min_filter) )
|
2005-05-06 17:04:41 +08:00
|
|
|
{
|
|
|
|
width = image.s();
|
|
|
|
height = image.t();
|
|
|
|
depth = image.r();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
width = Image::computeNearestPowerOfTwo(image.s()-2*_borderWidth)+2*_borderWidth;
|
|
|
|
height = Image::computeNearestPowerOfTwo(image.t()-2*_borderWidth)+2*_borderWidth;
|
|
|
|
depth = Image::computeNearestPowerOfTwo(image.r()-2*_borderWidth)+2*_borderWidth;
|
|
|
|
}
|
2004-09-30 17:10:29 +08:00
|
|
|
|
|
|
|
// cap the size to what the graphics hardware can handle.
|
|
|
|
if (width>extensions->maxTexture3DSize()) width = extensions->maxTexture3DSize();
|
|
|
|
if (height>extensions->maxTexture3DSize()) height = extensions->maxTexture3DSize();
|
|
|
|
if (depth>extensions->maxTexture3DSize()) depth = extensions->maxTexture3DSize();
|
|
|
|
|
|
|
|
inwidth = width;
|
|
|
|
inheight = height;
|
|
|
|
indepth = depth;
|
|
|
|
|
2008-05-07 21:42:29 +08:00
|
|
|
bool useHardwareMipMapGeneration = !image.isMipmap() && _useHardwareMipMapGeneration && texExtensions->isGenerateMipMapSupported();
|
2004-09-30 17:10:29 +08:00
|
|
|
|
|
|
|
if( _min_filter == LINEAR || _min_filter == NEAREST || useHardwareMipMapGeneration )
|
|
|
|
{
|
|
|
|
numMipmapLevels = 1;
|
|
|
|
}
|
|
|
|
else if( image.isMipmap() )
|
|
|
|
{
|
|
|
|
numMipmapLevels = image.getNumMipmapLevels();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
numMipmapLevels = 0;
|
|
|
|
for( ; (width || height || depth) ;++numMipmapLevels)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (width == 0)
|
|
|
|
width = 1;
|
|
|
|
if (height == 0)
|
|
|
|
height = 1;
|
|
|
|
if (depth == 0)
|
|
|
|
depth = 1;
|
|
|
|
|
|
|
|
width >>= 1;
|
|
|
|
height >>= 1;
|
|
|
|
depth >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
void Texture3D::apply(State& state) const
|
|
|
|
{
|
2002-08-26 18:24:01 +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-14 19:41:52 +08:00
|
|
|
const unsigned int contextID = state.getContextID();
|
2009-09-27 22:38:38 +08:00
|
|
|
|
2009-11-07 00:09:55 +08:00
|
|
|
Texture::TextureObjectManager* tom = Texture::getTextureObjectManager(contextID).get();
|
2009-09-27 22:38:38 +08:00
|
|
|
ElapsedTime elapsedTime(&(tom->getApplyTime()));
|
|
|
|
tom->getNumberApplied()++;
|
|
|
|
|
2002-09-05 23:45:10 +08:00
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
2002-09-05 19:42:55 +08:00
|
|
|
|
2002-09-05 23:45:10 +08:00
|
|
|
if (!extensions->isTexture3DSupported())
|
2002-09-05 19:42:55 +08:00
|
|
|
{
|
2010-05-28 23:47:52 +08:00
|
|
|
OSG_WARN<<"Warning: Texture3D::apply(..) failed, 3D texturing is not support by OpenGL driver."<<std::endl;
|
2002-09-05 19:42:55 +08:00
|
|
|
return;
|
|
|
|
}
|
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);
|
2009-12-09 01:33:01 +08:00
|
|
|
|
|
|
|
if (textureObject)
|
|
|
|
{
|
|
|
|
if (_image.valid() && getModifiedCount(contextID) != _image->getModifiedCount())
|
|
|
|
{
|
|
|
|
// compute the internal texture format, this set the _internalFormat to an appropriate value.
|
|
|
|
computeInternalFormat();
|
|
|
|
|
|
|
|
GLsizei new_width, new_height, new_depth, new_numMipmapLevels;
|
|
|
|
|
|
|
|
// compute the dimensions of the texture.
|
|
|
|
computeRequiredTextureDimensions(state, *_image, new_width, new_height, new_depth, new_numMipmapLevels);
|
|
|
|
|
|
|
|
if (!textureObject->match(GL_TEXTURE_3D, new_numMipmapLevels, _internalFormat, new_width, new_height, new_depth, _borderWidth))
|
|
|
|
{
|
|
|
|
Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get());
|
|
|
|
_textureObjectBuffer[contextID] = 0;
|
|
|
|
textureObject = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
if (textureObject)
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
2003-07-14 22:42:10 +08:00
|
|
|
// we have a valid image
|
|
|
|
textureObject->bind();
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2002-11-12 00:11:48 +08:00
|
|
|
if (getTextureParameterDirty(state.getContextID())) applyTexParameters(GL_TEXTURE_3D,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.get() && getModifiedCount(contextID) != _image->getModifiedCount())
|
2002-11-19 20:48:58 +08:00
|
|
|
{
|
2008-07-22 20:28:46 +08:00
|
|
|
computeRequiredTextureDimensions(state,*_image,_textureWidth, _textureHeight, _textureDepth,_numMipmapLevels);
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
applyTexImage3D(GL_TEXTURE_3D,_image.get(),state, _textureWidth, _textureHeight, _textureDepth,_numMipmapLevels);
|
2003-04-07 17:46:06 +08:00
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
// update the modified count to show that it is upto date.
|
|
|
|
getModifiedCount(contextID) = _image->getModifiedCount();
|
2002-11-19 20:48:58 +08:00
|
|
|
}
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else if (_subloadCallback.valid())
|
|
|
|
{
|
|
|
|
|
2009-09-23 02:45:24 +08:00
|
|
|
_textureObjectBuffer[contextID] = textureObject = generateTextureObject(this, contextID,GL_TEXTURE_3D);
|
2003-07-14 22:42:10 +08:00
|
|
|
|
|
|
|
textureObject->bind();
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
applyTexParameters(GL_TEXTURE_3D,state);
|
|
|
|
|
|
|
|
_subloadCallback->load(*this,state);
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,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_3D, handle );
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else if (_image.valid() && _image->data())
|
|
|
|
{
|
|
|
|
|
2004-09-30 17:10:29 +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, _textureDepth,_numMipmapLevels);
|
|
|
|
|
2009-09-23 02:45:24 +08:00
|
|
|
textureObject = generateTextureObject(this, contextID,GL_TEXTURE_3D);
|
2003-07-14 22:42:10 +08:00
|
|
|
|
|
|
|
textureObject->bind();
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2004-09-30 17:10:29 +08:00
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
applyTexParameters(GL_TEXTURE_3D,state);
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
applyTexImage3D(GL_TEXTURE_3D,_image.get(),state, _textureWidth, _textureHeight, _textureDepth,_numMipmapLevels);
|
|
|
|
|
|
|
|
textureObject->setAllocated(_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0);
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
// update the modified count to show that it is upto date.
|
|
|
|
getModifiedCount(contextID) = _image->getModifiedCount();
|
2003-04-07 17:46:06 +08:00
|
|
|
|
2008-07-16 21:23:58 +08:00
|
|
|
_textureObjectBuffer[contextID] = textureObject;
|
|
|
|
|
2010-09-14 21:19:55 +08:00
|
|
|
// unref image data?
|
|
|
|
if (isSafeToUnrefImageData(state) && _image->getDataVariance()==STATIC)
|
2003-04-07 20:51:00 +08:00
|
|
|
{
|
2003-07-14 22:42:10 +08:00
|
|
|
Texture3D* non_const_this = const_cast<Texture3D*>(this);
|
2010-09-14 21:19:55 +08:00
|
|
|
non_const_this->_image = NULL;
|
2003-04-07 20:51:00 +08:00
|
|
|
}
|
|
|
|
|
2005-11-08 23:52:21 +08:00
|
|
|
}
|
|
|
|
else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_textureDepth!=0) && (_internalFormat!=0) )
|
|
|
|
{
|
|
|
|
_textureObjectBuffer[contextID] = textureObject = generateTextureObject(
|
2009-09-23 02:45:24 +08:00
|
|
|
this, contextID,GL_TEXTURE_3D,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,_textureDepth,0);
|
2005-11-08 23:52:21 +08:00
|
|
|
|
|
|
|
textureObject->bind();
|
|
|
|
|
|
|
|
applyTexParameters(GL_TEXTURE_3D,state);
|
|
|
|
|
|
|
|
// no image present, but dimensions at set so lets create the texture
|
|
|
|
extensions->glTexImage3D( GL_TEXTURE_3D, 0, _internalFormat,
|
|
|
|
_textureWidth, _textureHeight, _textureDepth,
|
|
|
|
_borderWidth,
|
|
|
|
_sourceFormat ? _sourceFormat : _internalFormat,
|
|
|
|
_sourceType ? _sourceType : GL_UNSIGNED_BYTE,
|
|
|
|
0);
|
|
|
|
|
|
|
|
if (_readPBuffer.valid())
|
|
|
|
{
|
|
|
|
_readPBuffer->bindPBufferToTexture(GL_FRONT);
|
|
|
|
}
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindTexture( GL_TEXTURE_3D, 0 );
|
|
|
|
}
|
2007-09-11 20:04:58 +08:00
|
|
|
|
|
|
|
// if texture object is now valid and we have to allocate mipmap levels, then
|
|
|
|
if (textureObject != 0 && _texMipmapGenerationDirtyList[contextID])
|
|
|
|
{
|
|
|
|
generateMipmap(state);
|
|
|
|
}
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Texture3D::computeInternalFormat() const
|
|
|
|
{
|
|
|
|
if (_image.valid()) computeInternalFormatWithImage(*_image);
|
2007-09-11 20:04:58 +08:00
|
|
|
else computeInternalFormatType();
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
void Texture3D::applyTexImage3D(GLenum target, Image* image, State& state, GLsizei& inwidth, GLsizei& inheight, GLsizei& indepth, GLsizei& numMipmapLevels) const
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
|
|
|
// if we don't have a valid image we can't create a texture!
|
|
|
|
if (!image || !image->data())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// get the contextID (user defined ID of 0 upwards) for the
|
|
|
|
// current OpenGL context.
|
2003-02-14 19:41:52 +08:00
|
|
|
const unsigned int contextID = state.getContextID();
|
2003-04-07 17:46:06 +08:00
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
2005-05-06 17:04:41 +08:00
|
|
|
const Texture::Extensions* texExtensions = Texture::getExtensions(contextID,true);
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
// compute the internal texture format, this set the _internalFormat to an appropriate value.
|
|
|
|
computeInternalFormat();
|
|
|
|
|
|
|
|
// select the internalFormat required for the texture.
|
|
|
|
bool compressed = isCompressedInternalFormat(_internalFormat);
|
2004-09-30 17:10:29 +08:00
|
|
|
bool compressed_image = isCompressedInternalFormat((GLenum)image->getPixelFormat());
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
if (compressed)
|
|
|
|
{
|
2010-05-28 23:47:52 +08:00
|
|
|
//OSG_WARN<<"Warning::cannot currently use compressed format with 3D textures."<<std::endl;
|
2004-09-29 18:01:46 +08:00
|
|
|
//return;
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
|
2005-05-06 17:04:41 +08:00
|
|
|
//Rescale if resize hint is set or NPOT not supported or dimensions exceed max size
|
2005-05-17 19:00:13 +08:00
|
|
|
if( _resizeNonPowerOfTwoHint || !texExtensions->isNonPowerOfTwoTextureSupported(_min_filter)
|
2005-05-06 17:04:41 +08:00
|
|
|
|| inwidth > extensions->maxTexture3DSize()
|
|
|
|
|| inheight > extensions->maxTexture3DSize()
|
|
|
|
|| indepth > extensions->maxTexture3DSize() )
|
|
|
|
image->ensureValidSizeForTexturing(extensions->maxTexture3DSize());
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT,image->getPacking());
|
|
|
|
|
2008-05-07 21:42:29 +08:00
|
|
|
bool useHardwareMipMapGeneration = !image->isMipmap() && _useHardwareMipMapGeneration && texExtensions->isGenerateMipMapSupported();
|
|
|
|
|
|
|
|
if( _min_filter == LINEAR || _min_filter == NEAREST || useHardwareMipMapGeneration )
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
2008-05-07 21:42:29 +08:00
|
|
|
bool hardwareMipMapOn = false;
|
|
|
|
if (_min_filter != LINEAR && _min_filter != NEAREST)
|
|
|
|
{
|
|
|
|
if (useHardwareMipMapGeneration) glTexParameteri(GL_TEXTURE_3D, GL_GENERATE_MIPMAP_SGIS,GL_TRUE);
|
|
|
|
hardwareMipMapOn = true;
|
|
|
|
}
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
numMipmapLevels = 1;
|
2004-09-30 17:10:29 +08:00
|
|
|
|
|
|
|
if (!compressed_image)
|
|
|
|
{
|
|
|
|
extensions->glTexImage3D( target, 0, _internalFormat,
|
|
|
|
inwidth, inheight, indepth,
|
|
|
|
_borderWidth,
|
|
|
|
(GLenum)image->getPixelFormat(),
|
|
|
|
(GLenum)image->getDataType(),
|
|
|
|
image->data() );
|
|
|
|
}
|
|
|
|
else if (extensions->isCompressedTexImage3DSupported())
|
|
|
|
{
|
2010-05-28 23:47:52 +08:00
|
|
|
// OSG_WARN<<"glCompressedTexImage3D "<<inwidth<<", "<<inheight<<", "<<indepth<<std::endl;
|
2004-09-30 17:10:29 +08:00
|
|
|
numMipmapLevels = 1;
|
|
|
|
|
|
|
|
GLint blockSize, size;
|
|
|
|
getCompressedSize(_internalFormat, inwidth, inheight, indepth, blockSize,size);
|
|
|
|
|
|
|
|
extensions->glCompressedTexImage3D(target, 0, _internalFormat,
|
|
|
|
inwidth, inheight, indepth,
|
|
|
|
_borderWidth,
|
|
|
|
size,
|
|
|
|
image->data());
|
|
|
|
}
|
|
|
|
|
2008-05-07 21:42:29 +08:00
|
|
|
if (hardwareMipMapOn) glTexParameteri(GL_TEXTURE_3D, GL_GENERATE_MIPMAP_SGIS,GL_FALSE);
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!image->isMipmap())
|
|
|
|
{
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
numMipmapLevels = 1;
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2010-10-08 19:20:56 +08:00
|
|
|
gluBuild3DMipmaps( extensions->glTexImage3D,
|
|
|
|
target, _internalFormat,
|
|
|
|
image->s(),image->t(),image->r(),
|
|
|
|
(GLenum)image->getPixelFormat(), (GLenum)image->getDataType(),
|
|
|
|
image->data() );
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-07-14 22:42:10 +08:00
|
|
|
numMipmapLevels = image->getNumMipmapLevels();
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
int width = image->s();
|
|
|
|
int height = image->t();
|
|
|
|
int depth = image->r();
|
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
for( GLsizei k = 0 ; k < numMipmapLevels && (width || height || depth) ;k++)
|
2002-08-25 03:39:39 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if (width == 0)
|
|
|
|
width = 1;
|
|
|
|
if (height == 0)
|
|
|
|
height = 1;
|
|
|
|
if (depth == 0)
|
|
|
|
depth = 1;
|
|
|
|
|
2002-09-05 19:42:55 +08:00
|
|
|
extensions->glTexImage3D( target, k, _internalFormat,
|
2004-01-28 05:29:58 +08:00
|
|
|
width, height, depth, _borderWidth,
|
2002-09-05 19:42:55 +08:00
|
|
|
(GLenum)image->getPixelFormat(),
|
|
|
|
(GLenum)image->getDataType(),
|
|
|
|
image->getMipmapData(k));
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
width >>= 1;
|
|
|
|
height >>= 1;
|
|
|
|
depth >>= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
inwidth = image->s();
|
|
|
|
inheight = image->t();
|
|
|
|
indepth = image->r();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void Texture3D::copyTexSubImage3D(State& state, int xoffset, int yoffset, int zoffset, int x, int y, int width, int height )
|
|
|
|
{
|
2003-02-14 19:41:52 +08:00
|
|
|
const unsigned int contextID = state.getContextID();
|
2002-09-05 23:45:10 +08:00
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
2003-07-14 22:42:10 +08:00
|
|
|
|
|
|
|
// get the texture object for the current contextID.
|
|
|
|
TextureObject* textureObject = getTextureObject(contextID);
|
|
|
|
|
|
|
|
if (textureObject != 0)
|
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_3D,state);
|
2002-09-05 19:42:55 +08:00
|
|
|
extensions->glCopyTexSubImage3D( GL_TEXTURE_3D, 0, xoffset,yoffset,zoffset, x, y, width, height);
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
/* Redundant, delete later */
|
2003-07-14 22:42:10 +08:00
|
|
|
//glBindTexture( GL_TEXTURE_3D, handle );
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
// inform state that this texture is the current one bound.
|
2005-03-25 19:07:48 +08:00
|
|
|
state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-05-28 23:47:52 +08:00
|
|
|
OSG_WARN<<"Warning: Texture3D::copyTexSubImage3D(..) failed, cannot not copy to a non existant texture."<<std::endl;
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
}
|
2002-09-05 19:42:55 +08:00
|
|
|
|
2007-09-11 20:04:58 +08:00
|
|
|
void Texture3D::allocateMipmap(State& state) const
|
|
|
|
{
|
|
|
|
const unsigned int contextID = state.getContextID();
|
|
|
|
|
|
|
|
// get the texture object for the current contextID.
|
|
|
|
TextureObject* textureObject = getTextureObject(contextID);
|
|
|
|
|
|
|
|
if (textureObject && _textureWidth != 0 && _textureHeight != 0 && _textureDepth != 0)
|
|
|
|
{
|
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
|
|
|
|
|
|
|
// bind texture
|
|
|
|
textureObject->bind();
|
|
|
|
|
|
|
|
// compute number of mipmap levels
|
|
|
|
int width = _textureWidth;
|
|
|
|
int height = _textureHeight;
|
|
|
|
int depth = _textureDepth;
|
2007-10-01 16:50:58 +08:00
|
|
|
int numMipmapLevels = Image::computeNumberOfMipmapLevels(width, height, depth);
|
2007-09-11 20:04:58 +08:00
|
|
|
|
|
|
|
// we do not reallocate the level 0, since it was already allocated
|
|
|
|
width >>= 1;
|
|
|
|
height >>= 1;
|
|
|
|
depth >>= 1;
|
|
|
|
|
|
|
|
for( GLsizei k = 1; k < numMipmapLevels && (width || height || depth); k++)
|
|
|
|
{
|
|
|
|
if (width == 0)
|
|
|
|
width = 1;
|
|
|
|
if (height == 0)
|
|
|
|
height = 1;
|
|
|
|
if (depth == 0)
|
|
|
|
depth = 1;
|
|
|
|
|
|
|
|
extensions->glTexImage3D( GL_TEXTURE_3D, k, _internalFormat,
|
|
|
|
width, height, depth, _borderWidth,
|
|
|
|
_sourceFormat ? _sourceFormat : _internalFormat,
|
|
|
|
_sourceType ? _sourceType : GL_UNSIGNED_BYTE, NULL);
|
|
|
|
|
|
|
|
width >>= 1;
|
|
|
|
height >>= 1;
|
|
|
|
depth >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// inform state that this texture is the current one bound.
|
|
|
|
state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-14 18:38:28 +08:00
|
|
|
typedef buffered_value< ref_ptr<Texture3D::Extensions> > BufferedExtensions;
|
|
|
|
static BufferedExtensions s_extensions;
|
|
|
|
|
2003-04-10 21:41:45 +08:00
|
|
|
Texture3D::Extensions* Texture3D::getExtensions(unsigned int contextID,bool createIfNotInitalized)
|
2002-09-05 19:42:55 +08:00
|
|
|
{
|
2005-04-26 21:15:27 +08:00
|
|
|
if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions(contextID);
|
2002-09-05 19:42:55 +08:00
|
|
|
return s_extensions[contextID].get();
|
|
|
|
}
|
|
|
|
|
2003-02-14 19:41:52 +08:00
|
|
|
void Texture3D::setExtensions(unsigned int contextID,Extensions* extensions)
|
2002-09-14 18:38:28 +08:00
|
|
|
{
|
|
|
|
s_extensions[contextID] = extensions;
|
|
|
|
}
|
|
|
|
|
2002-09-05 23:45:10 +08:00
|
|
|
#ifndef GL_MAX_3D_TEXTURE_SIZE
|
|
|
|
#define GL_MAX_3D_TEXTURE_SIZE 0x8073
|
|
|
|
#endif
|
|
|
|
|
2005-04-26 21:15:27 +08:00
|
|
|
Texture3D::Extensions::Extensions(unsigned int contextID)
|
2002-09-14 18:38:28 +08:00
|
|
|
{
|
2006-03-09 00:11:54 +08:00
|
|
|
setupGLExtensions(contextID);
|
2002-09-14 18:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Texture3D::Extensions::Extensions(const Extensions& rhs):
|
|
|
|
Referenced()
|
|
|
|
{
|
|
|
|
_isTexture3DSupported = rhs._isTexture3DSupported;
|
|
|
|
_isTexture3DFast = rhs._isTexture3DFast;
|
|
|
|
_maxTexture3DSize = rhs._maxTexture3DSize;
|
|
|
|
|
2010-10-08 19:20:56 +08:00
|
|
|
glTexImage3D = rhs.glTexImage3D;
|
|
|
|
glTexSubImage3D = rhs.glTexSubImage3D;
|
|
|
|
glCompressedTexImage3D = rhs.glCompressedTexImage3D;
|
|
|
|
glCompressedTexSubImage3D = rhs.glCompressedTexSubImage3D;
|
|
|
|
glCopyTexSubImage3D = rhs.glCopyTexSubImage3D;
|
2002-09-14 18:38:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Texture3D::Extensions::lowestCommonDenominator(const Extensions& rhs)
|
|
|
|
{
|
|
|
|
if (!rhs._isTexture3DSupported) _isTexture3DSupported = false;
|
|
|
|
if (!rhs._isTexture3DFast) _isTexture3DFast = false;
|
|
|
|
if (rhs._maxTexture3DSize<_maxTexture3DSize) _maxTexture3DSize = rhs._maxTexture3DSize;
|
|
|
|
|
2010-10-08 19:20:56 +08:00
|
|
|
if (!rhs.glTexImage3D) glTexImage3D = 0;
|
|
|
|
if (!rhs.glTexSubImage3D) glTexSubImage3D = 0;
|
|
|
|
if (!rhs.glCompressedTexImage3D) glCompressedTexImage3D = 0;
|
|
|
|
if (!rhs.glCompressedTexSubImage3D) glCompressedTexSubImage3D = 0;
|
|
|
|
if (!rhs.glCopyTexSubImage3D) glCopyTexSubImage3D = 0;
|
2002-09-14 18:38:28 +08:00
|
|
|
}
|
|
|
|
|
2006-03-09 00:11:54 +08:00
|
|
|
void Texture3D::Extensions::setupGLExtensions(unsigned int contextID)
|
2002-09-05 19:42:55 +08:00
|
|
|
{
|
2009-11-11 03:16:36 +08:00
|
|
|
_isTexture3DFast = OSG_GL3_FEATURES || isGLExtensionSupported(contextID,"GL_EXT_texture3D");
|
2002-09-05 19:42:55 +08:00
|
|
|
|
2002-09-05 23:45:10 +08:00
|
|
|
if (_isTexture3DFast) _isTexture3DSupported = true;
|
|
|
|
else _isTexture3DSupported = strncmp((const char*)glGetString(GL_VERSION),"1.2",3)>=0;
|
2002-09-05 19:42:55 +08:00
|
|
|
|
2002-09-05 23:45:10 +08:00
|
|
|
glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &_maxTexture3DSize);
|
|
|
|
|
2010-10-08 19:20:56 +08:00
|
|
|
setGLExtensionFuncPtr(glTexImage3D,"glTexImage3D","glTexImage3DEXT");
|
|
|
|
setGLExtensionFuncPtr(glTexSubImage3D,"glTexSubImage3D","glTexSubImage3DEXT");
|
|
|
|
setGLExtensionFuncPtr(glCompressedTexImage3D,"glCompressedTexImage3D","glCompressedTexImage3DARB");
|
|
|
|
setGLExtensionFuncPtr(glCompressedTexSubImage3D,"glCompressedTexSubImage3D","glCompressedTexSubImage3DARB");
|
|
|
|
setGLExtensionFuncPtr(glCopyTexSubImage3D,"glCopyTexSubImage3D","glCopyTexSubImage3DEXT");
|
2002-09-05 23:45:10 +08:00
|
|
|
|
2002-09-05 19:42:55 +08:00
|
|
|
}
|