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