]> git.mxchange.org Git - flightgear.git/blob - Scenery/tilemgr.cxx
Working on sorting by, and rendering by material properties.
[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 <Aircraft/aircraft.h>
37
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>
45
46 #include "material.hxx"
47 #include "obj.hxx"
48 #include "scenery.hxx"
49 #include "tilecache.hxx"
50
51
52 #define FG_LOCAL_X_Y         81  // max(o->tile_diameter) ** 2
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
64     // load default material library
65     material_mgr.load_lib();
66
67     return 1;
68 }
69
70
71 // load a tile
72 void fgTileMgrLoadTile( struct fgBUCKET *p, int *index) {
73     fgTILECACHE *c;
74
75     c = &global_tile_cache;
76
77     fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating for bucket %d %d %d %d\n", 
78            p->lon, p->lat, p->x, p->y);
79     
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);
84     }
85
86     fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
87 }
88
89
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 ) {
93     fgTILECACHE *c;
94     fgFLIGHT *f;
95     fgOPTIONS *o;
96     struct fgBUCKET p1, p2;
97     static struct fgBUCKET p_last = {-1000, 0, 0, 0};
98     int i, j, dw, dh;
99
100     c = &global_tile_cache;
101     f = current_aircraft.flight;
102     o = &current_options;
103
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;
107
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
114         // relavant tiles
115
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);
121
122         // wipe/initialize tile cache
123         c->Init();
124
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]);
130             }
131         }
132     } else {
133         // We've moved to a new bucket, we need to scroll our
134         // structures, and load in the new tiles
135
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.
139
140         fgPrintf( FG_TERRAIN, FG_INFO, "Updating Tile list for %d,%d %d,%d\n",
141                   p1.lon, p1.lat, p1.x, p1.y);
142
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", 
146                       o->tile_diameter);
147             for ( j = 0; j < o->tile_diameter; j++ ) {
148                 // scrolling East
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];
152                 }
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]);
157             }
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", 
161                       o->tile_diameter);
162             for ( j = 0; j < o->tile_diameter; j++ ) {
163                 // scrolling West
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];
167                 }
168                 // load in new column
169                 fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
170                 fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + 0]);
171             }
172         }
173
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", 
177                       o->tile_diameter);
178             for ( i = 0; i < o->tile_diameter; i++ ) {
179                 // scrolling North
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];
183                 }
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]);
188             }
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", 
192                       o->tile_diameter);
193             for ( i = 0; i < o->tile_diameter; i++ ) {
194                 // scrolling South
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];
198                 }
199                 // load in new column
200                 fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
201                 fgTileMgrLoadTile(&p2, &tiles[0 + i]);
202             }
203         }
204     }
205     p_last.lon = p1.lon;
206     p_last.lat = p1.lat;
207     p_last.x = p1.x;
208     p_last.y = p1.y;
209     return 1;
210 }
211
212
213 // Calculate if point/radius is inside view frustum
214 static int viewable( fgCartesianPoint3d *cp, double radius ) {
215     fgVIEW *v;
216     MAT3hvec world, eye;
217     int viewable = 1; // start by assuming it's viewable
218     double x0, x1, y1, slope;
219
220     v = &current_view;
221
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);
226
227     // Check near clip plane
228     if ( eye[2] - radius > 0.0 ) {
229         return(0);
230     }
231
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;
236     slope = v->slope_x;
237     x0 = x1 - y1 / slope;
238
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);
243
244     if ( eye[2] > slope * (eye[0] - x0) ) {
245         return(0);
246     }
247
248     // check left clip plane (from eye perspective)
249     x1 = -x1;
250     slope = -slope;
251     x0 = x1 - y1 / slope;
252
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);
257
258     if ( eye[2] > slope * (eye[0] - x0) ) {
259         return(0);
260     }
261
262     // check bottom clip plane (from eye perspective)
263     x1 = -(v->cos_fov_y) * radius;
264     y1 = v->sin_fov_y * radius;
265     slope = v->slope_y;
266     x0 = x1 - y1 / slope;
267
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);
272
273     if ( eye[2] > slope * (eye[1] - x0) ) {
274         return(0);
275     }
276
277     // check top clip plane (from eye perspective)
278     x1 = -x1;
279     slope = -slope;
280     x0 = x1 - y1 / slope;
281
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);
286
287     if ( eye[2] > slope * (eye[1] - x0) ) {
288         return(0);
289     }
290
291     return(viewable);
292 }
293
294
295 // Render the local tiles
296 void fgTileMgrRender( void ) {
297     fgTILECACHE *c;
298     fgFLIGHT *f;
299     fgOPTIONS *o;
300     fgTILE *t;
301     fgVIEW *v;
302     struct fgBUCKET p;
303     fgCartesianPoint3d offset, last_center;
304     fgFRAGMENT fragment, *frag_ptr;
305     fgMATERIAL *mtl_ptr;
306     list < fgFRAGMENT > :: iterator current;
307     list < fgFRAGMENT > :: iterator last;
308     int i, size;
309     int index;
310     int culled = 0;
311     int drawn = 0;
312
313     c = &global_tile_cache;
314     f = current_aircraft.flight;
315     o = &current_options;
316     v = &current_view;
317
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);
322
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) );
327
328     // initialize the transient per-material fragment lists
329     material_mgr.init_transient_material_lists();
330
331     // traverse the potentially viewable tile list
332     for ( i = 0; i < (o->tile_diameter * o->tile_diameter); i++ ) {
333         index = tiles[i];
334         // fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index);
335         t = c->GetTile(index);
336
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;
341
342         // Course (tile based) culling
343         if ( viewable(&offset, t->bounding_radius) ) {
344             // at least a portion of this tile is viewable
345             
346             xglPushMatrix();
347             xglTranslatef(offset.x, offset.y, offset.z);
348
349             // traverse fragment list for tile
350             current = t->fragment_list.begin();
351             last = t->fragment_list.end();
352
353             while ( current != last ) {
354                 fragment = *current++;
355
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;
361
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)++;
369                         }
370
371                         xglCallList(fragment.display_list);
372                         drawn++;
373                     } else {
374                         // printf("Culled a fragment %.2f %.2f %.2f %.2f\n",
375                         //        fragment.center.x, fragment.center.y,
376                         //        fragment.center.z, fragment.bounding_radius);
377                         culled++;
378                     }
379                 }
380             }
381
382             xglPopMatrix();
383         } else {
384             culled += t->fragment_list.size();
385         }
386     }
387
388     if ( (drawn + culled) > 0 ) {
389         v->vfc_ratio = (double)culled / (double)(drawn + culled);
390     } else {
391         v->vfc_ratio = 0.0;
392     }
393     // printf("drawn = %d  culled = %d  saved = %.2f\n", drawn, culled, 
394     //        v->vfc_ratio);
395
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();
402
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
410         }
411
412         *mapcurrent++;
413     }
414
415 }
416
417
418 // $Log$
419 // Revision 1.16  1998/06/05 22:39:55  curt
420 // Working on sorting by, and rendering by material properties.
421 //
422 // Revision 1.15  1998/06/03 00:47:51  curt
423 // No .h for STL includes.
424 // Minor view culling optimizations.
425 //
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.
429 //
430 // Revision 1.13  1998/05/24 02:49:10  curt
431 // Implimented fragment level view frustum culling.
432 //
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.
437 //
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.
444 //
445 // Revision 1.10  1998/05/17 16:59:34  curt
446 // Frist pass at view frustum culling now operational.
447 //
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.
452 //
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.
456 //
457 // Revision 1.7  1998/05/06 03:16:42  curt
458 // Added an option to control square tile radius.
459 //
460 // Revision 1.6  1998/05/02 01:52:18  curt
461 // Playing around with texture coordinates.
462 //
463 // Revision 1.5  1998/04/30 12:35:32  curt
464 // Added a command line rendering option specify smooth/flat shading.
465 //
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.
472 //
473 // Revision 1.3  1998/04/25 22:06:32  curt
474 // Edited cvs log messages in source files ... bad bad bad!
475 //
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.)
480 //
481 // Revision 1.1  1998/04/22 13:22:48  curt
482 // C++ - ifing the code a bit.
483 //
484 // Revision 1.25  1998/04/18 04:14:07  curt
485 // Moved fg_debug.c to it's own library.
486 //
487 // Revision 1.24  1998/04/14 02:23:18  curt
488 // Code reorganizations.  Added a Lib/ directory for more general libraries.
489 //
490 // Revision 1.23  1998/04/08 23:30:08  curt
491 // Adopted Gnu automake/autoconf system.
492 //
493 // Revision 1.22  1998/04/03 22:11:38  curt
494 // Converting to Gnu autoconf system.
495 //
496 // Revision 1.21  1998/03/23 21:23:05  curt
497 // Debugging output tweaks.
498 //
499 // Revision 1.20  1998/03/14 00:30:51  curt
500 // Beginning initial terrain texturing experiments.
501 //
502 // Revision 1.19  1998/02/20 00:16:25  curt
503 // Thursday's tweaks.
504 //
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.
509 //
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.
513 //
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>
517 //
518 // Revision 1.14  1998/02/09 21:30:19  curt
519 // Fixed a nagging problem with terrain tiles not "quite" matching up perfectly.
520 //
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>
524 //
525 // Revision 1.12  1998/02/01 03:39:55  curt
526 // Minor tweaks.
527 //
528 // Revision 1.11  1998/01/31 00:43:27  curt
529 // Added MetroWorks patches from Carmen Volpe.
530 //
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.
533 //
534 // Revision 1.9  1998/01/27 03:26:44  curt
535 // Playing with new fgPrintf command.
536 //
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.
540 //
541 // Revision 1.7  1998/01/26 15:55:25  curt
542 // Progressing on building dynamic scenery system.
543 //
544 // Revision 1.6  1998/01/24 00:03:30  curt
545 // Initial revision.
546 //
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.
550 //
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.
554 //
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.
558 //
559 // Revision 1.2  1998/01/08 02:22:27  curt
560 // Continue working on basic features.
561 //
562 // Revision 1.1  1998/01/07 23:50:51  curt
563 // "area" renamed to "tile"
564 //
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.
570 //
571 // Revision 1.1  1998/01/07 02:05:48  curt
572 // Initial revision.