1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version. The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * OpenSceneGraph Public License for more details.
15#define OSG_BLENDFUNC 1
17#include <osg/StateAttribute>
20#define GL_CONSTANT_COLOR 0x8001
21#define GL_ONE_MINUS_CONSTANT_COLOR 0x8002
22#define GL_CONSTANT_ALPHA 0x8003
23#define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004
24#define GL_BLEND_COLOR 0x8005
28#define GL_BLEND_DST_RGB 0x80C8
29#define GL_BLEND_SRC_RGB 0x80C9
30#define GL_BLEND_DST_ALPHA 0x80CA
31#define GL_BLEND_SRC_ALPHA 0x80CB
37/** Encapsulates OpenGL blend/transparency state.
39 * Blending combines incoming fragment with a fragment
40 * already present in the target buffer.
42 * OpenGL 1.1 supports following source and destination blending factors:
43 * GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA,
44 * GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA,
47 * Moreover, there are three source-only blending factors:
48 * GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA_SATURATE
49 * and two destination-only blending factors:
50 * GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR.
51 * OpenGL 1.4 allowed to use these five blending factors
52 * as both - source and destination blending factors.
54 * Following four source and destination blending factors
55 * were added by Imaging subset of OpenGL 1.2
56 * and made mandatory by OpenGL 1.4:
57 * GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR,
58 * GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA
60 * OpenGL 1.4 further provides glBlendFuncSeparate
61 * (promoted from GL_EXT_blend_func_separate).
62 * It makes possible to set blending functions for RGB and Alpha separately.
63 * Before, it was possible to set just one blending function for RGBA.
65class OSG_EXPORT BlendFunc : public StateAttribute
71 BlendFunc(GLenum source, GLenum destination);
72 BlendFunc(GLenum source, GLenum destination, GLenum source_alpha, GLenum destination_alpha);
74 /** Copy constructor using CopyOp to manage deep vs shallow copy. */
75 BlendFunc(const BlendFunc& trans,const CopyOp& copyop=CopyOp::SHALLOW_COPY):
76 StateAttribute(trans,copyop),
77 _source_factor(trans._source_factor),
78 _destination_factor(trans._destination_factor),
79 _source_factor_alpha(trans._source_factor_alpha),
80 _destination_factor_alpha(trans._destination_factor_alpha) {}
82 META_StateAttribute(osg, BlendFunc,BLENDFUNC);
84 /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */
85 virtual int compare(const StateAttribute& sa) const
87 // Check for equal types, then create the rhs variable
88 // used by the COMPARE_StateAttribute_Parameter macros below.
89 COMPARE_StateAttribute_Types(BlendFunc,sa)
91 // Compare each parameter in turn against the rhs.
92 COMPARE_StateAttribute_Parameter(_source_factor)
93 COMPARE_StateAttribute_Parameter(_destination_factor)
94 COMPARE_StateAttribute_Parameter(_source_factor_alpha)
95 COMPARE_StateAttribute_Parameter(_destination_factor_alpha)
97 return 0; // Passed all the above comparison macros, so must be equal.
100 virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const
102 usage.usesMode(GL_BLEND);
107 DST_ALPHA = GL_DST_ALPHA,
108 DST_COLOR = GL_DST_COLOR,
110 ONE_MINUS_DST_ALPHA = GL_ONE_MINUS_DST_ALPHA,
111 ONE_MINUS_DST_COLOR = GL_ONE_MINUS_DST_COLOR,
112 ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA,
113 ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR,
114 SRC_ALPHA = GL_SRC_ALPHA,
115 SRC_ALPHA_SATURATE = GL_SRC_ALPHA_SATURATE,
116 SRC_COLOR = GL_SRC_COLOR,
117 CONSTANT_COLOR = GL_CONSTANT_COLOR,
118 ONE_MINUS_CONSTANT_COLOR = GL_ONE_MINUS_CONSTANT_COLOR,
119 CONSTANT_ALPHA = GL_CONSTANT_ALPHA,
120 ONE_MINUS_CONSTANT_ALPHA = GL_ONE_MINUS_CONSTANT_ALPHA,
124 inline void setFunction( GLenum source, GLenum destination )
126 _source_factor = source;
127 _destination_factor = destination;
128 _source_factor_alpha = source;
129 _destination_factor_alpha = destination;
132 inline void setFunction( GLenum source_rgb, GLenum destination_rgb, GLenum source_alpha, GLenum destination_alpha )
134 _source_factor = source_rgb;
135 _destination_factor = destination_rgb;
136 _source_factor_alpha = source_alpha;
137 _destination_factor_alpha = destination_alpha;
140 void setSource(GLenum source) { _source_factor = _source_factor_alpha = source; }
141 inline GLenum getSource() const { return _source_factor; }
143 void setSourceRGB(GLenum source) { _source_factor = source; }
144 inline GLenum getSourceRGB() const { return _source_factor; }
146 void setSourceAlpha(GLenum source) { _source_factor_alpha = source; }
147 inline GLenum getSourceAlpha() const { return _source_factor_alpha; }
149 void setDestination(GLenum destination) { _destination_factor = _destination_factor_alpha = destination; }
150 inline GLenum getDestination() const { return _destination_factor; }
152 void setDestinationRGB(GLenum destination) { _destination_factor = destination; }
153 inline GLenum getDestinationRGB() const { return _destination_factor; }
155 void setDestinationAlpha(GLenum destination) { _destination_factor_alpha = destination; }
156 inline GLenum getDestinationAlpha() const { return _destination_factor_alpha; }
158 virtual void apply(State& state) const;
162 virtual ~BlendFunc();
164 GLenum _source_factor;
165 GLenum _destination_factor;
166 GLenum _source_factor_alpha;
167 GLenum _destination_factor_alpha;