2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* 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
|
2003-01-22 00:45:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2003-01-22 00:45:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2002-07-12 22:25:10 +08:00
|
|
|
|
|
|
|
#ifndef OSG_BLENDFUNC
|
|
|
|
#define OSG_BLENDFUNC 1
|
|
|
|
|
|
|
|
#include <osg/StateAttribute>
|
2014-12-02 19:10:23 +08:00
|
|
|
#include <osg/GLExtensions>
|
2002-07-12 22:25:10 +08:00
|
|
|
|
2003-09-17 20:04:48 +08:00
|
|
|
|
|
|
|
#ifndef GL_VERSION_1_2
|
|
|
|
#define GL_CONSTANT_COLOR 0x8001
|
|
|
|
#define GL_ONE_MINUS_CONSTANT_COLOR 0x8002
|
|
|
|
#define GL_CONSTANT_ALPHA 0x8003
|
|
|
|
#define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004
|
|
|
|
#define GL_BLEND_COLOR 0x8005
|
|
|
|
#endif
|
|
|
|
|
2006-06-30 21:50:02 +08:00
|
|
|
#ifndef GL_VERSION_1_4
|
|
|
|
#define GL_BLEND_DST_RGB 0x80C8
|
|
|
|
#define GL_BLEND_SRC_RGB 0x80C9
|
|
|
|
#define GL_BLEND_DST_ALPHA 0x80CA
|
|
|
|
#define GL_BLEND_SRC_ALPHA 0x80CB
|
|
|
|
#endif
|
2003-09-17 20:04:48 +08:00
|
|
|
|
|
|
|
|
2002-07-12 22:25:10 +08:00
|
|
|
namespace osg {
|
|
|
|
|
2010-06-14 23:20:47 +08:00
|
|
|
/** Encapsulates OpenGL blend/transparency state.
|
|
|
|
*
|
|
|
|
* Blending combines incoming fragment with a fragment
|
|
|
|
* already present in the target buffer.
|
|
|
|
*
|
|
|
|
* OpenGL 1.1 supports following source and destination blending factors:
|
|
|
|
* GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
|
|
|
|
* GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA,
|
|
|
|
* GL_ZERO, GL_ONE.
|
|
|
|
*
|
|
|
|
* Moreover, there are three source-only blending factors:
|
|
|
|
* GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA_SATURATE
|
|
|
|
* and two destination-only blending factors:
|
|
|
|
* GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR.
|
|
|
|
* OpenGL 1.4 allowed to use these five blending factors
|
|
|
|
* as both - source and destination blending factors.
|
|
|
|
*
|
|
|
|
* Following four source and destination blending factors
|
|
|
|
* were added by Imaging subset of OpenGL 1.2
|
|
|
|
* and made mandatory by OpenGL 1.4:
|
|
|
|
* GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
|
|
|
|
* GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA
|
|
|
|
*
|
|
|
|
* OpenGL 1.4 further provides glBlendFuncSeparate
|
|
|
|
* (promoted from GL_EXT_blend_func_separate).
|
|
|
|
* It makes possible to set blending functions for RGB and Alpha separately.
|
|
|
|
* Before, it was possible to set just one blending function for RGBA.
|
|
|
|
*/
|
2005-04-12 01:14:17 +08:00
|
|
|
class OSG_EXPORT BlendFunc : public StateAttribute
|
2002-07-12 22:25:10 +08:00
|
|
|
{
|
|
|
|
public :
|
|
|
|
|
|
|
|
BlendFunc();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2003-05-07 02:04:27 +08:00
|
|
|
BlendFunc(GLenum source, GLenum destination);
|
2006-06-30 21:50:02 +08:00
|
|
|
BlendFunc(GLenum source, GLenum destination, GLenum source_alpha, GLenum destination_alpha);
|
2003-05-07 02:04:27 +08:00
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Copy constructor using CopyOp to manage deep vs shallow copy. */
|
2002-07-12 22:25:10 +08:00
|
|
|
BlendFunc(const BlendFunc& trans,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
|
|
|
|
StateAttribute(trans,copyop),
|
|
|
|
_source_factor(trans._source_factor),
|
2006-06-30 21:50:02 +08:00
|
|
|
_destination_factor(trans._destination_factor),
|
|
|
|
_source_factor_alpha(trans._source_factor_alpha),
|
|
|
|
_destination_factor_alpha(trans._destination_factor_alpha) {}
|
2002-07-12 22:25:10 +08:00
|
|
|
|
|
|
|
META_StateAttribute(osg, BlendFunc,BLENDFUNC);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
/** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
|
2002-07-12 22:25:10 +08:00
|
|
|
virtual int compare(const StateAttribute& sa) const
|
|
|
|
{
|
2004-09-01 16:15:36 +08:00
|
|
|
// Check for equal types, then create the rhs variable
|
2010-10-01 00:57:02 +08:00
|
|
|
// used by the COMPARE_StateAttribute_Parameter macros below.
|
2002-07-12 22:25:10 +08:00
|
|
|
COMPARE_StateAttribute_Types(BlendFunc,sa)
|
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
// Compare each parameter in turn against the rhs.
|
2002-07-12 22:25:10 +08:00
|
|
|
COMPARE_StateAttribute_Parameter(_source_factor)
|
|
|
|
COMPARE_StateAttribute_Parameter(_destination_factor)
|
2006-06-30 21:50:02 +08:00
|
|
|
COMPARE_StateAttribute_Parameter(_source_factor_alpha)
|
|
|
|
COMPARE_StateAttribute_Parameter(_destination_factor_alpha)
|
2002-07-12 22:25:10 +08:00
|
|
|
|
2004-09-01 16:15:36 +08:00
|
|
|
return 0; // Passed all the above comparison macros, so must be equal.
|
2002-07-12 22:25:10 +08:00
|
|
|
}
|
|
|
|
|
2007-03-06 01:34:36 +08:00
|
|
|
virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
|
2002-07-12 22:25:10 +08:00
|
|
|
{
|
2004-03-03 23:38:22 +08:00
|
|
|
usage.usesMode(GL_BLEND);
|
|
|
|
return true;
|
2002-07-12 22:25:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enum BlendFuncMode {
|
2003-09-17 20:04:48 +08:00
|
|
|
DST_ALPHA = GL_DST_ALPHA,
|
|
|
|
DST_COLOR = GL_DST_COLOR,
|
|
|
|
ONE = GL_ONE,
|
|
|
|
ONE_MINUS_DST_ALPHA = GL_ONE_MINUS_DST_ALPHA,
|
|
|
|
ONE_MINUS_DST_COLOR = GL_ONE_MINUS_DST_COLOR,
|
|
|
|
ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA,
|
|
|
|
ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR,
|
|
|
|
SRC_ALPHA = GL_SRC_ALPHA,
|
|
|
|
SRC_ALPHA_SATURATE = GL_SRC_ALPHA_SATURATE,
|
|
|
|
SRC_COLOR = GL_SRC_COLOR,
|
2004-09-01 16:59:58 +08:00
|
|
|
CONSTANT_COLOR = GL_CONSTANT_COLOR,
|
|
|
|
ONE_MINUS_CONSTANT_COLOR = GL_ONE_MINUS_CONSTANT_COLOR,
|
2003-09-17 20:04:48 +08:00
|
|
|
CONSTANT_ALPHA = GL_CONSTANT_ALPHA,
|
|
|
|
ONE_MINUS_CONSTANT_ALPHA = GL_ONE_MINUS_CONSTANT_ALPHA,
|
|
|
|
ZERO = GL_ZERO
|
2002-07-12 22:25:10 +08:00
|
|
|
};
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
inline void setFunction( GLenum source, GLenum destination )
|
2002-07-12 22:25:10 +08:00
|
|
|
{
|
|
|
|
_source_factor = source;
|
|
|
|
_destination_factor = destination;
|
2006-07-11 22:24:18 +08:00
|
|
|
_source_factor_alpha = source;
|
|
|
|
_destination_factor_alpha = destination;
|
2002-07-12 22:25:10 +08:00
|
|
|
}
|
|
|
|
|
2006-06-30 21:50:02 +08:00
|
|
|
inline void setFunction( GLenum source_rgb, GLenum destination_rgb, GLenum source_alpha, GLenum destination_alpha )
|
|
|
|
{
|
|
|
|
_source_factor = source_rgb;
|
|
|
|
_destination_factor = destination_rgb;
|
|
|
|
_source_factor_alpha = source_alpha;
|
|
|
|
_destination_factor_alpha = destination_alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setSource(GLenum source) { _source_factor = _source_factor_alpha = source; }
|
2002-09-02 20:31:35 +08:00
|
|
|
inline GLenum getSource() const { return _source_factor; }
|
2006-06-30 21:50:02 +08:00
|
|
|
|
|
|
|
void setSourceRGB(GLenum source) { _source_factor = source; }
|
|
|
|
inline GLenum getSourceRGB() const { return _source_factor; }
|
|
|
|
|
|
|
|
void setSourceAlpha(GLenum source) { _source_factor_alpha = source; }
|
|
|
|
inline GLenum getSourceAlpha() const { return _source_factor_alpha; }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2006-06-30 21:50:02 +08:00
|
|
|
void setDestination(GLenum destination) { _destination_factor = _destination_factor_alpha = destination; }
|
2002-09-02 20:31:35 +08:00
|
|
|
inline GLenum getDestination() const { return _destination_factor; }
|
2002-07-12 22:25:10 +08:00
|
|
|
|
2006-06-30 21:50:02 +08:00
|
|
|
void setDestinationRGB(GLenum destination) { _destination_factor = destination; }
|
|
|
|
inline GLenum getDestinationRGB() const { return _destination_factor; }
|
|
|
|
|
|
|
|
void setDestinationAlpha(GLenum destination) { _destination_factor_alpha = destination; }
|
|
|
|
inline GLenum getDestinationAlpha() const { return _destination_factor_alpha; }
|
|
|
|
|
2002-07-12 22:25:10 +08:00
|
|
|
virtual void apply(State& state) const;
|
2014-12-02 19:10:23 +08:00
|
|
|
|
2006-06-30 21:50:02 +08:00
|
|
|
/** Encapsulates queries of extension availability, obtains extension
|
|
|
|
* function pointers, and provides convenience wrappers for
|
2012-03-22 01:36:20 +08:00
|
|
|
* calling extension functions. */
|
2014-12-02 19:10:23 +08:00
|
|
|
struct Extensions : public osg::Referenced
|
2006-06-30 21:50:02 +08:00
|
|
|
{
|
2014-12-02 19:10:23 +08:00
|
|
|
bool isBlendFuncSeparateSupported;
|
|
|
|
void (GL_APIENTRY * glBlendFuncSeparate) (GLenum sfactorRGB, GLenum dfactorRGB, GLenum sfactorAlpha, GLenum dfactorAlpha) ;
|
2006-06-30 21:50:02 +08:00
|
|
|
|
2014-12-02 19:10:23 +08:00
|
|
|
Extensions(unsigned int contextID)
|
|
|
|
{
|
|
|
|
isBlendFuncSeparateSupported = OSG_GLES2_FEATURES || OSG_GL3_FEATURES ||
|
|
|
|
osg::isGLExtensionSupported(contextID, "GL_EXT_blend_func_separate") ||
|
|
|
|
strncmp((const char*)glGetString(GL_VERSION), "1.4", 3) >= 0;
|
2006-06-30 21:50:02 +08:00
|
|
|
|
2014-12-02 19:10:23 +08:00
|
|
|
setGLExtensionFuncPtr(glBlendFuncSeparate, "glBlendFuncSeparate", "glBlendFuncSeparateEXT");
|
|
|
|
}
|
2006-06-30 21:50:02 +08:00
|
|
|
};
|
|
|
|
|
2002-07-12 22:25:10 +08:00
|
|
|
|
|
|
|
protected :
|
|
|
|
|
|
|
|
virtual ~BlendFunc();
|
|
|
|
|
2002-09-02 20:31:35 +08:00
|
|
|
GLenum _source_factor;
|
|
|
|
GLenum _destination_factor;
|
2006-06-30 21:50:02 +08:00
|
|
|
GLenum _source_factor_alpha;
|
|
|
|
GLenum _destination_factor_alpha;
|
2002-07-12 22:25:10 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|