]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/CheckSceneryVisitor.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / model / CheckSceneryVisitor.cxx
1 // Copyright (C) 2008 Till Busch buti@bux.at
2 // Copyright (C) 2011 Mathias Froehlich
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License as
6 // published by the Free Software Foundation; either version 2 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but
10 // WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18 #ifdef HAVE_CONFIG_H
19 #  include <simgear_config.h>
20 #endif
21
22 #include <osg/Transform>
23 #include <osg/ProxyNode>
24 #include <osgDB/DatabasePager>
25
26 #include <simgear/debug/logstream.hxx>
27
28 #include "CheckSceneryVisitor.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), _matrix(osg::Matrix::identity())
37 {
38     setDatabaseRequestHandler(dbp);
39     setFrameStamp(framestamp);
40 }
41
42 void CheckSceneryVisitor::apply(osg::Node& node)
43 {
44     traverse(node);
45 }
46
47 void CheckSceneryVisitor::apply(osg::ProxyNode& node)
48 {
49     osg::Vec3 pos = node.getCenter() * _matrix;
50     double dist = (pos - _position).length();
51     if (dist < _range) {
52         for (unsigned i = 0; i < node.getNumFileNames(); ++i) {
53             if (node.getFileName(i).empty())
54                 continue;
55             // Check if this is already loaded.
56             if (i < node.getNumChildren() && node.getChild(i))
57                 continue;
58
59             // if the DatabasePager would load LODs while the splashscreen
60             // is there, we could just wait for the models to be loaded
61             // by only setting setLoaded(false) here
62             osg::NodePath nodePath = getNodePath();
63             DatabaseRequestHandler* db = getDatabaseRequestHandler();
64             const osg::FrameStamp* fs = getFrameStamp();
65             db->requestNodeFile(node.getFileName(i), nodePath, 1.0 /*priority*/, fs,
66                                 node.getDatabaseRequest(i), node.getDatabaseOptions());
67             setLoaded(false);
68         }
69     }
70     traverse(node);
71 }
72
73 void CheckSceneryVisitor::apply(osg::PagedLOD& node)
74 {
75     osg::Vec3 pos = node.getCenter() * _matrix;
76     double dist = (pos - _position).length();
77     if (dist < _range) {
78         for (unsigned i = 0; i < node.getNumFileNames(); ++i) {
79             if (node.getFileName(i).empty())
80                 continue;
81             // Check if this is already loaded.
82             if (i < node.getNumChildren() && node.getChild(i))
83                 continue;
84
85             // if the DatabasePager would load LODs while the splashscreen
86             // is there, we could just wait for the models to be loaded
87             // by only setting setLoaded(false) here
88             osg::NodePath nodePath = getNodePath();
89             DatabaseRequestHandler* db = getDatabaseRequestHandler();
90             const osg::FrameStamp* fs = getFrameStamp();
91             db->requestNodeFile(node.getFileName(i), nodePath, 1.0 /*priority*/, fs,
92                                 node.getDatabaseRequest(i), node.getDatabaseOptions());
93             setLoaded(false);
94         }
95     }
96     traverse(node);
97 }
98
99 void CheckSceneryVisitor::apply(osg::Transform &node)
100 {
101     osg::Matrix matrix = _matrix;
102     node.computeLocalToWorldMatrix(_matrix, this);
103     traverse(node);
104     _matrix = matrix;
105 }