]> git.mxchange.org Git - flightgear.git/blob - Scenery/tilemgr.cxx
Fixed a glTexImage2D() usage bug where width and height were mis-swapped.
[flightgear.git] / Scenery / tilemgr.cxx
1 /*
2  * tilemgr.cxx -- routines to handle dynamic management of scenery tiles
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 <Scenery/obj.hxx>
39 #include <Scenery/scenery.hxx>
40 #include <Scenery/tilecache.hxx>
41
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>
48
49
50 #define FG_LOCAL_X_Y         81  /* max(o->tile_radius) ** 2 */
51
52
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];
56
57
58 /* Initialize the Tile Manager subsystem */
59 int fgTileMgrInit( void ) {
60     fgPrintf( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem.\n");
61     return 1;
62 }
63
64
65 /* load a tile */
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);
69     
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);
74     }
75
76     fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
77 }
78
79
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 ) {
83     fgFLIGHT *f;
84     fgOPTIONS *o;
85     struct fgBUCKET p1, p2;
86     static struct fgBUCKET p_last = {-1000, 0, 0, 0};
87     int i, j, dw, dh;
88
89     f = current_aircraft.flight;
90     o = &current_options;
91
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;
95
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
102          * relavant tiles */
103
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);
109
110         /* wipe tile cache */
111         fgTileCacheInit();
112
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]);
118             }
119         }
120     } else {
121         /* We've moved to a new bucket, we need to scroll our
122          * structures, and load in the new tiles */
123
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. */
127
128         fgPrintf( FG_TERRAIN, FG_INFO, "Updating Tile list for %d,%d %d,%d\n",
129                   p1.lon, p1.lat, p1.x, p1.y);
130
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", 
134                       o->tile_radius);
135             for ( j = 0; j < o->tile_radius; j++ ) {
136                 /* scrolling East */
137                 for ( i = 0; i < o->tile_radius - 1; i++ ) {
138                     tiles[(j*o->tile_radius) + i] = tiles[(j*o->tile_radius) + i + 1];
139                 }
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]);
143             }
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", 
147                       o->tile_radius);
148             for ( j = 0; j < o->tile_radius; j++ ) {
149                 /* scrolling West */
150                 for ( i = o->tile_radius - 1; i > 0; i-- ) {
151                     tiles[(j*o->tile_radius) + i] = tiles[(j*o->tile_radius) + i - 1];
152                 }
153                 /* load in new column */
154                 fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
155                 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_radius) + 0]);
156             }
157         }
158
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", 
162                       o->tile_radius);
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];
168                 }
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]);
172             }
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", 
176                       o->tile_radius);
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];
182                 }
183                 /* load in new column */
184                 fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
185                 fgTileMgrLoadTile(&p2, &tiles[0 + i]);
186             }
187         }
188     }
189     p_last.lon = p1.lon;
190     p_last.lat = p1.lat;
191     p_last.x = p1.x;
192     p_last.y = p1.y;
193     return 1;
194 }
195
196
197 /* Render the local tiles */
198 void fgTileMgrRender( void ) {
199     fgFLIGHT *f;
200     fgOPTIONS *o;
201     struct fgBUCKET p;
202     fgCartesianPoint3d local_ref;
203     GLint display_list;
204     int i;
205     int index;
206
207     f = current_aircraft.flight;
208     o = &current_options;
209
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 );
214
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) );
219
220     for ( i = 0; i < (o->tile_radius * o->tile_radius); i++ ) {
221         index = tiles[i];
222         /* fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index); */
223         fgTileCacheEntryInfo(index, &display_list, &local_ref );
224
225         if ( display_list >= 0 ) {
226             xglPushMatrix();
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);
233             xglPopMatrix();
234         }
235     }
236 }
237
238
239 /* $Log$
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.
243 /*
244  * Revision 1.7  1998/05/06 03:16:42  curt
245  * Added an option to control square tile radius.
246  *
247  * Revision 1.6  1998/05/02 01:52:18  curt
248  * Playing around with texture coordinates.
249  *
250  * Revision 1.5  1998/04/30 12:35:32  curt
251  * Added a command line rendering option specify smooth/flat shading.
252  *
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.
259  *
260  * Revision 1.3  1998/04/25 22:06:32  curt
261  * Edited cvs log messages in source files ... bad bad bad!
262  *
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.)
267  *
268  * Revision 1.1  1998/04/22 13:22:48  curt
269  * C++ - ifing the code a bit.
270  *
271  * Revision 1.25  1998/04/18 04:14:07  curt
272  * Moved fg_debug.c to it's own library.
273  *
274  * Revision 1.24  1998/04/14 02:23:18  curt
275  * Code reorganizations.  Added a Lib/ directory for more general libraries.
276  *
277  * Revision 1.23  1998/04/08 23:30:08  curt
278  * Adopted Gnu automake/autoconf system.
279  *
280  * Revision 1.22  1998/04/03 22:11:38  curt
281  * Converting to Gnu autoconf system.
282  *
283  * Revision 1.21  1998/03/23 21:23:05  curt
284  * Debugging output tweaks.
285  *
286  * Revision 1.20  1998/03/14 00:30:51  curt
287  * Beginning initial terrain texturing experiments.
288  *
289  * Revision 1.19  1998/02/20 00:16:25  curt
290  * Thursday's tweaks.
291  *
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.
296  *
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.
300  *
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>
304  *
305  * Revision 1.14  1998/02/09 21:30:19  curt
306  * Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
307  *
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>
311  *
312  * Revision 1.12  1998/02/01 03:39:55  curt
313  * Minor tweaks.
314  *
315  * Revision 1.11  1998/01/31 00:43:27  curt
316  * Added MetroWorks patches from Carmen Volpe.
317  *
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.
320  *
321  * Revision 1.9  1998/01/27 03:26:44  curt
322  * Playing with new fgPrintf command.
323  *
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.
327  *
328  * Revision 1.7  1998/01/26 15:55:25  curt
329  * Progressing on building dynamic scenery system.
330  *
331  * Revision 1.6  1998/01/24 00:03:30  curt
332  * Initial revision.
333  *
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.
337  *
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.
341  *
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.
345  *
346  * Revision 1.2  1998/01/08 02:22:27  curt
347  * Continue working on basic features.
348  *
349  * Revision 1.1  1998/01/07 23:50:51  curt
350  * "area" renamed to "tile"
351  *
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.
357  *
358  * Revision 1.1  1998/01/07 02:05:48  curt
359  * Initial revision.
360  * */
361
362