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