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