]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tileentry.hxx
ssgSharedPtr.hxx -> SGSharedPtr.hxx
[flightgear.git] / src / Scenery / tileentry.hxx
1 // tileentry.hxx -- routines to handle an individual 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 // $Id$
22
23
24 #ifndef _TILEENTRY_HXX
25 #define _TILEENTRY_HXX
26
27
28 #ifndef __cplusplus
29 # error This library requires C++
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 #  include <config.h>
34 #endif
35
36 #include <simgear/compiler.h>
37
38 #include <vector>
39 #include STL_STRING
40
41 #include <simgear/bucket/newbucket.hxx>
42 #include <simgear/math/point3d.hxx>
43 #include <simgear/misc/sg_path.hxx>
44 #include <simgear/scene/model/placementtrans.hxx>
45
46 #include <osg/ref_ptr>
47 #include <osg/Array>
48 #include <osg/Group>
49 #include <osg/LOD>
50 #include <osg/MatrixTransform>
51 #include <osg/Switch>
52
53 #if defined( sgi )
54 #include <strings.h>
55 #endif
56
57 SG_USING_STD(string);
58 SG_USING_STD(vector);
59
60
61 typedef vector < Point3D > point_list;
62 typedef point_list::iterator point_list_iterator;
63 typedef point_list::const_iterator const_point_list_iterator;
64
65
66 class FGTileEntry;
67
68
69 /**
70  * A class to hold deferred model loading info
71  */
72 class FGDeferredModel {
73
74 private:
75
76     string model_path;
77     string texture_path;
78     FGTileEntry *tile;
79     osg::ref_ptr<osg::MatrixTransform> obj_trans;
80     SGBucket bucket;
81     bool cache_obj;
82
83
84 public:
85
86     inline FGDeferredModel() { }
87     inline FGDeferredModel( const string& mp, const string& tp, SGBucket b,
88                             FGTileEntry *t, osg::MatrixTransform *ot, bool co )
89     {
90         model_path = mp;
91         texture_path = tp;
92         bucket = b;
93         tile = t;
94         obj_trans = ot;
95         cache_obj = co;
96     }
97     inline ~FGDeferredModel() { }
98     inline const string& get_model_path() const { return model_path; }
99     inline const string& get_texture_path() const { return texture_path; }
100     inline const SGBucket& get_bucket() const { return bucket; }
101     inline const bool get_cache_state() const { return cache_obj; }
102     inline FGTileEntry *get_tile() const { return tile; }
103     inline osg::MatrixTransform *get_obj_trans() const { return obj_trans.get(); }
104 };
105
106
107 /**
108  * A class to encapsulate everything we need to know about a scenery tile.
109  */
110 class FGTileEntry {
111
112 public:
113
114     // global tile culling data
115     Point3D center;
116     double bounding_radius;
117
118     // this tile's official location in the world
119     SGBucket tile_bucket;
120
121 private:
122
123     // ssg tree structure for this tile is as follows:
124     // osg::Group(scene)
125     //     - osg::Group(terrain)
126     //        - SGPlacementTransform(tile)
127     //           - osg::LOD(tile)
128     //              - ssgEntity(tile)
129     //                 - kid1(fan)
130     //                 - kid2(fan)
131     //                   ...
132     //                 - kidn(fan)
133
134     // pointer to ssg transform for this tile
135     osg::ref_ptr<SGPlacementTransform> terra_transform;
136     osg::ref_ptr<SGPlacementTransform> vasi_lights_transform;
137     osg::ref_ptr<SGPlacementTransform> rwy_lights_transform;
138     osg::ref_ptr<SGPlacementTransform> taxi_lights_transform;
139     osg::ref_ptr<SGPlacementTransform> gnd_lights_transform;
140
141     // pointer to ssg range selector for this tile
142     osg::ref_ptr<osg::LOD> terra_range;
143     osg::ref_ptr<osg::LOD> gnd_lights_range;
144
145     // we create several preset brightness and can choose which one we
146     // want based on lighting conditions.
147     osg::ref_ptr<osg::Switch> gnd_lights_brightness;
148
149     // we need to be able to turn runway lights on or off (doing this
150     // via a call back would be nifty, but then the call back needs to
151     // know about the higher level application's global state which is
152     // a problem if we move the code into simgear.)
153     osg::ref_ptr<osg::Switch> vasi_lights_selector;
154     osg::ref_ptr<osg::Switch> rwy_lights_selector;
155     osg::ref_ptr<osg::Switch> taxi_lights_selector;
156
157     /**
158      * Indicates this tile has been loaded from a file and connected
159      * into the scene graph.  Note that this may be set asynchronously
160      * by another thread.
161      */
162     volatile bool loaded;
163
164     /**
165      * Count of pending models to load for this tile.  This tile
166      * cannot be removed until this number reaches zero (i.e. no
167      * pending models to load for this tile.)
168      */
169     volatile int pending_models;
170
171     bool obj_load( const string& path,
172                    osg::Group* geometry,
173                    osg::Group* vasi_lights,
174                    osg::Group* rwy_lights,
175                    osg::Group* taxi_lights,
176                    osg::Vec3Array* gound_lights,
177                    bool is_base );
178
179     osg::Node* gen_lights( SGMaterialLib *matlib, osg::Vec3Array *lights,
180                            int inc, float bright );
181
182     double timestamp;
183
184     /**
185      * this value is used by the tile scheduler/loader to mark which
186      * tiles are in the primary ring (i.e. the current tile or the
187      * surrounding eight.)  Other routines then can use this as an
188      * optimization and not do some operation to tiles outside of this
189      * inner ring.  (For instance vasi color updating)
190      */
191     bool is_inner_ring;
192
193     /**
194      * this variable tracks the status of the incremental memory
195      * freeing.
196      */
197     enum {
198         NODES = 0x01,
199         VEC_PTRS = 0x02,
200         TERRA_NODE = 0x04,
201         GROUND_LIGHTS = 0x08,
202         VASI_LIGHTS = 0x10,
203         RWY_LIGHTS = 0x20,
204         TAXI_LIGHTS = 0x40,
205         LIGHTMAPS = 0x80
206     };
207     int free_tracker;
208
209 public:
210
211     // Constructor
212     FGTileEntry( const SGBucket& b );
213
214     // Destructor
215     ~FGTileEntry();
216
217     // Clean up the memory used by this tile and delete the arrays
218     // used by ssg as well as the whole ssg branch.  This does a
219     // partial clean up and exits so we can spread the load across
220     // multiple frames.  Returns false if work remaining to be done,
221     // true if dynamically allocated memory used by this tile is
222     // completely freed.
223     bool free_tile();
224
225     // Update the ssg transform node for this tile so it can be
226     // properly drawn relative to our (0,0,0) point
227     void prep_ssg_node( const Point3D& p, sgVec3 up, float vis);
228
229     /**
230      * Load tile data from a file.
231      * @param base name of directory containing tile data file.
232      * @param is_base is this a base terrain object for which we should generate
233      *        random ground light points */
234     void load( const string_list &base_path, bool is_base );
235
236     /**
237      * Return true if the tile entry is loaded, otherwise return false
238      * indicating that the loading thread is still working on this.
239      */
240     inline bool is_loaded() const { return loaded; }
241
242     /**
243      * decrement the pending models count
244      */
245     inline void dec_pending_models() { pending_models--; }
246
247     /**
248      * return the number of remaining pending models for this tile
249      */
250     inline int get_pending_models() const { return pending_models; }
251
252     /**
253      * Return the "bucket" for this tile
254      */
255     inline const SGBucket& get_tile_bucket() const { return tile_bucket; }
256
257     /**
258      * Add terrain mesh and ground lighting to scene graph.
259      */
260     void add_ssg_nodes( osg::Group *terrain_branch,
261                         osg::Group *gnd_lights_branch,
262                         osg::Group *vasi_lights_branch,
263                         osg::Group *rwy_lights_branch,
264                         osg::Group *taxi_lights_branch );
265
266     /**
267      * disconnect terrain mesh and ground lighting nodes from scene
268      * graph for this tile.
269      */
270     void disconnect_ssg_nodes();
271
272         
273     /**
274      * return the SSG Transform node for the terrain
275      */
276     inline SGPlacementTransform *get_terra_transform() const { return terra_transform.get(); }
277
278     inline double get_timestamp() const { return timestamp; }
279     inline void set_timestamp( double time_ms ) { timestamp = time_ms; }
280
281     inline bool get_inner_ring() const { return is_inner_ring; }
282     inline void set_inner_ring( bool val ) { is_inner_ring = val; }
283 };
284
285
286 #endif // _TILEENTRY_HXX