2002-07-17 04:07:32 +08:00
|
|
|
//C++ header - Open Scene Graph - Copyright (C) 1998-2002 Robert Osfield
|
2001-10-04 23:12:57 +08:00
|
|
|
//Distributed under the terms of the GNU Library General Public License (LGPL)
|
|
|
|
//as published by the Free Software Foundation.
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
#ifndef OSG_TEXTURE
|
|
|
|
#define OSG_TEXTURE 1
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
#include <osg/GL>
|
|
|
|
#include <osg/Types>
|
|
|
|
#include <osg/Image>
|
|
|
|
#include <osg/StateAttribute>
|
|
|
|
#include <osg/ref_ptr>
|
|
|
|
#include <osg/Vec4>
|
2002-09-05 19:42:55 +08:00
|
|
|
#include <osg/buffered_value>
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
// if not defined by gl.h use the definition found in:
|
|
|
|
// http://oss.sgi.com/projects/ogl-sample/registry/EXT/texture_filter_anisotropic.txt
|
|
|
|
#ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT
|
|
|
|
#define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef GL_ARB_texture_compression
|
|
|
|
#define GL_COMPRESSED_ALPHA_ARB 0x84E9
|
|
|
|
#define GL_COMPRESSED_LUMINANCE_ARB 0x84EA
|
|
|
|
#define GL_COMPRESSED_LUMINANCE_ALPHA_ARB 0x84EB
|
|
|
|
#define GL_COMPRESSED_INTENSITY_ARB 0x84EC
|
|
|
|
#define GL_COMPRESSED_RGB_ARB 0x84ED
|
|
|
|
#define GL_COMPRESSED_RGBA_ARB 0x84EE
|
|
|
|
#define GL_TEXTURE_COMPRESSION_HINT_ARB 0x84EF
|
|
|
|
#define GL_TEXTURE_IMAGE_SIZE_ARB 0x86A0
|
|
|
|
#define GL_TEXTURE_COMPRESSED_ARB 0x86A1
|
|
|
|
#define GL_NUM_COMPRESSED_TEXTURE_FORMATS_ARB 0x86A2
|
|
|
|
#define GL_COMPRESSED_TEXTURE_FORMATS_ARB 0x86A3
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef GL_EXT_texture_compression_s3tc
|
|
|
|
#define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
|
|
|
|
#define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
|
|
|
|
#define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
|
|
|
|
#define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef GL_MIRRORED_REPEAT_IBM
|
|
|
|
#define GL_MIRRORED_REPEAT_IBM 0x8370
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef GL_CLAMP_TO_EDGE
|
|
|
|
#define GL_CLAMP_TO_EDGE 0x812F
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef GL_CLAMP_TO_BORDER_ARB
|
|
|
|
#define GL_CLAMP_TO_BORDER_ARB 0x812D
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef GL_GENERATE_MIPMAP_SGIS
|
|
|
|
#define GL_GENERATE_MIPMAP_SGIS 0x8191
|
|
|
|
#define GL_GENERATE_MIPMAP_HINT_SGIS 0x8192
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef GL_TEXTURE_3D
|
|
|
|
#define GL_TEXTURE_3D 0x806F
|
|
|
|
#endif
|
2002-08-25 23:31:44 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
namespace osg {
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Texture base class which encapsulates OpenGl texture functionality which common betweent the various types of OpenGL textures.*/
|
|
|
|
class SG_EXPORT Texture : public osg::StateAttribute
|
2001-01-11 00:32:10 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
public :
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
|
2001-01-11 00:32:10 +08:00
|
|
|
Texture();
|
|
|
|
|
2002-01-29 22:04:06 +08:00
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy.*/
|
2002-07-29 07:28:27 +08:00
|
|
|
Texture(const Texture& text,const CopyOp& copyop=CopyOp::SHALLOW_COPY);
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
virtual osg::Object* cloneType() const = 0;
|
|
|
|
virtual osg::Object* clone(const CopyOp& copyop) const = 0;
|
|
|
|
virtual bool isSameKindAs(const osg::Object* obj) const { return dynamic_cast<const Texture *>(obj)!=NULL; }
|
|
|
|
virtual const char* libraryName() const { return "osg"; }
|
|
|
|
virtual const char* className() const { return "Texture"; }
|
2002-09-02 20:31:35 +08:00
|
|
|
virtual Type getType() const { return TEXTURE; }
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
virtual bool isTextureAttribute() const { return true; }
|
|
|
|
|
|
|
|
enum WrapParameter {
|
|
|
|
WRAP_S,
|
|
|
|
WRAP_T,
|
|
|
|
WRAP_R
|
|
|
|
};
|
|
|
|
|
|
|
|
enum WrapMode {
|
|
|
|
CLAMP = GL_CLAMP,
|
|
|
|
CLAMP_TO_EDGE = GL_CLAMP_TO_EDGE,
|
|
|
|
CLAMP_TO_BORDER = GL_CLAMP_TO_BORDER_ARB,
|
|
|
|
REPEAT = GL_REPEAT,
|
|
|
|
MIRROR = GL_MIRRORED_REPEAT_IBM
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Set the texture wrap mode.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
void setWrap(WrapParameter which, WrapMode wrap);
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Get the texture wrap mode.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
WrapMode getWrap(WrapParameter which) const;
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
|
|
|
|
/** Sets the border color for this texture. Makes difference only if
|
|
|
|
* wrap mode is CLAMP_TO_BORDER */
|
2002-09-04 16:14:04 +08:00
|
|
|
void setBorderColor(const Vec4& color) { _borderColor = color; dirtyTextureParameters(); }
|
2002-08-28 23:28:11 +08:00
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
const Vec4& getBorderColor() const { return _borderColor; }
|
2002-07-29 07:28:27 +08:00
|
|
|
|
2002-07-09 17:35:42 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
enum FilterParameter {
|
|
|
|
MIN_FILTER,
|
|
|
|
MAG_FILTER
|
|
|
|
};
|
2001-09-22 10:42:08 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
enum FilterMode {
|
|
|
|
LINEAR = GL_LINEAR,
|
|
|
|
LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR,
|
|
|
|
LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST,
|
|
|
|
NEAREST = GL_NEAREST,
|
|
|
|
NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR,
|
|
|
|
NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST
|
|
|
|
};
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Set the texture filter mode.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
void setFilter(FilterParameter which, FilterMode filter);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Get the texture filter mode.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
FilterMode getFilter(FilterParameter which) const;
|
2002-07-29 07:28:27 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Set the maximum anisotropy value, default value is 1.0 for
|
|
|
|
* no anisotropic filtering. If hardware does not support anisotropic
|
|
|
|
* filtering then normal filtering is used, equivilant to a max anisotropy value of 1.0.
|
|
|
|
* valid range is 1.0f upwards. The maximum value depends on the graphics
|
|
|
|
* system being used.*/
|
|
|
|
void setMaxAnisotropy(float anis);
|
|
|
|
|
|
|
|
/** Get the maximum anisotropy value.*/
|
|
|
|
inline float getMaxAnisotropy() const { return _maxAnisotropy; }
|
|
|
|
|
|
|
|
enum InternalFormatMode {
|
|
|
|
USE_IMAGE_DATA_FORMAT,
|
|
|
|
USE_USER_DEFINED_FORMAT,
|
|
|
|
USE_ARB_COMPRESSION,
|
|
|
|
USE_S3TC_DXT1_COMPRESSION,
|
|
|
|
USE_S3TC_DXT3_COMPRESSION,
|
|
|
|
USE_S3TC_DXT5_COMPRESSION
|
|
|
|
};
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Set the internal format mode.
|
|
|
|
* Note, If the mode is set USE_IMAGE_DATA_FORMAT, USE_ARB_COMPRESSION,
|
|
|
|
* USE_S3TC_COMPRESSION the internalFormat is automatically selected,
|
|
|
|
* and will overwrite the previous _internalFormat.
|
2001-09-20 05:08:56 +08:00
|
|
|
*/
|
2002-09-02 20:31:35 +08:00
|
|
|
inline void setInternalFormatMode(InternalFormatMode mode) { _internalFormatMode = mode; }
|
2002-03-14 08:07:29 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Get the internal format mode.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
inline InternalFormatMode getInternalFormatMode() const { return _internalFormatMode; }
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
/** Set the internal format to use when creating OpenGL textures.
|
|
|
|
* Also sets the internalFormatMode to USE_USER_DEFINED_FORMAT.
|
|
|
|
*/
|
2002-09-02 20:31:35 +08:00
|
|
|
inline void setInternalFormat(GLint internalFormat)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
_internalFormatMode = USE_USER_DEFINED_FORMAT;
|
2002-08-25 03:39:39 +08:00
|
|
|
_internalFormat = internalFormat;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Get the internal format to use when creating OpenGL textures.*/
|
2002-09-02 20:31:35 +08:00
|
|
|
inline GLint getInternalFormat() const { if (_internalFormat==0) computeInternalFormat(); return _internalFormat; }
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
bool isCompressedInternalFormat() const;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
|
2002-09-05 19:42:55 +08:00
|
|
|
// /** Get the handle to the texture object for the current context.*/
|
|
|
|
// /** return the OpenGL texture object for specified context.*/
|
|
|
|
// inline GLuint& getTextureObject(uint contextID) const
|
|
|
|
// {
|
|
|
|
// // pad out handle list if required.
|
|
|
|
// if (_handleList.size()<=contextID)
|
|
|
|
// _handleList.resize(contextID+1,0);
|
|
|
|
//
|
|
|
|
// // get the globj for the current contextID.
|
|
|
|
// return _handleList[contextID];
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// inline uint& getModifiedTag(uint contextID) const
|
|
|
|
// {
|
|
|
|
// // pad out handle list if required.
|
|
|
|
// if (_modifiedTag.size()<=contextID)
|
|
|
|
// _modifiedTag.resize(contextID+1,0);
|
|
|
|
//
|
|
|
|
// // get the modified tag for the current contextID.
|
|
|
|
// return _modifiedTag[contextID];
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// inline uint& getTextureParameterDity(uint contextID) const
|
|
|
|
// {
|
|
|
|
// // pad out handle list if required.
|
|
|
|
// if (_texParametersDirtyList.size()<=contextID)
|
|
|
|
// _texParametersDirtyList.resize(contextID+1,0);
|
|
|
|
//
|
|
|
|
// // get the dirty flag for the current contextID.
|
|
|
|
// return _texParametersDirtyList[contextID];
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Get the handle to the texture object for the current context.*/
|
|
|
|
/** return the OpenGL texture object for specified context.*/
|
2002-09-04 16:14:04 +08:00
|
|
|
inline GLuint& getTextureObject(uint contextID) const
|
2002-07-29 07:28:27 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
// get the globj for the current contextID.
|
|
|
|
return _handleList[contextID];
|
2002-07-29 07:28:27 +08:00
|
|
|
}
|
|
|
|
|
2002-09-04 16:14:04 +08:00
|
|
|
inline uint& getModifiedTag(uint contextID) const
|
2002-07-29 07:28:27 +08:00
|
|
|
{
|
2002-08-28 23:28:11 +08:00
|
|
|
// get the modified tag for the current contextID.
|
|
|
|
return _modifiedTag[contextID];
|
2002-07-29 07:28:27 +08:00
|
|
|
}
|
|
|
|
|
2002-09-04 16:14:04 +08:00
|
|
|
inline uint& getTextureParameterDity(uint contextID) const
|
|
|
|
{
|
|
|
|
// get the dirty flag for the current contextID.
|
|
|
|
return _texParametersDirtyList[contextID];
|
|
|
|
}
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Force a recompile on next apply() of associated OpenGL texture objects.*/
|
|
|
|
void dirtyTextureObject();
|
2002-07-29 07:28:27 +08:00
|
|
|
|
2002-09-04 16:14:04 +08:00
|
|
|
/** Force a resetting on next apply() of associated OpenGL texture parameters.*/
|
|
|
|
void dirtyTextureParameters();
|
2002-03-28 06:51:58 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
|
|
|
|
/** 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.*/
|
|
|
|
static void deleteTextureObject(uint contextID,GLuint handle);
|
|
|
|
|
|
|
|
/** flush all the cached display list which need to be deleted
|
|
|
|
* in the OpenGL context related to contextID.*/
|
|
|
|
static void flushDeletedTextureObjects(uint contextID);
|
2002-08-16 23:14:43 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Texture is pure virtual base class, apply must be overriden. */
|
|
|
|
virtual void apply(State& state) const = 0;
|
2002-08-16 23:14:43 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Calls apply(state) to compile the texture. */
|
|
|
|
virtual void compile(State& state) const;
|
2002-08-16 23:14:43 +08:00
|
|
|
|
2002-09-17 04:58:05 +08:00
|
|
|
/** Extensions class which encapsulates the querring of extensions and
|
|
|
|
* associated function pointers, and provide convinience wrappers to
|
|
|
|
* check for the extensions or use the associated functions.*/
|
|
|
|
class Extensions : public osg::Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Extensions();
|
|
|
|
|
|
|
|
Extensions(const Extensions& rhs);
|
|
|
|
|
|
|
|
void lowestCommonDenominator(const Extensions& rhs);
|
|
|
|
|
|
|
|
void setupGLExtenions();
|
|
|
|
|
|
|
|
bool isTextureFilterAnisotropicSupported() const { return _isTextureFilterAnisotropicSupported; }
|
|
|
|
bool isTextureCompressionARBSupported() const { return _isTextureCompressionARBSupported; }
|
|
|
|
bool isTextureCompressionS3TCSupported() const { return _isTextureCompressionS3TCSupported; }
|
|
|
|
bool isTextureMirroredRepeatSupported() const { return _isTextureMirroredRepeatSupported; }
|
|
|
|
bool isTextureEdgeClampSupported() const { return _isTextureEdgeClampSupported; }
|
|
|
|
bool isTextureBorderClampSupported() const { return _isTextureBorderClampSupported; }
|
|
|
|
|
|
|
|
GLint maxTextureSize() const { return _maxTextureSize; }
|
|
|
|
|
|
|
|
bool isCompressedTexImage2DSupported() const { return _glCompressedTexImage2D!=0; }
|
|
|
|
|
|
|
|
void glCompressedTexImage2D(GLenum target, GLint level, GLenum internalformat, GLsizei width, GLsizei height, GLint border, GLsizei imageSize, const GLvoid *data) const;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
~Extensions() {}
|
|
|
|
|
|
|
|
bool _isTextureFilterAnisotropicSupported;
|
|
|
|
bool _isTextureCompressionARBSupported;
|
|
|
|
bool _isTextureCompressionS3TCSupported;
|
|
|
|
bool _isTextureMirroredRepeatSupported;
|
|
|
|
bool _isTextureEdgeClampSupported;
|
|
|
|
bool _isTextureBorderClampSupported;
|
|
|
|
|
|
|
|
GLint _maxTextureSize;
|
|
|
|
|
|
|
|
void* _glCompressedTexImage2D;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Function to call to get the extension of a specified context.
|
|
|
|
* If the Exentsion object for that context has not yet been created then
|
|
|
|
* and the 'createIfNotInitalized' flag been set to false then returns NULL.
|
|
|
|
* If 'createIfNotInitalized' is true then the Extensions object is
|
|
|
|
* automatically created. However, in this case the extension object
|
|
|
|
* only be created with the graphics context associated with ContextID..*/
|
|
|
|
static const Extensions* getExtensions(uint contextID,bool createIfNotInitalized);
|
|
|
|
|
|
|
|
/** setExtensions allows users to override the extensions across graphics contexts.
|
|
|
|
* typically used when you have different extensions supported across graphics pipes
|
|
|
|
* but need to ensure that they all use the same low common denominator extensions.*/
|
|
|
|
static void setExtensions(uint contextID,Extensions* extensions);
|
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
protected :
|
|
|
|
|
|
|
|
virtual ~Texture();
|
2002-08-16 21:33:32 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
virtual void computeInternalFormat() const = 0;
|
2002-08-16 21:33:32 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
void computeInternalFormatWithImage(osg::Image& image) const;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
bool isCompressedInternalFormat(GLint internalFormat) const;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Helper method which does setting of texture paramters. */
|
|
|
|
void applyTexParameters(GLenum target, State& state) const;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** Helper method which does the creation of the texture itself, and
|
|
|
|
* does not set or use texture binding. */
|
|
|
|
void applyTexImage2D(GLenum target, Image* image, State& state, GLsizei& width, GLsizei& height,GLsizei& numMimpmapLevels) const;
|
2002-03-15 00:01:21 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
/** return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs.*/
|
|
|
|
int compareTexture(const Texture& rhs) const;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-09-05 19:42:55 +08:00
|
|
|
// typedef std::vector<GLuint> TextureNameList;
|
|
|
|
typedef buffered_value<GLuint> TextureNameList;
|
2002-08-28 23:28:11 +08:00
|
|
|
mutable TextureNameList _handleList;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-09-05 19:42:55 +08:00
|
|
|
// typedef std::vector<uint> ImageModifiedTag;
|
|
|
|
typedef buffered_value<uint> ImageModifiedTag;
|
2002-08-28 23:28:11 +08:00
|
|
|
mutable ImageModifiedTag _modifiedTag;
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2002-09-05 19:42:55 +08:00
|
|
|
// typedef std::vector<uint> TexParameterDirtyList;
|
|
|
|
typedef buffered_value<uint> TexParameterDirtyList;
|
2002-09-04 16:14:04 +08:00
|
|
|
mutable TexParameterDirtyList _texParametersDirtyList;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
WrapMode _wrap_s;
|
|
|
|
WrapMode _wrap_t;
|
|
|
|
WrapMode _wrap_r;
|
2001-01-11 00:32:10 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
FilterMode _min_filter;
|
|
|
|
FilterMode _mag_filter;
|
|
|
|
float _maxAnisotropy;
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
Vec4 _borderColor;
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
InternalFormatMode _internalFormatMode;
|
|
|
|
mutable GLint _internalFormat;
|
2002-08-25 03:39:39 +08:00
|
|
|
|
2002-08-28 23:28:11 +08:00
|
|
|
};
|
2002-08-25 03:39:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2002-08-25 23:31:44 +08:00
|
|
|
#endif
|