3 * tilemgr.c -- routines to handle dynamic management of scenery tiles
5 * Written by Curtis Olson, started January 1998.
7 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of the
12 * License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * (Log is kept at end of this file)
25 **************************************************************************/
39 #include <Scenery/obj.hxx>
40 #include <Scenery/scenery.hxx>
41 #include <Scenery/tilecache.hxx>
43 #include <Aircraft/aircraft.h>
44 #include <Bucket/bucketutils.h>
45 #include <Debug/fg_debug.h>
46 #include <Include/fg_constants.h>
47 #include <Include/fg_types.h>
48 #include <Main/options.hxx>
51 #define FG_LOCAL_X 7 /* should be odd */
52 #define FG_LOCAL_Y 7 /* should be odd */
53 #define FG_LOCAL_X_Y 49 /* At least FG_LOCAL_X times FG_LOCAL_Y */
56 /* closest (potentially viewable) tiles, centered on current tile.
57 * This is an array of pointers to cache indexes. */
58 int tiles[FG_LOCAL_X_Y];
61 /* Initialize the Tile Manager subsystem */
62 int fgTileMgrInit( void ) {
63 fgPrintf( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem.\n");
69 void fgTileMgrLoadTile( struct fgBUCKET *p, int *index) {
70 fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating for bucket %d %d %d %d\n",
71 p->lon, p->lat, p->x, p->y);
73 /* if not in cache, load tile into the next available slot */
74 if ( (*index = fgTileCacheExists(p)) < 0 ) {
75 *index = fgTileCacheNextAvail();
76 fgTileCacheEntryFillIn(*index, p);
79 fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
83 /* given the current lon/lat, fill in the array of local chunks. If
84 * the chunk isn't already in the cache, then read it from disk. */
85 int fgTileMgrUpdate( void ) {
88 struct fgBUCKET p1, p2;
89 static struct fgBUCKET p_last = {-1000, 0, 0, 0};
92 f = current_aircraft.flight;
95 fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1);
96 dw = o->tile_radius / 2;
97 dh = o->tile_radius / 2;
99 if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
100 (p1.x == p_last.x) && (p1.y == p_last.y) ) {
101 /* same bucket as last time */
102 fgPrintf( FG_TERRAIN, FG_DEBUG, "Same bucket as last time\n");
103 } else if ( p_last.lon == -1000 ) {
104 /* First time through, initialize the system and load all
107 fgPrintf( FG_TERRAIN, FG_DEBUG, "First time through ... \n");
108 fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating Tile list for %d,%d %d,%d\n",
109 p1.lon, p1.lat, p1.x, p1.y);
111 /* wipe tile cache */
114 /* build the local area list and update cache */
115 for ( j = 0; j < o->tile_radius; j++ ) {
116 for ( i = 0; i < o->tile_radius; i++ ) {
117 fgBucketOffset(&p1, &p2, i - dw, j - dh);
118 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_radius) + i]);
122 /* We've moved to a new bucket, we need to scroll our
123 * structures, and load in the new tiles */
125 /* CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
126 AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
127 THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION. */
129 if ( (p1.lon > p_last.lon) ||
130 ( (p1.lon == p_last.lon) && (p1.x > p_last.x) ) ) {
131 for ( j = 0; j < o->tile_radius; j++ ) {
133 for ( i = 0; i < o->tile_radius - 1; i++ ) {
134 tiles[(j*o->tile_radius) + i] =
135 tiles[(j*o->tile_radius) + i + 1];
137 /* load in new column */
138 fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
139 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_radius) +
140 o->tile_radius - 1]);
142 } else if ( (p1.lon < p_last.lon) ||
143 ( (p1.lon == p_last.lon) && (p1.x < p_last.x) ) ) {
144 for ( j = 0; j < o->tile_radius; j++ ) {
146 for ( i = o->tile_radius - 1; i > 0; i-- ) {
147 tiles[(j*o->tile_radius) + i] =
148 tiles[(j*o->tile_radius) + i - 1];
150 /* load in new column */
151 fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
152 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_radius) + 0]);
156 if ( (p1.lat > p_last.lat) ||
157 ( (p1.lat == p_last.lat) && (p1.y > p_last.y) ) ) {
158 for ( i = 0; i < o->tile_radius; i++ ) {
159 /* scrolling North */
160 for ( j = 0; j < o->tile_radius - 1; j++ ) {
161 tiles[(j * o->tile_radius) + i] =
162 tiles[((j+1) * o->tile_radius) + i];
164 /* load in new column */
165 fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
166 fgTileMgrLoadTile(&p2, &tiles[((o->tile_radius-1)*o->tile_radius) + i]);
168 } else if ( (p1.lat < p_last.lat) ||
169 ( (p1.lat == p_last.lat) && (p1.y < p_last.y) ) ) {
170 for ( i = 0; i < o->tile_radius; i++ ) {
171 /* scrolling South */
172 for ( j = o->tile_radius - 1; j > 0; j-- ) {
173 tiles[(j * o->tile_radius) + i] =
174 tiles[((j-1) * o->tile_radius) + i];
176 /* load in new column */
177 fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
178 fgTileMgrLoadTile(&p2, &tiles[0 + i]);
190 /* Render the local tiles */
191 void fgTileMgrRender( void ) {
195 fgCartesianPoint3d local_ref;
200 f = current_aircraft.flight;
201 o = ¤t_options;
203 /* Find current translation offset */
204 fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p);
205 index = fgTileCacheExists(&p);
206 fgTileCacheEntryInfo(index, &display_list, &scenery.next_center );
208 fgPrintf( FG_TERRAIN, FG_DEBUG,
209 "Pos = (%.2f, %.2f) Current bucket = %d %d %d %d Index = %ld\n",
210 FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
211 p.lon, p.lat, p.x, p.y, fgBucketGenIndex(&p) );
213 for ( i = 0; i < 2 * o->tile_radius; i++ ) {
215 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index); */
216 fgTileCacheEntryInfo(index, &display_list, &local_ref );
218 if ( display_list >= 0 ) {
220 xglTranslatef(local_ref.x - scenery.center.x,
221 local_ref.y - scenery.center.y,
222 local_ref.z - scenery.center.z);
223 /* xglTranslatef(-scenery.center.x, -scenery.center.y,
224 -scenery.center.z); */
225 xglCallList(display_list);
233 /* Revision 1.7 1998/05/06 03:16:42 curt
234 /* Added an option to control square tile radius.
236 * Revision 1.6 1998/05/02 01:52:18 curt
237 * Playing around with texture coordinates.
239 * Revision 1.5 1998/04/30 12:35:32 curt
240 * Added a command line rendering option specify smooth/flat shading.
242 * Revision 1.4 1998/04/27 03:30:14 curt
243 * Minor transformation adjustments to try to keep scenery tiles closer to
244 * (0, 0, 0) GLfloats run out of precision at the distances we need to model
245 * the earth, but we can do a bunch of pre-transformations using double math
246 * and then cast to GLfloat once everything is close in where we have less
247 * precision problems.
249 * Revision 1.3 1998/04/25 22:06:32 curt
250 * Edited cvs log messages in source files ... bad bad bad!
252 * Revision 1.2 1998/04/24 00:51:09 curt
253 * Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
254 * Tweaked the scenery file extentions to be "file.obj" (uncompressed)
255 * or "file.obz" (compressed.)
257 * Revision 1.1 1998/04/22 13:22:48 curt
258 * C++ - ifing the code a bit.
260 * Revision 1.25 1998/04/18 04:14:07 curt
261 * Moved fg_debug.c to it's own library.
263 * Revision 1.24 1998/04/14 02:23:18 curt
264 * Code reorganizations. Added a Lib/ directory for more general libraries.
266 * Revision 1.23 1998/04/08 23:30:08 curt
267 * Adopted Gnu automake/autoconf system.
269 * Revision 1.22 1998/04/03 22:11:38 curt
270 * Converting to Gnu autoconf system.
272 * Revision 1.21 1998/03/23 21:23:05 curt
273 * Debugging output tweaks.
275 * Revision 1.20 1998/03/14 00:30:51 curt
276 * Beginning initial terrain texturing experiments.
278 * Revision 1.19 1998/02/20 00:16:25 curt
281 * Revision 1.18 1998/02/19 13:05:54 curt
282 * Incorporated some HUD tweaks from Michelle America.
283 * Tweaked the sky's sunset/rise colors.
284 * Other misc. tweaks.
286 * Revision 1.17 1998/02/16 13:39:46 curt
287 * Miscellaneous weekend tweaks. Fixed? a cache problem that caused whole
288 * tiles to occasionally be missing.
290 * Revision 1.16 1998/02/12 21:59:53 curt
291 * Incorporated code changes contributed by Charlie Hotchkiss
292 * <chotchkiss@namg.us.anritsu.com>
294 * Revision 1.14 1998/02/09 21:30:19 curt
295 * Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
297 * Revision 1.13 1998/02/07 15:29:46 curt
298 * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
299 * <chotchkiss@namg.us.anritsu.com>
301 * Revision 1.12 1998/02/01 03:39:55 curt
304 * Revision 1.11 1998/01/31 00:43:27 curt
305 * Added MetroWorks patches from Carmen Volpe.
307 * Revision 1.10 1998/01/29 00:51:40 curt
308 * First pass at tile cache, dynamic tile loading and tile unloading now works.
310 * Revision 1.9 1998/01/27 03:26:44 curt
311 * Playing with new fgPrintf command.
313 * Revision 1.8 1998/01/27 00:48:04 curt
314 * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
315 * system and commandline/config file processing code.
317 * Revision 1.7 1998/01/26 15:55:25 curt
318 * Progressing on building dynamic scenery system.
320 * Revision 1.6 1998/01/24 00:03:30 curt
323 * Revision 1.5 1998/01/19 19:27:18 curt
324 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
325 * This should simplify things tremendously.
327 * Revision 1.4 1998/01/19 18:40:38 curt
328 * Tons of little changes to clean up the code and to remove fatal errors
329 * when building with the c++ compiler.
331 * Revision 1.3 1998/01/13 00:23:11 curt
332 * Initial changes to support loading and management of scenery tiles. Note,
333 * there's still a fair amount of work left to be done.
335 * Revision 1.2 1998/01/08 02:22:27 curt
336 * Continue working on basic features.
338 * Revision 1.1 1998/01/07 23:50:51 curt
339 * "area" renamed to "tile"
341 * Revision 1.2 1998/01/07 03:29:29 curt
342 * Given an arbitrary lat/lon, we can now:
343 * generate a unique index for the chunk containing the lat/lon
344 * generate a path name to the chunk file
345 * build a list of the indexes of all the nearby areas.
347 * Revision 1.1 1998/01/07 02:05:48 curt