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