]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/EffectCullVisitor.cxx
Hack to avoid redrawing the whole scene another time when only light volume are requested
[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 (node.getNodeMask() != simgear::MODELLIGHT_BIT ) // HACK
56         node.setNodeMask(node.getNodeMask() & ~simgear::MODELLIGHT_BIT);
57     if (isCulled(node))
58         return;
59     EffectGeode *eg = dynamic_cast<EffectGeode*>(&node);
60     if (!eg) {
61         CullVisitor::apply(node);
62         return;
63     }
64     if (_collectLights && ( eg->getNodeMask() & MODELLIGHT_BIT ) ) {
65         _lightList.push_back( eg );
66     }
67     Effect* effect = eg->getEffect();
68     Technique* technique = 0;
69     if (!effect) {
70         CullVisitor::apply(node);
71         return;
72     } else if (!(technique = effect->chooseTechnique(&getRenderInfo()))) {
73         return;
74     }
75     // push the node's state.
76     osg::StateSet* node_state = node.getStateSet();
77     if (node_state)
78         pushStateSet(node_state);
79     for (EffectGeode::DrawablesIterator beginItr = eg->drawablesBegin(),
80              e = eg->drawablesEnd();
81          beginItr != e;
82          beginItr = technique->processDrawables(beginItr, e, this,
83                                                 eg->isCullingActive()))
84         ;
85     // pop the node's state off the geostate stack.
86     if (node_state)
87         popStateSet();
88
89 }
90
91 void EffectCullVisitor::reset()
92 {
93     _lightList.clear();
94
95     osgUtil::CullVisitor::reset();
96 }
97
98 void EffectCullVisitor::clearBufferList()
99 {
100     _bufferList.clear();
101 }
102
103 void EffectCullVisitor::addBuffer(std::string b, osg::Texture2D* tex)
104 {
105     _bufferList.insert(std::make_pair(b,tex));
106 }
107
108 osg::Texture2D* EffectCullVisitor::getBuffer(std::string b)
109 {
110     return _bufferList[b];
111 }
112
113 }