00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <simgear_config.h>
00019 #endif
00020
00021 #include <osg/Transform>
00022
00023 #include <simgear/debug/logstream.hxx>
00024
00025 #include "CheckSceneryVisitor.hxx"
00026 #include "SGPagedLOD.hxx"
00027
00028 #include <simgear/math/SGMath.hxx>
00029
00030 using namespace simgear;
00031
00032 CheckSceneryVisitor::CheckSceneryVisitor(osgDB::DatabasePager* dbp, const osg::Vec3 &position, double range)
00033 :osg::NodeVisitor(osg::NodeVisitor::NODE_VISITOR,
00034 osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
00035 _position(position), _range(range), _loaded(true), _dbp(dbp)
00036 {
00037 _viewMatrices.push_back(osg::Matrix::identity());
00038 }
00039
00040 void CheckSceneryVisitor::apply(osg::Node& node)
00041 {
00042 traverse(node);
00043 }
00044
00045 void CheckSceneryVisitor::apply(osg::PagedLOD& node)
00046 {
00047 SGPagedLOD *sgplod = dynamic_cast<SGPagedLOD*>(&node);
00048 if (sgplod) {
00049 osg::Vec3 pos = sgplod->getCenter() * _viewMatrices.back();
00050 double dist = (pos-_position).length();
00051 if (dist < _range) {
00052 if (sgplod->getNumChildren() < 1) {
00053
00054
00055
00056 sgplod->forceLoad(_dbp);
00057 setLoaded(false);
00058 }
00059 }
00060 }
00061 traverse(node);
00062 }
00063
00064 void CheckSceneryVisitor::apply(osg::Transform &node)
00065 {
00066 osg::Matrix currMatrix = _viewMatrices.back();
00067 bool pushMatrix = node.computeLocalToWorldMatrix(currMatrix, this);
00068
00069 if (pushMatrix) {
00070 _viewMatrices.push_back(currMatrix);
00071 }
00072 traverse(node);
00073 if (pushMatrix) {
00074 _viewMatrices.pop_back();
00075 }
00076 }