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>
|
2002-03-19 07:10:33 +08:00
|
|
|
#include <osg/ref_ptr>
|
|
|
|
#include <osg/Image>
|
|
|
|
#include <osg/State>
|
|
|
|
#include <osg/TextureCubeMap>
|
2005-07-20 00:30:55 +08:00
|
|
|
#include <osg/Notify>
|
2002-03-19 07:10:33 +08:00
|
|
|
|
|
|
|
#include <osg/GLU>
|
|
|
|
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
|
|
|
static GLenum faceTarget[6] =
|
|
|
|
{
|
2005-07-25 21:05:57 +08:00
|
|
|
GL_TEXTURE_CUBE_MAP_POSITIVE_X,
|
|
|
|
GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
|
|
|
|
GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
|
|
|
|
GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
|
|
|
|
GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
|
|
|
|
GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
|
2002-03-19 07:10:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
TextureCubeMap::TextureCubeMap():
|
|
|
|
_textureWidth(0),
|
|
|
|
_textureHeight(0),
|
2003-04-30 19:40:17 +08:00
|
|
|
_numMipmapLevels(0)
|
2002-03-19 07:10:33 +08:00
|
|
|
{
|
2003-04-07 23:07:45 +08:00
|
|
|
setUseHardwareMipMapGeneration(false);
|
2002-03-19 07:10:33 +08:00
|
|
|
}
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
TextureCubeMap::TextureCubeMap(const TextureCubeMap& text,const CopyOp& copyop):
|
2002-08-28 23:28:11 +08:00
|
|
|
Texture(text,copyop),
|
2002-08-25 03:39:39 +08:00
|
|
|
_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)
|
|
|
|
{
|
|
|
|
_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());
|
|
|
|
|
2005-02-09 18:39:45 +08:00
|
|
|
_modifiedCount[0].setAllElementsTo(0);
|
|
|
|
_modifiedCount[1].setAllElementsTo(0);
|
|
|
|
_modifiedCount[2].setAllElementsTo(0);
|
|
|
|
_modifiedCount[3].setAllElementsTo(0);
|
|
|
|
_modifiedCount[4].setAllElementsTo(0);
|
|
|
|
_modifiedCount[5].setAllElementsTo(0);
|
2003-04-07 17:46:06 +08:00
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
|
2002-03-19 07:10:33 +08:00
|
|
|
|
|
|
|
TextureCubeMap::~TextureCubeMap()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int TextureCubeMap::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-03-19 07:10:33 +08:00
|
|
|
COMPARE_StateAttribute_Types(TextureCubeMap,sa)
|
|
|
|
|
2006-08-15 03:42:22 +08:00
|
|
|
bool noImages = true;
|
2002-03-19 07:10:33 +08:00
|
|
|
for (int n=0; n<6; n++)
|
|
|
|
{
|
2006-08-15 03:42:22 +08:00
|
|
|
if (noImages && _images[n].valid()) noImages = false;
|
|
|
|
if (noImages && rhs._images[n].valid()) noImages = false;
|
|
|
|
|
2002-03-19 07:10:33 +08:00
|
|
|
if (_images[n]!=rhs._images[n]) // smart pointer comparison.
|
|
|
|
{
|
|
|
|
if (_images[n].valid())
|
|
|
|
{
|
|
|
|
if (rhs._images[n].valid())
|
|
|
|
{
|
2003-03-11 21:30:03 +08:00
|
|
|
int result = _images[n]->compare(*rhs._images[n]);
|
|
|
|
if (result!=0) return result;
|
2002-03-19 07:10:33 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 1; // valid lhs._image is greater than null.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (rhs._images[n].valid())
|
|
|
|
{
|
|
|
|
return -1; // valid rhs._image is greater than null.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-15 03:42:22 +08:00
|
|
|
if (noImages)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
|
2010-10-01 00:57:02 +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(_subloadCallback)
|
|
|
|
|
2010-10-01 00:57:02 +08:00
|
|
|
return 0; // passed all the above comparison macros, must be equal.
|
2002-03-19 07:10:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-07-27 18:11:45 +08:00
|
|
|
void TextureCubeMap::setImage( unsigned int face, Image* image)
|
2002-03-19 07:10:33 +08:00
|
|
|
{
|
2008-07-22 20:28:46 +08:00
|
|
|
if (_images[face] == image) return;
|
|
|
|
|
2010-01-07 20:14:47 +08:00
|
|
|
unsigned numImageRequireUpdateBefore = 0;
|
2008-07-22 20:28:46 +08:00
|
|
|
for (unsigned int i=0; i<getNumImages(); ++i)
|
|
|
|
{
|
2010-01-07 20:14:47 +08:00
|
|
|
if (_images[i].valid() && _images[i]->requiresUpdateCall()) ++numImageRequireUpdateBefore;
|
2008-07-22 20:28:46 +08:00
|
|
|
}
|
|
|
|
|
2002-03-19 07:10:33 +08:00
|
|
|
_images[face] = image;
|
2005-02-09 18:39:45 +08:00
|
|
|
_modifiedCount[face].setAllElementsTo(0);
|
2008-07-22 20:28:46 +08:00
|
|
|
|
|
|
|
|
2010-01-07 20:14:47 +08:00
|
|
|
// find out if we need to reset the update callback to handle the animation of image
|
|
|
|
unsigned numImageRequireUpdateAfter = 0;
|
2008-07-22 20:28:46 +08:00
|
|
|
for (unsigned int i=0; i<getNumImages(); ++i)
|
|
|
|
{
|
2010-01-07 20:14:47 +08:00
|
|
|
if (_images[i].valid() && _images[i]->requiresUpdateCall()) ++numImageRequireUpdateAfter;
|
2008-07-22 20:28:46 +08:00
|
|
|
}
|
|
|
|
|
2010-01-07 20:14:47 +08:00
|
|
|
if (numImageRequireUpdateBefore>0)
|
2008-07-22 20:28:46 +08:00
|
|
|
{
|
2010-01-07 20:14:47 +08:00
|
|
|
if (numImageRequireUpdateAfter==0)
|
2008-07-22 20:28:46 +08:00
|
|
|
{
|
|
|
|
setUpdateCallback(0);
|
|
|
|
setDataVariance(osg::Object::STATIC);
|
|
|
|
}
|
|
|
|
}
|
2010-01-07 20:14:47 +08:00
|
|
|
else if (numImageRequireUpdateAfter>0)
|
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-03-19 07:10:33 +08:00
|
|
|
}
|
|
|
|
|
2004-07-27 18:11:45 +08:00
|
|
|
Image* TextureCubeMap::getImage(unsigned int face)
|
2002-03-21 06:39:51 +08:00
|
|
|
{
|
|
|
|
return _images[face].get();
|
|
|
|
}
|
|
|
|
|
2004-07-27 18:11:45 +08:00
|
|
|
const Image* TextureCubeMap::getImage(unsigned int face) const
|
2002-03-21 06:39:51 +08:00
|
|
|
{
|
|
|
|
return _images[face].get();
|
|
|
|
}
|
2002-03-19 07:10:33 +08:00
|
|
|
|
|
|
|
bool TextureCubeMap::imagesValid() const
|
|
|
|
{
|
|
|
|
for (int n=0; n<6; n++)
|
|
|
|
{
|
|
|
|
if (!_images[n].valid() || !_images[n]->data())
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
void TextureCubeMap::computeInternalFormat() const
|
|
|
|
{
|
|
|
|
if (imagesValid()) computeInternalFormatWithImage(*_images[0]);
|
2007-09-11 20:04:58 +08:00
|
|
|
else computeInternalFormatType();
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
2002-03-19 07:10:33 +08:00
|
|
|
|
|
|
|
void TextureCubeMap::apply(State& state) const
|
|
|
|
{
|
|
|
|
// 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();
|
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-17 04:58:05 +08:00
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
|
|
|
|
|
|
|
if (!extensions->isCubeMapSupported())
|
|
|
|
return;
|
2002-03-19 07:10:33 +08:00
|
|
|
|
2003-07-14 22:42:10 +08:00
|
|
|
// get the texture object for the current contextID.
|
|
|
|
TextureObject* textureObject = getTextureObject(contextID);
|
2002-03-19 07:10:33 +08:00
|
|
|
|
2009-12-09 01:33:01 +08:00
|
|
|
if (textureObject)
|
|
|
|
{
|
|
|
|
const osg::Image* image = _images[0].get();
|
|
|
|
if (image && getModifiedCount(0, contextID) != image->getModifiedCount())
|
|
|
|
{
|
|
|
|
// compute the internal texture format, this set the _internalFormat to an appropriate value.
|
|
|
|
computeInternalFormat();
|
|
|
|
|
|
|
|
GLsizei new_width, new_height, new_numMipmapLevels;
|
|
|
|
|
|
|
|
// compute the dimensions of the texture.
|
|
|
|
computeRequiredTextureDimensions(state, *image, new_width, new_height, new_numMipmapLevels);
|
|
|
|
|
|
|
|
if (!textureObject->match(GL_TEXTURE_CUBE_MAP, new_numMipmapLevels, _internalFormat, new_width, new_height, 1, _borderWidth))
|
|
|
|
{
|
|
|
|
Texture::releaseTextureObject(contextID, _textureObjectBuffer[contextID].get());
|
|
|
|
_textureObjectBuffer[contextID] = 0;
|
|
|
|
textureObject = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (textureObject)
|
2002-03-19 07:10:33 +08:00
|
|
|
{
|
2003-07-14 22:42:10 +08:00
|
|
|
textureObject->bind();
|
|
|
|
|
2002-11-12 00:11:48 +08:00
|
|
|
if (getTextureParameterDirty(state.getContextID())) applyTexParameters(GL_TEXTURE_CUBE_MAP,state);
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
if (_subloadCallback.valid())
|
2002-03-19 07:10:33 +08:00
|
|
|
{
|
2002-08-25 03:39:39 +08:00
|
|
|
_subloadCallback->subload(*this,state);
|
2002-03-19 07:10:33 +08:00
|
|
|
}
|
2003-04-01 05:41:17 +08:00
|
|
|
else
|
|
|
|
{
|
2003-04-07 17:46:06 +08:00
|
|
|
for (int n=0; n<6; n++)
|
|
|
|
{
|
2003-04-07 20:51:00 +08:00
|
|
|
const osg::Image* image = _images[n].get();
|
2005-02-09 18:39:45 +08:00
|
|
|
if (image && getModifiedCount((Face)n,contextID) != image->getModifiedCount())
|
2003-04-07 17:46:06 +08:00
|
|
|
{
|
2004-01-23 21:25:45 +08:00
|
|
|
applyTexImage2D_subload( state, faceTarget[n], _images[n].get(), _textureWidth, _textureHeight, _internalFormat, _numMipmapLevels);
|
2005-02-09 18:39:45 +08:00
|
|
|
getModifiedCount((Face)n,contextID) = image->getModifiedCount();
|
2003-04-07 17:46:06 +08:00
|
|
|
}
|
|
|
|
}
|
2003-04-01 05:41:17 +08:00
|
|
|
}
|
2002-08-16 21:33:32 +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_CUBE_MAP);
|
2003-07-14 22:42:10 +08:00
|
|
|
|
|
|
|
textureObject->bind();
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
applyTexParameters(GL_TEXTURE_CUBE_MAP,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...
|
2003-07-14 22:42:10 +08:00
|
|
|
//glBindTexture( GL_TEXTURE_CUBE_MAP, handle );
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2002-03-19 07:10:33 +08:00
|
|
|
}
|
|
|
|
else if (imagesValid())
|
|
|
|
{
|
2002-08-25 03:39:39 +08:00
|
|
|
|
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,*_images[0],_textureWidth, _textureHeight, _numMipmapLevels);
|
|
|
|
|
2005-05-06 17:04:41 +08:00
|
|
|
// cubemap textures must have square dimensions
|
|
|
|
if( _textureWidth != _textureHeight )
|
|
|
|
{
|
|
|
|
_textureWidth = _textureHeight = minimum( _textureWidth , _textureHeight );
|
|
|
|
}
|
|
|
|
|
2008-07-16 21:23:58 +08:00
|
|
|
textureObject = generateTextureObject(
|
2009-09-23 02:45:24 +08:00
|
|
|
this, contextID,GL_TEXTURE_CUBE_MAP,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0);
|
2003-07-14 22:42:10 +08:00
|
|
|
|
|
|
|
textureObject->bind();
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
applyTexParameters(GL_TEXTURE_CUBE_MAP,state);
|
2002-03-19 07:10:33 +08:00
|
|
|
|
2003-04-07 17:46:06 +08:00
|
|
|
for (int n=0; n<6; n++)
|
|
|
|
{
|
2003-04-07 20:51:00 +08:00
|
|
|
const osg::Image* image = _images[n].get();
|
2003-04-07 17:46:06 +08:00
|
|
|
if (image)
|
|
|
|
{
|
2003-07-14 22:42:10 +08:00
|
|
|
if (textureObject->isAllocated())
|
|
|
|
{
|
2004-01-23 21:25:45 +08:00
|
|
|
applyTexImage2D_subload( state, faceTarget[n], image, _textureWidth, _textureHeight, _internalFormat, _numMipmapLevels);
|
2003-07-14 22:42:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
applyTexImage2D_load( state, faceTarget[n], image, _textureWidth, _textureHeight, _numMipmapLevels);
|
|
|
|
}
|
2005-02-09 18:39:45 +08:00
|
|
|
getModifiedCount((Face)n,contextID) = image->getModifiedCount();
|
2003-04-07 17:46:06 +08:00
|
|
|
}
|
2003-04-07 20:51:00 +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))
|
2003-04-07 20:51:00 +08:00
|
|
|
{
|
2003-07-14 22:42:10 +08:00
|
|
|
TextureCubeMap* non_const_this = const_cast<TextureCubeMap*>(this);
|
|
|
|
for (int n=0; n<6; n++)
|
2004-07-27 21:24:35 +08:00
|
|
|
{
|
|
|
|
if (_images[n].valid() && _images[n]->getDataVariance()==STATIC)
|
|
|
|
{
|
2010-09-14 21:19:55 +08:00
|
|
|
non_const_this->_images[n] = NULL;
|
2004-07-27 21:24:35 +08:00
|
|
|
}
|
2003-04-07 20:51:00 +08:00
|
|
|
}
|
2003-04-07 17:46:06 +08:00
|
|
|
}
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2005-07-20 00:30:55 +08:00
|
|
|
}
|
|
|
|
else if ( (_textureWidth!=0) && (_textureHeight!=0) && (_internalFormat!=0) )
|
|
|
|
{
|
|
|
|
_textureObjectBuffer[contextID] = textureObject = generateTextureObject(
|
2009-09-23 02:45:24 +08:00
|
|
|
this, contextID,GL_TEXTURE_CUBE_MAP,_numMipmapLevels,_internalFormat,_textureWidth,_textureHeight,1,0);
|
2005-07-20 00:30:55 +08:00
|
|
|
|
|
|
|
textureObject->bind();
|
|
|
|
|
|
|
|
applyTexParameters(GL_TEXTURE_CUBE_MAP,state);
|
|
|
|
|
|
|
|
for (int n=0; n<6; n++)
|
|
|
|
{
|
|
|
|
// no image present, but dimensions at set so less create the texture
|
|
|
|
glTexImage2D( faceTarget[n], 0, _internalFormat,
|
|
|
|
_textureWidth, _textureHeight, _borderWidth,
|
2005-11-08 23:52:21 +08:00
|
|
|
_sourceFormat ? _sourceFormat : _internalFormat,
|
|
|
|
_sourceType ? _sourceType : GL_UNSIGNED_BYTE,
|
2005-07-20 00:30:55 +08:00
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
2002-08-25 03:39:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindTexture( GL_TEXTURE_CUBE_MAP, 0 );
|
2002-03-19 07:10:33 +08:00
|
|
|
}
|
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-03-19 07:10:33 +08:00
|
|
|
}
|
2002-09-17 04:58:05 +08:00
|
|
|
|
2005-07-25 21:05:57 +08:00
|
|
|
void TextureCubeMap::copyTexSubImageCubeMap(State& state, int face, int xoffset, int yoffset, int x, int y, int width, int height )
|
|
|
|
{
|
|
|
|
const unsigned int contextID = state.getContextID();
|
|
|
|
const Extensions* extensions = getExtensions(contextID,true);
|
|
|
|
|
|
|
|
if (!extensions->isCubeMapSupported())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_internalFormat==0) _internalFormat=GL_RGBA;
|
|
|
|
|
|
|
|
// get the texture object for the current contextID.
|
|
|
|
TextureObject* textureObject = getTextureObject(contextID);
|
|
|
|
|
|
|
|
if (!textureObject)
|
|
|
|
{
|
2005-11-04 20:08:16 +08:00
|
|
|
|
|
|
|
if (_textureWidth==0) _textureWidth = width;
|
|
|
|
if (_textureHeight==0) _textureHeight = height;
|
|
|
|
|
2005-07-25 21:05:57 +08:00
|
|
|
// create texture object.
|
|
|
|
apply(state);
|
|
|
|
|
|
|
|
textureObject = getTextureObject(contextID);
|
|
|
|
|
|
|
|
if (!textureObject)
|
|
|
|
{
|
|
|
|
// failed to create texture object
|
2010-05-28 23:47:52 +08:00
|
|
|
OSG_NOTICE<<"Warning : failed to create TextureCubeMap texture obeject, copyTexSubImageCubeMap abondoned."<<std::endl;
|
2005-07-25 21:05:57 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
GLenum target = faceTarget[face];
|
|
|
|
|
|
|
|
if (textureObject)
|
|
|
|
{
|
|
|
|
// we have a valid image
|
|
|
|
textureObject->bind();
|
|
|
|
|
|
|
|
applyTexParameters(GL_TEXTURE_CUBE_MAP, state);
|
|
|
|
|
|
|
|
bool needHardwareMipMap = (_min_filter != LINEAR && _min_filter != NEAREST);
|
|
|
|
bool hardwareMipMapOn = false;
|
|
|
|
if (needHardwareMipMap)
|
|
|
|
{
|
2008-05-28 19:19:41 +08:00
|
|
|
hardwareMipMapOn = isHardwareMipmapGenerationEnabled(state);
|
2005-07-25 21:05:57 +08:00
|
|
|
|
|
|
|
if (!hardwareMipMapOn)
|
|
|
|
{
|
2008-05-28 19:19:41 +08:00
|
|
|
// have to switch off mip mapping
|
2010-05-28 23:47:52 +08:00
|
|
|
OSG_NOTICE<<"Warning: TextureCubeMap::copyTexImage2D(,,,,) switch off mip mapping as hardware support not available."<<std::endl;
|
2005-07-25 21:05:57 +08:00
|
|
|
_min_filter = LINEAR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-05-28 19:19:41 +08:00
|
|
|
GenerateMipmapMode mipmapResult = mipmapBeforeTexImage(state, hardwareMipMapOn);
|
2005-07-25 21:05:57 +08:00
|
|
|
|
|
|
|
glCopyTexSubImage2D( target , 0, xoffset, yoffset, x, y, width, height);
|
|
|
|
|
2008-05-28 19:19:41 +08:00
|
|
|
mipmapAfterTexImage(state, mipmapResult);
|
2005-07-25 21:05:57 +08:00
|
|
|
|
|
|
|
// inform state that this texture is the current one bound.
|
|
|
|
state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-11 20:04:58 +08:00
|
|
|
void TextureCubeMap::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)
|
|
|
|
{
|
|
|
|
// bind texture
|
|
|
|
textureObject->bind();
|
|
|
|
|
|
|
|
// compute number of mipmap levels
|
|
|
|
int width = _textureWidth;
|
|
|
|
int height = _textureHeight;
|
2007-10-01 16:50:58 +08:00
|
|
|
int numMipmapLevels = Image::computeNumberOfMipmapLevels(width, height);
|
2007-09-11 20:04:58 +08:00
|
|
|
|
|
|
|
// we do not reallocate the level 0, since it was already allocated
|
|
|
|
width >>= 1;
|
|
|
|
height >>= 1;
|
|
|
|
|
|
|
|
for( GLsizei k = 1; k < numMipmapLevels && (width || height); k++)
|
|
|
|
{
|
|
|
|
if (width == 0)
|
|
|
|
width = 1;
|
|
|
|
if (height == 0)
|
|
|
|
height = 1;
|
|
|
|
|
|
|
|
for (int n=0; n<6; n++)
|
|
|
|
{
|
|
|
|
glTexImage2D( faceTarget[n], k, _internalFormat,
|
|
|
|
width, height, _borderWidth,
|
|
|
|
_sourceFormat ? _sourceFormat : _internalFormat,
|
|
|
|
_sourceType ? _sourceType : GL_UNSIGNED_BYTE,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
width >>= 1;
|
|
|
|
height >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// inform state that this texture is the current one bound.
|
|
|
|
state.haveAppliedTextureAttribute(state.getActiveTextureUnit(), this);
|
|
|
|
}
|
|
|
|
}
|
2005-07-25 21:05:57 +08:00
|
|
|
|
2002-09-17 04:58:05 +08:00
|
|
|
typedef buffered_value< ref_ptr<TextureCubeMap::Extensions> > BufferedExtensions;
|
|
|
|
static BufferedExtensions s_extensions;
|
|
|
|
|
2003-04-10 21:41:45 +08:00
|
|
|
TextureCubeMap::Extensions* TextureCubeMap::getExtensions(unsigned int contextID,bool createIfNotInitalized)
|
2002-09-17 04:58:05 +08:00
|
|
|
{
|
2005-04-26 21:15:27 +08:00
|
|
|
if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions(contextID);
|
2002-09-17 04:58:05 +08:00
|
|
|
return s_extensions[contextID].get();
|
|
|
|
}
|
|
|
|
|
2003-02-15 04:27:23 +08:00
|
|
|
void TextureCubeMap::setExtensions(unsigned int contextID,Extensions* extensions)
|
2002-09-17 04:58:05 +08:00
|
|
|
{
|
|
|
|
s_extensions[contextID] = extensions;
|
|
|
|
}
|
|
|
|
|
2005-04-26 21:15:27 +08:00
|
|
|
TextureCubeMap::Extensions::Extensions(unsigned int contextID)
|
2002-09-17 04:58:05 +08:00
|
|
|
{
|
2007-06-28 04:36:16 +08:00
|
|
|
setupGLExtensions(contextID);
|
2002-09-17 04:58:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TextureCubeMap::Extensions::Extensions(const Extensions& rhs):
|
|
|
|
Referenced()
|
|
|
|
{
|
|
|
|
_isCubeMapSupported = rhs._isCubeMapSupported;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCubeMap::Extensions::lowestCommonDenominator(const Extensions& rhs)
|
|
|
|
{
|
|
|
|
if (!rhs._isCubeMapSupported) _isCubeMapSupported = false;
|
|
|
|
}
|
|
|
|
|
2007-06-28 04:36:16 +08:00
|
|
|
void TextureCubeMap::Extensions::setupGLExtensions(unsigned int contextID)
|
2002-09-17 04:58:05 +08:00
|
|
|
{
|
2009-11-11 01:01:08 +08:00
|
|
|
_isCubeMapSupported = OSG_GLES2_FEATURES || OSG_GL3_FEATURES ||
|
|
|
|
isGLExtensionSupported(contextID,"GL_ARB_texture_cube_map") ||
|
2005-04-26 21:15:27 +08:00
|
|
|
isGLExtensionSupported(contextID,"GL_EXT_texture_cube_map") ||
|
2002-09-17 04:58:05 +08:00
|
|
|
strncmp((const char*)glGetString(GL_VERSION),"1.3",3)>=0;;
|
|
|
|
}
|