root/OpenSceneGraph/trunk/src/osg/BlendFunc.cpp @ 13041

Revision 13041, 4.2 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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#include <osg/BlendFunc>
14#include <osg/GLExtensions>
15#include <osg/State>
16#include <osg/Notify>
17
18using namespace osg;
19
20BlendFunc::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
28BlendFunc::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
36BlendFunc::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
44BlendFunc::~BlendFunc()
45{
46}
47
48void BlendFunc::apply(State& state) const
49{
50    if (_source_factor != _source_factor_alpha ||
51        _destination_factor != _destination_factor_alpha)
52    {
53        // get the contextID (user defined ID of 0 upwards) for the
54        // current OpenGL context.
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
74typedef buffered_value< ref_ptr<BlendFunc::Extensions> > BufferedExtensions;
75static BufferedExtensions s_extensions;
76
77BlendFunc::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
83void BlendFunc::setExtensions(unsigned int contextID,Extensions* extensions)
84{
85    s_extensions[contextID] = extensions;
86}
87
88
89BlendFunc::Extensions::Extensions(unsigned int contextID)
90{
91    setupGLExtensions(contextID);
92}
93
94BlendFunc::Extensions::Extensions(const Extensions& rhs):
95    Referenced()
96{
97    _isBlendFuncSeparateSupported = rhs._isBlendFuncSeparateSupported;
98    _glBlendFuncSeparate = rhs._glBlendFuncSeparate;
99}
100
101void BlendFunc::Extensions::lowestCommonDenominator(const Extensions& rhs)
102{
103    if (!rhs._isBlendFuncSeparateSupported)  _isBlendFuncSeparateSupported = false;
104    if (!rhs._glBlendFuncSeparate)           _glBlendFuncSeparate = 0;
105}
106
107void 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
116void 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}
Note: See TracBrowser for help on using the browser.