]> git.mxchange.org Git - simgear.git/blob - simgear/scene/model/CheckSceneryVisitor.cxx
Add preliminary spot light animation
[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 #include <simgear/math/SGMath.hxx>
31
32 using namespace simgear;
33
34 CheckSceneryVisitor::CheckSceneryVisitor(osgDB::DatabasePager* dbp, const osg::Vec3 &position, double range,
35                                          osg::FrameStamp* framestamp)
36 :osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR,
37                   osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
38  _position(position), _range(range), _loaded(true), _matrix(osg::Matrix::identity())
39 {
40     setDatabaseRequestHandler(dbp);
41     setFrameStamp(framestamp);
42 }
43
44 void CheckSceneryVisitor::apply(osg::Node& node)
45 {
46     traverse(node);
47 }
48
49 void CheckSceneryVisitor::apply(osg::ProxyNode& node)
50 {
51     osg::Vec3 pos = node.getCenter() * _matrix;
52     double dist = (pos - _position).length();
53     if (dist < _range) {
54         for (unsigned i = 0; i < node.getNumFileNames(); ++i) {
55             if (node.getFileName(i).empty())
56                 continue;
57             // Check if this is already loaded.
58             if (i < node.getNumChildren() && node.getChild(i))
59                 continue;
60
61             // if the DatabasePager would load LODs while the splashscreen
62             // is there, we could just wait for the models to be loaded
63             // by only setting setLoaded(false) here
64             osg::NodePath nodePath = getNodePath();
65             DatabaseRequestHandler* db = getDatabaseRequestHandler();
66             const osg::FrameStamp* fs = getFrameStamp();
67             db->requestNodeFile(node.getFileName(i), nodePath, 1.0 /*priority*/, fs,
68                                 node.getDatabaseRequest(i), node.getDatabaseOptions());
69             setLoaded(false);
70         }
71     }
72     traverse(node);
73 }
74
75 void CheckSceneryVisitor::apply(osg::PagedLOD& node)
76 {
77     osg::Vec3 pos = node.getCenter() * _matrix;
78     double dist = (pos - _position).length();
79     if (dist < _range) {
80         for (unsigned i = 0; i < node.getNumFileNames(); ++i) {
81             if (node.getFileName(i).empty())
82                 continue;
83             // Check if this is already loaded.
84             if (i < node.getNumChildren() && node.getChild(i))
85                 continue;
86
87             // if the DatabasePager would load LODs while the splashscreen
88             // is there, we could just wait for the models to be loaded
89             // by only setting setLoaded(false) here
90             osg::NodePath nodePath = getNodePath();
91             DatabaseRequestHandler* db = getDatabaseRequestHandler();
92             const osg::FrameStamp* fs = getFrameStamp();
93             db->requestNodeFile(node.getFileName(i), nodePath, 1.0 /*priority*/, fs,
94                                 node.getDatabaseRequest(i), node.getDatabaseOptions());
95             setLoaded(false);
96         }
97     }
98     traverse(node);
99 }
100
101 void CheckSceneryVisitor::apply(osg::Transform &node)
102 {
103     osg::Matrix matrix = _matrix;
104     node.computeLocalToWorldMatrix(_matrix, this);
105     traverse(node);
106     _matrix = matrix;
107 }