]> git.mxchange.org Git - flightgear.git/blob - Scenery/tilecache.cxx
Converted to new logstream debugging facility. This allows release
[flightgear.git] / Scenery / tilecache.cxx
1 // tilecache.cxx -- routines to handle scenery tile caching
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 <Airports/genapt.hxx>
37 #include <Bucket/bucketutils.h>
38 #include <Debug/logstream.hxx>
39 #include <Main/options.hxx>
40 #include <Main/views.hxx>
41 #include <Objects/obj.hxx>
42
43 #include "tile.hxx"
44 #include "tilecache.hxx"
45
46
47 // the tile cache
48 fgTILECACHE global_tile_cache;
49
50
51 // Constructor
52 fgTILECACHE::fgTILECACHE( void ) {
53 }
54
55
56 // Initialize the tile cache subsystem
57 void
58 fgTILECACHE::init( void )
59 {
60     int i;
61
62     FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
63
64     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
65         tile_cache[i].used = 0;
66     }
67 }
68
69
70 // Search for the specified "bucket" in the cache
71 int
72 fgTILECACHE::exists( fgBUCKET *p )
73 {
74     int i;
75
76     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
77         if ( tile_cache[i].tile_bucket.lon == p->lon ) {
78             if ( tile_cache[i].tile_bucket.lat == p->lat ) {
79                 if ( tile_cache[i].tile_bucket.x == p->x ) {
80                     if ( tile_cache[i].tile_bucket.y == p->y ) {
81                         FG_LOG( FG_TERRAIN, FG_DEBUG, 
82                                 "TILE EXISTS in cache ... index = " << i );
83                         return( i );
84                     }
85                 }
86             }
87         }
88     }
89     
90     return( -1 );
91 }
92
93
94 // Fill in a tile cache entry with real data for the specified bucket
95 void
96 fgTILECACHE::fill_in( int index, fgBUCKET *p )
97 {
98     string root, tile_path, apt_path;
99     char index_str[256];
100     char base_path[256];
101
102     // Mark this cache entry as used
103     tile_cache[index].used = 1;
104
105     // Update the bucket
106     tile_cache[index].tile_bucket.lon = p->lon;
107     tile_cache[index].tile_bucket.lat = p->lat;
108     tile_cache[index].tile_bucket.x = p->x;
109     tile_cache[index].tile_bucket.y = p->y;
110
111     // Load the appropriate data file and built tile fragment list
112     fgBucketGenBasePath(p, base_path);
113     root = current_options.get_fg_root();
114     sprintf( index_str, "%ld", fgBucketGenIndex(p) );
115
116     tile_path = root + "/Scenery/" + base_path + "/" + index_str;
117     fgObjLoad( tile_path.c_str(), &tile_cache[index] );
118
119     // cout << " ncount before = " << tile_cache[index].ncount << "\n";
120     // cout << " fragments before = " << tile_cache[index].fragment_list.size()
121     //      << "\n";
122
123     apt_path = tile_path + ".apt";
124     fgAptGenerate( apt_path, &tile_cache[index] );
125
126     // cout << " ncount after = " << tile_cache[index].ncount << "\n";
127     // cout << " fragments after = " << tile_cache[index].fragment_list.size()
128     //      << "\n";
129 }
130
131
132 // Free a tile cache entry
133 void
134 fgTILECACHE::entry_free( int index )
135 {
136     fgFRAGMENT *fragment;
137
138     // Mark this cache entry as un-used
139     tile_cache[index].used = 0;
140
141     // Update the bucket
142     FG_LOG( FG_TERRAIN, FG_DEBUG, 
143             "FREEING TILE = ("
144             << tile_cache[index].tile_bucket.lon << " "
145             << tile_cache[index].tile_bucket.lat << " "
146             << tile_cache[index].tile_bucket.x << " "
147             << tile_cache[index].tile_bucket.y << ")" );
148
149     // Step through the fragment list, deleting the display list, then
150     // the fragment, until the list is empty.
151     while ( tile_cache[index].fragment_list.size() ) {
152         list < fgFRAGMENT > :: iterator current =
153             tile_cache[index].fragment_list.begin();
154         fragment = &(*current);
155         xglDeleteLists( fragment->display_list, 1 );
156
157         tile_cache[index].fragment_list.pop_front();
158     }
159 }
160
161
162 // Return the specified tile cache entry 
163 fgTILE *
164 fgTILECACHE::get_tile( int index )
165 {
166     return ( &tile_cache[index] );
167 }
168
169
170 // Return index of next available slot in tile cache
171 int
172 fgTILECACHE::next_avail( void )
173 {
174     fgVIEW *v;
175     Point3D delta;
176     int i;
177     float max, med, min, tmp;
178     float dist, max_dist;
179     int max_index;
180     
181     v = &current_view;
182
183     max_dist = 0.0;
184     max_index = 0;
185
186     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
187         if ( tile_cache[i].used == 0 ) {
188             return(i);
189         } else {
190             // calculate approximate distance from view point
191             FG_LOG( FG_TERRAIN, FG_DEBUG,
192                     "DIST Abs view pos = "
193                     << v->abs_view_pos.x() << ", "
194                     << v->abs_view_pos.y() << ", "
195                     << v->abs_view_pos.z() );
196             FG_LOG( FG_TERRAIN, FG_DEBUG,
197                     "    ref point = "
198                     << tile_cache[i].center.x() << ", "
199                     << tile_cache[i].center.y() << ", "
200                     << tile_cache[i].center.z() );
201
202             delta.setx( fabs(tile_cache[i].center.x() - v->abs_view_pos.x() ) );
203             delta.sety( fabs(tile_cache[i].center.y() - v->abs_view_pos.y() ) );
204             delta.setz( fabs(tile_cache[i].center.z() - v->abs_view_pos.z() ) );
205
206             max = delta.x(); med = delta.y(); min = delta.z();
207             if ( max < med ) {
208                 tmp = max; max = med; med = tmp;
209             }
210             if ( max < min ) {
211                 tmp = max; max = min; min = tmp;
212             }
213             dist = max + (med + min) / 4;
214
215             FG_LOG( FG_TERRAIN, FG_DEBUG, "    distance = " << dist );
216
217             if ( dist > max_dist ) {
218                 max_dist = dist;
219                 max_index = i;
220             }
221         }
222     }
223
224     // If we made it this far, then there were no open cache entries.
225     // We will instead free the furthest cache entry and return it's
226     // index.
227     
228     entry_free( max_index );
229     return( max_index );
230 }
231
232
233 // Destructor
234 fgTILECACHE::~fgTILECACHE( void ) {
235 }
236
237
238 // $Log$
239 // Revision 1.19  1998/11/06 21:18:21  curt
240 // Converted to new logstream debugging facility.  This allows release
241 // builds with no messages at all (and no performance impact) by using
242 // the -DFG_NDEBUG flag.
243 //
244 // Revision 1.18  1998/10/16 18:12:28  curt
245 // Fixed a bug in the conversion to Point3D.
246 //
247 // Revision 1.17  1998/10/16 00:55:48  curt
248 // Converted to Point3D class.
249 //
250 // Revision 1.16  1998/09/14 12:45:23  curt
251 // minor tweaks.
252 //
253 // Revision 1.15  1998/08/27 17:02:10  curt
254 // Contributions from Bernie Bright <bbright@c031.aone.net.au>
255 // - use strings for fg_root and airport_id and added methods to return
256 //   them as strings,
257 // - inlined all access methods,
258 // - made the parsing functions private methods,
259 // - deleted some unused functions.
260 // - propogated some of these changes out a bit further.
261 //
262 // Revision 1.14  1998/08/25 16:52:43  curt
263 // material.cxx material.hxx obj.cxx obj.hxx texload.c texload.h moved to
264 //   ../Objects
265 //
266 // Revision 1.13  1998/07/13 21:02:00  curt
267 // Wrote access functions for current fgOPTIONS.
268 //
269 // Revision 1.12  1998/07/12 03:18:29  curt
270 // Added ground collision detection.  This involved:
271 // - saving the entire vertex list for each tile with the tile records.
272 // - saving the face list for each fragment with the fragment records.
273 // - code to intersect the current vertical line with the proper face in
274 //   an efficient manner as possible.
275 // Fixed a bug where the tiles weren't being shifted to "near" (0,0,0)
276 //
277 // Revision 1.11  1998/07/04 00:54:30  curt
278 // Added automatic mipmap generation.
279 //
280 // When rendering fragments, use saved model view matrix from associated tile
281 // rather than recalculating it with push() translate() pop().
282 //
283 // Revision 1.10  1998/05/23 14:09:22  curt
284 // Added tile.cxx and tile.hxx.
285 // Working on rewriting the tile management system so a tile is just a list
286 // fragments, and the fragment record contains the display list for that fragment.
287 //
288 // Revision 1.9  1998/05/20 20:53:54  curt
289 // Moved global ref point and radius (bounding sphere info, and offset) to
290 // data file rather than calculating it on the fly.
291 // Fixed polygon winding problem in scenery generation stage rather than
292 // compensating for it on the fly.
293 // Made a fgTILECACHE class.
294 //
295 // Revision 1.8  1998/05/16 13:09:57  curt
296 // Beginning to add support for view frustum culling.
297 // Added some temporary code to calculate bouding radius, until the
298 //   scenery generation tools and scenery can be updated.
299 //
300 // Revision 1.7  1998/05/13 18:26:41  curt
301 // Root path info moved to fgOPTIONS.
302 //
303 // Revision 1.6  1998/05/02 01:52:17  curt
304 // Playing around with texture coordinates.
305 //
306 // Revision 1.5  1998/04/30 12:35:31  curt
307 // Added a command line rendering option specify smooth/flat shading.
308 //
309 // Revision 1.4  1998/04/28 01:21:43  curt
310 // Tweaked texture parameter calculations to keep the number smaller.  This
311 // avoids the "swimming" problem.
312 // Type-ified fgTIME and fgVIEW.
313 //
314 // Revision 1.3  1998/04/25 22:06:32  curt
315 // Edited cvs log messages in source files ... bad bad bad!
316 //
317 // Revision 1.2  1998/04/24 00:51:08  curt
318 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
319 // Tweaked the scenery file extentions to be "file.obj" (uncompressed)
320 // or "file.obz" (compressed.)
321 //
322 // Revision 1.1  1998/04/22 13:22:46  curt
323 // C++ - ifing the code a bit.
324 //
325 // Revision 1.11  1998/04/18 04:14:07  curt
326 // Moved fg_debug.c to it's own library.
327 //
328 // Revision 1.10  1998/04/14 02:23:17  curt
329 // Code reorganizations.  Added a Lib/ directory for more general libraries.
330 //
331 // Revision 1.9  1998/04/08 23:30:07  curt
332 // Adopted Gnu automake/autoconf system.
333 //
334 // Revision 1.8  1998/04/03 22:11:38  curt
335 // Converting to Gnu autoconf system.
336 //
337 // Revision 1.7  1998/02/01 03:39:55  curt
338 // Minor tweaks.
339 //
340 // Revision 1.6  1998/01/31 00:43:26  curt
341 // Added MetroWorks patches from Carmen Volpe.
342 //
343 // Revision 1.5  1998/01/29 00:51:39  curt
344 // First pass at tile cache, dynamic tile loading and tile unloading now works.
345 //
346 // Revision 1.4  1998/01/27 03:26:43  curt
347 // Playing with new fgPrintf command.
348 //
349 // Revision 1.3  1998/01/27 00:48:03  curt
350 // Incorporated Paul Bleisch's <pbleisch@acm.org> new debug message
351 // system and commandline/config file processing code.
352 //
353 // Revision 1.2  1998/01/26 15:55:24  curt
354 // Progressing on building dynamic scenery system.
355 //
356 // Revision 1.1  1998/01/24 00:03:29  curt
357 // Initial revision.
358
359
360