]> git.mxchange.org Git - flightgear.git/blob - Scenery/tilecache.cxx
Contributions from Bernie Bright <bbright@c031.aone.net.au>
[flightgear.git] / Scenery / tilecache.cxx
1 // tilecache.cxx -- routines to handle scenery tile caching
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 // (Log is kept at end of this file)
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifdef HAVE_WINDOWS_H
30 #  include <windows.h>
31 #endif
32
33 #include <GL/glut.h>
34 #include <XGL/xgl.h>
35
36 #include <Bucket/bucketutils.h>
37 #include <Debug/fg_debug.h>
38 #include <Main/options.hxx>
39 #include <Main/views.hxx>
40 #include <Objects/obj.hxx>
41
42 #include "tile.hxx"
43 #include "tilecache.hxx"
44
45
46 // the tile cache
47 fgTILECACHE global_tile_cache;
48
49
50 // Constructor
51 fgTILECACHE::fgTILECACHE( void ) {
52 }
53
54
55 // Initialize the tile cache subsystem
56 void fgTILECACHE::Init( void ) {
57     int i;
58
59     fgPrintf(FG_TERRAIN, FG_INFO, "Initializing the tile cache.\n");
60
61     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
62         tile_cache[i].used = 0;
63     }
64 }
65
66
67 // Search for the specified "bucket" in the cache
68 int fgTILECACHE::Exists( fgBUCKET *p ) {
69     int i;
70
71     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
72         if ( tile_cache[i].tile_bucket.lon == p->lon ) {
73             if ( tile_cache[i].tile_bucket.lat == p->lat ) {
74                 if ( tile_cache[i].tile_bucket.x == p->x ) {
75                     if ( tile_cache[i].tile_bucket.y == p->y ) {
76                         fgPrintf( FG_TERRAIN, FG_DEBUG, 
77                                   "TILE EXISTS in cache ... index = %d\n", i );
78                         return( i );
79                     }
80                 }
81             }
82         }
83     }
84     
85     return( -1 );
86 }
87
88
89 // Fill in a tile cache entry with real data for the specified bucket
90 void fgTILECACHE::EntryFillIn( int index, fgBUCKET *p ) {
91     string root;
92     char base_path[256];
93     char file_name[256];
94
95     // Mark this cache entry as used
96     tile_cache[index].used = 1;
97
98     // Update the bucket
99     tile_cache[index].tile_bucket.lon = p->lon;
100     tile_cache[index].tile_bucket.lat = p->lat;
101     tile_cache[index].tile_bucket.x = p->x;
102     tile_cache[index].tile_bucket.y = p->y;
103
104     // Load the appropriate data file and built tile fragment list
105     fgBucketGenBasePath(p, base_path);
106     root = current_options.get_fg_root();
107     sprintf(file_name, "%s/Scenery/%s/%ld", root.c_str(), 
108             base_path, fgBucketGenIndex(p));
109     fgObjLoad(file_name, &tile_cache[index]);
110     /*
111     tile_cache[index].display_list = 
112         fgObjLoad(file_name, &tile_cache[index].local_ref,
113                   &tile_cache[index].bounding_radius);    
114                   */
115 }
116
117
118 // Free a tile cache entry
119 void fgTILECACHE::EntryFree( int index ) {
120     fgFRAGMENT *fragment;
121
122     // Mark this cache entry as un-used
123     tile_cache[index].used = 0;
124
125     // Update the bucket
126     fgPrintf( FG_TERRAIN, FG_DEBUG, 
127               "FREEING TILE = (%d %d %d %d)\n",
128               tile_cache[index].tile_bucket.lon, 
129               tile_cache[index].tile_bucket.lat, 
130               tile_cache[index].tile_bucket.x,
131               tile_cache[index].tile_bucket.y );
132
133     // Step through the fragment list, deleting the display list, then
134     // the fragment, until the list is empty.
135     while ( tile_cache[index].fragment_list.size() ) {
136         list < fgFRAGMENT > :: iterator current =
137             tile_cache[index].fragment_list.begin();
138         fragment = &(*current);
139         xglDeleteLists( fragment->display_list, 1 );
140
141         tile_cache[index].fragment_list.pop_front();
142     }
143 }
144
145
146 // Return the specified tile cache entry 
147 fgTILE *fgTILECACHE::GetTile( int index ) {
148     return ( &tile_cache[index] );
149 }
150
151
152 // Return index of next available slot in tile cache
153 int fgTILECACHE::NextAvail( void ) {
154     fgVIEW *v;
155     int i;
156     float dx, dy, dz, max, med, min, tmp;
157     float dist, max_dist;
158     int max_index;
159     
160     v = &current_view;
161
162     max_dist = 0.0;
163     max_index = 0;
164
165     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
166         if ( tile_cache[i].used == 0 ) {
167             return(i);
168         } else {
169             // calculate approximate distance from view point
170             fgPrintf( FG_TERRAIN, FG_DEBUG,
171                       "DIST Abs view pos = %.4f, %.4f, %.4f\n", 
172                       v->abs_view_pos.x, v->abs_view_pos.y, v->abs_view_pos.z );
173             fgPrintf( FG_TERRAIN, FG_DEBUG,
174                       "    ref point = %.4f, %.4f, %.4f\n", 
175                       tile_cache[i].center.x, tile_cache[i].center.y,
176                       tile_cache[i].center.z);
177
178             dx = fabs(tile_cache[i].center.x - v->abs_view_pos.x);
179             dy = fabs(tile_cache[i].center.y - v->abs_view_pos.y);
180             dz = fabs(tile_cache[i].center.z - v->abs_view_pos.z);
181
182             max = dx; med = dy; min = dz;
183             if ( max < med ) {
184                 tmp = max; max = med; med = tmp;
185             }
186             if ( max < min ) {
187                 tmp = max; max = min; min = tmp;
188             }
189             dist = max + (med + min) / 4;
190
191             fgPrintf( FG_TERRAIN, FG_DEBUG, "    distance = %.2f\n", dist);
192
193             if ( dist > max_dist ) {
194                 max_dist = dist;
195                 max_index = i;
196             }
197         }
198     }
199
200     // If we made it this far, then there were no open cache entries.
201     // We will instead free the furthest cache entry and return it's
202     // index.
203     
204     EntryFree( max_index );
205     return( max_index );
206 }
207
208
209 // Destructor
210 fgTILECACHE::~fgTILECACHE( void ) {
211 }
212
213
214 // $Log$
215 // Revision 1.15  1998/08/27 17:02:10  curt
216 // Contributions from Bernie Bright <bbright@c031.aone.net.au>
217 // - use strings for fg_root and airport_id and added methods to return
218 //   them as strings,
219 // - inlined all access methods,
220 // - made the parsing functions private methods,
221 // - deleted some unused functions.
222 // - propogated some of these changes out a bit further.
223 //
224 // Revision 1.14  1998/08/25 16:52:43  curt
225 // material.cxx material.hxx obj.cxx obj.hxx texload.c texload.h moved to
226 //   ../Objects
227 //
228 // Revision 1.13  1998/07/13 21:02:00  curt
229 // Wrote access functions for current fgOPTIONS.
230 //
231 // Revision 1.12  1998/07/12 03:18:29  curt
232 // Added ground collision detection.  This involved:
233 // - saving the entire vertex list for each tile with the tile records.
234 // - saving the face list for each fragment with the fragment records.
235 // - code to intersect the current vertical line with the proper face in
236 //   an efficient manner as possible.
237 // Fixed a bug where the tiles weren't being shifted to "near" (0,0,0)
238 //
239 // Revision 1.11  1998/07/04 00:54:30  curt
240 // Added automatic mipmap generation.
241 //
242 // When rendering fragments, use saved model view matrix from associated tile
243 // rather than recalculating it with push() translate() pop().
244 //
245 // Revision 1.10  1998/05/23 14:09:22  curt
246 // Added tile.cxx and tile.hxx.
247 // Working on rewriting the tile management system so a tile is just a list
248 // fragments, and the fragment record contains the display list for that fragment.
249 //
250 // Revision 1.9  1998/05/20 20:53:54  curt
251 // Moved global ref point and radius (bounding sphere info, and offset) to
252 // data file rather than calculating it on the fly.
253 // Fixed polygon winding problem in scenery generation stage rather than
254 // compensating for it on the fly.
255 // Made a fgTILECACHE class.
256 //
257 // Revision 1.8  1998/05/16 13:09:57  curt
258 // Beginning to add support for view frustum culling.
259 // Added some temporary code to calculate bouding radius, until the
260 //   scenery generation tools and scenery can be updated.
261 //
262 // Revision 1.7  1998/05/13 18:26:41  curt
263 // Root path info moved to fgOPTIONS.
264 //
265 // Revision 1.6  1998/05/02 01:52:17  curt
266 // Playing around with texture coordinates.
267 //
268 // Revision 1.5  1998/04/30 12:35:31  curt
269 // Added a command line rendering option specify smooth/flat shading.
270 //
271 // Revision 1.4  1998/04/28 01:21:43  curt
272 // Tweaked texture parameter calculations to keep the number smaller.  This
273 // avoids the "swimming" problem.
274 // Type-ified fgTIME and fgVIEW.
275 //
276 // Revision 1.3  1998/04/25 22:06:32  curt
277 // Edited cvs log messages in source files ... bad bad bad!
278 //
279 // Revision 1.2  1998/04/24 00:51:08  curt
280 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
281 // Tweaked the scenery file extentions to be "file.obj" (uncompressed)
282 // or "file.obz" (compressed.)
283 //
284 // Revision 1.1  1998/04/22 13:22:46  curt
285 // C++ - ifing the code a bit.
286 //
287 // Revision 1.11  1998/04/18 04:14:07  curt
288 // Moved fg_debug.c to it's own library.
289 //
290 // Revision 1.10  1998/04/14 02:23:17  curt
291 // Code reorganizations.  Added a Lib/ directory for more general libraries.
292 //
293 // Revision 1.9  1998/04/08 23:30:07  curt
294 // Adopted Gnu automake/autoconf system.
295 //
296 // Revision 1.8  1998/04/03 22:11:38  curt
297 // Converting to Gnu autoconf system.
298 //
299 // Revision 1.7  1998/02/01 03:39:55  curt
300 // Minor tweaks.
301 //
302 // Revision 1.6  1998/01/31 00:43:26  curt
303 // Added MetroWorks patches from Carmen Volpe.
304 //
305 // Revision 1.5  1998/01/29 00:51:39  curt
306 // First pass at tile cache, dynamic tile loading and tile unloading now works.
307 //
308 // Revision 1.4  1998/01/27 03:26:43  curt
309 // Playing with new fgPrintf command.
310 //
311 // Revision 1.3  1998/01/27 00:48:03  curt
312 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
313 // system and commandline/config file processing code.
314 //
315 // Revision 1.2  1998/01/26 15:55:24  curt
316 // Progressing on building dynamic scenery system.
317 //
318 // Revision 1.1  1998/01/24 00:03:29  curt
319 // Initial revision.
320
321
322