| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/BlendEquation> |
|---|
| 14 | #include <osg/GLExtensions> |
|---|
| 15 | #include <osg/State> |
|---|
| 16 | #include <osg/Notify> |
|---|
| 17 | #include <osg/buffered_value> |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | using namespace osg; |
|---|
| 21 | |
|---|
| 22 | BlendEquation::BlendEquation(): |
|---|
| 23 | _equationRGB(FUNC_ADD), |
|---|
| 24 | _equationAlpha(FUNC_ADD) |
|---|
| 25 | { |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | BlendEquation::BlendEquation(Equation equation): |
|---|
| 29 | _equationRGB(equation), |
|---|
| 30 | _equationAlpha(equation) |
|---|
| 31 | { |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | BlendEquation::BlendEquation(Equation equationRGB, Equation equationAlpha): |
|---|
| 35 | _equationRGB(equationRGB), |
|---|
| 36 | _equationAlpha(equationAlpha) |
|---|
| 37 | { |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | BlendEquation::~BlendEquation() |
|---|
| 41 | { |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | void BlendEquation::apply(State& state) const |
|---|
| 45 | { |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | const unsigned int contextID = state.getContextID(); |
|---|
| 50 | |
|---|
| 51 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 52 | |
|---|
| 53 | if (!extensions->isBlendEquationSupported()) |
|---|
| 54 | { |
|---|
| 55 | OSG_WARN<<"Warning: BlendEquation::apply(..) failed, BlendEquation is not support by OpenGL driver."<<std::endl; |
|---|
| 56 | return; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | if((_equationRGB == ALPHA_MIN || _equationRGB == ALPHA_MAX) && !extensions->isSGIXMinMaxSupported()) |
|---|
| 60 | { |
|---|
| 61 | OSG_WARN<<"Warning: BlendEquation::apply(..) failed, SGIX_blend_alpha_minmax extension is not supported by OpenGL driver." << std::endl; |
|---|
| 62 | return; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | if(_equationRGB == LOGIC_OP && !extensions->isLogicOpSupported()) |
|---|
| 66 | { |
|---|
| 67 | OSG_WARN<<"Warning: BlendEquation::apply(..) failed, EXT_blend_logic_op extension is not supported by OpenGL driver." << std::endl; |
|---|
| 68 | return; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | if (_equationRGB == _equationAlpha) |
|---|
| 72 | extensions->glBlendEquation(static_cast<GLenum>(_equationRGB)); |
|---|
| 73 | else |
|---|
| 74 | { |
|---|
| 75 | if (extensions->isBlendEquationSeparateSupported()) |
|---|
| 76 | extensions->glBlendEquationSeparate(static_cast<GLenum>(_equationRGB), static_cast<GLenum>(_equationAlpha)); |
|---|
| 77 | else |
|---|
| 78 | { |
|---|
| 79 | OSG_WARN<<"Warning: BlendEquation::apply(..) failed, EXT_blend_equation_separate extension is not supported by OpenGL driver." << std::endl; |
|---|
| 80 | return; |
|---|
| 81 | } |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | typedef buffered_value< ref_ptr<BlendEquation::Extensions> > BufferedExtensions; |
|---|
| 87 | static BufferedExtensions s_extensions; |
|---|
| 88 | |
|---|
| 89 | BlendEquation::Extensions* BlendEquation::getExtensions(unsigned int contextID,bool createIfNotInitalized) |
|---|
| 90 | { |
|---|
| 91 | if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions(contextID); |
|---|
| 92 | return s_extensions[contextID].get(); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | void BlendEquation::setExtensions(unsigned int contextID,Extensions* extensions) |
|---|
| 96 | { |
|---|
| 97 | s_extensions[contextID] = extensions; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | BlendEquation::Extensions::Extensions(unsigned int contextID) |
|---|
| 102 | { |
|---|
| 103 | setupGLExtensions(contextID); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | BlendEquation::Extensions::Extensions(const Extensions& rhs): |
|---|
| 107 | Referenced() |
|---|
| 108 | { |
|---|
| 109 | _isBlendEquationSupported = rhs._isBlendEquationSupported; |
|---|
| 110 | _isBlendEquationSeparateSupported = rhs._isBlendEquationSeparateSupported; |
|---|
| 111 | _isSGIXMinMaxSupported = rhs._isSGIXMinMaxSupported; |
|---|
| 112 | _isLogicOpSupported = rhs._isLogicOpSupported; |
|---|
| 113 | _glBlendEquation = rhs._glBlendEquation; |
|---|
| 114 | _glBlendEquationSeparate = rhs._glBlendEquationSeparate; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | void BlendEquation::Extensions::lowestCommonDenominator(const Extensions& rhs) |
|---|
| 118 | { |
|---|
| 119 | if (!rhs._isBlendEquationSupported) _isBlendEquationSupported = false; |
|---|
| 120 | if (!rhs._glBlendEquation) _glBlendEquation = 0; |
|---|
| 121 | if (!rhs._isBlendEquationSeparateSupported) _isBlendEquationSeparateSupported = false; |
|---|
| 122 | if (!rhs._glBlendEquationSeparate) _glBlendEquationSeparate = 0; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | void BlendEquation::Extensions::setupGLExtensions(unsigned int contextID) |
|---|
| 126 | { |
|---|
| 127 | bool bultInSupport = OSG_GLES2_FEATURES || OSG_GL3_FEATURES; |
|---|
| 128 | _isBlendEquationSupported = bultInSupport || |
|---|
| 129 | isGLExtensionSupported(contextID, "GL_EXT_blend_equation") || |
|---|
| 130 | strncmp((const char*)glGetString(GL_VERSION), "1.2", 3) >= 0; |
|---|
| 131 | |
|---|
| 132 | _isBlendEquationSeparateSupported = bultInSupport || |
|---|
| 133 | isGLExtensionSupported(contextID, "GL_EXT_blend_equation_separate") || |
|---|
| 134 | strncmp((const char*)glGetString(GL_VERSION), "2.0", 3) >= 0; |
|---|
| 135 | |
|---|
| 136 | _isSGIXMinMaxSupported = isGLExtensionSupported(contextID, "GL_SGIX_blend_alpha_minmax"); |
|---|
| 137 | _isLogicOpSupported = isGLExtensionSupported(contextID, "GL_EXT_blend_logic_op"); |
|---|
| 138 | |
|---|
| 139 | setGLExtensionFuncPtr(_glBlendEquation, "glBlendEquation", "glBlendEquationEXT"); |
|---|
| 140 | setGLExtensionFuncPtr(_glBlendEquationSeparate, "glBlendEquationSeparate", "glBlendEquationSeparateEXT"); |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | void BlendEquation::Extensions::glBlendEquation(GLenum mode) const |
|---|
| 144 | { |
|---|
| 145 | if (_glBlendEquation) |
|---|
| 146 | { |
|---|
| 147 | _glBlendEquation(mode); |
|---|
| 148 | } |
|---|
| 149 | else |
|---|
| 150 | { |
|---|
| 151 | OSG_WARN<<"Error: glBlendEquation not supported by OpenGL driver"<<std::endl; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | void BlendEquation::Extensions::glBlendEquationSeparate(GLenum modeRGB, GLenum modeAlpha) const |
|---|
| 156 | { |
|---|
| 157 | if (_glBlendEquationSeparate) |
|---|
| 158 | { |
|---|
| 159 | _glBlendEquationSeparate(modeRGB, modeAlpha); |
|---|
| 160 | } |
|---|
| 161 | else |
|---|
| 162 | { |
|---|
| 163 | OSG_WARN<<"Error: glBlendEquationSeparate not supported by OpenGL driver"<<std::endl; |
|---|
| 164 | } |
|---|
| 165 | } |
|---|