]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tileentry.cxx
Fix native protocol crashes.
[flightgear.git] / src / Scenery / tileentry.cxx
1 // tileentry.cxx -- routines to handle a scenery tile
2 //
3 // Written by Curtis Olson, started May 1998.
4 //
5 // Copyright (C) 1998 - 2001  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #ifdef HAVE_CONFIG_H
22 #  include <config.h>
23 #endif
24
25 #include <simgear/compiler.h>
26
27 #include <string>
28 #include <sstream>
29 #include <istream>
30
31 #include <osg/LOD>
32
33 #include <simgear/bucket/newbucket.hxx>
34 #include <simgear/debug/logstream.hxx>
35
36 #include "tileentry.hxx"
37
38 using std::string;
39
40 // Constructor
41 TileEntry::TileEntry ( const SGBucket& b )
42     : tile_bucket( b ),
43       tileFileName(b.gen_index_str()),
44       _node( new osg::LOD ),
45       _priority(-FLT_MAX),
46       _current_view(false),
47       _time_expired(-1.0)
48 {
49     tileFileName += ".stg";
50     _node->setName(tileFileName);
51     // Give a default LOD range so that traversals that traverse
52     // active children (like the groundcache lookup) will work before
53     // tile manager has had a chance to update this node.
54     _node->setRange(0, 0.0, 10000.0);
55 }
56
57 TileEntry::TileEntry( const TileEntry& t )
58 : tile_bucket( t.tile_bucket ),
59   tileFileName(t.tileFileName),
60   _node( new osg::LOD ),
61   _priority(t._priority),
62   _current_view(t._current_view),
63   _time_expired(t._time_expired)
64 {
65     _node->setName(tileFileName);
66     // Give a default LOD range so that traversals that traverse
67     // active children (like the groundcache lookup) will work before
68     // tile manager has had a chance to update this node.
69     _node->setRange(0, 0.0, 10000.0);
70 }
71
72 // Destructor
73 TileEntry::~TileEntry ()
74 {
75 }
76
77 // Update the ssg transform node for this tile so it can be
78 // properly drawn relative to our (0,0,0) point
79 void TileEntry::prep_ssg_node(float vis) {
80     if (!is_loaded())
81         return;
82     // visibility can change from frame to frame so we update the
83     // range selector cutoff's each time.
84     float bounding_radius = _node->getChild(0)->getBound().radius();
85     _node->setRange( 0, 0, vis + bounding_radius );
86 }
87
88 void
89 TileEntry::addToSceneGraph(osg::Group *terrain_branch)
90 {
91     terrain_branch->addChild( _node.get() );
92
93     SG_LOG( SG_TERRAIN, SG_DEBUG,
94             "connected a tile into scene graph.  _node = "
95             << _node.get() );
96     SG_LOG( SG_TERRAIN, SG_DEBUG, "num parents now = "
97             << _node->getNumParents() );
98 }
99
100
101 void
102 TileEntry::removeFromSceneGraph()
103 {
104     SG_LOG( SG_TERRAIN, SG_DEBUG, "disconnecting TileEntry nodes" );
105
106     if (! is_loaded()) {
107         SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a not-fully loaded tile!" );
108     } else {
109         SG_LOG( SG_TERRAIN, SG_DEBUG, "removing a fully loaded tile!  _node = " << _node.get() );
110     }
111
112     // find the nodes branch parent
113     if ( _node->getNumParents() > 0 ) {
114         // find the first parent (should only be one)
115         osg::Group *parent = _node->getParent( 0 ) ;
116         if( parent ) {
117             parent->removeChild( _node.get() );
118         }
119     }
120 }
121
122 void
123 TileEntry::refresh()
124 {
125     osg::Group *parent = NULL;
126     // find the nodes branch parent
127     if ( _node->getNumParents() > 0 ) {
128         // find the first parent (should only be one)
129         parent = _node->getParent( 0 ) ;
130         if( parent ) {
131             parent->removeChild( _node.get() );
132         }
133     }
134     _node = new osg::LOD;
135     if (parent)
136         parent->addChild(_node.get());
137 }