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

Revision 13041, 5.5 kB (checked in by robert, 3 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/BlendEquation>
14#include <osg/GLExtensions>
15#include <osg/State>
16#include <osg/Notify>
17#include <osg/buffered_value>
18
19
20using namespace osg;
21
22BlendEquation::BlendEquation():
23    _equationRGB(FUNC_ADD),
24    _equationAlpha(FUNC_ADD)
25{
26}
27
28BlendEquation::BlendEquation(Equation equation):
29    _equationRGB(equation),
30    _equationAlpha(equation)
31{
32}
33
34BlendEquation::BlendEquation(Equation equationRGB, Equation equationAlpha):
35    _equationRGB(equationRGB),
36    _equationAlpha(equationAlpha)
37{
38}
39
40BlendEquation::~BlendEquation()
41{
42}
43
44void BlendEquation::apply(State& state) const
45{
46
47   // get the contextID (user defined ID of 0 upwards) for the
48    // current OpenGL context.
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
86typedef buffered_value< ref_ptr<BlendEquation::Extensions> > BufferedExtensions;
87static BufferedExtensions s_extensions;
88
89BlendEquation::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
95void BlendEquation::setExtensions(unsigned int contextID,Extensions* extensions)
96{
97    s_extensions[contextID] = extensions;
98}
99
100
101BlendEquation::Extensions::Extensions(unsigned int contextID)
102{
103    setupGLExtensions(contextID);
104}
105
106BlendEquation::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
117void 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
125void 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
143void 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
155void 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}
Note: See TracBrowser for help on using the browser.