]> git.mxchange.org Git - flightgear.git/blob - Scenery/tilemgr.cxx
Added an option to control square tile radius.
[flightgear.git] / Scenery / tilemgr.cxx
1 /* -*- Mode: C++ -*-
2  *
3  * tilemgr.c -- routines to handle dynamic management of scenery tiles
4  *
5  * Written by Curtis Olson, started January 1998.
6  *
7  * Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
8  *
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.
13  *
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.
18  *
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.
22  *
23  * $Id$
24  * (Log is kept at end of this file)
25  **************************************************************************/
26
27
28 #ifdef HAVE_CONFIG_H
29 #  include <config.h>
30 #endif
31
32 #ifdef HAVE_WINDOWS_H
33 #  include <windows.h>
34 #endif
35
36 #include <GL/glut.h>
37 #include <XGL/xgl.h>
38
39 #include <Scenery/obj.hxx>
40 #include <Scenery/scenery.hxx>
41 #include <Scenery/tilecache.hxx>
42
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>
49
50
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 */
54
55
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];
59
60
61 /* Initialize the Tile Manager subsystem */
62 int fgTileMgrInit( void ) {
63     fgPrintf( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem.\n");
64     return 1;
65 }
66
67
68 /* load a tile */
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);
72     
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);
77     }
78
79     fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
80 }
81
82
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 ) {
86     fgFLIGHT *f;
87     fgOPTIONS *o;
88     struct fgBUCKET p1, p2;
89     static struct fgBUCKET p_last = {-1000, 0, 0, 0};
90     int i, j, dw, dh;
91
92     f = current_aircraft.flight;
93     o = &current_options;
94
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;
98
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
105          * relavant tiles */
106
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);
110
111         /* wipe tile cache */
112         fgTileCacheInit();
113
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]);
119             }
120         }
121     } else {
122         /* We've moved to a new bucket, we need to scroll our
123          * structures, and load in the new tiles */
124
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. */
128
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++ ) {
132                 /* scrolling East */
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];
136                 }
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]);
141             }
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++ ) {
145                 /* scrolling West */
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];
149                 }
150                 /* load in new column */
151                 fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
152                 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_radius) + 0]);
153             }
154         }
155
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];
163                 }
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]);
167             }
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];
175                 }
176                 /* load in new column */
177                 fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
178                 fgTileMgrLoadTile(&p2, &tiles[0 + i]);
179             }
180         }
181     }
182     p_last.lon = p1.lon;
183     p_last.lat = p1.lat;
184     p_last.x = p1.x;
185     p_last.y = p1.y;
186     return 1;
187 }
188
189
190 /* Render the local tiles */
191 void fgTileMgrRender( void ) {
192     fgFLIGHT *f;
193     fgOPTIONS *o;
194     struct fgBUCKET p;
195     fgCartesianPoint3d local_ref;
196     GLint display_list;
197     int i;
198     int index;
199
200     f = current_aircraft.flight;
201     o = &current_options;
202
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 );
207
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) );
212
213     for ( i = 0; i < 2 * o->tile_radius; i++ ) {
214         index = tiles[i];
215         /* fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index); */
216         fgTileCacheEntryInfo(index, &display_list, &local_ref );
217
218         if ( display_list >= 0 ) {
219             xglPushMatrix();
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);
226             xglPopMatrix();
227         }
228     }
229 }
230
231
232 /* $Log$
233 /* Revision 1.7  1998/05/06 03:16:42  curt
234 /* Added an option to control square tile radius.
235 /*
236  * Revision 1.6  1998/05/02 01:52:18  curt
237  * Playing around with texture coordinates.
238  *
239  * Revision 1.5  1998/04/30 12:35:32  curt
240  * Added a command line rendering option specify smooth/flat shading.
241  *
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.
248  *
249  * Revision 1.3  1998/04/25 22:06:32  curt
250  * Edited cvs log messages in source files ... bad bad bad!
251  *
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.)
256  *
257  * Revision 1.1  1998/04/22 13:22:48  curt
258  * C++ - ifing the code a bit.
259  *
260  * Revision 1.25  1998/04/18 04:14:07  curt
261  * Moved fg_debug.c to it's own library.
262  *
263  * Revision 1.24  1998/04/14 02:23:18  curt
264  * Code reorganizations.  Added a Lib/ directory for more general libraries.
265  *
266  * Revision 1.23  1998/04/08 23:30:08  curt
267  * Adopted Gnu automake/autoconf system.
268  *
269  * Revision 1.22  1998/04/03 22:11:38  curt
270  * Converting to Gnu autoconf system.
271  *
272  * Revision 1.21  1998/03/23 21:23:05  curt
273  * Debugging output tweaks.
274  *
275  * Revision 1.20  1998/03/14 00:30:51  curt
276  * Beginning initial terrain texturing experiments.
277  *
278  * Revision 1.19  1998/02/20 00:16:25  curt
279  * Thursday's tweaks.
280  *
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.
285  *
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.
289  *
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>
293  *
294  * Revision 1.14  1998/02/09 21:30:19  curt
295  * Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
296  *
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>
300  *
301  * Revision 1.12  1998/02/01 03:39:55  curt
302  * Minor tweaks.
303  *
304  * Revision 1.11  1998/01/31 00:43:27  curt
305  * Added MetroWorks patches from Carmen Volpe.
306  *
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.
309  *
310  * Revision 1.9  1998/01/27 03:26:44  curt
311  * Playing with new fgPrintf command.
312  *
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.
316  *
317  * Revision 1.7  1998/01/26 15:55:25  curt
318  * Progressing on building dynamic scenery system.
319  *
320  * Revision 1.6  1998/01/24 00:03:30  curt
321  * Initial revision.
322  *
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.
326  *
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.
330  *
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.
334  *
335  * Revision 1.2  1998/01/08 02:22:27  curt
336  * Continue working on basic features.
337  *
338  * Revision 1.1  1998/01/07 23:50:51  curt
339  * "area" renamed to "tile"
340  *
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.
346  *
347  * Revision 1.1  1998/01/07 02:05:48  curt
348  * Initial revision.
349  * */
350
351