]> git.mxchange.org Git - simgear.git/blob - simgear/scene/material/EffectCullVisitor.cxx
Disable LOD when rendering to the shadow map.
[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 #include <osg/LOD>
24
25 #include "EffectCullVisitor.hxx"
26
27 #include "EffectGeode.hxx"
28 #include "Effect.hxx"
29 #include "Technique.hxx"
30
31 #include <simgear/scene/util/RenderConstants.hxx>
32
33 namespace simgear
34 {
35
36 using osgUtil::CullVisitor;
37
38 EffectCullVisitor::EffectCullVisitor() :
39     _ignoreLOD(false),
40     _collectLights(false)
41 {
42 }
43
44 EffectCullVisitor::EffectCullVisitor(bool ignoreLOD, bool collectLights) :
45     _ignoreLOD(ignoreLOD),
46     _collectLights(collectLights)
47 {
48 }
49
50 EffectCullVisitor::EffectCullVisitor(const EffectCullVisitor& rhs) :
51     osg::Referenced(rhs),
52     CullVisitor(rhs),
53     _ignoreLOD(rhs._ignoreLOD),
54     _collectLights(rhs._collectLights)
55 {
56 }
57
58 CullVisitor* EffectCullVisitor::clone() const
59 {
60     return new EffectCullVisitor(*this);
61 }
62
63 void EffectCullVisitor::apply(osg::LOD& node)
64 {
65     if (_ignoreLOD) {
66         if (isCulled(node)) return;
67
68         // push the culling mode.
69         pushCurrentMask();
70
71         // push the node's state.
72         osg::StateSet* node_state = node.getStateSet();
73         if (node_state) pushStateSet(node_state);
74
75         if (_traversalMode==TRAVERSE_PARENTS) node.osg::Group::ascend(*this);
76         else if (_traversalMode!=TRAVERSE_NONE) node.osg::Group::traverse(*this);
77         // pop the node's state off the render graph stack.
78         if (node_state) popStateSet();
79
80         // pop the culling mode.
81         popCurrentMask();
82     }
83     else
84         CullVisitor::apply(node);
85 }
86
87 void EffectCullVisitor::apply(osg::Geode& node)
88 {
89     if (isCulled(node))
90         return;
91     EffectGeode *eg = dynamic_cast<EffectGeode*>(&node);
92     if (!eg) {
93         CullVisitor::apply(node);
94         return;
95     }
96     if (_collectLights && ( eg->getNodeMask() & MODELLIGHT_BIT ) ) {
97         _lightList.push_back( eg );
98     }
99     Effect* effect = eg->getEffect();
100     Technique* technique = 0;
101     if (!effect) {
102         CullVisitor::apply(node);
103         return;
104     } else if (!(technique = effect->chooseTechnique(&getRenderInfo()))) {
105         return;
106     }
107     // push the node's state.
108     osg::StateSet* node_state = node.getStateSet();
109     if (node_state)
110         pushStateSet(node_state);
111     for (EffectGeode::DrawablesIterator beginItr = eg->drawablesBegin(),
112              e = eg->drawablesEnd();
113          beginItr != e;
114          beginItr = technique->processDrawables(beginItr, e, this,
115                                                 eg->isCullingActive()))
116         ;
117     // pop the node's state off the geostate stack.
118     if (node_state)
119         popStateSet();
120
121 }
122
123 void EffectCullVisitor::reset()
124 {
125     _lightList.clear();
126
127     osgUtil::CullVisitor::reset();
128 }
129
130 void EffectCullVisitor::clearBufferList()
131 {
132     _bufferList.clear();
133 }
134
135 void EffectCullVisitor::addBuffer(int i, osg::Texture2D* tex)
136 {
137     _bufferList.insert(std::make_pair(i,tex));
138 }
139
140 osg::Texture2D* EffectCullVisitor::getBuffer(int i)
141 {
142     return _bufferList[i];
143 }
144
145 }