From 8fefec9f478417873a27e159f8f75e3fd6e3ebb3 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 17 Dec 2003 19:26:16 +0000 Subject: [PATCH] From Pavel Moloshtan, added GL_ARB_shadow_support to osg::Texture. --- include/osg/Texture | 39 +++++++++++++++++++++++++++++++++++++-- src/osg/Texture.cpp | 28 ++++++++++++++++++++++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/include/osg/Texture b/include/osg/Texture index 27961800a..eec1e98ea 100644 --- a/include/osg/Texture +++ b/include/osg/Texture @@ -75,6 +75,11 @@ #define GL_TEXTURE_3D 0x806F #endif +#ifndef GL_TEXTURE_COMPARE_MODE_ARB +#define GL_TEXTURE_COMPARE_MODE_ARB 0x884C +#define GL_TEXTURE_COMPARE_FUNC_ARB 0x884D +#define GL_COMPARE_R_TO_TEXTURE_ARB 0x884E +#endif namespace osg { @@ -232,6 +237,29 @@ class SG_EXPORT Texture : public osg::StateAttribute void dirtyTextureParameters(); + // set mode of GL_TEXTURE_COMPARE_MODE_ARB to GL_COMPARE_R_TO_TEXTURE_ARB + // See http://oss.sgi.com/projects/ogl-sample/registry/ARB/shadow.txt + void setShadowComparison(bool flag) { _use_shadow_comparison = flag; } + + enum ShadowCompareFunc { + LEQUAL = GL_LEQUAL, + GEQUAL = GL_GEQUAL + }; + + // set shadow texture comparison function + void setShadowCompareFunc(ShadowCompareFunc func) { _shadow_compare_func = func; } + ShadowCompareFunc getShadowCompareFunc() { return _shadow_compare_func; } + + enum ShadowTextureMode { + LUMINANCE = GL_LUMINANCE, + INTENSITY = GL_INTENSITY, + ALPHA = GL_ALPHA + }; + + // set shadow texture mode after comparison + void setShadowTextureMode(ShadowTextureMode mode) { _shadow_texture_mode = mode; } + ShadowTextureMode getShadowTextureMode() { return _shadow_texture_mode; } + /** Texture is pure virtual base class, apply must be overriden. */ @@ -278,6 +306,9 @@ class SG_EXPORT Texture : public osg::StateAttribute void setGenerateMipMapSupported(bool flag) { _isGenerateMipMapSupported=flag; } bool isGenerateMipMapSupported() const { return _isGenerateMipMapSupported; } + void setShadowSupported(bool flag) { _isShadowSupported = flag; } + bool isShadowSupported() const { return _isShadowSupported; } + void setMaxTextureSize(GLint maxsize) { _maxTextureSize=maxsize; } GLint maxTextureSize() const { return _maxTextureSize; } @@ -304,6 +335,7 @@ class SG_EXPORT Texture : public osg::StateAttribute bool _isTextureEdgeClampSupported; bool _isTextureBorderClampSupported; bool _isGenerateMipMapSupported; + bool _isShadowSupported; GLint _maxTextureSize; @@ -372,8 +404,11 @@ class SG_EXPORT Texture : public osg::StateAttribute InternalFormatMode _internalFormatMode; mutable GLint _internalFormat; - - + + bool _use_shadow_comparison; + ShadowCompareFunc _shadow_compare_func; + ShadowTextureMode _shadow_texture_mode; + public: class TextureObject : public osg::Referenced diff --git a/src/osg/Texture.cpp b/src/osg/Texture.cpp index d26efa0bf..8b17c45e1 100644 --- a/src/osg/Texture.cpp +++ b/src/osg/Texture.cpp @@ -174,7 +174,10 @@ Texture::Texture(): _unrefImageDataAfterApply(false), _borderColor(0.0, 0.0, 0.0, 0.0), _internalFormatMode(USE_IMAGE_DATA_FORMAT), - _internalFormat(0) + _internalFormat(0), + _use_shadow_comparison(false), + _shadow_compare_func(LEQUAL), + _shadow_texture_mode(LUMINANCE) { } @@ -190,7 +193,10 @@ Texture::Texture(const Texture& text,const CopyOp& copyop): _unrefImageDataAfterApply(text._unrefImageDataAfterApply), _borderColor(text._borderColor), _internalFormatMode(text._internalFormatMode), - _internalFormat(text._internalFormat) + _internalFormat(text._internalFormat), + _use_shadow_comparison(text._use_shadow_comparison), + _shadow_compare_func(text._shadow_compare_func), + _shadow_texture_mode(text._shadow_texture_mode) { } @@ -211,6 +217,9 @@ int Texture::compareTexture(const Texture& rhs) const COMPARE_StateAttribute_Parameter(_useHardwareMipMapGeneration) COMPARE_StateAttribute_Parameter(_internalFormatMode) COMPARE_StateAttribute_Parameter(_internalFormat) + COMPARE_StateAttribute_Parameter(_use_shadow_comparison) + COMPARE_StateAttribute_Parameter(_shadow_compare_func) + COMPARE_StateAttribute_Parameter(_shadow_texture_mode) return 0; } @@ -474,6 +483,20 @@ void Texture::applyTexParameters(GLenum target, State& state) const glTexParameterfv(target, GL_TEXTURE_BORDER_COLOR, _borderColor.ptr()); } + if (extensions->isShadowSupported() && target == GL_TEXTURE_2D) + { + if (_use_shadow_comparison) + { + glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE); + glTexParameteri(target, GL_TEXTURE_COMPARE_FUNC_ARB, _shadow_compare_func); + glTexParameteri(target, GL_DEPTH_TEXTURE_MODE_ARB, _shadow_texture_mode); + } + else + { + glTexParameteri(target, GL_TEXTURE_COMPARE_MODE_ARB, GL_NONE); + } + } + getTextureParameterDirty(state.getContextID()) = false; } @@ -1038,6 +1061,7 @@ void Texture::Extensions::setupGLExtenions() _isTextureBorderClampSupported = isGLExtensionSupported("GL_ARB_texture_border_clamp"); _isGenerateMipMapSupported = (strncmp((const char*)glGetString(GL_VERSION),"1.4",3)>=0) || isGLExtensionSupported("GL_SGIS_generate_mipmap"); + _isShadowSupported = isGLExtensionSupported("GL_ARB_shadow"); glGetIntegerv(GL_MAX_TEXTURE_SIZE,&_maxTextureSize);