1 /**************************************************************************
2 * tilecache.c -- routines to handle scenery tile caching
4 * Written by Curtis Olson, started January 1998.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * (Log is kept at end of this file)
24 **************************************************************************/
36 #include <Include/general.h>
38 #include <Bucket/bucketutils.h>
39 #include <Debug/fg_debug.h>
40 #include <Main/views.hxx>
43 #include "tilecache.hxx"
47 struct fgTILE tile_cache[FG_TILE_CACHE_SIZE];
50 /* Initialize the tile cache subsystem */
51 void fgTileCacheInit( void ) {
54 fgPrintf(FG_TERRAIN, FG_INFO, "Initializing the tile cache.\n");
56 for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
57 tile_cache[i].used = 0;
62 /* Search for the specified "bucket" in the cache */
63 int fgTileCacheExists( struct fgBUCKET *p ) {
66 for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
67 if ( tile_cache[i].tile_bucket.lon == p->lon ) {
68 if ( tile_cache[i].tile_bucket.lat == p->lat ) {
69 if ( tile_cache[i].tile_bucket.x == p->x ) {
70 if ( tile_cache[i].tile_bucket.y == p->y ) {
71 fgPrintf( FG_TERRAIN, FG_DEBUG,
72 "TILE EXISTS in cache ... index = %d\n", i );
84 /* Fill in a tile cache entry with real data for the specified bucket */
85 void fgTileCacheEntryFillIn( int index, struct fgBUCKET *p ) {
92 /* Mark this cache entry as used */
93 tile_cache[index].used = 1;
95 /* Update the bucket */
96 tile_cache[index].tile_bucket.lon = p->lon;
97 tile_cache[index].tile_bucket.lat = p->lat;
98 tile_cache[index].tile_bucket.x = p->x;
99 tile_cache[index].tile_bucket.y = p->y;
101 /* Load the appropriate area and get the display list pointer */
102 fgBucketGenBasePath(p, base_path);
103 sprintf(file_name, "%s/Scenery/%s/%ld.obj", g->root_dir,
104 base_path, fgBucketGenIndex(p));
105 tile_cache[index].display_list =
106 fgObjLoad(file_name, &tile_cache[index].local_ref,
107 &tile_cache[index].bounding_radius);
111 /* Free a tile cache entry */
112 void fgTileCacheEntryFree( int index ) {
113 /* Mark this cache entry as un-used */
114 tile_cache[index].used = 0;
116 /* Update the bucket */
117 fgPrintf( FG_TERRAIN, FG_DEBUG,
118 "FREEING TILE = (%d %d %d %d)\n",
119 tile_cache[index].tile_bucket.lon,
120 tile_cache[index].tile_bucket.lat,
121 tile_cache[index].tile_bucket.x,
122 tile_cache[index].tile_bucket.y );
124 /* Load the appropriate area and get the display list pointer */
125 if ( tile_cache[index].display_list >= 0 ) {
126 xglDeleteLists( tile_cache[index].display_list, 1 );
131 /* Return info for a tile cache entry */
132 void fgTileCacheEntryInfo( int index, GLint *display_list,
133 struct fgCartesianPoint *local_ref ) {
134 *display_list = tile_cache[index].display_list;
135 /* fgPrintf(FG_TERRAIN, FG_DEBUG, "Display list = %d\n", *display_list); */
137 local_ref->x = tile_cache[index].local_ref.x;
138 local_ref->y = tile_cache[index].local_ref.y;
139 local_ref->z = tile_cache[index].local_ref.z;
143 /* Return index of next available slot in tile cache */
144 int fgTileCacheNextAvail( void ) {
147 float dx, dy, dz, max, med, min, tmp;
148 float dist, max_dist;
156 for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
157 if ( tile_cache[i].used == 0 ) {
160 /* calculate approximate distance from view point */
161 fgPrintf( FG_TERRAIN, FG_DEBUG,
162 "DIST Abs view pos = %.4f, %.4f, %.4f\n",
163 v->abs_view_pos.x, v->abs_view_pos.y, v->abs_view_pos.z );
164 fgPrintf( FG_TERRAIN, FG_DEBUG,
165 " ref point = %.4f, %.4f, %.4f\n",
166 tile_cache[i].local_ref.x, tile_cache[i].local_ref.y,
167 tile_cache[i].local_ref.z);
169 dx = fabs(tile_cache[i].local_ref.x - v->abs_view_pos.x);
170 dy = fabs(tile_cache[i].local_ref.y - v->abs_view_pos.y);
171 dz = fabs(tile_cache[i].local_ref.z - v->abs_view_pos.z);
173 max = dx; med = dy; min = dz;
175 tmp = max; max = med; med = tmp;
178 tmp = max; max = min; min = tmp;
180 dist = max + (med + min) / 4;
182 fgPrintf( FG_TERRAIN, FG_DEBUG, " distance = %.2f\n", dist);
184 if ( dist > max_dist ) {
191 /* If we made it this far, then there were no open cache entries.
192 * We will instead free the furthest cache entry and return it's
195 fgTileCacheEntryFree( max_index );
201 /* Revision 1.1 1998/04/22 13:22:46 curt
202 /* C++ - ifing the code a bit.
204 * Revision 1.11 1998/04/18 04:14:07 curt
205 * Moved fg_debug.c to it's own library.
207 * Revision 1.10 1998/04/14 02:23:17 curt
208 * Code reorganizations. Added a Lib/ directory for more general libraries.
210 * Revision 1.9 1998/04/08 23:30:07 curt
211 * Adopted Gnu automake/autoconf system.
213 * Revision 1.8 1998/04/03 22:11:38 curt
214 * Converting to Gnu autoconf system.
216 * Revision 1.7 1998/02/01 03:39:55 curt
219 * Revision 1.6 1998/01/31 00:43:26 curt
220 * Added MetroWorks patches from Carmen Volpe.
222 * Revision 1.5 1998/01/29 00:51:39 curt
223 * First pass at tile cache, dynamic tile loading and tile unloading now works.
225 * Revision 1.4 1998/01/27 03:26:43 curt
226 * Playing with new fgPrintf command.
228 * Revision 1.3 1998/01/27 00:48:03 curt
229 * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
230 * system and commandline/config file processing code.
232 * Revision 1.2 1998/01/26 15:55:24 curt
233 * Progressing on building dynamic scenery system.
235 * Revision 1.1 1998/01/24 00:03:29 curt