]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/CheckSceneryVisitor.cxx
Merge branch 'next' of git.gitorious.org:fg/simgear into next
[simgear.git] / simgear / scene / model / CheckSceneryVisitor.cxx
1 // Copyright (C) 2008 Till Busch buti@bux.at
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/Transform>
22
23 #include <simgear/debug/logstream.hxx>
24
25 #include "CheckSceneryVisitor.hxx"
26 #include "SGPagedLOD.hxx"
27
28 #include <simgear/math/SGMath.hxx>
29
30 using namespace simgear;
31
32 CheckSceneryVisitor::CheckSceneryVisitor(osgDB::DatabasePager* dbp, const osg::Vec3 &position, double range,
33                                          osg::FrameStamp* framestamp)
34 :osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR,
35                   osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
36 _position(position), _range(range), _loaded(true), _dbp(dbp), _framestamp(framestamp)
37 {
38     _viewMatrices.push_back(osg::Matrix::identity());
39 }
40
41 void CheckSceneryVisitor::apply(osg::Node& node)
42 {
43     traverse(node);
44 }
45
46 void CheckSceneryVisitor::apply(osg::PagedLOD& node)
47 {
48     SGPagedLOD *sgplod = dynamic_cast<SGPagedLOD*>(&node);
49     if (sgplod) {
50         osg::Vec3 pos = sgplod->getCenter() * _viewMatrices.back();
51         double dist = (pos-_position).length();
52         if (dist < _range) {
53             if (sgplod->getNumChildren() < 1) {
54                 // if the DatabasePager would load LODs while the splashscreen
55                 // is there, we could just wait for the models to be loaded
56                 // by only setting setLoaded(false) here
57                 sgplod->forceLoad(_dbp,_framestamp, getNodePath());
58                 setLoaded(false);
59             }
60         }
61     }
62     traverse(node);
63 }
64
65 void CheckSceneryVisitor::apply(osg::Transform &node)
66 {
67     osg::Matrix currMatrix = _viewMatrices.back();
68     bool pushMatrix = node.computeLocalToWorldMatrix(currMatrix, this);
69
70     if (pushMatrix) {
71         _viewMatrices.push_back(currMatrix);
72     }
73     traverse(node);
74     if (pushMatrix) {
75         _viewMatrices.pop_back();
76     }
77 }