diff --git a/include/osg/Texture3D b/include/osg/Texture3D index ee46d8ce5..5f8b9c0c3 100644 --- a/include/osg/Texture3D +++ b/include/osg/Texture3D @@ -2,8 +2,6 @@ //Distributed under the terms of the GNU Library General Public License (LGPL) //as published by the Free Software Foundation. -// -*-c++-*- - #ifndef OSG_TEXTURE3D #define OSG_TEXTURE3D 1 @@ -93,25 +91,51 @@ class SG_EXPORT Texture3D : public Texture * texture and bind it, subsequent apply will simple bind to texture.*/ virtual void apply(State& state) const; - - struct Extensions : public osg::Referenced - { - Extensions(); - - bool isTexture3DSupported; - bool isTexture3DFast; - GLint maxTexture3DSize; - - - void glTexImage3D( GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels); - void glTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels); - void glCopyTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height ); - void gluBuild3DMipmaps( GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *data); + /** 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(); + + bool isTexture3DSupported() const { return _isTexture3DSupported; } + bool isTexture3DFast() const { return _isTexture3DFast; } + GLint maxTexture3DSize() const { return _maxTexture3DSize; } + + void glTexImage3D( GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels) const; + void glTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels) const; + void glCopyTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height ) const; + void gluBuild3DMipmaps( GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *data) const; + + protected: + + ~Extensions() {} + + typedef void (APIENTRY * GLTexImage3DProc) ( GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + typedef void (APIENTRY * GLTexSubImage3DProc) ( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels); + typedef void (APIENTRY * GLCopyTexSubImageProc) ( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height ); + typedef void (APIENTRY * GLUBuild3DMipMapsProc) ( GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *data); + + bool _isTexture3DSupported; + bool _isTexture3DFast; + GLint _maxTexture3DSize; + + void* _glTexImage3D; + void* _glTexSubImage3D; + void* _glCopyTexSubImage3D; + void* _gluBuild3DMipmaps; }; - static Extensions* getExtensions(uint contextID=0); + /** 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); protected : diff --git a/src/Demos/osgtexture3D/osgtexture3D.cpp b/src/Demos/osgtexture3D/osgtexture3D.cpp index e73f34759..076b4b610 100644 --- a/src/Demos/osgtexture3D/osgtexture3D.cpp +++ b/src/Demos/osgtexture3D/osgtexture3D.cpp @@ -49,8 +49,7 @@ class ConstructStateCallback : public osg::NodeCallback } // get max 3D texture size - GLint textureSize; - glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &textureSize); + GLint textureSize = osg::Texture3D::getExtensions(0,true)->maxTexture3DSize(); if (textureSize > 256) textureSize = 256; diff --git a/src/osg/Texture3D.cpp b/src/osg/Texture3D.cpp index e1f889da7..c206d1290 100644 --- a/src/osg/Texture3D.cpp +++ b/src/osg/Texture3D.cpp @@ -87,9 +87,9 @@ void Texture3D::apply(State& state) const // current OpenGL context. const uint contextID = state.getContextID(); - Extensions* extensions = getExtensions(contextID); + const Extensions* extensions = getExtensions(contextID,true); - if (!extensions->isTexture3DSupported) + if (!extensions->isTexture3DSupported()) { notify(WARN)<<"Warning: Texture3D::apply(..) failed, 3D texturing is not support by OpenGL driver."< > BufferedExtensions; static BufferedExtensions s_extensions; - if (!s_extensions[contextID]) s_extensions[contextID] = new Extensions; + if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions; return s_extensions[contextID].get(); } +#ifndef GL_MAX_3D_TEXTURE_SIZE +#define GL_MAX_3D_TEXTURE_SIZE 0x8073 +#endif + Texture3D::Extensions::Extensions() { - isTexture3DFast = isGLExtensionSupported("GL_EXT_texture3D"); + _isTexture3DFast = isGLExtensionSupported("GL_EXT_texture3D"); - if (isTexture3DFast) isTexture3DSupported = true; - else isTexture3DSupported = strncmp((const char*)glGetString(GL_VERSION),"1.2",3)>=0; + if (_isTexture3DFast) _isTexture3DSupported = true; + else _isTexture3DSupported = strncmp((const char*)glGetString(GL_VERSION),"1.2",3)>=0; - glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &maxTexture3DSize); + glGetIntegerv(GL_MAX_3D_TEXTURE_SIZE, &_maxTexture3DSize); + + _glTexImage3D = getGLExtensionFuncPtr("glTexImage3D","glTexImage3DEXT");; + _glTexSubImage3D = getGLExtensionFuncPtr("glTexSubImage3D","glTexSubImage3DEXT"); + _glCopyTexSubImage3D = getGLExtensionFuncPtr("glCopyTexSubImage3D","glCopyTexSubImage3DEXT"); + _gluBuild3DMipmaps = getGLUExtensionFuncPtr("gluBuild3DMipmaps"); + } -void Texture3D::Extensions::glTexImage3D( GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels) +void Texture3D::Extensions::glTexImage3D( GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels) const { - typedef void (APIENTRY * GLTexImage3DProc) ( GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels); - static GLTexImage3DProc s_glTexImage3D = (GLTexImage3DProc) getGLExtensionFuncPtr("glTexImage3D","glTexImage3DEXT"); - - if (s_glTexImage3D) + if (_glTexImage3D) { - (*s_glTexImage3D)( target, level, internalFormat, width, height, depth, border, format, type, pixels); + typedef void (APIENTRY * GLTexImage3DProc) ( GLenum target, GLint level, GLenum internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLint border, GLenum format, GLenum type, const GLvoid *pixels); + (*((GLTexImage3DProc)_glTexImage3D))( target, level, internalFormat, width, height, depth, border, format, type, pixels); } else { @@ -311,14 +319,12 @@ void Texture3D::Extensions::glTexImage3D( GLenum target, GLint level, GLenum int } } -void Texture3D::Extensions::glTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels) +void Texture3D::Extensions::glTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels) const { - typedef void (APIENTRY * GLTexSubImage3DProc) ( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels); - static GLTexSubImage3DProc s_glTexSubImage3D = (GLTexSubImage3DProc) getGLExtensionFuncPtr("glTexSubImage3D","glTexSubImage3DEXT"); - - if (s_glTexSubImage3D) + if (_glTexSubImage3D) { - (*s_glTexSubImage3D)( target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels); + typedef void (APIENTRY * GLTexSubImage3DProc) ( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *pixels); + (*((GLTexSubImage3DProc)_glTexSubImage3D))( target, level, xoffset, yoffset, zoffset, width, height, depth, format, type, pixels); } else { @@ -326,14 +332,12 @@ void Texture3D::Extensions::glTexSubImage3D( GLenum target, GLint level, GLint x } } -void Texture3D::Extensions::glCopyTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height ) +void Texture3D::Extensions::glCopyTexSubImage3D( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height ) const { - typedef void (APIENTRY * GLCopyTexSubImageProc) ( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height ); - static GLCopyTexSubImageProc s_glCopyTexSubImage3D = (GLCopyTexSubImageProc) getGLExtensionFuncPtr("glCopyTexSubImage3D","glCopyTexSubImage3DEXT"); - - if (s_glCopyTexSubImage3D) + if (_glCopyTexSubImage3D) { - (*s_glCopyTexSubImage3D)(target, level, xoffset, yoffset, zoffset, x, y, width, height); + typedef void (APIENTRY * GLCopyTexSubImageProc) ( GLenum target, GLint level, GLint xoffset, GLint yoffset, GLint zoffset, GLint x, GLint y, GLsizei width, GLsizei height ); + (*((GLCopyTexSubImageProc)_glCopyTexSubImage3D))(target, level, xoffset, yoffset, zoffset, x, y, width, height); } else { @@ -341,14 +345,12 @@ void Texture3D::Extensions::glCopyTexSubImage3D( GLenum target, GLint level, GLi } } -void Texture3D::Extensions::gluBuild3DMipmaps( GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *data) +void Texture3D::Extensions::gluBuild3DMipmaps( GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *data) const { - typedef void (APIENTRY * GLUBuild3DMipMapsProc) ( GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *data); - static GLUBuild3DMipMapsProc s_gluBuild3DMipmaps = (GLUBuild3DMipMapsProc) getGLUExtensionFuncPtr("gluBuild3DMipmaps"); - - if (s_gluBuild3DMipmaps) + if (_gluBuild3DMipmaps) { - (*s_gluBuild3DMipmaps)(target, internalFormat, width, height, depth, format, type, data); + typedef void (APIENTRY * GLUBuild3DMipMapsProc) ( GLenum target, GLint internalFormat, GLsizei width, GLsizei height, GLsizei depth, GLenum format, GLenum type, const GLvoid *data); + (*((GLUBuild3DMipMapsProc)_gluBuild3DMipmaps))(target, internalFormat, width, height, depth, format, type, data); } else {