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.
|
|
|
|
*/
|
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>
|
|
|
|
|
|
|
|
#include <osg/GLU>
|
|
|
|
|
|
|
|
|
|
|
|
using namespace osg;
|
|
|
|
|
|
|
|
|
2002-03-21 06:39:51 +08:00
|
|
|
// include/osg/TextureCubeMap defines GL_TEXTURE_CUBE_MAP to be
|
|
|
|
// 0x8513 which is the same as GL_TEXTURE_CUBE_MAP_ARB & _EXT.
|
|
|
|
// assume its the same as what OpenGL 1.3 defines.
|
|
|
|
|
2002-03-19 07:10:33 +08:00
|
|
|
#ifndef GL_ARB_texture_cube_map
|
|
|
|
#define GL_ARB_texture_cube_map 1
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_ARB 0x8513
|
|
|
|
#define GL_TEXTURE_BINDING_CUBE_MAP_ARB 0x8514
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB 0x8515
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB 0x8516
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB 0x8517
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB 0x8518
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB 0x8519
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB 0x851A
|
|
|
|
#define GL_PROXY_TEXTURE_CUBE_MAP_ARB 0x851B
|
|
|
|
#define GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB 0x851C
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef GL_EXT_texture_cube_map
|
|
|
|
#define GL_EXT_texture_cube_map 1
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_EXT 0x8513
|
|
|
|
#define GL_TEXTURE_BINDING_CUBE_MAP_EXT 0x8514
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT 0x8515
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT 0x8516
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT 0x8517
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT 0x8518
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT 0x8519
|
|
|
|
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT 0x851A
|
|
|
|
#define GL_PROXY_TEXTURE_CUBE_MAP_EXT 0x851B
|
|
|
|
#define GL_MAX_CUBE_MAP_TEXTURE_SIZE_EXT 0x851C
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef GL_ARB_texture_cube_map
|
|
|
|
# define CUBE_MAP_POSITIVE_X GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB
|
|
|
|
# define CUBE_MAP_NEGATIVE_X GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB
|
|
|
|
# define CUBE_MAP_POSITIVE_Y GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB
|
|
|
|
# define CUBE_MAP_NEGATIVE_Y GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB
|
|
|
|
# define CUBE_MAP_POSITIVE_Z GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB
|
|
|
|
# define CUBE_MAP_NEGATIVE_Z GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB
|
|
|
|
#elif GL_EXT_texture_cube_map
|
|
|
|
# define CUBE_MAP_POSITIVE_X GL_TEXTURE_CUBE_MAP_POSITIVE_X_EXT
|
|
|
|
# define CUBE_MAP_NEGATIVE_X GL_TEXTURE_CUBE_MAP_NEGATIVE_X_EXT
|
|
|
|
# define CUBE_MAP_POSITIVE_Y GL_TEXTURE_CUBE_MAP_POSITIVE_Y_EXT
|
|
|
|
# define CUBE_MAP_NEGATIVE_Y GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_EXT
|
|
|
|
# define CUBE_MAP_POSITIVE_Z GL_TEXTURE_CUBE_MAP_POSITIVE_Z_EXT
|
|
|
|
# define CUBE_MAP_NEGATIVE_Z GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_EXT
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
# if GL_EXT_texture_cube_map || GL_ARB_texture_cube_map
|
|
|
|
static GLenum faceTarget[6] =
|
|
|
|
{
|
|
|
|
CUBE_MAP_POSITIVE_X,
|
|
|
|
CUBE_MAP_NEGATIVE_X,
|
|
|
|
CUBE_MAP_POSITIVE_Y,
|
|
|
|
CUBE_MAP_NEGATIVE_Y,
|
|
|
|
CUBE_MAP_POSITIVE_Z,
|
|
|
|
CUBE_MAP_NEGATIVE_Z
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
2003-04-07 17:46:06 +08:00
|
|
|
_modifiedTag[0].setAllElementsTo(0);
|
|
|
|
_modifiedTag[1].setAllElementsTo(0);
|
|
|
|
_modifiedTag[2].setAllElementsTo(0);
|
|
|
|
_modifiedTag[3].setAllElementsTo(0);
|
|
|
|
_modifiedTag[4].setAllElementsTo(0);
|
|
|
|
_modifiedTag[5].setAllElementsTo(0);
|
|
|
|
|
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
|
|
|
|
// used by the COMPARE_StateAttribute_Paramter macro's below.
|
|
|
|
COMPARE_StateAttribute_Types(TextureCubeMap,sa)
|
|
|
|
|
|
|
|
for (int n=0; n<6; n++)
|
|
|
|
{
|
|
|
|
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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
COMPARE_StateAttribute_Parameter(_textureWidth)
|
|
|
|
COMPARE_StateAttribute_Parameter(_textureHeight)
|
|
|
|
COMPARE_StateAttribute_Parameter(_subloadCallback)
|
|
|
|
|
2002-03-19 07:10:33 +08:00
|
|
|
return 0; // passed all the above comparison macro's, must be equal.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
void TextureCubeMap::setImage( Face face, Image* image)
|
2002-03-19 07:10:33 +08:00
|
|
|
{
|
|
|
|
_images[face] = image;
|
2003-04-07 17:46:06 +08:00
|
|
|
_modifiedTag[face].setAllElementsTo(0);
|
2002-03-19 07:10:33 +08:00
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
Image* TextureCubeMap::getImage(Face face)
|
2002-03-21 06:39:51 +08:00
|
|
|
{
|
|
|
|
return _images[face].get();
|
|
|
|
}
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
const Image* TextureCubeMap::getImage(Face 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]);
|
|
|
|
}
|
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();
|
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
|
|
|
|
|
|
|
// get the globj for the current contextID.
|
2002-08-16 21:33:32 +08:00
|
|
|
GLuint& handle = getTextureObject(contextID);
|
2002-03-19 07:10:33 +08:00
|
|
|
|
|
|
|
if (handle != 0)
|
|
|
|
{
|
2002-08-25 03:39:39 +08:00
|
|
|
glBindTexture( GL_TEXTURE_CUBE_MAP, handle );
|
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();
|
2003-04-07 17:46:06 +08:00
|
|
|
if (image && getModifiedTag((Face)n,contextID) != image->getModifiedTag())
|
|
|
|
{
|
2003-04-30 19:40:17 +08:00
|
|
|
applyTexImage2D_subload( faceTarget[n], _images[n].get(), state, _textureWidth, _textureHeight, _numMipmapLevels);
|
2003-04-07 17:46:06 +08:00
|
|
|
getModifiedTag((Face)n,contextID) = image->getModifiedTag();
|
|
|
|
}
|
|
|
|
}
|
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())
|
|
|
|
{
|
|
|
|
glGenTextures( 1L, (GLuint *)&handle );
|
|
|
|
glBindTexture( GL_TEXTURE_CUBE_MAP, handle );
|
|
|
|
|
|
|
|
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...
|
|
|
|
glBindTexture( GL_TEXTURE_CUBE_MAP, handle );
|
|
|
|
|
2002-03-19 07:10:33 +08:00
|
|
|
}
|
|
|
|
else if (imagesValid())
|
|
|
|
{
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2002-03-19 07:10:33 +08:00
|
|
|
glGenTextures( 1L, (GLuint *)&handle );
|
2002-08-25 03:39:39 +08:00
|
|
|
glBindTexture( GL_TEXTURE_CUBE_MAP, handle );
|
|
|
|
|
|
|
|
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-04-30 19:40:17 +08:00
|
|
|
applyTexImage2D_load( faceTarget[n], image, state, _textureWidth, _textureHeight, _numMipmapLevels);
|
2003-04-07 17:46:06 +08:00
|
|
|
getModifiedTag((Face)n,contextID) = image->getModifiedTag();
|
|
|
|
}
|
2003-04-07 20:51:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_unrefImageDataAfterApply)
|
|
|
|
{
|
|
|
|
// only unref image once all the graphics contexts has been set up.
|
|
|
|
unsigned int numLeftToBind=0;
|
|
|
|
for(unsigned int i=0;i<DisplaySettings::instance()->getMaxNumberOfGraphicsContexts();++i)
|
|
|
|
{
|
|
|
|
if (_handleList[i]==0) ++numLeftToBind;
|
|
|
|
}
|
|
|
|
if (numLeftToBind==0)
|
|
|
|
{
|
|
|
|
TextureCubeMap* non_const_this = const_cast<TextureCubeMap*>(this);
|
|
|
|
for (int n=0; n<6; n++)
|
|
|
|
{
|
|
|
|
non_const_this->_images[n] = 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
|
|
|
// 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...
|
2002-08-25 03:39:39 +08:00
|
|
|
glBindTexture( GL_TEXTURE_CUBE_MAP, handle );
|
|
|
|
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glBindTexture( GL_TEXTURE_CUBE_MAP, 0 );
|
2002-03-19 07:10:33 +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
|
|
|
{
|
|
|
|
if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureCubeMap::Extensions::Extensions()
|
|
|
|
{
|
|
|
|
setupGLExtenions();
|
|
|
|
}
|
|
|
|
|
|
|
|
TextureCubeMap::Extensions::Extensions(const Extensions& rhs):
|
|
|
|
Referenced()
|
|
|
|
{
|
|
|
|
_isCubeMapSupported = rhs._isCubeMapSupported;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCubeMap::Extensions::lowestCommonDenominator(const Extensions& rhs)
|
|
|
|
{
|
|
|
|
if (!rhs._isCubeMapSupported) _isCubeMapSupported = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextureCubeMap::Extensions::setupGLExtenions()
|
|
|
|
{
|
|
|
|
_isCubeMapSupported = isGLExtensionSupported("GL_ARB_texture_cube_map") ||
|
|
|
|
isGLExtensionSupported("GL_EXT_texture_cube_map") ||
|
|
|
|
strncmp((const char*)glGetString(GL_VERSION),"1.3",3)>=0;;
|
|
|
|
}
|