]> git.mxchange.org Git - flightgear.git/blob - Simulator/Scenery/tilecache.cxx
Removed in-src cvs logs.
[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
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef HAVE_WINDOWS_H
29 #  include <windows.h>
30 #endif
31
32 #include <GL/glut.h>
33 #include <XGL/xgl.h>
34
35 #include <Debug/logstream.hxx>
36 #include <Airports/genapt.hxx>
37 // #include <Bucket/bucketutils.hxx>
38 #include <Main/options.hxx>
39 #include <Main/views.hxx>
40 #include <Objects/obj.hxx>
41
42 #include "tile.hxx"
43 #include "tilecache.hxx"
44
45
46 // the tile cache
47 fgTILECACHE global_tile_cache;
48
49
50 // Constructor
51 fgTILECACHE::fgTILECACHE( void ) {
52 }
53
54
55 // Initialize the tile cache subsystem
56 void
57 fgTILECACHE::init( void )
58 {
59     int i;
60
61     FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
62
63     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
64         tile_cache[i].used = 0;
65     }
66 }
67
68
69 // Search for the specified "bucket" in the cache
70 int
71 fgTILECACHE::exists( const FGBucket& p )
72 {
73     int i;
74
75     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
76         if ( tile_cache[i].tile_bucket == p ) {
77             FG_LOG( FG_TERRAIN, FG_DEBUG, 
78                     "TILE EXISTS in cache ... index = " << i );
79             return( i );
80         }
81     }
82     
83     return( -1 );
84 }
85
86
87 // Fill in a tile cache entry with real data for the specified bucket
88 void
89 fgTILECACHE::fill_in( int index, FGBucket& p )
90 {
91     // Load the appropriate data file and build tile fragment list
92     string tile_path = current_options.get_fg_root() +
93         "/Scenery/" + p.gen_base_path() + "/" + p.gen_index_str();
94
95     tile_cache[index].used = true;
96     tile_cache[index].tile_bucket = p;
97     fgObjLoad( tile_path, &tile_cache[index] );
98 //     tile_cache[ index ].ObjLoad( tile_path, p );
99
100     // cout << " ncount before = " << tile_cache[index].ncount << "\n";
101     // cout << " fragments before = " << tile_cache[index].fragment_list.size()
102     //      << "\n";
103
104     string apt_path = tile_path + ".apt";
105     fgAptGenerate( apt_path, &tile_cache[index] );
106
107     // cout << " ncount after = " << tile_cache[index].ncount << "\n";
108     // cout << " fragments after = " << tile_cache[index].fragment_list.size()
109     //      << "\n";
110 }
111
112
113 // Free a tile cache entry
114 void
115 fgTILECACHE::entry_free( int index )
116 {
117     tile_cache[index].release_fragments();
118 }
119
120
121 // Return index of next available slot in tile cache
122 int
123 fgTILECACHE::next_avail( void )
124 {
125     Point3D delta, abs_view_pos;
126     int i;
127     float max, med, min, tmp;
128     float dist, max_dist;
129     int max_index;
130     
131     max_dist = 0.0;
132     max_index = 0;
133
134     for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
135         if ( ! tile_cache[i].used ) {
136             return(i);
137         } else {
138             // calculate approximate distance from view point
139             abs_view_pos = current_view.get_abs_view_pos();
140
141             FG_LOG( FG_TERRAIN, FG_DEBUG,
142                     "DIST Abs view pos = " << abs_view_pos );
143             FG_LOG( FG_TERRAIN, FG_DEBUG,
144                     "    ref point = " << tile_cache[i].center );
145
146             delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
147             delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
148             delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
149
150             max = delta.x(); med = delta.y(); min = delta.z();
151             if ( max < med ) {
152                 tmp = max; max = med; med = tmp;
153             }
154             if ( max < min ) {
155                 tmp = max; max = min; min = tmp;
156             }
157             dist = max + (med + min) / 4;
158
159             FG_LOG( FG_TERRAIN, FG_DEBUG, "    distance = " << dist );
160
161             if ( dist > max_dist ) {
162                 max_dist = dist;
163                 max_index = i;
164             }
165         }
166     }
167
168     // If we made it this far, then there were no open cache entries.
169     // We will instead free the furthest cache entry and return it's
170     // index.
171     
172     entry_free( max_index );
173     return( max_index );
174 }
175
176
177 // Destructor
178 fgTILECACHE::~fgTILECACHE( void ) {
179 }
180
181