| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield |
|---|
| 2 | * |
|---|
| 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. |
|---|
| 7 | * |
|---|
| 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. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_BLENDFUNC |
|---|
| 15 | #define OSG_BLENDFUNC 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/StateAttribute> |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #ifndef GL_VERSION_1_2 |
|---|
| 21 | #define GL_CONSTANT_COLOR 0x8001 |
|---|
| 22 | #define GL_ONE_MINUS_CONSTANT_COLOR 0x8002 |
|---|
| 23 | #define GL_CONSTANT_ALPHA 0x8003 |
|---|
| 24 | #define GL_ONE_MINUS_CONSTANT_ALPHA 0x8004 |
|---|
| 25 | #define GL_BLEND_COLOR 0x8005 |
|---|
| 26 | #endif |
|---|
| 27 | |
|---|
| 28 | #ifndef GL_VERSION_1_4 |
|---|
| 29 | #define GL_BLEND_DST_RGB 0x80C8 |
|---|
| 30 | #define GL_BLEND_SRC_RGB 0x80C9 |
|---|
| 31 | #define GL_BLEND_DST_ALPHA 0x80CA |
|---|
| 32 | #define GL_BLEND_SRC_ALPHA 0x80CB |
|---|
| 33 | #endif |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | namespace osg { |
|---|
| 37 | |
|---|
| 38 | /** Encapsulates OpenGL blend/transparency state. |
|---|
| 39 | * |
|---|
| 40 | * Blending combines incoming fragment with a fragment |
|---|
| 41 | * already present in the target buffer. |
|---|
| 42 | * |
|---|
| 43 | * OpenGL 1.1 supports following source and destination blending factors: |
|---|
| 44 | * GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, |
|---|
| 45 | * GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA, |
|---|
| 46 | * GL_ZERO, GL_ONE. |
|---|
| 47 | * |
|---|
| 48 | * Moreover, there are three source-only blending factors: |
|---|
| 49 | * GL_DST_COLOR, GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA_SATURATE |
|---|
| 50 | * and two destination-only blending factors: |
|---|
| 51 | * GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR. |
|---|
| 52 | * OpenGL 1.4 allowed to use these five blending factors |
|---|
| 53 | * as both - source and destination blending factors. |
|---|
| 54 | * |
|---|
| 55 | * Following four source and destination blending factors |
|---|
| 56 | * were added by Imaging subset of OpenGL 1.2 |
|---|
| 57 | * and made mandatory by OpenGL 1.4: |
|---|
| 58 | * GL_CONSTANT_COLOR, GL_ONE_MINUS_CONSTANT_COLOR, |
|---|
| 59 | * GL_CONSTANT_ALPHA, GL_ONE_MINUS_CONSTANT_ALPHA |
|---|
| 60 | * |
|---|
| 61 | * OpenGL 1.4 further provides glBlendFuncSeparate |
|---|
| 62 | * (promoted from GL_EXT_blend_func_separate). |
|---|
| 63 | * It makes possible to set blending functions for RGB and Alpha separately. |
|---|
| 64 | * Before, it was possible to set just one blending function for RGBA. |
|---|
| 65 | */ |
|---|
| 66 | class OSG_EXPORT BlendFunc : public StateAttribute |
|---|
| 67 | { |
|---|
| 68 | public : |
|---|
| 69 | |
|---|
| 70 | BlendFunc(); |
|---|
| 71 | |
|---|
| 72 | BlendFunc(GLenum source, GLenum destination); |
|---|
| 73 | BlendFunc(GLenum source, GLenum destination, GLenum source_alpha, GLenum destination_alpha); |
|---|
| 74 | |
|---|
| 75 | /** Copy constructor using CopyOp to manage deep vs shallow copy. */ |
|---|
| 76 | BlendFunc(const BlendFunc& trans,const CopyOp& copyop=CopyOp::SHALLOW_COPY): |
|---|
| 77 | StateAttribute(trans,copyop), |
|---|
| 78 | _source_factor(trans._source_factor), |
|---|
| 79 | _destination_factor(trans._destination_factor), |
|---|
| 80 | _source_factor_alpha(trans._source_factor_alpha), |
|---|
| 81 | _destination_factor_alpha(trans._destination_factor_alpha) {} |
|---|
| 82 | |
|---|
| 83 | META_StateAttribute(osg, BlendFunc,BLENDFUNC); |
|---|
| 84 | |
|---|
| 85 | /** Return -1 if *this < *rhs, 0 if *this==*rhs, 1 if *this>*rhs. */ |
|---|
| 86 | virtual int compare(const StateAttribute& sa) const |
|---|
| 87 | { |
|---|
| 88 | // Check for equal types, then create the rhs variable |
|---|
| 89 | // used by the COMPARE_StateAttribute_Parameter macros below. |
|---|
| 90 | COMPARE_StateAttribute_Types(BlendFunc,sa) |
|---|
| 91 | |
|---|
| 92 | // Compare each parameter in turn against the rhs. |
|---|
| 93 | COMPARE_StateAttribute_Parameter(_source_factor) |
|---|
| 94 | COMPARE_StateAttribute_Parameter(_destination_factor) |
|---|
| 95 | COMPARE_StateAttribute_Parameter(_source_factor_alpha) |
|---|
| 96 | COMPARE_StateAttribute_Parameter(_destination_factor_alpha) |
|---|
| 97 | |
|---|
| 98 | return 0; // Passed all the above comparison macros, so must be equal. |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | virtual bool getModeUsage(StateAttribute::ModeUsage& usage) const |
|---|
| 102 | { |
|---|
| 103 | usage.usesMode(GL_BLEND); |
|---|
| 104 | return true; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | enum BlendFuncMode { |
|---|
| 108 | DST_ALPHA = GL_DST_ALPHA, |
|---|
| 109 | DST_COLOR = GL_DST_COLOR, |
|---|
| 110 | ONE = GL_ONE, |
|---|
| 111 | ONE_MINUS_DST_ALPHA = GL_ONE_MINUS_DST_ALPHA, |
|---|
| 112 | ONE_MINUS_DST_COLOR = GL_ONE_MINUS_DST_COLOR, |
|---|
| 113 | ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA, |
|---|
| 114 | ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR, |
|---|
| 115 | SRC_ALPHA = GL_SRC_ALPHA, |
|---|
| 116 | SRC_ALPHA_SATURATE = GL_SRC_ALPHA_SATURATE, |
|---|
| 117 | SRC_COLOR = GL_SRC_COLOR, |
|---|
| 118 | CONSTANT_COLOR = GL_CONSTANT_COLOR, |
|---|
| 119 | ONE_MINUS_CONSTANT_COLOR = GL_ONE_MINUS_CONSTANT_COLOR, |
|---|
| 120 | CONSTANT_ALPHA = GL_CONSTANT_ALPHA, |
|---|
| 121 | ONE_MINUS_CONSTANT_ALPHA = GL_ONE_MINUS_CONSTANT_ALPHA, |
|---|
| 122 | ZERO = GL_ZERO |
|---|
| 123 | }; |
|---|
| 124 | |
|---|
| 125 | inline void setFunction( GLenum source, GLenum destination ) |
|---|
| 126 | { |
|---|
| 127 | _source_factor = source; |
|---|
| 128 | _destination_factor = destination; |
|---|
| 129 | _source_factor_alpha = source; |
|---|
| 130 | _destination_factor_alpha = destination; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | inline void setFunction( GLenum source_rgb, GLenum destination_rgb, GLenum source_alpha, GLenum destination_alpha ) |
|---|
| 134 | { |
|---|
| 135 | _source_factor = source_rgb; |
|---|
| 136 | _destination_factor = destination_rgb; |
|---|
| 137 | _source_factor_alpha = source_alpha; |
|---|
| 138 | _destination_factor_alpha = destination_alpha; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | void setSource(GLenum source) { _source_factor = _source_factor_alpha = source; } |
|---|
| 142 | inline GLenum getSource() const { return _source_factor; } |
|---|
| 143 | |
|---|
| 144 | void setSourceRGB(GLenum source) { _source_factor = source; } |
|---|
| 145 | inline GLenum getSourceRGB() const { return _source_factor; } |
|---|
| 146 | |
|---|
| 147 | void setSourceAlpha(GLenum source) { _source_factor_alpha = source; } |
|---|
| 148 | inline GLenum getSourceAlpha() const { return _source_factor_alpha; } |
|---|
| 149 | |
|---|
| 150 | void setDestination(GLenum destination) { _destination_factor = _destination_factor_alpha = destination; } |
|---|
| 151 | inline GLenum getDestination() const { return _destination_factor; } |
|---|
| 152 | |
|---|
| 153 | void setDestinationRGB(GLenum destination) { _destination_factor = destination; } |
|---|
| 154 | inline GLenum getDestinationRGB() const { return _destination_factor; } |
|---|
| 155 | |
|---|
| 156 | void setDestinationAlpha(GLenum destination) { _destination_factor_alpha = destination; } |
|---|
| 157 | inline GLenum getDestinationAlpha() const { return _destination_factor_alpha; } |
|---|
| 158 | |
|---|
| 159 | virtual void apply(State& state) const; |
|---|
| 160 | /** Encapsulates queries of extension availability, obtains extension |
|---|
| 161 | * function pointers, and provides convenience wrappers for |
|---|
| 162 | * calling extension functions. */ |
|---|
| 163 | class OSG_EXPORT Extensions : public osg::Referenced |
|---|
| 164 | { |
|---|
| 165 | public: |
|---|
| 166 | Extensions(unsigned int contextID); |
|---|
| 167 | |
|---|
| 168 | Extensions(const Extensions& rhs); |
|---|
| 169 | |
|---|
| 170 | void lowestCommonDenominator(const Extensions& rhs); |
|---|
| 171 | |
|---|
| 172 | void setupGLExtensions(unsigned int contextID); |
|---|
| 173 | |
|---|
| 174 | void setBlendFuncSeparateSupported(bool flag) { _isBlendFuncSeparateSupported=flag; } |
|---|
| 175 | bool isBlendFuncSeparateSupported() const { return _isBlendFuncSeparateSupported; } |
|---|
| 176 | |
|---|
| 177 | void glBlendFuncSeparate(GLenum sfactorRGB, |
|---|
| 178 | GLenum dfactorRGB, |
|---|
| 179 | GLenum sfactorAlpha, |
|---|
| 180 | GLenum dfactorAlpha) const; |
|---|
| 181 | |
|---|
| 182 | protected: |
|---|
| 183 | |
|---|
| 184 | ~Extensions() {} |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | typedef void (GL_APIENTRY * GLBlendFuncSeparateProc) (GLenum sfactorRGB, |
|---|
| 188 | GLenum dfactorRGB, |
|---|
| 189 | GLenum sfactorAlpha, |
|---|
| 190 | GLenum dfactorAlpha); |
|---|
| 191 | bool _isBlendFuncSeparateSupported; |
|---|
| 192 | GLBlendFuncSeparateProc _glBlendFuncSeparate; |
|---|
| 193 | |
|---|
| 194 | }; |
|---|
| 195 | |
|---|
| 196 | /** Returns the Extensions object for the given context. |
|---|
| 197 | * If createIfNotInitalized is true and the Extensions object doesn't |
|---|
| 198 | * exist, getExtensions() creates it on the given context. |
|---|
| 199 | * Returns NULL if createIfNotInitalized is false and the Extensions |
|---|
| 200 | * object doesn't exist. */ |
|---|
| 201 | static Extensions* getExtensions(unsigned int contextID,bool createIfNotInitalized); |
|---|
| 202 | |
|---|
| 203 | /** setExtensions() allows users to override the extensions across graphics contexts. |
|---|
| 204 | * Typically used when you have different extensions supported across graphics pipes, |
|---|
| 205 | * but need to ensure that they all use the same low common denominator extensions. */ |
|---|
| 206 | static void setExtensions(unsigned int contextID,Extensions* extensions); |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | protected : |
|---|
| 210 | |
|---|
| 211 | virtual ~BlendFunc(); |
|---|
| 212 | |
|---|
| 213 | GLenum _source_factor; |
|---|
| 214 | GLenum _destination_factor; |
|---|
| 215 | GLenum _source_factor_alpha; |
|---|
| 216 | GLenum _destination_factor_alpha; |
|---|
| 217 | }; |
|---|
| 218 | |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | #endif |
|---|