From 9dcdb982dc9e77c2ee96351a20e06808f694099c Mon Sep 17 00:00:00 2001 From: curt Date: Sat, 24 Jan 1998 00:03:27 +0000 Subject: [PATCH] Initial revision. --- Scenery/Makefile | 2 +- Scenery/bucketutils.c | 35 +++++++------ Scenery/bucketutils.h | 29 ++++++----- Scenery/tilecache.c | 114 ++++++++++++++++++++++++++++++++++++++++++ Scenery/tilecache.h | 80 +++++++++++++++++++++++++++++ Scenery/tilemgr.c | 105 +++++++++++++++++++++++--------------- Simulator/setup_env | 2 + 7 files changed, 297 insertions(+), 70 deletions(-) create mode 100644 Scenery/tilecache.c create mode 100644 Scenery/tilecache.h create mode 100644 Simulator/setup_env diff --git a/Scenery/Makefile b/Scenery/Makefile index fe6996e71..22abe6c73 100644 --- a/Scenery/Makefile +++ b/Scenery/Makefile @@ -28,7 +28,7 @@ ARLIBRARY = libScenery.a TARGETS = $(ARLIBRARY) CFILES = bucketutils.c common.c geometry.c mesh.c obj.c scenery.c texload.c \ - tilemgr.c + tilecache.c tilemgr.c CXXFILES = LDIRT = $(FG_ROOT_LIB)/$(ARLIBRARY) diff --git a/Scenery/bucketutils.c b/Scenery/bucketutils.c index b8c832daf..219318b35 100644 --- a/Scenery/bucketutils.c +++ b/Scenery/bucketutils.c @@ -1,5 +1,5 @@ /************************************************************************** - * tileutils.c -- support routines to handle dynamic management of scenery tiles + * bucketutils.c -- support routines to handle fgBUCKET operations * * Written by Curtis Olson, started January 1998. * @@ -27,7 +27,7 @@ #include #include -#include +#include #include @@ -44,7 +44,7 @@ 3 bits - to represent x (0 to 7) 3 bits - to represent y (0 to 7) */ -long int gen_index(struct bucket *p) { +long int fgBucketGenIndex(struct fgBUCKET *p) { long index = 0; index = ((p->lon + 180) << 14) + ((p->lat + 90) << 6) + (p->y << 3) + p->x; @@ -55,7 +55,7 @@ long int gen_index(struct bucket *p) { /* Parse a unique scenery tile index and find the lon, lat, x, and y */ -void parse_index(long int index, struct bucket *p) { +void fgBucketParseIndex(long int index, struct fgBUCKET *p) { p->lon = index >> 14; index -= p->lon << 14; p->lon -= 180; @@ -72,12 +72,12 @@ void parse_index(long int index, struct bucket *p) { /* Build a path name from an tile index */ -void gen_base_path(struct bucket *p, char *path) { +void fgBucketGenBasePath(struct fgBUCKET *p, char *path) { long int index; int top_lon, top_lat, main_lon, main_lat; char hem, pole; - index = gen_index(p); + index = fgBucketGenIndex(p); path[0] = '\0'; @@ -120,7 +120,7 @@ void gen_base_path(struct bucket *p, char *path) { /* offset an bucket struct by the specified amounts in the X & Y direction */ -void offset_bucket(struct bucket *in, struct bucket *out, int x, int y) { +void fgBucketOffset(struct fgBUCKET *in, struct fgBUCKET *out, int x, int y) { int diff, temp; int dist_lat; @@ -176,7 +176,7 @@ void offset_bucket(struct bucket *in, struct bucket *out, int x, int y) { /* Given a lat/lon, find the "bucket" or tile that it falls within */ -void find_bucket(double lon, double lat, struct bucket *p) { +void fgBucketFind(double lon, double lat, struct fgBUCKET *p) { double diff; diff = lon - (double)(int)lon; @@ -205,20 +205,20 @@ void find_bucket(double lon, double lat, struct bucket *p) { /* Given a lat/lon, fill in the local tile index array */ -void gen_idx_array(struct bucket *p1, struct bucket *tiles, - int width, int height) { - struct bucket *p2; +void fgBucketGenIdxArray(struct fgBUCKET *p1, struct fgBUCKET *tiles, + int width, int height) { + struct fgBUCKET *p2; int dw, dh, i, j; dh = height / 2; dw = width / 2; for ( j = 0; j < height; j++ ) { for ( i = 0; i < width; i++ ) { - offset_bucket(p1, &tiles[(j*width)+i], i - dw, j - dh); + fgBucketOffset(p1, &tiles[(j*width)+i], i - dw, j - dh); p2 = &tiles[(j*width)+i]; printf(" bucket = %d %d %d %d index = %ld\n", p2->lon, p2->lat, p2->x, p2->y, - gen_index(&tiles[(j*width)+i])); + fgBucketGenIndex(&tiles[(j*width)+i])); } } } @@ -226,7 +226,7 @@ void gen_idx_array(struct bucket *p1, struct bucket *tiles, /* sample main for testing int main() { - struct bucket p1; + struct fgBUCKET p1; long int tile[49]; char path[256]; double lon, lat; @@ -266,9 +266,12 @@ int main() { /* $Log$ -/* Revision 1.1 1998/01/23 20:06:51 curt -/* tileutils.* renamed to bucketutils.* +/* Revision 1.2 1998/01/24 00:03:28 curt +/* Initial revision. /* + * Revision 1.1 1998/01/23 20:06:51 curt + * tileutils.* renamed to bucketutils.* + * * Revision 1.6 1998/01/19 19:27:18 curt * Merged in make system changes from Bob Kuehne * This should simplify things tremendously. diff --git a/Scenery/bucketutils.h b/Scenery/bucketutils.h index 1f71d5978..e9dd2b3bf 100644 --- a/Scenery/bucketutils.h +++ b/Scenery/bucketutils.h @@ -1,5 +1,5 @@ /************************************************************************** - * tileutils.h -- support routines to handle dynamic management of scenery tiles + * bucketutils.h -- support routines to handle fgBUCKET operations * * Written by Curtis Olson, started January 1998. * @@ -24,11 +24,11 @@ **************************************************************************/ -#ifndef _TILEUTILS_H -#define _TILEUTILS_H +#ifndef _BUCKETUTILS_H +#define _BUCKETUTILS_H -struct bucket { +struct fgBUCKET { int lon; /* longitude (-180 to 179) */ int lat; /* latitude (-90 to 89) */ int x; /* x (0 to 7) */ @@ -49,37 +49,40 @@ struct bucket { 3 bits - to represent x (0 to 7) 3 bits - to represent y (0 to 7) */ -long int gen_index(struct bucket *p); +long int fgBucketGenIndex(struct fgBUCKET *p); /* Parse a unique scenery tile index and find the lon, lat, x, and y */ -void parse_index(long int index, struct bucket *p); +void fgBucketParseIndex(long int index, struct fgBUCKET *p); /* Build a path name from an tile index */ -void gen_base_path(struct bucket *p, char *path); +void fgBucketGenBasePath(struct fgBUCKET *p, char *path); /* offset an bucket struct by the specified amounts in the X & Y direction */ -void offset_bucket(struct bucket *in, struct bucket *out, int x, int y); +void fgBucketOffset(struct fgBUCKET *in, struct fgBUCKET *out, int x, int y); /* Given a lat/lon, find the "bucket" or tile that it falls within */ -void find_bucket(double lon, double lat, struct bucket *p); +void fgBucketFind(double lon, double lat, struct fgBUCKET *p); /* Given a lat/lon, fill in the local tile index array */ -void gen_idx_array(struct bucket *p1, struct bucket *tiles, +void fgBucketGenIdxArray(struct fgBUCKET *p1, struct fgBUCKET *tiles, int width, int height); -#endif /* _TILEUTILS_H */ +#endif /* _BUCKETUTILS_H */ /* $Log$ -/* Revision 1.1 1998/01/23 20:06:52 curt -/* tileutils.* renamed to bucketutils.* +/* Revision 1.2 1998/01/24 00:03:28 curt +/* Initial revision. /* + * Revision 1.1 1998/01/23 20:06:52 curt + * tileutils.* renamed to bucketutils.* + * * Revision 1.6 1998/01/22 02:59:42 curt * Changed #ifdef FILE_H to #ifdef _FILE_H * diff --git a/Scenery/tilecache.c b/Scenery/tilecache.c new file mode 100644 index 000000000..9e958d858 --- /dev/null +++ b/Scenery/tilecache.c @@ -0,0 +1,114 @@ +/************************************************************************** + * tilecache.c -- routines to hancle 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$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifdef WIN32 +# include +#endif + +#include +#include + +#include +#include +#include + +#include + +/* tile cache */ +struct fgTILE tile_cache[FG_TILE_CACHE_SIZE]; + + +/* Initialize the tile cache subsystem */ +void fgTileCacheInit( void ) { + int i; + + printf("Initializing the tile cache.\n"); + + for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) { + tile_cache[i].used = 0; + } +} + + +/* Return index of next available slot in tile cache */ +int fgTileCacheNextAvail( void ) { + int i; + + for ( i = 0; i < FG_TILE_CACHE_SIZE; i++ ) { + if ( tile_cache[i].used == 0 ) { + return(i); + } + } + + return(-1); +} + + +/* Fill in a tile cache entry with real data for the specified bucket */ +void fgTileCacheEntryFillIn( int index, struct fgBUCKET *p ) { + struct fgGENERAL *g; + char base_path[256]; + char file_name[256]; + + g = &general; + + /* Mark this cache entry as used */ + tile_cache[index].used = 1; + + /* Update the bucket */ + tile_cache[index].tile_bucket.lon = p->lon; + tile_cache[index].tile_bucket.lat = p->lat; + tile_cache[index].tile_bucket.x = p->x; + tile_cache[index].tile_bucket.y = p->y; + + /* Load the appropriate area and get the display list pointer */ + fgBucketGenBasePath(p, base_path); + sprintf(file_name, "%s/Scenery/%s/%ld.obj", g->root_dir, + base_path, fgBucketGenIndex(p)); + tile_cache[index].display_list = + fgObjLoad(file_name, &tile_cache[index].local_ref); +} + + +/* Return info for a tile cache entry */ +void fgTileCacheEntryInfo( int index, GLint *display_list, + struct fgCartesianPoint *local_ref ) { + *display_list = tile_cache[index].display_list; + /* printf("Display list = %d\n", *display_list); */ + + local_ref->x = tile_cache[index].local_ref.x; + local_ref->y = tile_cache[index].local_ref.y; + local_ref->z = tile_cache[index].local_ref.z; +} + + +/* $Log$ +/* Revision 1.1 1998/01/24 00:03:29 curt +/* Initial revision. +/* + */ + + diff --git a/Scenery/tilecache.h b/Scenery/tilecache.h new file mode 100644 index 000000000..47f201c65 --- /dev/null +++ b/Scenery/tilecache.h @@ -0,0 +1,80 @@ +/************************************************************************** + * tilecache.h -- routines to hancle 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$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifndef _TILECACHE_H +#define _TILECACHE_H + + +#ifdef WIN32 +# include +#endif + +#include +#include + +#include +#include + + +#define FG_TILE_CACHE_SIZE 100 /* Must be > FG_LOCAL_X_Y */ + + +/* Tile cache record */ +struct fgTILE { + struct fgBUCKET tile_bucket; + GLint display_list; + struct fgCartesianPoint local_ref; + int used; +}; + +/* tile cache */ +extern struct fgTILE tile_cache[FG_TILE_CACHE_SIZE]; + + +/* Initialize the tile cache subsystem */ +void fgTileCacheInit( void ); + +/* Return index of next available slot in tile cache */ +int fgTileCacheNextAvail( void ); + +/* Fill in a tile cache entry with real data for the specified bucket */ +void fgTileCacheEntryFillIn( int index, struct fgBUCKET *p ); + +/* Return info for a tile cache entry */ +void fgTileCacheEntryInfo( int index, GLint *display_list, + struct fgCartesianPoint *local_ref ); + + +#endif /* _TILECACHE_H */ + + +/* $Log$ +/* Revision 1.1 1998/01/24 00:03:29 curt +/* Initial revision. +/* + */ + + diff --git a/Scenery/tilemgr.c b/Scenery/tilemgr.c index e37db89bb..e80b77491 100644 --- a/Scenery/tilemgr.c +++ b/Scenery/tilemgr.c @@ -32,25 +32,34 @@ #include #include -#include +#include #include +#include #include #include -#include #include -/* here's where we keep the array of closest (potentially viewable) tiles */ -struct bucket local_tiles[49]; -GLint local_display_lists[49]; -struct fgCartesianPoint local_refs[49]; +#define FG_LOCAL_X 3 /* should be odd */ +#define FG_LOCAL_Y 3 /* should be odd */ +#define FG_LOCAL_X_Y 9 /* At least FG_LOCAL_X times FG_LOCAL_Y */ + +#define FG_TILE_CACHE_SIZE 100 /* Must be > FG_LOCAL_X_Y */ + + +/* closest (potentially viewable) tiles, centered on current tile. + * This is an array of pointers to cache indexes. */ +int tiles[FG_LOCAL_X_Y]; + +/* tile cache */ +struct fgTILE tile_cache[FG_TILE_CACHE_SIZE]; /* Initialize the Tile Manager subsystem */ void fgTileMgrInit( void ) { printf("Initializing Tile Manager subsystem.\n"); - /* fgTileCacheInit(); */ + fgTileCacheInit(); } @@ -58,40 +67,46 @@ void fgTileMgrInit( void ) { * the chunk isn't already in the cache, then read it from disk. */ void fgTileMgrUpdate( void ) { struct fgFLIGHT *f; - struct fgGENERAL *g; - struct bucket p; - struct bucket p_last = {-1000, 0, 0, 0}; - char base_path[256]; - char file_name[256]; - int i, j; + struct fgBUCKET p1, p2; + static struct fgBUCKET p_last = {-1000, 0, 0, 0}; + int i, j, dw, dh; + int index; f = ¤t_aircraft.flight; - g = &general; - find_bucket(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p); - printf("Updating Tile list for %d,%d %d,%d\n", p.lon, p.lat, p.x, p.y); + fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1); - if ( (p.lon == p_last.lon) && (p.lat == p_last.lat) && - (p.x == p_last.x) && (p.y == p_last.y) ) { + if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) && + (p1.x == p_last.x) && (p1.y == p_last.y) ) { /* same bucket as last time */ + printf("Same bucket as last time\n"); + return; } - gen_idx_array(&p, local_tiles, 7, 7); + if ( p_last.lon == -1000 ) { + printf("First time through ... \n"); + printf("Updating Tile list for %d,%d %d,%d\n", + p1.lon, p1.lat, p1.x, p1.y); - /* scenery.center = ref; */ + /* wipe tile cache */ + fgTileCacheInit(); - for ( i = 0; i < 49; i++ ) { - gen_base_path(&local_tiles[i], base_path); - sprintf(file_name, "%s/Scenery/%s/%ld.obj", - g->root_dir, base_path, gen_index(&local_tiles[i])); - local_display_lists[i] = - fgObjLoad(file_name, &local_refs[i]); + /* build the local area list and update cache */ + dw = FG_LOCAL_X / 2; + dh = FG_LOCAL_Y / 2; - if ( (local_tiles[i].lon == p.lon) && - (local_tiles[i].lat == p.lat) && - (local_tiles[i].x == p.x) && - (local_tiles[i].y == p.y) ) { - scenery.center = local_refs[i]; + for ( j = 0; j < FG_LOCAL_Y; j++ ) { + for ( i = 0; i < FG_LOCAL_X; i++ ) { + fgBucketOffset(&p1, &p2, i - dw, j - dh); + printf("Updating for bucket %d %d %d %d\n", + p2.lon, p2.lat, p2.x, p2.y); + + index = fgTileCacheNextAvail(); + printf("Selected cache index of %d\n", index); + + tiles[(j*FG_LOCAL_Y) + i] = index; + fgTileCacheEntryFillIn(index, &p2); + } } } } @@ -102,7 +117,10 @@ void fgTileMgrRender( void ) { static GLfloat terrain_color[4] = { 0.6, 0.8, 0.4, 1.0 }; static GLfloat terrain_ambient[4]; static GLfloat terrain_diffuse[4]; - int i, j; + struct fgCartesianPoint local_ref; + GLint display_list; + int i; + int index; for ( i = 0; i < 4; i++ ) { terrain_ambient[i] = terrain_color[i] * 0.5; @@ -112,22 +130,29 @@ void fgTileMgrRender( void ) { xglMaterialfv(GL_FRONT, GL_AMBIENT, terrain_ambient); xglMaterialfv(GL_FRONT, GL_DIFFUSE, terrain_diffuse); - for ( i = 0; i < 49; i++ ) { + for ( i = 0; i < FG_LOCAL_X_Y; i++ ) { + index = tiles[i]; + /* printf("Index = %d\n", index); */ + fgTileCacheEntryInfo(index, &display_list, &local_ref ); + xglPushMatrix(); - xglTranslatef(local_refs[i].x - scenery.center.x, - local_refs[i].y - scenery.center.y, - local_refs[i].z - scenery.center.z); - xglCallList(local_display_lists[i]); + xglTranslatef(local_ref.x - scenery.center.x, + local_ref.y - scenery.center.y, + local_ref.z - scenery.center.z); + xglCallList(display_list); xglPopMatrix(); } } /* $Log$ -/* Revision 1.5 1998/01/19 19:27:18 curt -/* Merged in make system changes from Bob Kuehne -/* This should simplify things tremendously. +/* Revision 1.6 1998/01/24 00:03:30 curt +/* Initial revision. /* + * Revision 1.5 1998/01/19 19:27:18 curt + * Merged in make system changes from Bob Kuehne + * This should simplify things tremendously. + * * Revision 1.4 1998/01/19 18:40:38 curt * Tons of little changes to clean up the code and to remove fatal errors * when building with the c++ compiler. diff --git a/Simulator/setup_env b/Simulator/setup_env new file mode 100644 index 000000000..48801ace2 --- /dev/null +++ b/Simulator/setup_env @@ -0,0 +1,2 @@ +export FG_ROOT_SRC=~/projects/FlightGear/Src +export FG_ROOT_LIB=~/projects/FlightGear/Lib -- 2.39.2