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