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