]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/EffectCullVisitor.cxx
c44d8d695c1cf086ba42793030a2d829c71f2db1
[simgear.git] / simgear / scene / material / EffectCullVisitor.cxx
1 // Copyright (C) 2008  Timothy Moore timoore@redhat.com
2 //
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License as
5 // published by the Free Software Foundation; either version 2 of the
6 // License, or (at your option) any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but
9 // WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
16
17 #ifdef HAVE_CONFIG_H
18 #  include <simgear_config.h>
19 #endif
20
21 #include <osg/StateSet>
22 #include <osg/Texture2D>
23
24 #include "EffectCullVisitor.hxx"
25
26 #include "EffectGeode.hxx"
27 #include "Effect.hxx"
28 #include "Technique.hxx"
29
30 #include <simgear/scene/util/RenderConstants.hxx>
31
32 namespace simgear
33 {
34
35 using osgUtil::CullVisitor;
36
37 EffectCullVisitor::EffectCullVisitor(bool collectLights) :
38     _collectLights(collectLights)
39 {
40 }
41
42 EffectCullVisitor::EffectCullVisitor(const EffectCullVisitor& rhs) :
43     osg::Referenced(rhs),
44     CullVisitor(rhs)
45 {
46 }
47
48 CullVisitor* EffectCullVisitor::clone() const
49 {
50     return new EffectCullVisitor(*this);
51 }
52
53 void EffectCullVisitor::apply(osg::Geode& node)
54 {
55     if (isCulled(node))
56         return;
57     EffectGeode *eg = dynamic_cast<EffectGeode*>(&node);
58     if (!eg) {
59         CullVisitor::apply(node);
60         return;
61     }
62     if (_collectLights && ( eg->getNodeMask() & MODELLIGHT_BIT ) ) {
63         _lightList.push_back( eg );
64     }
65     Effect* effect = eg->getEffect();
66     Technique* technique = 0;
67     if (!effect) {
68         CullVisitor::apply(node);
69         return;
70     } else if (!(technique = effect->chooseTechnique(&getRenderInfo()))) {
71         return;
72     }
73     // push the node's state.
74     osg::StateSet* node_state = node.getStateSet();
75     if (node_state)
76         pushStateSet(node_state);
77     for (EffectGeode::DrawablesIterator beginItr = eg->drawablesBegin(),
78              e = eg->drawablesEnd();
79          beginItr != e;
80          beginItr = technique->processDrawables(beginItr, e, this,
81                                                 eg->isCullingActive()))
82         ;
83     // pop the node's state off the geostate stack.
84     if (node_state)
85         popStateSet();
86
87 }
88
89 void EffectCullVisitor::reset()
90 {
91     _lightList.clear();
92
93     osgUtil::CullVisitor::reset();
94 }
95
96 void EffectCullVisitor::clearBufferList()
97 {
98     _bufferList.clear();
99 }
100
101 void EffectCullVisitor::addBuffer(int i, osg::Texture2D* tex)
102 {
103     _bufferList.insert(std::make_pair(i,tex));
104 }
105
106 osg::Texture2D* EffectCullVisitor::getBuffer(int i)
107 {
108     return _bufferList[i];
109 }
110
111 }