]> git.mxchange.org Git - flightgear.git/blob - Scenery/tilecache.c
c30d36438b937459b3df849eda4a1092232aaf5a
[flightgear.git] / Scenery / tilecache.c
1 /**************************************************************************
2  * tilecache.c -- routines to handle scenery tile caching
3  *
4  * Written by Curtis Olson, started January 1998.
5  *
6  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7  *
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.
12  *
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.
17  *
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.
21  *
22  * $Id$
23  * (Log is kept at end of this file)
24  **************************************************************************/
25
26
27 #ifdef WIN32
28 #  include <windows.h>
29 #endif
30
31 #include <GL/glut.h>
32 #include <XGL/xgl.h>
33
34 #include <Include/general.h>
35 #include <Main/fg_debug.h>
36 #include <Main/views.h>
37 #include <Scenery/bucketutils.h>
38 #include <Scenery/obj.h>
39 #include <Scenery/tilecache.h>
40
41
42 /* tile cache */
43 struct fgTILE tile_cache[FG_TILE_CACHE_SIZE];
44
45
46 /* Initialize the tile cache subsystem */
47 void fgTileCacheInit( void ) {
48     int i;
49
50     fgPrintf(FG_TERRAIN, FG_INFO, "Initializing the tile cache.\n");
51
52     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
53         tile_cache[i].used = 0;
54     }
55 }
56
57
58 /* Search for the specified "bucket" in the cache */
59 int fgTileCacheExists( struct fgBUCKET *p ) {
60     int i;
61
62     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
63         if ( tile_cache[i].tile_bucket.lon == p->lon ) {
64             if ( tile_cache[i].tile_bucket.lat == p->lat ) {
65                 if ( tile_cache[i].tile_bucket.x == p->x ) {
66                     if ( tile_cache[i].tile_bucket.y == p->y ) {
67                         fgPrintf( FG_TERRAIN, FG_DEBUG, 
68                                   "TILE EXISTS in cache ... index = %d\n", i );
69                         return( i );
70                     }
71                 }
72             }
73         }
74     }
75     
76     return( -1 );
77 }
78
79
80 /* Fill in a tile cache entry with real data for the specified bucket */
81 void fgTileCacheEntryFillIn( int index, struct fgBUCKET *p ) {
82     struct fgGENERAL *g;
83     char base_path[256];
84     char file_name[256];
85
86     g = &general;
87
88     /* Mark this cache entry as used */
89     tile_cache[index].used = 1;
90
91     /* Update the bucket */
92     tile_cache[index].tile_bucket.lon = p->lon;
93     tile_cache[index].tile_bucket.lat = p->lat;
94     tile_cache[index].tile_bucket.x = p->x;
95     tile_cache[index].tile_bucket.y = p->y;
96
97     /* Load the appropriate area and get the display list pointer */
98     fgBucketGenBasePath(p, base_path);
99     sprintf(file_name, "%s/Scenery/%s/%ld.obj", g->root_dir, 
100             base_path, fgBucketGenIndex(p));
101     tile_cache[index].display_list = 
102         fgObjLoad(file_name, &tile_cache[index].local_ref,
103                   &tile_cache[index].bounding_radius);    
104 }
105
106
107 /* Free a tile cache entry */
108 void fgTileCacheEntryFree( int index ) {
109     /* Mark this cache entry as un-used */
110     tile_cache[index].used = 0;
111
112     /* Update the bucket */
113     fgPrintf( FG_TERRAIN, FG_DEBUG, 
114               "FREEING TILE = (%d %d %d %d)\n",
115               tile_cache[index].tile_bucket.lon, 
116               tile_cache[index].tile_bucket.lat, 
117               tile_cache[index].tile_bucket.x,
118               tile_cache[index].tile_bucket.y );
119
120     /* Load the appropriate area and get the display list pointer */
121     xglDeleteLists( tile_cache[index].display_list, 1 );
122 }
123
124
125 /* Return info for a tile cache entry */
126 void fgTileCacheEntryInfo( int index, GLint *display_list, 
127                            struct fgCartesianPoint *local_ref ) {
128     *display_list = tile_cache[index].display_list;
129     /* fgPrintf(FG_TERRAIN, FG_DEBUG, "Display list = %d\n", *display_list); */
130
131     local_ref->x = tile_cache[index].local_ref.x;
132     local_ref->y = tile_cache[index].local_ref.y;
133     local_ref->z = tile_cache[index].local_ref.z;
134 }
135
136
137 /* Return index of next available slot in tile cache */
138 int fgTileCacheNextAvail( void ) {
139     struct fgVIEW *v;
140     int i;
141     float dx, dy, dz, max, med, min, tmp;
142     float dist, max_dist;
143     int max_index;
144     
145     v = &current_view;
146
147     max_dist = 0.0;
148     max_index = 0;
149
150     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
151         if ( tile_cache[i].used == 0 ) {
152             return(i);
153         } else {
154             /* calculate approximate distance from view point */
155             fgPrintf( FG_TERRAIN, FG_DEBUG,
156                       "DIST Abs view pos = %.4f, %.4f, %.4f\n", 
157                       v->abs_view_pos.x, v->abs_view_pos.y, v->abs_view_pos.z );
158             fgPrintf( FG_TERRAIN, FG_DEBUG,
159                       "    ref point = %.4f, %.4f, %.4f\n", 
160                       tile_cache[i].local_ref.x, tile_cache[i].local_ref.y,
161                       tile_cache[i].local_ref.z);
162
163             dx = fabs(tile_cache[i].local_ref.x - v->abs_view_pos.x);
164             dy = fabs(tile_cache[i].local_ref.y - v->abs_view_pos.y);
165             dz = fabs(tile_cache[i].local_ref.z - v->abs_view_pos.z);
166
167             max = dx; med = dy; min = dz;
168             if ( max < med ) {
169                 tmp = max; max = med; med = tmp;
170             }
171             if ( max < min ) {
172                 tmp = max; max = min; min = tmp;
173             }
174             dist = max + (med + min) / 4;
175
176             fgPrintf( FG_TERRAIN, FG_DEBUG, "    distance = %.2f\n", dist);
177
178             if ( dist > max_dist ) {
179                 max_dist = dist;
180                 max_index = i;
181             }
182         }
183     }
184
185     /* If we made it this far, then there were no open cache entries.
186      * We will instead free the furthest cache entry and return it's
187      * index. */
188     
189     fgTileCacheEntryFree( max_index );
190     return( max_index );
191 }
192
193
194 /* $Log$
195 /* Revision 1.6  1998/01/31 00:43:26  curt
196 /* Added MetroWorks patches from Carmen Volpe.
197 /*
198  * Revision 1.5  1998/01/29 00:51:39  curt
199  * First pass at tile cache, dynamic tile loading and tile unloading now works.
200  *
201  * Revision 1.4  1998/01/27 03:26:43  curt
202  * Playing with new fgPrintf command.
203  *
204  * Revision 1.3  1998/01/27 00:48:03  curt
205  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
206  * system and commandline/config file processing code.
207  *
208  * Revision 1.2  1998/01/26 15:55:24  curt
209  * Progressing on building dynamic scenery system.
210  *
211  * Revision 1.1  1998/01/24 00:03:29  curt
212  * Initial revision.
213  *
214  */
215
216