]> git.mxchange.org Git - flightgear.git/blob - Scenery/tilemgr.c
Playing with new fgPrintf command.
[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           3   /* should be odd */
46 #define FG_LOCAL_Y           3   /* should be odd */
47 #define FG_LOCAL_X_Y         9   /* At least FG_LOCAL_X times FG_LOCAL_Y */
48
49 #define FG_TILE_CACHE_SIZE 100   /* Must be > FG_LOCAL_X_Y */
50
51
52 /* closest (potentially viewable) tiles, centered on current tile.
53  * This is an array of pointers to cache indexes. */
54 int tiles[FG_LOCAL_X_Y];
55
56 /* tile cache */
57 struct fgTILE tile_cache[FG_TILE_CACHE_SIZE];
58
59
60 /* Initialize the Tile Manager subsystem */
61 void fgTileMgrInit( void ) {
62     fgPrintf( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem.\n");
63
64     fgTileCacheInit();
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     *index = fgTileCacheNextAvail();
74     fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
75     
76     fgTileCacheEntryFillIn(*index, p);
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 void fgTileMgrUpdate( void ) {
83     struct 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, 
159                     &tiles[((FG_LOCAL_Y-1)*FG_LOCAL_Y) + i]);
160             }
161         } else if ( (p1.lat < p_last.lat) || 
162                     ( (p1.lat == p_last.lat) && (p1.y < p_last.y) ) ) {
163             for ( i = 0; i < FG_LOCAL_X; i++ ) {
164                 /* scrolling South */
165                 for ( j = FG_LOCAL_Y - 1; j > 0; j-- ) {
166                     tiles[(j * FG_LOCAL_Y) + i] = 
167                         tiles[((j-1) * FG_LOCAL_Y) + i];
168                 }
169                 /* load in new column */
170                 fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
171                 fgTileMgrLoadTile(&p2, &tiles[0 + i]);
172             }
173         } 
174     }
175     p_last.lon = p1.lon;
176     p_last.lat = p1.lat;
177     p_last.x = p1.x;
178     p_last.y = p1.y;
179 }
180
181
182 /* Render the local tiles */
183 void fgTileMgrRender( void ) {
184     static GLfloat terrain_color[4] = { 0.6, 0.8, 0.4, 1.0 };
185     static GLfloat terrain_ambient[4];
186     static GLfloat terrain_diffuse[4];
187     struct fgCartesianPoint local_ref;
188     GLint display_list;
189     int i;
190     int index;
191
192     for ( i = 0; i < 4; i++ ) {
193         terrain_ambient[i] = terrain_color[i] * 0.5;
194         terrain_diffuse[i] = terrain_color[i];
195     }
196
197     xglMaterialfv(GL_FRONT, GL_AMBIENT, terrain_ambient);
198     xglMaterialfv(GL_FRONT, GL_DIFFUSE, terrain_diffuse);
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         xglPushMatrix();
206         xglTranslatef(local_ref.x - scenery.center.x,
207                       local_ref.y - scenery.center.y,
208                       local_ref.z - scenery.center.z);
209         xglCallList(display_list);
210         xglPopMatrix();
211     }
212 }
213
214
215 /* $Log$
216 /* Revision 1.9  1998/01/27 03:26:44  curt
217 /* Playing with new fgPrintf command.
218 /*
219  * Revision 1.8  1998/01/27 00:48:04  curt
220  * Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
221  * system and commandline/config file processing code.
222  *
223  * Revision 1.7  1998/01/26 15:55:25  curt
224  * Progressing on building dynamic scenery system.
225  *
226  * Revision 1.6  1998/01/24 00:03:30  curt
227  * Initial revision.
228  *
229  * Revision 1.5  1998/01/19 19:27:18  curt
230  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
231  * This should simplify things tremendously.
232  *
233  * Revision 1.4  1998/01/19 18:40:38  curt
234  * Tons of little changes to clean up the code and to remove fatal errors
235  * when building with the c++ compiler.
236  *
237  * Revision 1.3  1998/01/13 00:23:11  curt
238  * Initial changes to support loading and management of scenery tiles.  Note,
239  * there's still a fair amount of work left to be done.
240  *
241  * Revision 1.2  1998/01/08 02:22:27  curt
242  * Continue working on basic features.
243  *
244  * Revision 1.1  1998/01/07 23:50:51  curt
245  * "area" renamed to "tile"
246  *
247  * Revision 1.2  1998/01/07 03:29:29  curt
248  * Given an arbitrary lat/lon, we can now:
249  *   generate a unique index for the chunk containing the lat/lon
250  *   generate a path name to the chunk file
251  *   build a list of the indexes of all the nearby areas.
252  *
253  * Revision 1.1  1998/01/07 02:05:48  curt
254  * Initial revision.
255  * */
256
257