From 4ab7be2833236a17fdf1e49a1f2d982c7d36aebf Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Thu, 8 Jun 2006 15:27:18 +0000 Subject: [PATCH] Martin Spindler, new osg::ClampColor state attribute. --- VisualStudio/osg/osg.dsp | 8 +++ include/osg/ClampColor | 144 +++++++++++++++++++++++++++++++++++++ include/osg/StateAttribute | 8 +-- src/osg/ClampColor.cpp | 111 ++++++++++++++++++++++++++++ src/osg/GNUmakefile | 1 + 5 files changed, 265 insertions(+), 7 deletions(-) create mode 100644 include/osg/ClampColor create mode 100644 src/osg/ClampColor.cpp diff --git a/VisualStudio/osg/osg.dsp b/VisualStudio/osg/osg.dsp index ef3c1dc43..e3f5144ac 100755 --- a/VisualStudio/osg/osg.dsp +++ b/VisualStudio/osg/osg.dsp @@ -228,6 +228,10 @@ SOURCE=..\..\src\osg\ClipPlane.cpp # End Source File # Begin Source File +SOURCE=..\..\src\osg\ClampColor.cpp +# End Source File +# Begin Source File + SOURCE=..\..\src\osg\ClusterCullingCallback.cpp # End Source File # Begin Source File @@ -688,6 +692,10 @@ SOURCE=..\..\Include\Osg\ClipPlane # End Source File # Begin Source File +SOURCE=..\..\Include\Osg\ClampColor +# End Source File +# Begin Source File + SOURCE=..\..\Include\Osg\ClusterCullingCallback # End Source File # Begin Source File diff --git a/include/osg/ClampColor b/include/osg/ClampColor new file mode 100644 index 000000000..2c172520a --- /dev/null +++ b/include/osg/ClampColor @@ -0,0 +1,144 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 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. +*/ + +#ifndef OSG_CLAMPCOLOR +#define OSG_CLAMPCOLOR 1 + +#include + +#ifndef GL_ARB_color_buffer_float +#define GL_RGBA_FLOAT_MODE_ARB 0x8820 +#define GL_CLAMP_VERTEX_COLOR_ARB 0x891A +#define GL_CLAMP_FRAGMENT_COLOR_ARB 0x891B +#define GL_CLAMP_READ_COLOR_ARB 0x891C +#define GL_FIXED_ONLY_ARB 0x891D +#endif + +namespace osg { + +/** Encapsulates OpenGL ClampColor state. */ +class OSG_EXPORT ClampColor : public StateAttribute +{ + public : + + enum Target { + CLAMP_VERTEX_COLOR = GL_CLAMP_VERTEX_COLOR_ARB, + CLAMP_FRAGMENT_COLOR = GL_CLAMP_FRAGMENT_COLOR_ARB, + CLAMP_READ_COLOR = GL_CLAMP_READ_COLOR_ARB + }; + + enum Mode { + FIXED_ONLY = GL_FIXED_ONLY_ARB, + FALSE = GL_FALSE, + TRUE = GL_TRUE + }; + + + ClampColor(); + + ClampColor(Target target, Mode mode); + + /** Copy constructor using CopyOp to manage deep vs shallow copy. */ + ClampColor(const ClampColor& trans,const CopyOp& copyop=CopyOp::SHALLOW_COPY): + StateAttribute(trans,copyop), + _target(trans._target), + _mode(trans._mode) {} + + META_StateAttribute(osg, ClampColor,CLAMPCOLOR); + + /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */ + virtual int compare(const StateAttribute& sa) const + { + // Check for equal types, then create the rhs variable + // used by the COMPARE_StateAttribute_Paramter macros below. + COMPARE_StateAttribute_Types(ClampColor,sa) + + // Compare each parameter in turn against the rhs. + COMPARE_StateAttribute_Parameter(_target) + COMPARE_StateAttribute_Parameter(_mode) + + return 0; // Passed all the above comparison macros, so must be equal. + } + + inline void setTarget(Target target) + { + _target = target; + } + + inline Target getTarget() const { return _target; } + + + inline void setMode(Mode mode) + { + _mode = mode; + } + + inline Mode getMode() const { return _mode; } + + + virtual void apply(State& state) const; + /** Encapsulates queries of extension availability, obtains extension + * function pointers, and provides convinience wrappers for + * calling extension functions. */ + class OSG_EXPORT Extensions : public osg::Referenced + { + public: + Extensions(unsigned int contextID); + + Extensions(const Extensions& rhs); + + void lowestCommonDenominator(const Extensions& rhs); + + void setupGLExtenions(unsigned int contextID); + + void setClampColorSupported(bool flag) { _isClampColorSupported=flag; } + bool isClampColorSupported() const { return _isClampColorSupported; } + + void setClampColorProc(void* ptr) { _glClampColor = ptr; } + void glClampColor(GLenum target, GLenum mode) const; + + protected: + + ~Extensions() {} + + bool _isClampColorSupported; + + void* _glClampColor; + + }; + + /** Returns the Extensions object for the given context. + * If createIfNotInitalized is true and the Exentsions object doesn't + * exist, getExtensions() creates it on the given context. + * Returns NULL if createIfNotInitalized is false and the Extensions + * object doesn't exist. */ + static Extensions* getExtensions(unsigned int 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(unsigned int contextID,Extensions* extensions); + + + protected : + + virtual ~ClampColor(); + + + Target _target; + Mode _mode; +}; + +} + +#endif diff --git a/include/osg/StateAttribute b/include/osg/StateAttribute index f50bcf423..db46e8401 100644 --- a/include/osg/StateAttribute +++ b/include/osg/StateAttribute @@ -152,19 +152,13 @@ class OSG_EXPORT StateAttribute : public Object SCISSOR, BLENDCOLOR, MULTISAMPLE, - - CLIPPLANE, - COLORMATRIX, - VERTEXPROGRAM, FRAGMENTPROGRAM, - POINTSPRITE, - - /// core GLSL support PROGRAM, + CLAMPCOLOR, /// osgFX namespace VALIDATOR, diff --git a/src/osg/ClampColor.cpp b/src/osg/ClampColor.cpp new file mode 100644 index 000000000..4965973f1 --- /dev/null +++ b/src/osg/ClampColor.cpp @@ -0,0 +1,111 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 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. +*/ +#include +#include +#include +#include +#include + + +using namespace osg; + +ClampColor::ClampColor(): + _target(CLAMP_FRAGMENT_COLOR), + _mode(FIXED_ONLY) +{ +} + +ClampColor::ClampColor(Target target, Mode mode): + _target(target), + _mode(mode) +{ +} + +ClampColor::~ClampColor() +{ +} + +void ClampColor::apply(State& state) const +{ + + // get the contextID (user defined ID of 0 upwards) for the + // current OpenGL context. + const unsigned int contextID = state.getContextID(); + + const Extensions* extensions = getExtensions(contextID,true); + + if (!extensions->isClampColorSupported()) + { + notify(WARN)<<"Warning: ClampColor::apply(..) failed, ClampColor is not support by OpenGL driver."<glClampColor(static_cast(_target), static_cast(_mode)); +} + + +typedef buffered_value< ref_ptr > BufferedExtensions; +static BufferedExtensions s_extensions; + +ClampColor::Extensions* ClampColor::getExtensions(unsigned int contextID,bool createIfNotInitalized) +{ + if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions(contextID); + return s_extensions[contextID].get(); +} + +void ClampColor::setExtensions(unsigned int contextID,Extensions* extensions) +{ + s_extensions[contextID] = extensions; +} + + +ClampColor::Extensions::Extensions(unsigned int contextID) +{ + setupGLExtenions(contextID); +} + +ClampColor::Extensions::Extensions(const Extensions& rhs): + Referenced() +{ + _isClampColorSupported = rhs._isClampColorSupported; + _glClampColor = rhs._glClampColor; +} + +void ClampColor::Extensions::lowestCommonDenominator(const Extensions& rhs) +{ + if (!rhs._isClampColorSupported) _isClampColorSupported = false; + if (!rhs._glClampColor) _glClampColor = 0; +} + +void ClampColor::Extensions::setupGLExtenions(unsigned int contextID) +{ + _isClampColorSupported = isGLExtensionSupported(contextID,"GL_ARB_color_buffer_float") || + strncmp((const char*)glGetString(GL_VERSION),"2.0",3)>=0; + + _glClampColor = getGLExtensionFuncPtr("glClampColor", "glClampColorARB"); +} + +void ClampColor::Extensions::glClampColor(GLenum target, GLenum mode) const +{ + if (_glClampColor) + { + typedef void (APIENTRY * GLClampColorProc) (GLenum target, GLenum mode); + ((GLClampColorProc)_glClampColor)(target,mode); + } + else + { + notify(WARN)<<"Error: glClampColor not supported by OpenGL driver"<