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