]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/Effect.cxx
9625fb3bd80747a473364863516b498abc3ca225
[simgear.git] / simgear / scene / material / Effect.cxx
1 #include "Effect.hxx"
2 #include "Technique.hxx"
3 #include "Pass.hxx"
4
5 #include <algorithm>
6 #include <functional>
7 #include <iterator>
8
9 #include <boost/bind.hpp>
10 #include <boost/foreach.hpp>
11
12 #include <osg/Drawable>
13 #include <osg/RenderInfo>
14 #include <osg/StateSet>
15 #include <osgUtil/CullVisitor>
16 #include <osgDB/Registry>
17 #include <osgDB/Input>
18 #include <osgDB/ParameterOutput>
19
20 #include <simgear/structure/OSGUtils.hxx>
21
22
23
24 namespace simgear
25 {
26 using namespace osg;
27 using namespace osgUtil;
28
29 Effect::Effect()
30 {
31 }
32
33 Effect::Effect(const Effect& rhs, const CopyOp& copyop)
34 {
35     using namespace std;
36     using namespace boost;
37     transform(rhs.techniques.begin(), rhs.techniques.end(),
38               backRefInsertIterator(techniques),
39               bind(simgear::clone_ref<Technique>, _1, copyop));
40 }
41
42 // Assume that the last technique is always valid.
43 StateSet* Effect::getDefaultStateSet()
44 {
45     Technique* tniq = techniques.back().get();
46     if (!tniq)
47         return 0;
48     Pass* pass = tniq->passes.front().get();
49     if (!pass)
50         return 0;
51     return pass->getStateSet();
52 }
53
54 // There should always be a valid technique in an effect.
55
56 Technique* Effect::chooseTechnique(RenderInfo* info)
57 {
58     BOOST_FOREACH(ref_ptr<Technique>& technique, techniques)
59     {
60         if (technique->valid(info) == Technique::VALID)
61             return technique.get();
62     }
63     return 0;
64 }
65
66 void Effect::resizeGLObjectBuffers(unsigned int maxSize)
67 {
68     BOOST_FOREACH(const ref_ptr<Technique>& technique, techniques)
69     {
70         technique->resizeGLObjectBuffers(maxSize);
71     }
72 }
73
74 void Effect::releaseGLObjects(osg::State* state) const
75 {
76     BOOST_FOREACH(const ref_ptr<Technique>& technique, techniques)
77     {
78         technique->releaseGLObjects(state);
79     }
80 }
81
82 Effect::~Effect()
83 {
84 }
85
86 bool Effect_writeLocalData(const Object& obj, osgDB::Output& fw)
87 {
88     const Effect& effect = static_cast<const Effect&>(obj);
89
90     fw.indent() << "techniques " << effect.techniques.size() << "\n";
91     BOOST_FOREACH(const ref_ptr<Technique>& technique, effect.techniques) {
92         fw.writeObject(*technique);
93     }
94     return true;
95 }
96
97 namespace
98 {
99 osgDB::RegisterDotOsgWrapperProxy effectProxy
100 (
101     new Effect,
102     "simgear::Effect",
103     "Object simgear::Effect",
104     0,
105     &Effect_writeLocalData
106     );
107 }
108 }
109