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