1998-05-20 20:53:53 +00:00
|
|
|
// tilecache.cxx -- routines to handle scenery tile caching
|
|
|
|
//
|
|
|
|
// Written by Curtis Olson, started January 1998.
|
|
|
|
//
|
|
|
|
// Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
//
|
|
|
|
// $Id$
|
1998-01-24 00:03:27 +00:00
|
|
|
|
|
|
|
|
1998-04-24 00:51:07 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include <config.h>
|
|
|
|
#endif
|
1998-04-03 22:09:02 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_WINDOWS_H
|
1998-01-24 00:03:27 +00:00
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <GL/glut.h>
|
|
|
|
#include <XGL/xgl.h>
|
|
|
|
|
1999-02-26 22:08:34 +00:00
|
|
|
#include <Debug/logstream.hxx>
|
1998-09-14 12:45:23 +00:00
|
|
|
#include <Airports/genapt.hxx>
|
1999-03-25 19:03:24 +00:00
|
|
|
// #include <Bucket/bucketutils.hxx>
|
1998-05-13 18:25:34 +00:00
|
|
|
#include <Main/options.hxx>
|
1998-04-22 13:21:26 +00:00
|
|
|
#include <Main/views.hxx>
|
1998-08-25 16:52:38 +00:00
|
|
|
#include <Objects/obj.hxx>
|
1998-04-22 13:21:26 +00:00
|
|
|
|
1998-07-12 03:18:27 +00:00
|
|
|
#include "tile.hxx"
|
1998-04-22 13:21:26 +00:00
|
|
|
#include "tilecache.hxx"
|
1998-01-24 00:03:27 +00:00
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// the tile cache
|
|
|
|
fgTILECACHE global_tile_cache;
|
1998-01-24 00:03:27 +00:00
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Constructor
|
|
|
|
fgTILECACHE::fgTILECACHE( void ) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Initialize the tile cache subsystem
|
1998-09-14 12:45:23 +00:00
|
|
|
void
|
|
|
|
fgTILECACHE::init( void )
|
|
|
|
{
|
1998-01-24 00:03:27 +00:00
|
|
|
int i;
|
|
|
|
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_INFO, "Initializing the tile cache." );
|
1998-01-24 00:03:27 +00:00
|
|
|
|
|
|
|
for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
|
1999-04-27 15:52:32 +00:00
|
|
|
if ( tile_cache[i].used ) {
|
|
|
|
entry_free(i);
|
|
|
|
tile_cache[i].tile_bucket.make_bad();
|
|
|
|
}
|
1998-01-24 00:03:27 +00:00
|
|
|
tile_cache[i].used = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Search for the specified "bucket" in the cache
|
1998-09-14 12:45:23 +00:00
|
|
|
int
|
1999-03-25 19:03:24 +00:00
|
|
|
fgTILECACHE::exists( const FGBucket& p )
|
1998-09-14 12:45:23 +00:00
|
|
|
{
|
1998-01-29 00:51:38 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
|
1998-11-09 23:40:46 +00:00
|
|
|
if ( tile_cache[i].tile_bucket == p ) {
|
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG,
|
|
|
|
"TILE EXISTS in cache ... index = " << i );
|
|
|
|
return( i );
|
1998-01-29 00:51:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return( -1 );
|
1998-01-24 00:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Fill in a tile cache entry with real data for the specified bucket
|
1998-09-14 12:45:23 +00:00
|
|
|
void
|
1999-03-25 19:03:24 +00:00
|
|
|
fgTILECACHE::fill_in( int index, FGBucket& p )
|
1998-09-14 12:45:23 +00:00
|
|
|
{
|
1998-11-09 23:40:46 +00:00
|
|
|
// Load the appropriate data file and build tile fragment list
|
|
|
|
string tile_path = current_options.get_fg_root() +
|
1999-03-25 19:03:24 +00:00
|
|
|
"/Scenery/" + p.gen_base_path() + "/" + p.gen_index_str();
|
1998-09-14 12:45:23 +00:00
|
|
|
|
1998-11-09 23:40:46 +00:00
|
|
|
tile_cache[index].used = true;
|
|
|
|
tile_cache[index].tile_bucket = p;
|
|
|
|
fgObjLoad( tile_path, &tile_cache[index] );
|
|
|
|
// tile_cache[ index ].ObjLoad( tile_path, p );
|
1998-09-14 12:45:23 +00:00
|
|
|
|
|
|
|
// cout << " ncount before = " << tile_cache[index].ncount << "\n";
|
|
|
|
// cout << " fragments before = " << tile_cache[index].fragment_list.size()
|
|
|
|
// << "\n";
|
|
|
|
|
1998-11-09 23:40:46 +00:00
|
|
|
string apt_path = tile_path + ".apt";
|
1998-09-14 12:45:23 +00:00
|
|
|
fgAptGenerate( apt_path, &tile_cache[index] );
|
|
|
|
|
|
|
|
// cout << " ncount after = " << tile_cache[index].ncount << "\n";
|
|
|
|
// cout << " fragments after = " << tile_cache[index].fragment_list.size()
|
|
|
|
// << "\n";
|
1998-01-24 00:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Free a tile cache entry
|
1998-09-14 12:45:23 +00:00
|
|
|
void
|
|
|
|
fgTILECACHE::entry_free( int index )
|
|
|
|
{
|
1998-11-09 23:40:46 +00:00
|
|
|
tile_cache[index].release_fragments();
|
1998-01-24 00:03:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Return index of next available slot in tile cache
|
1998-09-14 12:45:23 +00:00
|
|
|
int
|
|
|
|
fgTILECACHE::next_avail( void )
|
|
|
|
{
|
1998-12-09 18:50:12 +00:00
|
|
|
Point3D delta, abs_view_pos;
|
1998-01-29 00:51:38 +00:00
|
|
|
int i;
|
1998-10-16 00:51:46 +00:00
|
|
|
float max, med, min, tmp;
|
1998-01-29 00:51:38 +00:00
|
|
|
float dist, max_dist;
|
|
|
|
int max_index;
|
|
|
|
|
|
|
|
max_dist = 0.0;
|
|
|
|
max_index = 0;
|
|
|
|
|
|
|
|
for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) {
|
1998-11-09 23:40:46 +00:00
|
|
|
if ( ! tile_cache[i].used ) {
|
1998-01-29 00:51:38 +00:00
|
|
|
return(i);
|
|
|
|
} else {
|
1998-05-20 20:53:53 +00:00
|
|
|
// calculate approximate distance from view point
|
1998-12-09 18:50:12 +00:00
|
|
|
abs_view_pos = current_view.get_abs_view_pos();
|
|
|
|
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG,
|
1998-12-09 18:50:12 +00:00
|
|
|
"DIST Abs view pos = " << abs_view_pos );
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG,
|
1998-11-09 23:40:46 +00:00
|
|
|
" ref point = " << tile_cache[i].center );
|
1998-01-29 00:51:38 +00:00
|
|
|
|
1998-12-09 18:50:12 +00:00
|
|
|
delta.setx( fabs(tile_cache[i].center.x() - abs_view_pos.x() ) );
|
|
|
|
delta.sety( fabs(tile_cache[i].center.y() - abs_view_pos.y() ) );
|
|
|
|
delta.setz( fabs(tile_cache[i].center.z() - abs_view_pos.z() ) );
|
1998-01-29 00:51:38 +00:00
|
|
|
|
1998-10-16 00:51:46 +00:00
|
|
|
max = delta.x(); med = delta.y(); min = delta.z();
|
1998-01-29 00:51:38 +00:00
|
|
|
if ( max < med ) {
|
|
|
|
tmp = max; max = med; med = tmp;
|
|
|
|
}
|
|
|
|
if ( max < min ) {
|
|
|
|
tmp = max; max = min; min = tmp;
|
|
|
|
}
|
|
|
|
dist = max + (med + min) / 4;
|
|
|
|
|
1998-11-06 21:17:31 +00:00
|
|
|
FG_LOG( FG_TERRAIN, FG_DEBUG, " distance = " << dist );
|
1998-01-29 00:51:38 +00:00
|
|
|
|
|
|
|
if ( dist > max_dist ) {
|
|
|
|
max_dist = dist;
|
|
|
|
max_index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// If we made it this far, then there were no open cache entries.
|
|
|
|
// We will instead free the furthest cache entry and return it's
|
|
|
|
// index.
|
1998-01-29 00:51:38 +00:00
|
|
|
|
1998-09-14 12:45:23 +00:00
|
|
|
entry_free( max_index );
|
1998-01-29 00:51:38 +00:00
|
|
|
return( max_index );
|
1998-01-26 15:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-05-20 20:53:53 +00:00
|
|
|
// Destructor
|
|
|
|
fgTILECACHE::~fgTILECACHE( void ) {
|
|
|
|
}
|
|
|
|
|
|
|
|
|