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 frag_offset, last_offset;
304 fgFRAGMENT *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();
332 // traverse the potentially viewable tile list
333 for ( i = 0; i < (o->tile_diameter * o->tile_diameter); i++ ) {
335 // fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index);
336 t = c->GetTile(index);
338 // calculate tile offset
339 t->offset.x = t->center.x - scenery.center.x;
340 t->offset.y = t->center.y - scenery.center.y;
341 t->offset.z = t->center.z - scenery.center.z;
343 // Course (tile based) culling
344 if ( viewable(&(t->offset), t->bounding_radius) ) {
345 // at least a portion of this tile is viewable
348 // xglTranslatef(t->offset.x, t->offset.y, t->offset.z);
350 // traverse fragment list for tile
351 current = t->fragment_list.begin();
352 last = t->fragment_list.end();
354 while ( current != last ) {
355 frag_ptr = &(*current);
358 if ( frag_ptr->display_list >= 0 ) {
359 // Fine (fragment based) culling
360 frag_offset.x = frag_ptr->center.x - scenery.center.x;
361 frag_offset.y = frag_ptr->center.y - scenery.center.y;
362 frag_offset.z = frag_ptr->center.z - scenery.center.z;
364 if ( viewable(&frag_offset, frag_ptr->bounding_radius*2) ) {
365 // add to transient per-material property fragment list
366 frag_ptr->tile_offset.x = t->offset.x;
367 frag_ptr->tile_offset.y = t->offset.y;
368 frag_ptr->tile_offset.z = t->offset.z;
371 frag_ptr->matrix[12] = t->offset.x;
372 frag_ptr->matrix[13] = t->offset.y;
373 frag_ptr->matrix[14] = t->offset.z;
375 xglGetFloatv(GL_MODELVIEW_MATRIX, frag_ptr->matrix);
376 for ( j = 1; j < 16; j++ ) {
377 printf(" a%d = %f\n", j, frag_ptr->matrix[j]);
381 mtl_ptr = (fgMATERIAL *)(frag_ptr->material_ptr);
382 // printf(" lookup = %s\n", mtl_ptr->texture_name);
383 if ( mtl_ptr->list_size < FG_MAX_MATERIAL_FRAGS ) {
384 mtl_ptr->list[mtl_ptr->list_size] = frag_ptr;
385 (mtl_ptr->list_size)++;
387 fgPrintf( FG_TERRAIN, FG_ALERT,
388 "Overran material sorting array\n" );
391 // xglCallList(frag_ptr->display_list);
394 // printf("Culled a fragment %.2f %.2f %.2f %.2f\n",
395 // frag_ptr->center.x, frag_ptr->center.y,
396 // frag_ptr->center.z, frag_ptr->bounding_radius);
404 culled += t->fragment_list.size();
408 if ( (drawn + culled) > 0 ) {
409 v->vfc_ratio = (double)culled / (double)(drawn + culled);
413 // printf("drawn = %d culled = %d saved = %.2f\n", drawn, culled,
417 // traverse the transient per-material fragment lists and render
418 // out all fragments for each material property.
419 map < string, fgMATERIAL, less<string> > :: iterator mapcurrent =
420 material_mgr.material_map.begin();
421 map < string, fgMATERIAL, less<string> > :: iterator maplast =
422 material_mgr.material_map.end();
426 while ( mapcurrent != maplast ) {
427 // (char *)key = (*mapcurrent).first;
428 // (fgMATERIAL)value = (*mapcurrent).second;
429 mtl_ptr = &(*mapcurrent).second;
431 last_offset.x = last_offset.y = last_offset.z = -99999999.0;
433 size = mtl_ptr->list_size;
435 if ( ! o->textures ) {
436 xglMaterialfv (GL_FRONT, GL_AMBIENT, mtl_ptr->ambient);
437 xglMaterialfv (GL_FRONT, GL_DIFFUSE, mtl_ptr->diffuse);
440 // printf("traversing = %s, size = %d\n",
441 // mtl_ptr->texture_name, size);
442 for ( i = 0; i < size; i++ ) {
443 frag_ptr = mtl_ptr->list[i];
445 if ( (frag_ptr->tile_offset.x == last_offset.x) &&
446 (frag_ptr->tile_offset.y == last_offset.y) &&
447 (frag_ptr->tile_offset.z == last_offset.z) ) {
448 // same tile as last time, no transform necessary
450 // new tile, new translate
451 // xglLoadMatrixf( frag_ptr->matrix );
454 xglTranslatef( frag_ptr->tile_offset.x,
455 frag_ptr->tile_offset.y,
456 frag_ptr->tile_offset.z );
459 // Woohoo!!! We finally get to draw something!
460 // printf(" display_list = %d\n", frag_ptr->display_list);
461 xglCallList(frag_ptr->display_list);
463 last_offset = frag_ptr->tile_offset;
475 // Revision 1.19 1998/06/08 17:57:54 curt
476 // Working first pass at material proporty sorting.
478 // Revision 1.18 1998/06/06 01:09:32 curt
479 // I goofed on the log message in the last commit ... now fixed.
481 // Revision 1.17 1998/06/06 01:07:18 curt
482 // Increased per material fragment list size from 100 to 400.
483 // Now correctly draw viewable fragments in per material order.
485 // Revision 1.16 1998/06/05 22:39:55 curt
486 // Working on sorting by, and rendering by material properties.
488 // Revision 1.15 1998/06/03 00:47:51 curt
489 // No .h for STL includes.
490 // Minor view culling optimizations.
492 // Revision 1.14 1998/06/01 17:56:20 curt
493 // Incremental additions to material.cxx (not fully functional)
494 // Tweaked vfc_ratio math to avoid divide by zero.
496 // Revision 1.13 1998/05/24 02:49:10 curt
497 // Implimented fragment level view frustum culling.
499 // Revision 1.12 1998/05/23 14:09:23 curt
500 // Added tile.cxx and tile.hxx.
501 // Working on rewriting the tile management system so a tile is just a list
502 // fragments, and the fragment record contains the display list for that fragment.
504 // Revision 1.11 1998/05/20 20:53:55 curt
505 // Moved global ref point and radius (bounding sphere info, and offset) to
506 // data file rather than calculating it on the fly.
507 // Fixed polygon winding problem in scenery generation stage rather than
508 // compensating for it on the fly.
509 // Made a fgTILECACHE class.
511 // Revision 1.10 1998/05/17 16:59:34 curt
512 // Frist pass at view frustum culling now operational.
514 // Revision 1.9 1998/05/16 13:09:58 curt
515 // Beginning to add support for view frustum culling.
516 // Added some temporary code to calculate bouding radius, until the
517 // scenery generation tools and scenery can be updated.
519 // Revision 1.8 1998/05/07 23:15:21 curt
520 // Fixed a glTexImage2D() usage bug where width and height were mis-swapped.
521 // Added support for --tile-radius=n option.
523 // Revision 1.7 1998/05/06 03:16:42 curt
524 // Added an option to control square tile radius.
526 // Revision 1.6 1998/05/02 01:52:18 curt
527 // Playing around with texture coordinates.
529 // Revision 1.5 1998/04/30 12:35:32 curt
530 // Added a command line rendering option specify smooth/flat shading.
532 // Revision 1.4 1998/04/27 03:30:14 curt
533 // Minor transformation adjustments to try to keep scenery tiles closer to
534 // (0, 0, 0) GLfloats run out of precision at the distances we need to model
535 // the earth, but we can do a bunch of pre-transformations using double math
536 // and then cast to GLfloat once everything is close in where we have less
537 // precision problems.
539 // Revision 1.3 1998/04/25 22:06:32 curt
540 // Edited cvs log messages in source files ... bad bad bad!
542 // Revision 1.2 1998/04/24 00:51:09 curt
543 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
544 // Tweaked the scenery file extentions to be "file.obj" (uncompressed)
545 // or "file.obz" (compressed.)
547 // Revision 1.1 1998/04/22 13:22:48 curt
548 // C++ - ifing the code a bit.
550 // Revision 1.25 1998/04/18 04:14:07 curt
551 // Moved fg_debug.c to it's own library.
553 // Revision 1.24 1998/04/14 02:23:18 curt
554 // Code reorganizations. Added a Lib/ directory for more general libraries.
556 // Revision 1.23 1998/04/08 23:30:08 curt
557 // Adopted Gnu automake/autoconf system.
559 // Revision 1.22 1998/04/03 22:11:38 curt
560 // Converting to Gnu autoconf system.
562 // Revision 1.21 1998/03/23 21:23:05 curt
563 // Debugging output tweaks.
565 // Revision 1.20 1998/03/14 00:30:51 curt
566 // Beginning initial terrain texturing experiments.
568 // Revision 1.19 1998/02/20 00:16:25 curt
569 // Thursday's tweaks.
571 // Revision 1.18 1998/02/19 13:05:54 curt
572 // Incorporated some HUD tweaks from Michelle America.
573 // Tweaked the sky's sunset/rise colors.
574 // Other misc. tweaks.
576 // Revision 1.17 1998/02/16 13:39:46 curt
577 // Miscellaneous weekend tweaks. Fixed? a cache problem that caused whole
578 // tiles to occasionally be missing.
580 // Revision 1.16 1998/02/12 21:59:53 curt
581 // Incorporated code changes contributed by Charlie Hotchkiss
582 // <chotchkiss@namg.us.anritsu.com>
584 // Revision 1.14 1998/02/09 21:30:19 curt
585 // Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
587 // Revision 1.13 1998/02/07 15:29:46 curt
588 // Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
589 // <chotchkiss@namg.us.anritsu.com>
591 // Revision 1.12 1998/02/01 03:39:55 curt
594 // Revision 1.11 1998/01/31 00:43:27 curt
595 // Added MetroWorks patches from Carmen Volpe.
597 // Revision 1.10 1998/01/29 00:51:40 curt
598 // First pass at tile cache, dynamic tile loading and tile unloading now works.
600 // Revision 1.9 1998/01/27 03:26:44 curt
601 // Playing with new fgPrintf command.
603 // Revision 1.8 1998/01/27 00:48:04 curt
604 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
605 // system and commandline/config file processing code.
607 // Revision 1.7 1998/01/26 15:55:25 curt
608 // Progressing on building dynamic scenery system.
610 // Revision 1.6 1998/01/24 00:03:30 curt
613 // Revision 1.5 1998/01/19 19:27:18 curt
614 // Merged in make system changes from Bob Kuehne <rpk@sgi.com>
615 // This should simplify things tremendously.
617 // Revision 1.4 1998/01/19 18:40:38 curt
618 // Tons of little changes to clean up the code and to remove fatal errors
619 // when building with the c++ compiler.
621 // Revision 1.3 1998/01/13 00:23:11 curt
622 // Initial changes to support loading and management of scenery tiles. Note,
623 // there's still a fair amount of work left to be done.
625 // Revision 1.2 1998/01/08 02:22:27 curt
626 // Continue working on basic features.
628 // Revision 1.1 1998/01/07 23:50:51 curt
629 // "area" renamed to "tile"
631 // Revision 1.2 1998/01/07 03:29:29 curt
632 // Given an arbitrary lat/lon, we can now:
633 // generate a unique index for the chunk containing the lat/lon
634 // generate a path name to the chunk file
635 // build a list of the indexes of all the nearby areas.
637 // Revision 1.1 1998/01/07 02:05:48 curt