]> git.mxchange.org Git - flightgear.git/blob - src/Scenery/tilemgr.hxx
Further restructuring of the scenery loading code.
[flightgear.git] / src / Scenery / tilemgr.hxx
1 // tilemgr.hxx -- routines to handle dynamic management of scenery tiles
2 //
3 // Written by Curtis Olson, started January 1998.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef _TILEMGR_HXX
25 #define _TILEMGR_HXX
26
27
28 #ifndef __cplusplus                                                          
29 # error This library requires C++
30 #endif                                   
31
32 #include <simgear/compiler.h>
33
34 #include <queue>
35
36 #include <plib/ssg.h>
37
38 #include <simgear/bucket/newbucket.hxx>
39 #ifdef ENABLE_THREADS
40 #  include <simgear/threads/SGQueue.hxx>
41 #endif // ENABLE_THREADS
42
43 #include "FGTileLoader.hxx"
44 #include "hitlist.hxx"
45 #include "newcache.hxx"
46
47 #if defined(USE_MEM) || defined(WIN32)
48 #  define FG_MEM_COPY(to,from,n)        memcpy(to, from, n)
49 #else
50 #  define FG_MEM_COPY(to,from,n)        bcopy(from, to, n)
51 #endif
52
53 SG_USING_STD( queue );
54
55
56 // forward declaration
57 class FGTileEntry;
58 class FGDeferredModel;
59
60
61 class FGTileMgr {
62
63 private:
64
65     // Tile loading state
66     enum load_state {
67         Start = 0,
68         Inited = 1,
69         Running = 2
70     };
71
72     load_state state;
73
74     // initialize the cache
75     void initialize_queue();
76
77     // schedule a tile for loading
78     void sched_tile( const SGBucket& b );
79
80     // schedule a needed buckets for loading
81     void schedule_needed();
82
83     // see comment at prep_ssg_nodes()
84     void prep_ssg_node( int idx );
85         
86     FGHitList hit_list;
87
88     SGBucket previous_bucket;
89     SGBucket current_bucket;
90     SGBucket pending;
91         
92     FGTileEntry *current_tile;
93         
94     // x and y distance of tiles to load/draw
95     float vis;
96     int xrange, yrange;
97         
98     // current longitude latitude
99     double longitude;
100     double latitude;
101     double last_longitude;
102     double last_latitude;
103
104     /**
105      * tile cache
106      */
107     FGNewCache tile_cache;
108
109     /**
110      * Queue tiles for loading.
111      */
112     FGTileLoader loader;
113     int counter_hack;
114
115     /**
116      * Work queues.
117      *
118      * attach_queue is the tiles that have been loaded [by the pager]
119      * that can be attached to the scene graph by the render thread.
120      *
121      * model_queue is the set of models that need to be loaded by the
122      * primary render thread.
123      */
124 #ifdef ENABLE_THREADS
125     static SGLockedQueue<FGTileEntry *> attach_queue;
126     static SGLockedQueue<FGDeferredModel *> model_queue;
127 #else
128     static queue<FGTileEntry *> attach_queue;
129     static queue<FGDeferredModel *> model_queue;
130 #endif // ENABLE_THREADS
131
132 public:
133
134     /**
135      * Add a loaded tile to the 'attach to the scene graph' queue.
136      */
137     static void ready_to_attach( FGTileEntry *t ) { attach_queue.push( t ); }
138
139 #ifdef WISH_PLIB_WAS_THREADED // but it isn't
140     /**
141      * Tile is detatched from scene graph and is ready to delete
142      */
143     inline void ready_to_delete( FGTileEntry *t ) { loader.remove( t ); }
144 #endif
145
146     /**
147      * Add a pending model to the 'deferred model load' queue
148      */
149     static void model_ready( FGDeferredModel *dm ) { model_queue.push( dm ); }
150
151 public:
152
153     // Constructor
154     FGTileMgr();
155
156     // Destructor
157     ~FGTileMgr();
158
159     // Initialize the Tile Manager subsystem
160     int init();
161
162     // given the current lon/lat (in degrees), fill in the array of
163     // local chunks.  If the chunk isn't already in the cache, then
164     // read it from disk.
165     int update( double lon, double lat );
166
167     // Determine scenery altitude.  Normally this just happens when we
168     // render the scene, but we'd also like to be able to do this
169     // explicitely.  lat & lon are in radians.  abs_view_pos in
170     // meters.  Returns result in meters.
171     void my_ssg_los( string s, ssgBranch *branch, sgdMat4 m, 
172                      const sgdVec3 p, const sgdVec3 dir, sgdVec3 normal );
173         
174     void my_ssg_los( ssgBranch *branch, sgdMat4 m, 
175                      const sgdVec3 p, const sgdVec3 dir,
176                      FGHitList *list );
177
178     // Prepare the ssg nodes ... for each tile, set it's proper
179     // transform and update it's range selector based on current
180     // visibilty
181     void prep_ssg_nodes();
182 };
183
184
185 // the tile manager
186 extern FGTileMgr global_tile_mgr;
187
188
189 #endif // _TILEMGR_HXX