1 // tilemgr.cxx -- routines to handle dynamic management of scenery tiles
3 // Written by Curtis Olson, started January 1998.
5 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
22 // (Log is kept at end of this file)
36 #include <Aircraft/aircraft.h>
38 #include <Bucket/bucketutils.h>
39 #include <Debug/fg_debug.h>
40 #include <Include/fg_constants.h>
41 #include <Include/fg_types.h>
42 #include <Main/options.hxx>
43 #include <Main/views.hxx>
44 #include <Math/mat3.h>
46 #include "material.hxx"
48 #include "scenery.hxx"
49 #include "tilecache.hxx"
52 #define FG_LOCAL_X_Y 81 // max(o->tile_diameter) ** 2
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];
60 // Initialize the Tile Manager subsystem
61 int fgTileMgrInit( void ) {
62 fgPrintf( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem.\n");
64 // load default material library
65 material_mgr.load_lib();
72 void fgTileMgrLoadTile( struct fgBUCKET *p, int *index) {
75 c = &global_tile_cache;
77 fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating for bucket %d %d %d %d\n",
78 p->lon, p->lat, p->x, p->y);
80 // if not in cache, load tile into the next available slot
81 if ( (*index = c->Exists(p)) < 0 ) {
82 *index = c->NextAvail();
83 c->EntryFillIn(*index, p);
86 fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
90 // given the current lon/lat, fill in the array of local chunks. If
91 // the chunk isn't already in the cache, then read it from disk.
92 int fgTileMgrUpdate( void ) {
96 struct fgBUCKET p1, p2;
97 static struct fgBUCKET p_last = {-1000, 0, 0, 0};
100 c = &global_tile_cache;
101 f = current_aircraft.flight;
102 o = ¤t_options;
104 fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1);
105 dw = o->tile_diameter / 2;
106 dh = o->tile_diameter / 2;
108 if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
109 (p1.x == p_last.x) && (p1.y == p_last.y) ) {
110 // same bucket as last time
111 fgPrintf( FG_TERRAIN, FG_DEBUG, "Same bucket as last time\n");
112 } else if ( p_last.lon == -1000 ) {
113 // First time through, initialize the system and load all
116 fgPrintf( FG_TERRAIN, FG_INFO, " First time through ... ");
117 fgPrintf( FG_TERRAIN, FG_INFO, " Updating Tile list for %d,%d %d,%d\n",
118 p1.lon, p1.lat, p1.x, p1.y);
119 fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
120 o->tile_diameter * o->tile_diameter);
122 // wipe/initialize tile cache
125 // build the local area list and update cache
126 for ( j = 0; j < o->tile_diameter; j++ ) {
127 for ( i = 0; i < o->tile_diameter; i++ ) {
128 fgBucketOffset(&p1, &p2, i - dw, j - dh);
129 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + i]);
133 // We've moved to a new bucket, we need to scroll our
134 // structures, and load in the new tiles
136 // CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
137 // AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
138 // THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION.
140 fgPrintf( FG_TERRAIN, FG_INFO, "Updating Tile list for %d,%d %d,%d\n",
141 p1.lon, p1.lat, p1.x, p1.y);
143 if ( (p1.lon > p_last.lon) ||
144 ( (p1.lon == p_last.lon) && (p1.x > p_last.x) ) ) {
145 fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
147 for ( j = 0; j < o->tile_diameter; j++ ) {
149 for ( i = 0; i < o->tile_diameter - 1; i++ ) {
150 tiles[(j*o->tile_diameter) + i] =
151 tiles[(j*o->tile_diameter) + i + 1];
153 // load in new column
154 fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
155 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) +
156 o->tile_diameter - 1]);
158 } else if ( (p1.lon < p_last.lon) ||
159 ( (p1.lon == p_last.lon) && (p1.x < p_last.x) ) ) {
160 fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
162 for ( j = 0; j < o->tile_diameter; j++ ) {
164 for ( i = o->tile_diameter - 1; i > 0; i-- ) {
165 tiles[(j*o->tile_diameter) + i] =
166 tiles[(j*o->tile_diameter) + i - 1];
168 // load in new column
169 fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
170 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + 0]);
174 if ( (p1.lat > p_last.lat) ||
175 ( (p1.lat == p_last.lat) && (p1.y > p_last.y) ) ) {
176 fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
178 for ( i = 0; i < o->tile_diameter; i++ ) {
180 for ( j = 0; j < o->tile_diameter - 1; j++ ) {
181 tiles[(j * o->tile_diameter) + i] =
182 tiles[((j+1) * o->tile_diameter) + i];
184 // load in new column
185 fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
186 fgTileMgrLoadTile(&p2, &tiles[((o->tile_diameter-1) *
187 o->tile_diameter) + i]);
189 } else if ( (p1.lat < p_last.lat) ||
190 ( (p1.lat == p_last.lat) && (p1.y < p_last.y) ) ) {
191 fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
193 for ( i = 0; i < o->tile_diameter; i++ ) {
195 for ( j = o->tile_diameter - 1; j > 0; j-- ) {
196 tiles[(j * o->tile_diameter) + i] =
197 tiles[((j-1) * o->tile_diameter) + i];
199 // load in new column
200 fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
201 fgTileMgrLoadTile(&p2, &tiles[0 + i]);
213 // Calculate if point/radius is inside view frustum
214 static int viewable( fgCartesianPoint3d *cp, double radius ) {
217 int viewable = 1; // start by assuming it's viewable
218 double x0, x1, y1, slope;
222 MAT3_SET_HVEC(world, cp->x, cp->y, cp->z, 1.0);
223 MAT3mult_vec(eye, world, v->WORLD_TO_EYE);
224 // printf( "\nworld -> eye = %.2f %.2f %.2f radius = %.2f\n",
225 // eye[0], eye[1], eye[2], radius);
227 // Check near clip plane
228 if ( eye[2] - radius > 0.0 ) {
232 // check right clip plane (from eye perspective)
233 // y = m * (x - x0) = equation of a line intercepting X axis at x0
234 x1 = v->cos_fov_x * radius;
235 y1 = v->sin_fov_x * radius;
237 x0 = x1 - y1 / slope;
239 // printf("(r) x1 = %.2f y1 = %.2f\n", x1, y1);
240 // printf("eye[0] = %.2f eye[2] = %.2f\n", eye[0], eye[2]);
241 // printf("(r) x0 = %.2f slope_x = %.2f radius = %.2f\n",
242 // x0, slope, radius);
244 if ( eye[2] > slope * (eye[0] - x0) ) {
248 // check left clip plane (from eye perspective)
251 x0 = x1 - y1 / slope;
253 // printf("(r) x1 = %.2f y1 = %.2f\n", x1, y1);
254 // printf("eye[0] = %.2f eye[2] = %.2f\n", eye[0], eye[2]);
255 // printf("(r) x0 = %.2f slope_x = %.2f radius = %.2f\n",
256 // x0, slope, radius);
258 if ( eye[2] > slope * (eye[0] - x0) ) {
262 // check bottom clip plane (from eye perspective)
263 x1 = -(v->cos_fov_y) * radius;
264 y1 = v->sin_fov_y * radius;
266 x0 = x1 - y1 / slope;
268 // printf("(r) x1 = %.2f y1 = %.2f\n", x1, y1);
269 // printf("eye[1] = %.2f eye[2] = %.2f\n", eye[1], eye[2]);
270 // printf("(r) x0 = %.2f slope_y = %.2f radius = %.2f\n",
271 // x0, slope, radius);
273 if ( eye[2] > slope * (eye[1] - x0) ) {
277 // check top clip plane (from eye perspective)
280 x0 = x1 - y1 / slope;
282 // printf("(r) x1 = %.2f y1 = %.2f\n", x1, y1);
283 // printf("eye[1] = %.2f eye[2] = %.2f\n", eye[1], eye[2]);
284 // printf("(r) x0 = %.2f slope_y = %.2f radius = %.2f\n",
285 // x0, slope, radius);
287 if ( eye[2] > slope * (eye[1] - x0) ) {
295 // Render the local tiles
296 void fgTileMgrRender( void ) {
303 fgCartesianPoint3d offset, last_center;
304 fgFRAGMENT fragment, *frag_ptr;
306 list < fgFRAGMENT > :: iterator current;
307 list < fgFRAGMENT > :: iterator last;
313 c = &global_tile_cache;
314 f = current_aircraft.flight;
315 o = ¤t_options;
318 // Find current translation offset
319 fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p);
320 index = c->Exists(&p);
321 t = c->GetTile(index);
323 fgPrintf( FG_TERRAIN, FG_DEBUG,
324 "Pos = (%.2f, %.2f) Current bucket = %d %d %d %d Index = %ld\n",
325 FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
326 p.lon, p.lat, p.x, p.y, fgBucketGenIndex(&p) );
328 // initialize the transient per-material fragment lists
329 material_mgr.init_transient_material_lists();
331 // traverse the potentially viewable tile list
332 for ( i = 0; i < (o->tile_diameter * o->tile_diameter); i++ ) {
334 // fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index);
335 t = c->GetTile(index);
337 // calculate tile offset
338 offset.x = t->center.x - scenery.center.x;
339 offset.y = t->center.y - scenery.center.y;
340 offset.z = t->center.z - scenery.center.z;
342 // Course (tile based) culling
343 if ( viewable(&offset, t->bounding_radius) ) {
344 // at least a portion of this tile is viewable
347 xglTranslatef(offset.x, offset.y, offset.z);
349 // traverse fragment list for tile
350 current = t->fragment_list.begin();
351 last = t->fragment_list.end();
353 while ( current != last ) {
354 fragment = *current++;
356 if ( fragment.display_list >= 0 ) {
357 // Fine (fragment based) culling
358 offset.x = fragment.center.x - scenery.center.x;
359 offset.y = fragment.center.y - scenery.center.y;
360 offset.z = fragment.center.z - scenery.center.z;
362 if ( viewable(&offset, fragment.bounding_radius * 2) ) {
363 // add to transient per-material property fragment list
364 mtl_ptr = (fgMATERIAL *)(fragment.material_ptr);
365 // printf(" lookup = %s\n", mtl_ptr->texture_name);
366 if ( mtl_ptr->list_size < FG_MAX_MATERIAL_FRAGS ) {
367 mtl_ptr->list[mtl_ptr->list_size] = &fragment;
368 (mtl_ptr->list_size)++;
371 xglCallList(fragment.display_list);
374 // printf("Culled a fragment %.2f %.2f %.2f %.2f\n",
375 // fragment.center.x, fragment.center.y,
376 // fragment.center.z, fragment.bounding_radius);
384 culled += t->fragment_list.size();
388 if ( (drawn + culled) > 0 ) {
389 v->vfc_ratio = (double)culled / (double)(drawn + culled);
393 // printf("drawn = %d culled = %d saved = %.2f\n", drawn, culled,
396 // traverse the transient per-material fragment lists and render
397 // out all fragments for each material property.
398 map < string, fgMATERIAL, less<string> > :: iterator mapcurrent =
399 material_mgr.material_map.begin();
400 map < string, fgMATERIAL, less<string> > :: iterator maplast =
401 material_mgr.material_map.end();
403 while ( mapcurrent != maplast ) {
404 // (char *)key = (*mapcurrent).first;
405 // (fgMATERIAL)value = (*mapcurrent).second;
406 size = (*mapcurrent).second.list_size;
407 for ( i = 0; i < size; i++ ) {
408 // frag_ptr = &(*mapcurrent).second.list[i];
409 // frag_ptr->tile_center
419 // Revision 1.16 1998/06/05 22:39:55 curt
420 // Working on sorting by, and rendering by material properties.
422 // Revision 1.15 1998/06/03 00:47:51 curt
423 // No .h for STL includes.
424 // Minor view culling optimizations.
426 // Revision 1.14 1998/06/01 17:56:20 curt
427 // Incremental additions to material.cxx (not fully functional)
428 // Tweaked vfc_ratio math to avoid divide by zero.
430 // Revision 1.13 1998/05/24 02:49:10 curt
431 // Implimented fragment level view frustum culling.
433 // Revision 1.12 1998/05/23 14:09:23 curt
434 // Added tile.cxx and tile.hxx.
435 // Working on rewriting the tile management system so a tile is just a list
436 // fragments, and the fragment record contains the display list for that fragment.
438 // Revision 1.11 1998/05/20 20:53:55 curt
439 // Moved global ref point and radius (bounding sphere info, and offset) to
440 // data file rather than calculating it on the fly.
441 // Fixed polygon winding problem in scenery generation stage rather than
442 // compensating for it on the fly.
443 // Made a fgTILECACHE class.
445 // Revision 1.10 1998/05/17 16:59:34 curt
446 // Frist pass at view frustum culling now operational.
448 // Revision 1.9 1998/05/16 13:09:58 curt
449 // Beginning to add support for view frustum culling.
450 // Added some temporary code to calculate bouding radius, until the
451 // scenery generation tools and scenery can be updated.
453 // Revision 1.8 1998/05/07 23:15:21 curt
454 // Fixed a glTexImage2D() usage bug where width and height were mis-swapped.
455 // Added support for --tile-radius=n option.
457 // Revision 1.7 1998/05/06 03:16:42 curt
458 // Added an option to control square tile radius.
460 // Revision 1.6 1998/05/02 01:52:18 curt
461 // Playing around with texture coordinates.
463 // Revision 1.5 1998/04/30 12:35:32 curt
464 // Added a command line rendering option specify smooth/flat shading.
466 // Revision 1.4 1998/04/27 03:30:14 curt
467 // Minor transformation adjustments to try to keep scenery tiles closer to
468 // (0, 0, 0) GLfloats run out of precision at the distances we need to model
469 // the earth, but we can do a bunch of pre-transformations using double math
470 // and then cast to GLfloat once everything is close in where we have less
471 // precision problems.
473 // Revision 1.3 1998/04/25 22:06:32 curt
474 // Edited cvs log messages in source files ... bad bad bad!
476 // Revision 1.2 1998/04/24 00:51:09 curt
477 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
478 // Tweaked the scenery file extentions to be "file.obj" (uncompressed)
479 // or "file.obz" (compressed.)
481 // Revision 1.1 1998/04/22 13:22:48 curt
482 // C++ - ifing the code a bit.
484 // Revision 1.25 1998/04/18 04:14:07 curt
485 // Moved fg_debug.c to it's own library.
487 // Revision 1.24 1998/04/14 02:23:18 curt
488 // Code reorganizations. Added a Lib/ directory for more general libraries.
490 // Revision 1.23 1998/04/08 23:30:08 curt
491 // Adopted Gnu automake/autoconf system.
493 // Revision 1.22 1998/04/03 22:11:38 curt
494 // Converting to Gnu autoconf system.
496 // Revision 1.21 1998/03/23 21:23:05 curt
497 // Debugging output tweaks.
499 // Revision 1.20 1998/03/14 00:30:51 curt
500 // Beginning initial terrain texturing experiments.
502 // Revision 1.19 1998/02/20 00:16:25 curt
503 // Thursday's tweaks.
505 // Revision 1.18 1998/02/19 13:05:54 curt
506 // Incorporated some HUD tweaks from Michelle America.
507 // Tweaked the sky's sunset/rise colors.
508 // Other misc. tweaks.
510 // Revision 1.17 1998/02/16 13:39:46 curt
511 // Miscellaneous weekend tweaks. Fixed? a cache problem that caused whole
512 // tiles to occasionally be missing.
514 // Revision 1.16 1998/02/12 21:59:53 curt
515 // Incorporated code changes contributed by Charlie Hotchkiss
516 // <chotchkiss@namg.us.anritsu.com>
518 // Revision 1.14 1998/02/09 21:30:19 curt
519 // Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
521 // Revision 1.13 1998/02/07 15:29:46 curt
522 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
523 // <chotchkiss@namg.us.anritsu.com>
525 // Revision 1.12 1998/02/01 03:39:55 curt
528 // Revision 1.11 1998/01/31 00:43:27 curt
529 // Added MetroWorks patches from Carmen Volpe.
531 // Revision 1.10 1998/01/29 00:51:40 curt
532 // First pass at tile cache, dynamic tile loading and tile unloading now works.
534 // Revision 1.9 1998/01/27 03:26:44 curt
535 // Playing with new fgPrintf command.
537 // Revision 1.8 1998/01/27 00:48:04 curt
538 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
539 // system and commandline/config file processing code.
541 // Revision 1.7 1998/01/26 15:55:25 curt
542 // Progressing on building dynamic scenery system.
544 // Revision 1.6 1998/01/24 00:03:30 curt
547 // Revision 1.5 1998/01/19 19:27:18 curt
548 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
549 // This should simplify things tremendously.
551 // Revision 1.4 1998/01/19 18:40:38 curt
552 // Tons of little changes to clean up the code and to remove fatal errors
553 // when building with the c++ compiler.
555 // Revision 1.3 1998/01/13 00:23:11 curt
556 // Initial changes to support loading and management of scenery tiles. Note,
557 // there's still a fair amount of work left to be done.
559 // Revision 1.2 1998/01/08 02:22:27 curt
560 // Continue working on basic features.
562 // Revision 1.1 1998/01/07 23:50:51 curt
563 // "area" renamed to "tile"
565 // Revision 1.2 1998/01/07 03:29:29 curt
566 // Given an arbitrary lat/lon, we can now:
567 // generate a unique index for the chunk containing the lat/lon
568 // generate a path name to the chunk file
569 // build a list of the indexes of all the nearby areas.
571 // Revision 1.1 1998/01/07 02:05:48 curt