]> git.mxchange.org Git - flightgear.git/blob - Scenery/tilemgr.c
Initial revision.
[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/constants.h>
41 #include <Include/types.h>
42
43
44 #define FG_LOCAL_X           3   /* should be odd */
45 #define FG_LOCAL_Y           3   /* should be odd */
46 #define FG_LOCAL_X_Y         9   /* At least FG_LOCAL_X times FG_LOCAL_Y */
47
48 #define FG_TILE_CACHE_SIZE 100   /* Must be > FG_LOCAL_X_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 /* tile cache */
56 struct fgTILE tile_cache[FG_TILE_CACHE_SIZE];
57
58
59 /* Initialize the Tile Manager subsystem */
60 void fgTileMgrInit( void ) {
61     printf("Initializing Tile Manager subsystem.\n");
62     fgTileCacheInit();
63 }
64
65
66 /* given the current lon/lat, fill in the array of local chunks.  If
67  * the chunk isn't already in the cache, then read it from disk. */
68 void fgTileMgrUpdate( void ) {
69     struct fgFLIGHT *f;
70     struct fgBUCKET p1, p2;
71     static struct fgBUCKET p_last = {-1000, 0, 0, 0};
72     int i, j, dw, dh;
73     int index;
74
75     f = &current_aircraft.flight;
76
77     fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1);
78
79     if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) && 
80          (p1.x == p_last.x) && (p1.y == p_last.y) ) {
81         /* same bucket as last time */
82         printf("Same bucket as last time\n");
83         return;
84     }
85
86     if ( p_last.lon == -1000 ) {
87         printf("First time through ... \n");
88         printf("Updating Tile list for %d,%d %d,%d\n", 
89                p1.lon, p1.lat, p1.x, p1.y);
90
91         /* wipe tile cache */
92         fgTileCacheInit();
93
94         /* build the local area list and update cache */
95         dw = FG_LOCAL_X / 2;
96         dh = FG_LOCAL_Y / 2;
97
98         for ( j = 0; j < FG_LOCAL_Y; j++ ) {
99             for ( i = 0; i < FG_LOCAL_X; i++ ) {
100                 fgBucketOffset(&p1, &p2, i - dw, j - dh);
101                 printf("Updating for bucket %d %d %d %d\n", 
102                        p2.lon, p2.lat, p2.x, p2.y);
103
104                 index = fgTileCacheNextAvail();
105                 printf("Selected cache index of %d\n", index);
106
107                 tiles[(j*FG_LOCAL_Y) + i] = index;
108                 fgTileCacheEntryFillIn(index, &p2);
109             }
110         }
111     }
112 }
113
114
115 /* Render the local tiles --- hack, hack, hack */
116 void fgTileMgrRender( void ) {
117     static GLfloat terrain_color[4] = { 0.6, 0.8, 0.4, 1.0 };
118     static GLfloat terrain_ambient[4];
119     static GLfloat terrain_diffuse[4];
120     struct fgCartesianPoint local_ref;
121     GLint display_list;
122     int i;
123     int index;
124
125     for ( i = 0; i < 4; i++ ) {
126         terrain_ambient[i] = terrain_color[i] * 0.5;
127         terrain_diffuse[i] = terrain_color[i];
128     }
129
130     xglMaterialfv(GL_FRONT, GL_AMBIENT, terrain_ambient);
131     xglMaterialfv(GL_FRONT, GL_DIFFUSE, terrain_diffuse);
132
133     for ( i = 0; i < FG_LOCAL_X_Y; i++ ) {
134         index = tiles[i];
135         /* printf("Index = %d\n", index); */
136         fgTileCacheEntryInfo(index, &display_list, &local_ref );
137
138         xglPushMatrix();
139         xglTranslatef(local_ref.x - scenery.center.x,
140                       local_ref.y - scenery.center.y,
141                       local_ref.z - scenery.center.z);
142         xglCallList(display_list);
143         xglPopMatrix();
144     }
145 }
146
147
148 /* $Log$
149 /* Revision 1.6  1998/01/24 00:03:30  curt
150 /* Initial revision.
151 /*
152  * Revision 1.5  1998/01/19 19:27:18  curt
153  * Merged in make system changes from Bob Kuehne <rpk@sgi.com>
154  * This should simplify things tremendously.
155  *
156  * Revision 1.4  1998/01/19 18:40:38  curt
157  * Tons of little changes to clean up the code and to remove fatal errors
158  * when building with the c++ compiler.
159  *
160  * Revision 1.3  1998/01/13 00:23:11  curt
161  * Initial changes to support loading and management of scenery tiles.  Note,
162  * there's still a fair amount of work left to be done.
163  *
164  * Revision 1.2  1998/01/08 02:22:27  curt
165  * Continue working on basic features.
166  *
167  * Revision 1.1  1998/01/07 23:50:51  curt
168  * "area" renamed to "tile"
169  *
170  * Revision 1.2  1998/01/07 03:29:29  curt
171  * Given an arbitrary lat/lon, we can now:
172  *   generate a unique index for the chunk containing the lat/lon
173  *   generate a path name to the chunk file
174  *   build a list of the indexes of all the nearby areas.
175  *
176  * Revision 1.1  1998/01/07 02:05:48  curt
177  * Initial revision.
178  * */
179
180