| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osg/BlendFunc> |
|---|
| 14 | #include <osg/GLExtensions> |
|---|
| 15 | #include <osg/State> |
|---|
| 16 | #include <osg/Notify> |
|---|
| 17 | |
|---|
| 18 | using namespace osg; |
|---|
| 19 | |
|---|
| 20 | BlendFunc::BlendFunc(): |
|---|
| 21 | _source_factor(SRC_ALPHA), |
|---|
| 22 | _destination_factor(ONE_MINUS_SRC_ALPHA), |
|---|
| 23 | _source_factor_alpha(SRC_ALPHA), |
|---|
| 24 | _destination_factor_alpha(ONE_MINUS_SRC_ALPHA) |
|---|
| 25 | { |
|---|
| 26 | } |
|---|
| 27 | |
|---|
| 28 | BlendFunc::BlendFunc(GLenum source, GLenum destination): |
|---|
| 29 | _source_factor(source), |
|---|
| 30 | _destination_factor(destination), |
|---|
| 31 | _source_factor_alpha(source), |
|---|
| 32 | _destination_factor_alpha(destination) |
|---|
| 33 | { |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | BlendFunc::BlendFunc(GLenum source, GLenum destination, GLenum source_alpha, GLenum destination_alpha): |
|---|
| 37 | _source_factor(source), |
|---|
| 38 | _destination_factor(destination), |
|---|
| 39 | _source_factor_alpha(source_alpha), |
|---|
| 40 | _destination_factor_alpha(destination_alpha) |
|---|
| 41 | { |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | BlendFunc::~BlendFunc() |
|---|
| 45 | { |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | void BlendFunc::apply(State& state) const |
|---|
| 49 | { |
|---|
| 50 | if (_source_factor != _source_factor_alpha || |
|---|
| 51 | _destination_factor != _destination_factor_alpha) |
|---|
| 52 | { |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | const unsigned int contextID = state.getContextID(); |
|---|
| 56 | |
|---|
| 57 | const Extensions* extensions = getExtensions(contextID,true); |
|---|
| 58 | |
|---|
| 59 | if (!extensions->isBlendFuncSeparateSupported()) |
|---|
| 60 | { |
|---|
| 61 | OSG_WARN<<"Warning: BlendFunc::apply(..) failed, BlendFuncSeparate is not support by OpenGL driver, falling back to BlendFunc."<<std::endl; |
|---|
| 62 | } |
|---|
| 63 | else |
|---|
| 64 | { |
|---|
| 65 | extensions->glBlendFuncSeparate(_source_factor, _destination_factor, _source_factor_alpha, _destination_factor_alpha); |
|---|
| 66 | return; |
|---|
| 67 | } |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | glBlendFunc( _source_factor, _destination_factor ); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | typedef buffered_value< ref_ptr<BlendFunc::Extensions> > BufferedExtensions; |
|---|
| 75 | static BufferedExtensions s_extensions; |
|---|
| 76 | |
|---|
| 77 | BlendFunc::Extensions* BlendFunc::getExtensions(unsigned int contextID,bool createIfNotInitalized) |
|---|
| 78 | { |
|---|
| 79 | if (!s_extensions[contextID] && createIfNotInitalized) s_extensions[contextID] = new Extensions(contextID); |
|---|
| 80 | return s_extensions[contextID].get(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void BlendFunc::setExtensions(unsigned int contextID,Extensions* extensions) |
|---|
| 84 | { |
|---|
| 85 | s_extensions[contextID] = extensions; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | BlendFunc::Extensions::Extensions(unsigned int contextID) |
|---|
| 90 | { |
|---|
| 91 | setupGLExtensions(contextID); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | BlendFunc::Extensions::Extensions(const Extensions& rhs): |
|---|
| 95 | Referenced() |
|---|
| 96 | { |
|---|
| 97 | _isBlendFuncSeparateSupported = rhs._isBlendFuncSeparateSupported; |
|---|
| 98 | _glBlendFuncSeparate = rhs._glBlendFuncSeparate; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | void BlendFunc::Extensions::lowestCommonDenominator(const Extensions& rhs) |
|---|
| 102 | { |
|---|
| 103 | if (!rhs._isBlendFuncSeparateSupported) _isBlendFuncSeparateSupported = false; |
|---|
| 104 | if (!rhs._glBlendFuncSeparate) _glBlendFuncSeparate = 0; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | void BlendFunc::Extensions::setupGLExtensions(unsigned int contextID) |
|---|
| 108 | { |
|---|
| 109 | _isBlendFuncSeparateSupported = OSG_GLES2_FEATURES || OSG_GL3_FEATURES || |
|---|
| 110 | isGLExtensionSupported(contextID, "GL_EXT_blend_func_separate") || |
|---|
| 111 | strncmp((const char*)glGetString(GL_VERSION), "1.4", 3) >= 0; |
|---|
| 112 | |
|---|
| 113 | setGLExtensionFuncPtr(_glBlendFuncSeparate, "glBlendFuncSeparate", "glBlendFuncSeparateEXT"); |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | void BlendFunc::Extensions::glBlendFuncSeparate(GLenum sfactorRGB, |
|---|
| 117 | GLenum dfactorRGB, |
|---|
| 118 | GLenum sfactorAlpha, |
|---|
| 119 | GLenum dfactorAlpha) const |
|---|
| 120 | { |
|---|
| 121 | if (_glBlendFuncSeparate) |
|---|
| 122 | { |
|---|
| 123 | _glBlendFuncSeparate(sfactorRGB, dfactorRGB, sfactorAlpha, dfactorAlpha); |
|---|
| 124 | } |
|---|
| 125 | else |
|---|
| 126 | { |
|---|
| 127 | OSG_WARN<<"Error: glBlendFuncSeparate not supported by OpenGL driver"<<std::endl; |
|---|
| 128 | } |
|---|
| 129 | } |
|---|