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