]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/parseBlendFunc.cxx
Fix removal of directories.
[simgear.git] / simgear / scene / material / parseBlendFunc.cxx
1 // Parse osg::BlendFunc from property nodes
2 //
3 // Copyright (C) 2008 - 2010  Tim Moore timoore33@gmail.com
4 // Copyright (C) 2013  Thomas Geymayer <tomgey@gmail.com>
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Library General Public
8 // License as published by the Free Software Foundation; either
9 // version 2 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Library General Public License for more details.
15 //
16 // You should have received a copy of the GNU Library General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
19
20 #include "parseBlendFunc.hxx"
21 #include "EffectBuilder.hxx"
22 #include <simgear/scene/util/StateAttributeFactory.hxx>
23 #include <osg/BlendFunc>
24
25 using osg::BlendFunc;
26
27 namespace simgear
28 {
29
30   //----------------------------------------------------------------------------
31   extern effect::EffectPropertyMap<BlendFunc::BlendFuncMode> blendFuncModes;
32
33   //----------------------------------------------------------------------------
34   bool parseBlendFunc( osg::StateSet* ss,
35                        const SGPropertyNode* src,
36                        const SGPropertyNode* dest,
37                        const SGPropertyNode* src_rgb,
38                        const SGPropertyNode* dest_rgb,
39                        const SGPropertyNode* src_alpha,
40                        const SGPropertyNode* dest_alpha )
41   {
42     if( !ss )
43       return false;
44
45     BlendFunc::BlendFuncMode src_mode = BlendFunc::ONE;
46     BlendFunc::BlendFuncMode dest_mode = BlendFunc::ZERO;
47
48     if( src )
49       findAttr(blendFuncModes, src, src_mode);
50     if( dest )
51       findAttr(blendFuncModes, dest, dest_mode);
52
53     if( src && dest
54         && !(src_rgb || src_alpha || dest_rgb || dest_alpha)
55         && src_mode == BlendFunc::SRC_ALPHA
56         && dest_mode == BlendFunc::ONE_MINUS_SRC_ALPHA )
57     {
58       ss->setAttributeAndModes(
59         StateAttributeFactory::instance()->getStandardBlendFunc()
60       );
61       return true;
62     }
63
64     BlendFunc* blend_func = new BlendFunc;
65     if( src )
66       blend_func->setSource(src_mode);
67     if( dest )
68       blend_func->setDestination(dest_mode);
69
70     if( src_rgb )
71     {
72       BlendFunc::BlendFuncMode sourceRGBMode;
73       findAttr(blendFuncModes, src_rgb, sourceRGBMode);
74       blend_func->setSourceRGB(sourceRGBMode);
75     }
76     if( dest_rgb)
77     {
78       BlendFunc::BlendFuncMode destRGBMode;
79       findAttr(blendFuncModes, dest_rgb, destRGBMode);
80       blend_func->setDestinationRGB(destRGBMode);
81     }
82     if( src_alpha )
83     {
84       BlendFunc::BlendFuncMode sourceAlphaMode;
85       findAttr(blendFuncModes, src_alpha, sourceAlphaMode);
86       blend_func->setSourceAlpha(sourceAlphaMode);
87     }
88     if( dest_alpha)
89     {
90       BlendFunc::BlendFuncMode destAlphaMode;
91       findAttr(blendFuncModes, dest_alpha, destAlphaMode);
92       blend_func->setDestinationAlpha(destAlphaMode);
93     }
94     ss->setAttributeAndModes(blend_func);
95     return true;
96   }
97
98 } // namespace simgear