2 * tilemgr.cxx -- routines to handle dynamic management of scenery tiles
4 * Written by Curtis Olson, started January 1998.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
23 * (Log is kept at end of this file)
24 **************************************************************************/
38 #include <Scenery/obj.hxx>
39 #include <Scenery/scenery.hxx>
40 #include <Scenery/tilecache.hxx>
42 #include <Aircraft/aircraft.h>
43 #include <Bucket/bucketutils.h>
44 #include <Debug/fg_debug.h>
45 #include <Include/fg_constants.h>
46 #include <Include/fg_types.h>
47 #include <Main/options.hxx>
50 #define FG_LOCAL_X_Y 81 /* max(o->tile_radius) ** 2 */
53 /* closest (potentially viewable) tiles, centered on current tile.
54 * This is an array of pointers to cache indexes. */
55 int tiles[FG_LOCAL_X_Y];
58 /* Initialize the Tile Manager subsystem */
59 int fgTileMgrInit( void ) {
60 fgPrintf( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem.\n");
66 void fgTileMgrLoadTile( struct fgBUCKET *p, int *index) {
67 fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating for bucket %d %d %d %d\n",
68 p->lon, p->lat, p->x, p->y);
70 /* if not in cache, load tile into the next available slot */
71 if ( (*index = fgTileCacheExists(p)) < 0 ) {
72 *index = fgTileCacheNextAvail();
73 fgTileCacheEntryFillIn(*index, p);
76 fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
80 /* given the current lon/lat, fill in the array of local chunks. If
81 * the chunk isn't already in the cache, then read it from disk. */
82 int fgTileMgrUpdate( void ) {
85 struct fgBUCKET p1, p2;
86 static struct fgBUCKET p_last = {-1000, 0, 0, 0};
89 f = current_aircraft.flight;
92 fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1);
93 dw = o->tile_radius / 2;
94 dh = o->tile_radius / 2;
96 if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
97 (p1.x == p_last.x) && (p1.y == p_last.y) ) {
98 /* same bucket as last time */
99 fgPrintf( FG_TERRAIN, FG_DEBUG, "Same bucket as last time\n");
100 } else if ( p_last.lon == -1000 ) {
101 /* First time through, initialize the system and load all
104 fgPrintf( FG_TERRAIN, FG_INFO, " First time through ... ");
105 fgPrintf( FG_TERRAIN, FG_INFO, " Updating Tile list for %d,%d %d,%d\n",
106 p1.lon, p1.lat, p1.x, p1.y);
107 fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
108 o->tile_radius * o->tile_radius);
110 /* wipe tile cache */
113 /* build the local area list and update cache */
114 for ( j = 0; j < o->tile_radius; j++ ) {
115 for ( i = 0; i < o->tile_radius; i++ ) {
116 fgBucketOffset(&p1, &p2, i - dw, j - dh);
117 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_radius) + i]);
121 /* We've moved to a new bucket, we need to scroll our
122 * structures, and load in the new tiles */
124 /* CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
125 AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
126 THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION. */
128 fgPrintf( FG_TERRAIN, FG_INFO, "Updating Tile list for %d,%d %d,%d\n",
129 p1.lon, p1.lat, p1.x, p1.y);
131 if ( (p1.lon > p_last.lon) ||
132 ( (p1.lon == p_last.lon) && (p1.x > p_last.x) ) ) {
133 fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
135 for ( j = 0; j < o->tile_radius; j++ ) {
137 for ( i = 0; i < o->tile_radius - 1; i++ ) {
138 tiles[(j*o->tile_radius) + i] = tiles[(j*o->tile_radius) + i + 1];
140 /* load in new column */
141 fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
142 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_radius) + o->tile_radius - 1]);
144 } else if ( (p1.lon < p_last.lon) ||
145 ( (p1.lon == p_last.lon) && (p1.x < p_last.x) ) ) {
146 fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
148 for ( j = 0; j < o->tile_radius; j++ ) {
150 for ( i = o->tile_radius - 1; i > 0; i-- ) {
151 tiles[(j*o->tile_radius) + i] = tiles[(j*o->tile_radius) + i - 1];
153 /* load in new column */
154 fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
155 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_radius) + 0]);
159 if ( (p1.lat > p_last.lat) ||
160 ( (p1.lat == p_last.lat) && (p1.y > p_last.y) ) ) {
161 fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
163 for ( i = 0; i < o->tile_radius; i++ ) {
164 /* scrolling North */
165 for ( j = 0; j < o->tile_radius - 1; j++ ) {
166 tiles[(j * o->tile_radius) + i] =
167 tiles[((j+1) * o->tile_radius) + i];
169 /* load in new column */
170 fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
171 fgTileMgrLoadTile(&p2, &tiles[((o->tile_radius-1)*o->tile_radius) + i]);
173 } else if ( (p1.lat < p_last.lat) ||
174 ( (p1.lat == p_last.lat) && (p1.y < p_last.y) ) ) {
175 fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
177 for ( i = 0; i < o->tile_radius; i++ ) {
178 /* scrolling South */
179 for ( j = o->tile_radius - 1; j > 0; j-- ) {
180 tiles[(j * o->tile_radius) + i] =
181 tiles[((j-1) * o->tile_radius) + i];
183 /* load in new column */
184 fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
185 fgTileMgrLoadTile(&p2, &tiles[0 + i]);
197 /* Render the local tiles */
198 void fgTileMgrRender( void ) {
202 fgCartesianPoint3d local_ref;
207 f = current_aircraft.flight;
208 o = ¤t_options;
210 /* Find current translation offset */
211 fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p);
212 index = fgTileCacheExists(&p);
213 fgTileCacheEntryInfo(index, &display_list, &scenery.next_center );
215 fgPrintf( FG_TERRAIN, FG_DEBUG,
216 "Pos = (%.2f, %.2f) Current bucket = %d %d %d %d Index = %ld\n",
217 FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
218 p.lon, p.lat, p.x, p.y, fgBucketGenIndex(&p) );
220 for ( i = 0; i < (o->tile_radius * o->tile_radius); i++ ) {
222 /* fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index); */
223 fgTileCacheEntryInfo(index, &display_list, &local_ref );
225 if ( display_list >= 0 ) {
227 xglTranslatef(local_ref.x - scenery.center.x,
228 local_ref.y - scenery.center.y,
229 local_ref.z - scenery.center.z);
230 /* xglTranslatef(-scenery.center.x, -scenery.center.y,
231 -scenery.center.z); */
232 xglCallList(display_list);
240 /* Revision 1.8 1998/05/07 23:15:21 curt
241 /* Fixed a glTexImage2D() usage bug where width and height were mis-swapped.
242 /* Added support for --tile-radius=n option.
244 * Revision 1.7 1998/05/06 03:16:42 curt
245 * Added an option to control square tile radius.
247 * Revision 1.6 1998/05/02 01:52:18 curt
248 * Playing around with texture coordinates.
250 * Revision 1.5 1998/04/30 12:35:32 curt
251 * Added a command line rendering option specify smooth/flat shading.
253 * Revision 1.4 1998/04/27 03:30:14 curt
254 * Minor transformation adjustments to try to keep scenery tiles closer to
255 * (0, 0, 0) GLfloats run out of precision at the distances we need to model
256 * the earth, but we can do a bunch of pre-transformations using double math
257 * and then cast to GLfloat once everything is close in where we have less
258 * precision problems.
260 * Revision 1.3 1998/04/25 22:06:32 curt
261 * Edited cvs log messages in source files ... bad bad bad!
263 * Revision 1.2 1998/04/24 00:51:09 curt
264 * Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
265 * Tweaked the scenery file extentions to be "file.obj" (uncompressed)
266 * or "file.obz" (compressed.)
268 * Revision 1.1 1998/04/22 13:22:48 curt
269 * C++ - ifing the code a bit.
271 * Revision 1.25 1998/04/18 04:14:07 curt
272 * Moved fg_debug.c to it's own library.
274 * Revision 1.24 1998/04/14 02:23:18 curt
275 * Code reorganizations. Added a Lib/ directory for more general libraries.
277 * Revision 1.23 1998/04/08 23:30:08 curt
278 * Adopted Gnu automake/autoconf system.
280 * Revision 1.22 1998/04/03 22:11:38 curt
281 * Converting to Gnu autoconf system.
283 * Revision 1.21 1998/03/23 21:23:05 curt
284 * Debugging output tweaks.
286 * Revision 1.20 1998/03/14 00:30:51 curt
287 * Beginning initial terrain texturing experiments.
289 * Revision 1.19 1998/02/20 00:16:25 curt
292 * Revision 1.18 1998/02/19 13:05:54 curt
293 * Incorporated some HUD tweaks from Michelle America.
294 * Tweaked the sky's sunset/rise colors.
295 * Other misc. tweaks.
297 * Revision 1.17 1998/02/16 13:39:46 curt
298 * Miscellaneous weekend tweaks. Fixed? a cache problem that caused whole
299 * tiles to occasionally be missing.
301 * Revision 1.16 1998/02/12 21:59:53 curt
302 * Incorporated code changes contributed by Charlie Hotchkiss
303 * <chotchkiss@namg.us.anritsu.com>
305 * Revision 1.14 1998/02/09 21:30:19 curt
306 * Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
308 * Revision 1.13 1998/02/07 15:29:46 curt
309 * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
310 * <chotchkiss@namg.us.anritsu.com>
312 * Revision 1.12 1998/02/01 03:39:55 curt
315 * Revision 1.11 1998/01/31 00:43:27 curt
316 * Added MetroWorks patches from Carmen Volpe.
318 * Revision 1.10 1998/01/29 00:51:40 curt
319 * First pass at tile cache, dynamic tile loading and tile unloading now works.
321 * Revision 1.9 1998/01/27 03:26:44 curt
322 * Playing with new fgPrintf command.
324 * Revision 1.8 1998/01/27 00:48:04 curt
325 * Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
326 * system and commandline/config file processing code.
328 * Revision 1.7 1998/01/26 15:55:25 curt
329 * Progressing on building dynamic scenery system.
331 * Revision 1.6 1998/01/24 00:03:30 curt
334 * Revision 1.5 1998/01/19 19:27:18 curt
335 * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
336 * This should simplify things tremendously.
338 * Revision 1.4 1998/01/19 18:40:38 curt
339 * Tons of little changes to clean up the code and to remove fatal errors
340 * when building with the c++ compiler.
342 * Revision 1.3 1998/01/13 00:23:11 curt
343 * Initial changes to support loading and management of scenery tiles. Note,
344 * there's still a fair amount of work left to be done.
346 * Revision 1.2 1998/01/08 02:22:27 curt
347 * Continue working on basic features.
349 * Revision 1.1 1998/01/07 23:50:51 curt
350 * "area" renamed to "tile"
352 * Revision 1.2 1998/01/07 03:29:29 curt
353 * Given an arbitrary lat/lon, we can now:
354 * generate a unique index for the chunk containing the lat/lon
355 * generate a path name to the chunk file
356 * build a list of the indexes of all the nearby areas.
358 * Revision 1.1 1998/01/07 02:05:48 curt