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