1 // tilecache.cxx -- routines to handle scenery tile caching
3 // Written by Curtis Olson, started January 1998.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
22 // (Log is kept at end of this file)
36 #include <Bucket/bucketutils.h>
37 #include <Debug/fg_debug.h>
38 #include <Main/options.hxx>
39 #include <Main/views.hxx>
42 #include "tilecache.hxx"
46 fgTILECACHE global_tile_cache;
50 fgTILECACHE::fgTILECACHE( void ) {
54 // Initialize the tile cache subsystem
55 void fgTILECACHE::Init( void ) {
58 fgPrintf(FG_TERRAIN, FG_INFO, "Initializing the tile cache.\n");
60 for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
61 tile_cache[i].used = 0;
66 // Search for the specified "bucket" in the cache
67 int fgTILECACHE::Exists( struct fgBUCKET *p ) {
70 for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
71 if ( tile_cache[i].tile_bucket.lon == p->lon ) {
72 if ( tile_cache[i].tile_bucket.lat == p->lat ) {
73 if ( tile_cache[i].tile_bucket.x == p->x ) {
74 if ( tile_cache[i].tile_bucket.y == p->y ) {
75 fgPrintf( FG_TERRAIN, FG_DEBUG,
76 "TILE EXISTS in cache ... index = %d\n", i );
88 // Fill in a tile cache entry with real data for the specified bucket
89 void fgTILECACHE::EntryFillIn( int index, struct fgBUCKET *p ) {
96 // Mark this cache entry as used
97 tile_cache[index].used = 1;
100 tile_cache[index].tile_bucket.lon = p->lon;
101 tile_cache[index].tile_bucket.lat = p->lat;
102 tile_cache[index].tile_bucket.x = p->x;
103 tile_cache[index].tile_bucket.y = p->y;
105 // Load the appropriate area and get the display list pointer
106 fgBucketGenBasePath(p, base_path);
107 sprintf(file_name, "%s/Scenery/%s/%ld", o->fg_root,
108 base_path, fgBucketGenIndex(p));
109 tile_cache[index].display_list =
110 fgObjLoad(file_name, &tile_cache[index].local_ref,
111 &tile_cache[index].bounding_radius);
115 // Free a tile cache entry
116 void fgTILECACHE::EntryFree( int index ) {
117 // Mark this cache entry as un-used
118 tile_cache[index].used = 0;
121 fgPrintf( FG_TERRAIN, FG_DEBUG,
122 "FREEING TILE = (%d %d %d %d)\n",
123 tile_cache[index].tile_bucket.lon,
124 tile_cache[index].tile_bucket.lat,
125 tile_cache[index].tile_bucket.x,
126 tile_cache[index].tile_bucket.y );
128 // Load the appropriate area and get the display list pointer
129 if ( tile_cache[index].display_list >= 0 ) {
130 xglDeleteLists( tile_cache[index].display_list, 1 );
135 // Return info for a tile cache entry
136 void fgTILECACHE::EntryInfo( int index, GLint *display_list,
137 fgCartesianPoint3d *local_ref,
139 *display_list = tile_cache[index].display_list;
140 // fgPrintf(FG_TERRAIN, FG_DEBUG, "Display list = %d\n", *display_list);
142 local_ref->x = tile_cache[index].local_ref.x;
143 local_ref->y = tile_cache[index].local_ref.y;
144 local_ref->z = tile_cache[index].local_ref.z;
146 *radius = tile_cache[index].bounding_radius;
150 // Return index of next available slot in tile cache
151 int fgTILECACHE::NextAvail( void ) {
154 float dx, dy, dz, max, med, min, tmp;
155 float dist, max_dist;
163 for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
164 if ( tile_cache[i].used == 0 ) {
167 // calculate approximate distance from view point
168 fgPrintf( FG_TERRAIN, FG_DEBUG,
169 "DIST Abs view pos = %.4f, %.4f, %.4f\n",
170 v->abs_view_pos.x, v->abs_view_pos.y, v->abs_view_pos.z );
171 fgPrintf( FG_TERRAIN, FG_DEBUG,
172 " ref point = %.4f, %.4f, %.4f\n",
173 tile_cache[i].local_ref.x, tile_cache[i].local_ref.y,
174 tile_cache[i].local_ref.z);
176 dx = fabs(tile_cache[i].local_ref.x - v->abs_view_pos.x);
177 dy = fabs(tile_cache[i].local_ref.y - v->abs_view_pos.y);
178 dz = fabs(tile_cache[i].local_ref.z - v->abs_view_pos.z);
180 max = dx; med = dy; min = dz;
182 tmp = max; max = med; med = tmp;
185 tmp = max; max = min; min = tmp;
187 dist = max + (med + min) / 4;
189 fgPrintf( FG_TERRAIN, FG_DEBUG, " distance = %.2f\n", dist);
191 if ( dist > max_dist ) {
198 // If we made it this far, then there were no open cache entries.
199 // We will instead free the furthest cache entry and return it's
202 EntryFree( max_index );
208 fgTILECACHE::~fgTILECACHE( void ) {
213 // Revision 1.9 1998/05/20 20:53:54 curt
214 // Moved global ref point and radius (bounding sphere info, and offset) to
215 // data file rather than calculating it on the fly.
216 // Fixed polygon winding problem in scenery generation stage rather than
217 // compensating for it on the fly.
218 // Made a fgTILECACHE class.
220 // Revision 1.8 1998/05/16 13:09:57 curt
221 // Beginning to add support for view frustum culling.
222 // Added some temporary code to calculate bouding radius, until the
223 // scenery generation tools and scenery can be updated.
225 // Revision 1.7 1998/05/13 18:26:41 curt
226 // Root path info moved to fgOPTIONS.
228 // Revision 1.6 1998/05/02 01:52:17 curt
229 // Playing around with texture coordinates.
231 // Revision 1.5 1998/04/30 12:35:31 curt
232 // Added a command line rendering option specify smooth/flat shading.
234 // Revision 1.4 1998/04/28 01:21:43 curt
235 // Tweaked texture parameter calculations to keep the number smaller. This
236 // avoids the "swimming" problem.
237 // Type-ified fgTIME and fgVIEW.
239 // Revision 1.3 1998/04/25 22:06:32 curt
240 // Edited cvs log messages in source files ... bad bad bad!
242 // Revision 1.2 1998/04/24 00:51:08 curt
243 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
244 // Tweaked the scenery file extentions to be "file.obj" (uncompressed)
245 // or "file.obz" (compressed.)
247 // Revision 1.1 1998/04/22 13:22:46 curt
248 // C++ - ifing the code a bit.
250 // Revision 1.11 1998/04/18 04:14:07 curt
251 // Moved fg_debug.c to it's own library.
253 // Revision 1.10 1998/04/14 02:23:17 curt
254 // Code reorganizations. Added a Lib/ directory for more general libraries.
256 // Revision 1.9 1998/04/08 23:30:07 curt
257 // Adopted Gnu automake/autoconf system.
259 // Revision 1.8 1998/04/03 22:11:38 curt
260 // Converting to Gnu autoconf system.
262 // Revision 1.7 1998/02/01 03:39:55 curt
265 // Revision 1.6 1998/01/31 00:43:26 curt
266 // Added MetroWorks patches from Carmen Volpe.
268 // Revision 1.5 1998/01/29 00:51:39 curt
269 // First pass at tile cache, dynamic tile loading and tile unloading now works.
271 // Revision 1.4 1998/01/27 03:26:43 curt
272 // Playing with new fgPrintf command.
274 // Revision 1.3 1998/01/27 00:48:03 curt
275 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
276 // system and commandline/config file processing code.
278 // Revision 1.2 1998/01/26 15:55:24 curt
279 // Progressing on building dynamic scenery system.
281 // Revision 1.1 1998/01/24 00:03:29 curt