]> git.mxchange.org Git - flightgear.git/blob - Scenery/tilemgr.c
Debugging output tweaks.
[flightgear.git] / Scenery / tilemgr.c
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 WIN32
29 #  include <windows.h>
30 #endif
31
32 #include <GL/glut.h>
33 #include <XGL/xgl.h>
34
35 #include <Scenery/scenery.h>
36 #include <Scenery/bucketutils.h>
37 #include <Scenery/obj.h>
38 #include <Scenery/tilecache.h>
39
40 #include <Aircraft/aircraft.h>
41 #include <Include/fg_constants.h>
42 #include <Include/fg_types.h>
43 #include <Main/fg_debug.h>
44
45
46 #define FG_LOCAL_X           7   /* should be odd */
47 #define FG_LOCAL_Y           7   /* should be odd */
48 #define FG_LOCAL_X_Y         49  /* At least FG_LOCAL_X times FG_LOCAL_Y */
49
50
51 /* closest (potentially viewable) tiles, centered on current tile.
52  * This is an array of pointers to cache indexes. */
53 int tiles[FG_LOCAL_X_Y];
54
55
56 /* Initialize the Tile Manager subsystem */
57 int fgTileMgrInit( void ) {
58     fgPrintf( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem.\n");
59     return 1;
60 }
61
62
63 /* load a tile */
64 void fgTileMgrLoadTile( struct fgBUCKET *p, int *index) {
65     fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating for bucket %d %d %d %d\n", 
66            p->lon, p->lat, p->x, p->y);
67     
68     /* if not in cache, load tile into the next available slot */
69     if ( (*index = fgTileCacheExists(p)) < 0 ) {
70         *index = fgTileCacheNextAvail();
71         fgTileCacheEntryFillIn(*index, p);
72     }
73
74     fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
75 }
76
77
78 /* given the current lon/lat, fill in the array of local chunks.  If
79  * the chunk isn't already in the cache, then read it from disk. */
80 int fgTileMgrUpdate( void ) {
81     fgFLIGHT *f;
82     struct fgBUCKET p1, p2;
83     static struct fgBUCKET p_last = {-1000, 0, 0, 0};
84     int i, j, dw, dh;
85
86     f = current_aircraft.flight;
87
88     fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1);
89     dw = FG_LOCAL_X / 2;
90     dh = FG_LOCAL_Y / 2;
91
92     if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
93          (p1.x == p_last.x) && (p1.y == p_last.y) ) {
94         /* same bucket as last time */
95         fgPrintf( FG_TERRAIN, FG_DEBUG, "Same bucket as last time\n");
96     } else if ( p_last.lon == -1000 ) {
97         /* First time through, initialize the system and load all
98          * relavant tiles */
99
100         fgPrintf( FG_TERRAIN, FG_DEBUG, "First time through ... \n");
101         fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating Tile list for %d,%d %d,%d\n",
102                   p1.lon, p1.lat, p1.x, p1.y);
103
104         /* wipe tile cache */
105         fgTileCacheInit();
106
107         /* build the local area list and update cache */
108         for ( j = 0; j < FG_LOCAL_Y; j++ ) {
109             for ( i = 0; i < FG_LOCAL_X; i++ ) {
110                 fgBucketOffset(&p1, &p2, i - dw, j - dh);
111                 fgTileMgrLoadTile(&p2, &tiles[(j*FG_LOCAL_Y) + i]);
112             }
113         }
114     } else {
115         /* We've moved to a new bucket, we need to scroll our
116          * structures, and load in the new tiles */
117
118         /* CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
119            AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
120            THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION. */
121
122         if ( (p1.lon > p_last.lon) ||
123              ( (p1.lon == p_last.lon) && (p1.x > p_last.x) ) ) {
124             for ( j = 0; j < FG_LOCAL_Y; j++ ) {
125                 /* scrolling East */
126                 for ( i = 0; i < FG_LOCAL_X - 1; i++ ) {
127                     tiles[(j*FG_LOCAL_Y) + i] = tiles[(j*FG_LOCAL_Y) + i + 1];
128                 }
129                 /* load in new column */
130                 fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
131                 fgTileMgrLoadTile(&p2, &tiles[(j*FG_LOCAL_Y) + FG_LOCAL_X - 1]);
132             }
133         } else if ( (p1.lon < p_last.lon) ||
134                     ( (p1.lon == p_last.lon) && (p1.x < p_last.x) ) ) {
135             for ( j = 0; j < FG_LOCAL_Y; j++ ) {
136                 /* scrolling West */
137                 for ( i = FG_LOCAL_X - 1; i > 0; i-- ) {
138                     tiles[(j*FG_LOCAL_Y) + i] = tiles[(j*FG_LOCAL_Y) + i - 1];
139                 }
140                 /* load in new column */
141                 fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
142                 fgTileMgrLoadTile(&p2, &tiles[(j*FG_LOCAL_Y) + 0]);
143             }
144         }
145
146         if ( (p1.lat > p_last.lat) ||
147              ( (p1.lat == p_last.lat) && (p1.y > p_last.y) ) ) {
148             for ( i = 0; i < FG_LOCAL_X; i++ ) {
149                 /* scrolling North */
150                 for ( j = 0; j < FG_LOCAL_Y - 1; j++ ) {
151                     tiles[(j * FG_LOCAL_Y) + i] =
152                         tiles[((j+1) * FG_LOCAL_Y) + i];
153                 }
154                 /* load in new column */
155                 fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
156                 fgTileMgrLoadTile(&p2, &tiles[((FG_LOCAL_Y-1)*FG_LOCAL_Y) + i]);
157             }
158         } else if ( (p1.lat < p_last.lat) ||
159                     ( (p1.lat == p_last.lat) && (p1.y < p_last.y) ) ) {
160             for ( i = 0; i < FG_LOCAL_X; i++ ) {
161                 /* scrolling South */
162                 for ( j = FG_LOCAL_Y - 1; j > 0; j-- ) {
163                     tiles[(j * FG_LOCAL_Y) + i] = 
164                         tiles[((j-1) * FG_LOCAL_Y) + i];
165                 }
166                 /* load in new column */
167                 fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
168                 fgTileMgrLoadTile(&p2, &tiles[0 + i]);
169             }
170         }
171     }
172     p_last.lon = p1.lon;
173     p_last.lat = p1.lat;
174     p_last.x = p1.x;
175     p_last.y = p1.y;
176     return 1;
177 }
178
179
180 /* Render the local tiles */
181 void fgTileMgrRender( void ) {
182     fgFLIGHT *f;
183     struct fgBUCKET p;
184     struct fgCartesianPoint local_ref;
185     GLint display_list;
186     int i;
187     int index;
188
189     f = current_aircraft.flight;
190
191     /* Find current translation offset */
192     fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p);
193     index = fgTileCacheExists(&p);
194     fgTileCacheEntryInfo(index, &display_list, &scenery.next_center );
195
196     printf("Pos = (%.2f, %.2f) Current bucket = %d %d %d %d  Index = %ld\n", 
197            FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
198            p.lon, p.lat, p.x, p.y, fgBucketGenIndex(&p) );
199
200     for ( i = 0; i < FG_LOCAL_X_Y; i++ ) {
201         index = tiles[i];
202         /* fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index); */
203         fgTileCacheEntryInfo(index, &display_list, &local_ref );
204
205         if ( display_list >= 0 ) {
206             xglPushMatrix();
207             /* xglTranslatef(local_ref.x - scenery.center.x,
208                           local_ref.y - scenery.center.y,
209                           local_ref.z - scenery.center.z); */
210             xglTranslatef(-scenery.center.x, -scenery.center.y, 
211                           -scenery.center.z);
212             xglCallList(display_list);
213             xglPopMatrix();
214         }
215     }
216 }
217
218
219 /* $Log$
220 /* Revision 1.21  1998/03/23 21:23:05  curt
221 /* Debugging output tweaks.
222 /*
223  * Revision 1.20  1998/03/14 00:30:51  curt
224  * Beginning initial terrain texturing experiments.
225  *
226  * Revision 1.19  1998/02/20 00:16:25  curt
227  * Thursday's tweaks.
228  *
229  * Revision 1.18  1998/02/19 13:05:54  curt
230  * Incorporated some HUD tweaks from Michelle America.
231  * Tweaked the sky's sunset/rise colors.
232  * Other misc. tweaks.
233  *
234  * Revision 1.17  1998/02/16 13:39:46  curt
235  * Miscellaneous weekend tweaks.  Fixed? a cache problem that caused whole
236  * tiles to occasionally be missing.
237  *
238  * Revision 1.16  1998/02/12 21:59:53  curt
239  * Incorporated code changes contributed by Charlie Hotchkiss
240  * <chotchkiss@namg.us.anritsu.com>
241  *
242  * Revision 1.14  1998/02/09 21:30:19  curt
243  * Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
244  *
245  * Revision 1.13  1998/02/07 15:29:46  curt
246  * Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
247  * <chotchkiss@namg.us.anritsu.com>
248  *
249  * Revision 1.12  1998/02/01 03:39:55  curt
250  * Minor tweaks.
251  *
252  * Revision 1.11  1998/01/31 00:43:27  curt
253  * Added MetroWorks patches from Carmen Volpe.
254  *
255  * Revision 1.10  1998/01/29 00:51:40  curt
256  * First pass at tile cache, dynamic tile loading and tile unloading now works.
257  *
258  * Revision 1.9  1998/01/27 03:26:44  curt
259  * Playing with new fgPrintf command.
260  *
261  * Revision 1.8  1998/01/27 00:48:04  curt
262  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
263  * system and commandline/config file processing code.
264  *
265  * Revision 1.7  1998/01/26 15:55:25  curt
266  * Progressing on building dynamic scenery system.
267  *
268  * Revision 1.6  1998/01/24 00:03:30  curt
269  * Initial revision.
270  *
271  * Revision 1.5  1998/01/19 19:27:18  curt
272  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
273  * This should simplify things tremendously.
274  *
275  * Revision 1.4  1998/01/19 18:40:38  curt
276  * Tons of little changes to clean up the code and to remove fatal errors
277  * when building with the c++ compiler.
278  *
279  * Revision 1.3  1998/01/13 00:23:11  curt
280  * Initial changes to support loading and management of scenery tiles.  Note,
281  * there's still a fair amount of work left to be done.
282  *
283  * Revision 1.2  1998/01/08 02:22:27  curt
284  * Continue working on basic features.
285  *
286  * Revision 1.1  1998/01/07 23:50:51  curt
287  * "area" renamed to "tile"
288  *
289  * Revision 1.2  1998/01/07 03:29:29  curt
290  * Given an arbitrary lat/lon, we can now:
291  *   generate a unique index for the chunk containing the lat/lon
292  *   generate a path name to the chunk file
293  *   build a list of the indexes of all the nearby areas.
294  *
295  * Revision 1.1  1998/01/07 02:05:48  curt
296  * Initial revision.
297  * */
298
299