]> git.mxchange.org Git - flightgear.git/blob - Scenery/tilemgr.cxx
Moved global ref point and radius (bounding sphere info, and offset) to
[flightgear.git] / Scenery / tilemgr.cxx
1 // tilemgr.cxx -- routines to handle dynamic management of scenery tiles
2 //
3 // Written by Curtis Olson, started January 1998.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifdef HAVE_WINDOWS_H
30 #  include <windows.h>
31 #endif
32
33 #include <GL/glut.h>
34 #include <XGL/xgl.h>
35
36 #include <Scenery/obj.hxx>
37 #include <Scenery/scenery.hxx>
38 #include <Scenery/tilecache.hxx>
39
40 #include <Aircraft/aircraft.h>
41 #include <Bucket/bucketutils.h>
42 #include <Debug/fg_debug.h>
43 #include <Include/fg_constants.h>
44 #include <Include/fg_types.h>
45 #include <Main/options.hxx>
46 #include <Main/views.hxx>
47 #include <Math/mat3.h>
48
49
50 #define FG_LOCAL_X_Y         81  // max(o->tile_diameter) ** 2
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     fgTILECACHE *c;
68
69     c = &global_tile_cache;
70
71     fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating for bucket %d %d %d %d\n", 
72            p->lon, p->lat, p->x, p->y);
73     
74     // if not in cache, load tile into the next available slot
75     if ( (*index = c->Exists(p)) < 0 ) {
76         *index = c->NextAvail();
77         c->EntryFillIn(*index, p);
78     }
79
80     fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
81 }
82
83
84 // given the current lon/lat, fill in the array of local chunks.  If
85 // the chunk isn't already in the cache, then read it from disk.
86 int fgTileMgrUpdate( void ) {
87     fgTILECACHE *c;
88     fgFLIGHT *f;
89     fgOPTIONS *o;
90     struct fgBUCKET p1, p2;
91     static struct fgBUCKET p_last = {-1000, 0, 0, 0};
92     int i, j, dw, dh;
93
94     c = &global_tile_cache;
95     f = current_aircraft.flight;
96     o = &current_options;
97
98     fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1);
99     dw = o->tile_diameter / 2;
100     dh = o->tile_diameter / 2;
101
102     if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
103          (p1.x == p_last.x) && (p1.y == p_last.y) ) {
104         // same bucket as last time
105         fgPrintf( FG_TERRAIN, FG_DEBUG, "Same bucket as last time\n");
106     } else if ( p_last.lon == -1000 ) {
107         // First time through, initialize the system and load all
108         // relavant tiles
109
110         fgPrintf( FG_TERRAIN, FG_INFO, "  First time through ... ");
111         fgPrintf( FG_TERRAIN, FG_INFO, "  Updating Tile list for %d,%d %d,%d\n",
112                   p1.lon, p1.lat, p1.x, p1.y);
113         fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
114                   o->tile_diameter * o->tile_diameter);
115
116         // wipe/initialize tile cache
117         c->Init();
118
119         // build the local area list and update cache
120         for ( j = 0; j < o->tile_diameter; j++ ) {
121             for ( i = 0; i < o->tile_diameter; i++ ) {
122                 fgBucketOffset(&p1, &p2, i - dw, j - dh);
123                 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + i]);
124             }
125         }
126     } else {
127         // We've moved to a new bucket, we need to scroll our
128         // structures, and load in the new tiles
129
130         // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
131         // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
132         // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
133
134         fgPrintf( FG_TERRAIN, FG_INFO, "Updating Tile list for %d,%d %d,%d\n",
135                   p1.lon, p1.lat, p1.x, p1.y);
136
137         if ( (p1.lon > p_last.lon) ||
138              ( (p1.lon == p_last.lon) && (p1.x > p_last.x) ) ) {
139             fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
140                       o->tile_diameter);
141             for ( j = 0; j < o->tile_diameter; j++ ) {
142                 // scrolling East
143                 for ( i = 0; i < o->tile_diameter - 1; i++ ) {
144                     tiles[(j*o->tile_diameter) + i] = 
145                         tiles[(j*o->tile_diameter) + i + 1];
146                 }
147                 // load in new column
148                 fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
149                 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + 
150                                              o->tile_diameter - 1]);
151             }
152         } else if ( (p1.lon < p_last.lon) ||
153                     ( (p1.lon == p_last.lon) && (p1.x < p_last.x) ) ) {
154             fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
155                       o->tile_diameter);
156             for ( j = 0; j < o->tile_diameter; j++ ) {
157                 // scrolling West
158                 for ( i = o->tile_diameter - 1; i > 0; i-- ) {
159                     tiles[(j*o->tile_diameter) + i] = 
160                         tiles[(j*o->tile_diameter) + i - 1];
161                 }
162                 // load in new column
163                 fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
164                 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + 0]);
165             }
166         }
167
168         if ( (p1.lat > p_last.lat) ||
169              ( (p1.lat == p_last.lat) && (p1.y > p_last.y) ) ) {
170             fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
171                       o->tile_diameter);
172             for ( i = 0; i < o->tile_diameter; i++ ) {
173                 // scrolling North
174                 for ( j = 0; j < o->tile_diameter - 1; j++ ) {
175                     tiles[(j * o->tile_diameter) + i] =
176                         tiles[((j+1) * o->tile_diameter) + i];
177                 }
178                 // load in new column
179                 fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
180                 fgTileMgrLoadTile(&p2, &tiles[((o->tile_diameter-1) * 
181                                                o->tile_diameter) + i]);
182             }
183         } else if ( (p1.lat < p_last.lat) ||
184                     ( (p1.lat == p_last.lat) && (p1.y < p_last.y) ) ) {
185             fgPrintf( FG_TERRAIN, FG_INFO, "  Loading %d tiles\n", 
186                       o->tile_diameter);
187             for ( i = 0; i < o->tile_diameter; i++ ) {
188                 // scrolling South
189                 for ( j = o->tile_diameter - 1; j > 0; j-- ) {
190                     tiles[(j * o->tile_diameter) + i] = 
191                         tiles[((j-1) * o->tile_diameter) + i];
192                 }
193                 // load in new column
194                 fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
195                 fgTileMgrLoadTile(&p2, &tiles[0 + i]);
196             }
197         }
198     }
199     p_last.lon = p1.lon;
200     p_last.lat = p1.lat;
201     p_last.x = p1.x;
202     p_last.y = p1.y;
203     return 1;
204 }
205
206
207 // Calculate if point/radius is inside view frustum
208 static int viewable( fgCartesianPoint3d *cp, double radius ) {
209     fgVIEW *v;
210     MAT3hvec world, eye;
211     int viewable = 1; // start by assuming it's viewable
212     double x0, x1, y1, slope;
213
214     v = &current_view;
215
216     MAT3_SET_HVEC(world, cp->x, cp->y, cp->z, 1.0);
217     MAT3mult_vec(eye, world, v->WORLD_TO_EYE);
218     // printf( "\nworld -> eye = %.2f %.2f %.2f  radius = %.2f\n", 
219     //         eye[0], eye[1], eye[2], radius);
220
221     // Check near clip plane
222     if ( eye[2] - radius > 0.0 ) {
223         return(0);
224     }
225
226     // check right clip plane (from eye perspective)
227     // y = m * (x - x0) = equation of a line intercepting X axis at x0
228     x1 = v->cos_fov_x * radius;
229     y1 = v->sin_fov_x * radius;
230     slope = -1.0 / v->slope_x;
231     x0 = x1 - y1 / slope;
232
233     // printf("(r) x1 = %.2f  y1 = %.2f\n", x1, y1);
234     // printf("eye[0] = %.2f  eye[2] = %.2f\n", eye[0], eye[2]);
235     // printf("(r) x0 = %.2f  slope_x = %.2f  radius = %.2f\n", 
236     //        x0, slope, radius);
237
238     if ( eye[2] > slope * (eye[0] - x0) ) {
239         return(0);
240     }
241
242     // check left clip plane (from eye perspective)
243     x1 = -x1;
244     slope = -slope;
245     x0 = x1 - y1 / slope;
246
247     // printf("(r) x1 = %.2f  y1 = %.2f\n", x1, y1);
248     // printf("eye[0] = %.2f  eye[2] = %.2f\n", eye[0], eye[2]);
249     // printf("(r) x0 = %.2f  slope_x = %.2f  radius = %.2f\n", 
250     //        x0, slope, radius);
251
252     if ( eye[2] > slope * (eye[0] - x0) ) {
253         return(0);
254     }
255
256     // check bottom clip plane (from eye perspective)
257     x1 = -(v->cos_fov_y) * radius;
258     y1 = v->sin_fov_y * radius;
259     slope = 1.0 / v->slope_y;
260     x0 = x1 - y1 / slope;
261
262     // printf("(r) x1 = %.2f  y1 = %.2f\n", x1, y1);
263     // printf("eye[1] = %.2f  eye[2] = %.2f\n", eye[1], eye[2]);
264     // printf("(r) x0 = %.2f  slope_y = %.2f  radius = %.2f\n", 
265     //       x0, slope, radius);
266
267     if ( eye[2] > slope * (eye[1] - x0) ) {
268         return(0);
269     }
270
271     // check top clip plane (from eye perspective)
272     x1 = -x1;
273     slope = -slope;
274     x0 = x1 - y1 / slope;
275
276     // printf("(r) x1 = %.2f  y1 = %.2f\n", x1, y1);
277     // printf("eye[1] = %.2f  eye[2] = %.2f\n", eye[1], eye[2]);
278     // printf("(r) x0 = %.2f  slope_y = %.2f  radius = %.2f\n", 
279     //        x0, slope, radius);
280
281     if ( eye[2] > slope * (eye[1] - x0) ) {
282         return(0);
283     }
284
285     return(viewable);
286 }
287
288
289 // Render the local tiles
290 void fgTileMgrRender( void ) {
291     fgTILECACHE *c;
292     fgFLIGHT *f;
293     fgOPTIONS *o;
294     fgVIEW *v;
295     struct fgBUCKET p;
296     fgCartesianPoint3d local_ref, offset;
297     GLint display_list;
298     double radius;
299     int i;
300     int index;
301     int culled = 0;
302     int drawn = 0;
303
304     c = &global_tile_cache;
305     f = current_aircraft.flight;
306     o = &current_options;
307     v = &current_view;
308
309     // Find current translation offset
310     fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p);
311     index = c->Exists(&p);
312     c->EntryInfo(index, &display_list, &scenery.next_center, &radius );
313
314     fgPrintf( FG_TERRAIN, FG_DEBUG, 
315               "Pos = (%.2f, %.2f) Current bucket = %d %d %d %d  Index = %ld\n", 
316               FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
317               p.lon, p.lat, p.x, p.y, fgBucketGenIndex(&p) );
318
319     for ( i = 0; i < (o->tile_diameter * o->tile_diameter); i++ ) {
320         index = tiles[i];
321         // fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index);
322         c->EntryInfo(index, &display_list, &local_ref, &radius );
323
324         if ( display_list >= 0 ) {
325
326             offset.x = local_ref.x - scenery.center.x;
327             offset.y = local_ref.y - scenery.center.y;
328             offset.z = local_ref.z - scenery.center.z;
329
330             if ( viewable(&offset, radius) ) {
331                 drawn++;
332                 xglPushMatrix();
333                 xglTranslatef(offset.x, offset.y, offset.z);
334                 xglCallList(display_list);
335                 xglPopMatrix();
336             } else {
337                 culled++;
338             }
339         }
340     }
341
342     v->vfc_ratio = (double)culled / (double)(drawn + culled);
343     // printf("drawn = %d  culled = %d  saved = %.2f\n", drawn, culled, 
344     //        v->vfc_ratio);
345 }
346
347
348 // $Log$
349 // Revision 1.11  1998/05/20 20:53:55  curt
350 // Moved global ref point and radius (bounding sphere info, and offset) to
351 // data file rather than calculating it on the fly.
352 // Fixed polygon winding problem in scenery generation stage rather than
353 // compensating for it on the fly.
354 // Made a fgTILECACHE class.
355 //
356 // Revision 1.10  1998/05/17 16:59:34  curt
357 // Frist pass at view frustum culling now operational.
358 //
359 // Revision 1.9  1998/05/16 13:09:58  curt
360 // Beginning to add support for view frustum culling.
361 // Added some temporary code to calculate bouding radius, until the
362 //   scenery generation tools and scenery can be updated.
363 //
364 // Revision 1.8  1998/05/07 23:15:21  curt
365 // Fixed a glTexImage2D() usage bug where width and height were mis-swapped.
366 // Added support for --tile-radius=n option.
367 //
368 // Revision 1.7  1998/05/06 03:16:42  curt
369 // Added an option to control square tile radius.
370 //
371 // Revision 1.6  1998/05/02 01:52:18  curt
372 // Playing around with texture coordinates.
373 //
374 // Revision 1.5  1998/04/30 12:35:32  curt
375 // Added a command line rendering option specify smooth/flat shading.
376 //
377 // Revision 1.4  1998/04/27 03:30:14  curt
378 // Minor transformation adjustments to try to keep scenery tiles closer to
379 // (0, 0, 0)  GLfloats run out of precision at the distances we need to model
380 // the earth, but we can do a bunch of pre-transformations using double math
381 // and then cast to GLfloat once everything is close in where we have less
382 // precision problems.
383 //
384 // Revision 1.3  1998/04/25 22:06:32  curt
385 // Edited cvs log messages in source files ... bad bad bad!
386 //
387 // Revision 1.2  1998/04/24 00:51:09  curt
388 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
389 // Tweaked the scenery file extentions to be "file.obj" (uncompressed)
390 // or "file.obz" (compressed.)
391 //
392 // Revision 1.1  1998/04/22 13:22:48  curt
393 // C++ - ifing the code a bit.
394 //
395 // Revision 1.25  1998/04/18 04:14:07  curt
396 // Moved fg_debug.c to it's own library.
397 //
398 // Revision 1.24  1998/04/14 02:23:18  curt
399 // Code reorganizations.  Added a Lib/ directory for more general libraries.
400 //
401 // Revision 1.23  1998/04/08 23:30:08  curt
402 // Adopted Gnu automake/autoconf system.
403 //
404 // Revision 1.22  1998/04/03 22:11:38  curt
405 // Converting to Gnu autoconf system.
406 //
407 // Revision 1.21  1998/03/23 21:23:05  curt
408 // Debugging output tweaks.
409 //
410 // Revision 1.20  1998/03/14 00:30:51  curt
411 // Beginning initial terrain texturing experiments.
412 //
413 // Revision 1.19  1998/02/20 00:16:25  curt
414 // Thursday's tweaks.
415 //
416 // Revision 1.18  1998/02/19 13:05:54  curt
417 // Incorporated some HUD tweaks from Michelle America.
418 // Tweaked the sky's sunset/rise colors.
419 // Other misc. tweaks.
420 //
421 // Revision 1.17  1998/02/16 13:39:46  curt
422 // Miscellaneous weekend tweaks.  Fixed? a cache problem that caused whole
423 // tiles to occasionally be missing.
424 //
425 // Revision 1.16  1998/02/12 21:59:53  curt
426 // Incorporated code changes contributed by Charlie Hotchkiss
427 // <chotchkiss@namg.us.anritsu.com>
428 //
429 // Revision 1.14  1998/02/09 21:30:19  curt
430 // Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
431 //
432 // Revision 1.13  1998/02/07 15:29:46  curt
433 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
434 // <chotchkiss@namg.us.anritsu.com>
435 //
436 // Revision 1.12  1998/02/01 03:39:55  curt
437 // Minor tweaks.
438 //
439 // Revision 1.11  1998/01/31 00:43:27  curt
440 // Added MetroWorks patches from Carmen Volpe.
441 //
442 // Revision 1.10  1998/01/29 00:51:40  curt
443 // First pass at tile cache, dynamic tile loading and tile unloading now works.
444 //
445 // Revision 1.9  1998/01/27 03:26:44  curt
446 // Playing with new fgPrintf command.
447 //
448 // Revision 1.8  1998/01/27 00:48:04  curt
449 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
450 // system and commandline/config file processing code.
451 //
452 // Revision 1.7  1998/01/26 15:55:25  curt
453 // Progressing on building dynamic scenery system.
454 //
455 // Revision 1.6  1998/01/24 00:03:30  curt
456 // Initial revision.
457 //
458 // Revision 1.5  1998/01/19 19:27:18  curt
459 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
460 // This should simplify things tremendously.
461 //
462 // Revision 1.4  1998/01/19 18:40:38  curt
463 // Tons of little changes to clean up the code and to remove fatal errors
464 // when building with the c++ compiler.
465 //
466 // Revision 1.3  1998/01/13 00:23:11  curt
467 // Initial changes to support loading and management of scenery tiles.  Note,
468 // there's still a fair amount of work left to be done.
469 //
470 // Revision 1.2  1998/01/08 02:22:27  curt
471 // Continue working on basic features.
472 //
473 // Revision 1.1  1998/01/07 23:50:51  curt
474 // "area" renamed to "tile"
475 //
476 // Revision 1.2  1998/01/07 03:29:29  curt
477 // Given an arbitrary lat/lon, we can now:
478 //   generate a unique index for the chunk containing the lat/lon
479 //   generate a path name to the chunk file
480 //   build a list of the indexes of all the nearby areas.
481 //
482 // Revision 1.1  1998/01/07 02:05:48  curt
483 // Initial revision.