Martin Spindler, new osg::ClampColor state attribute.

This commit is contained in:
Robert Osfield 2006-06-08 15:27:18 +00:00
parent 2e2684c05a
commit 4ab7be2833
5 changed files with 265 additions and 7 deletions

View File

@ -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

144
include/osg/ClampColor Normal file
View File

@ -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 <osg/StateAttribute>
#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

View File

@ -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,

111
src/osg/ClampColor.cpp Normal file
View File

@ -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 <osg/ClampColor>
#include <osg/GLExtensions>
#include <osg/State>
#include <osg/Notify>
#include <osg/buffered_value>
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."<<std::endl;
return;
}
extensions->glClampColor(static_cast<GLenum>(_target), static_cast<GLenum>(_mode));
}
typedef buffered_value< ref_ptr<ClampColor::Extensions> > 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"<<std::endl;
}
}

View File

@ -20,6 +20,7 @@ CXXFILES =\
ClearNode.cpp\
ClipNode.cpp\
ClipPlane.cpp\
ClampColor.cpp\
ClusterCullingCallback.cpp\
CollectOccludersVisitor.cpp\
ColorMask.cpp\