1998-01-07 23:50:01 +00:00
|
|
|
/**************************************************************************
|
|
|
|
* tilemgr.c -- routines to handle dynamic management of scenery tiles
|
|
|
|
*
|
|
|
|
* 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 <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <GL/glut.h>
|
1998-01-19 19:26:51 +00:00
|
|
|
#include <XGL/xgl.h>
|
1998-01-07 23:50:01 +00:00
|
|
|
|
1998-01-19 19:26:51 +00:00
|
|
|
#include <Scenery/scenery.h>
|
1998-01-24 00:03:27 +00:00
|
|
|
#include <Scenery/bucketutils.h>
|
1998-01-19 19:26:51 +00:00
|
|
|
#include <Scenery/obj.h>
|
1998-01-24 00:03:27 +00:00
|
|
|
#include <Scenery/tilecache.h>
|
1998-01-07 23:50:01 +00:00
|
|
|
|
1998-01-19 19:26:51 +00:00
|
|
|
#include <Aircraft/aircraft.h>
|
1998-01-27 00:47:41 +00:00
|
|
|
#include <Include/fg_constants.h>
|
|
|
|
#include <Include/fg_types.h>
|
1998-01-27 03:26:41 +00:00
|
|
|
#include <Main/fg_debug.h>
|
1998-01-07 23:50:01 +00:00
|
|
|
|
|
|
|
|
1998-01-24 00:03:27 +00:00
|
|
|
#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 */
|
|
|
|
|
|
|
|
|
|
|
|
/* closest (potentially viewable) tiles, centered on current tile.
|
|
|
|
* This is an array of pointers to cache indexes. */
|
|
|
|
int tiles[FG_LOCAL_X_Y];
|
|
|
|
|
1998-01-07 23:50:01 +00:00
|
|
|
|
|
|
|
/* Initialize the Tile Manager subsystem */
|
1998-01-19 18:40:15 +00:00
|
|
|
void fgTileMgrInit( void ) {
|
1998-01-27 03:26:41 +00:00
|
|
|
fgPrintf( FG_TERRAIN, FG_INFO, "Initializing Tile Manager subsystem.\n");
|
1998-01-07 23:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-26 15:55:24 +00:00
|
|
|
/* load a tile */
|
|
|
|
void fgTileMgrLoadTile( struct fgBUCKET *p, int *index) {
|
1998-01-27 03:26:41 +00:00
|
|
|
fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating for bucket %d %d %d %d\n",
|
1998-01-26 15:55:24 +00:00
|
|
|
p->lon, p->lat, p->x, p->y);
|
|
|
|
|
1998-01-29 00:51:38 +00:00
|
|
|
/* if not in cache, load tile into the next available slot */
|
|
|
|
if ( (*index = fgTileCacheExists(p)) < 0 ) {
|
|
|
|
*index = fgTileCacheNextAvail();
|
|
|
|
fgTileCacheEntryFillIn(*index, p);
|
|
|
|
}
|
|
|
|
|
1998-01-27 03:26:41 +00:00
|
|
|
fgPrintf( FG_TERRAIN, FG_DEBUG, "Selected cache index of %d\n", *index);
|
1998-01-26 15:55:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-07 23:50:01 +00:00
|
|
|
/* given the current lon/lat, fill in the array of local chunks. If
|
|
|
|
* the chunk isn't already in the cache, then read it from disk. */
|
1998-01-19 18:40:15 +00:00
|
|
|
void fgTileMgrUpdate( void ) {
|
1998-02-07 15:29:31 +00:00
|
|
|
fgFLIGHT *f;
|
1998-01-24 00:03:27 +00:00
|
|
|
struct fgBUCKET p1, p2;
|
|
|
|
static struct fgBUCKET p_last = {-1000, 0, 0, 0};
|
|
|
|
int i, j, dw, dh;
|
1998-01-07 23:50:01 +00:00
|
|
|
|
1998-02-07 15:29:31 +00:00
|
|
|
f = current_aircraft.flight;
|
1998-01-07 23:50:01 +00:00
|
|
|
|
1998-01-24 00:03:27 +00:00
|
|
|
fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1);
|
1998-01-26 15:55:24 +00:00
|
|
|
dw = FG_LOCAL_X / 2;
|
|
|
|
dh = FG_LOCAL_Y / 2;
|
1998-01-08 02:22:26 +00:00
|
|
|
|
1998-01-24 00:03:27 +00:00
|
|
|
if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
|
|
|
|
(p1.x == p_last.x) && (p1.y == p_last.y) ) {
|
1998-01-19 18:40:15 +00:00
|
|
|
/* same bucket as last time */
|
1998-01-27 03:26:41 +00:00
|
|
|
fgPrintf( FG_TERRAIN, FG_DEBUG, "Same bucket as last time\n");
|
1998-01-26 15:55:24 +00:00
|
|
|
} else if ( p_last.lon == -1000 ) {
|
|
|
|
/* First time through, initialize the system and load all
|
|
|
|
* relavant tiles */
|
1998-01-19 18:40:15 +00:00
|
|
|
|
1998-01-27 03:26:41 +00:00
|
|
|
fgPrintf( FG_TERRAIN, FG_DEBUG, "First time through ... \n");
|
|
|
|
fgPrintf( FG_TERRAIN, FG_DEBUG, "Updating Tile list for %d,%d %d,%d\n",
|
1998-01-24 00:03:27 +00:00
|
|
|
p1.lon, p1.lat, p1.x, p1.y);
|
1998-01-13 00:23:08 +00:00
|
|
|
|
1998-01-24 00:03:27 +00:00
|
|
|
/* wipe tile cache */
|
|
|
|
fgTileCacheInit();
|
1998-01-13 00:23:08 +00:00
|
|
|
|
1998-01-24 00:03:27 +00:00
|
|
|
/* build the local area list and update cache */
|
|
|
|
for ( j = 0; j < FG_LOCAL_Y; j++ ) {
|
|
|
|
for ( i = 0; i < FG_LOCAL_X; i++ ) {
|
|
|
|
fgBucketOffset(&p1, &p2, i - dw, j - dh);
|
1998-01-26 15:55:24 +00:00
|
|
|
fgTileMgrLoadTile(&p2, &tiles[(j*FG_LOCAL_Y) + i]);
|
1998-01-24 00:03:27 +00:00
|
|
|
}
|
1998-01-13 00:23:08 +00:00
|
|
|
}
|
1998-01-26 15:55:24 +00:00
|
|
|
} else {
|
|
|
|
/* We've moved to a new bucket, we need to scroll our
|
|
|
|
* structures, and load in the new tiles */
|
|
|
|
|
|
|
|
/* CURRENTLY THIS ASSUMES WE CAN ONLY MOVE TO ADJACENT TILES.
|
|
|
|
AT ULTRA HIGH SPEEDS THIS ASSUMPTION MAY NOT BE VALID IF
|
|
|
|
THE AIRCRAFT CAN SKIP A TILE IN A SINGLE ITERATION. */
|
|
|
|
|
1998-01-27 00:47:41 +00:00
|
|
|
if ( (p1.lon > p_last.lon) ||
|
|
|
|
( (p1.lon == p_last.lon) && (p1.x > p_last.x) ) ) {
|
1998-01-26 15:55:24 +00:00
|
|
|
for ( j = 0; j < FG_LOCAL_Y; j++ ) {
|
|
|
|
/* scrolling East */
|
|
|
|
for ( i = 0; i < FG_LOCAL_X - 1; i++ ) {
|
|
|
|
tiles[(j*FG_LOCAL_Y) + i] = tiles[(j*FG_LOCAL_Y) + i + 1];
|
|
|
|
}
|
|
|
|
/* load in new column */
|
|
|
|
fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
|
|
|
|
fgTileMgrLoadTile(&p2, &tiles[(j*FG_LOCAL_Y) + FG_LOCAL_X - 1]);
|
|
|
|
}
|
1998-01-27 00:47:41 +00:00
|
|
|
} else if ( (p1.lon < p_last.lon) ||
|
|
|
|
( (p1.lon == p_last.lon) && (p1.x < p_last.x) ) ) {
|
1998-01-26 15:55:24 +00:00
|
|
|
for ( j = 0; j < FG_LOCAL_Y; j++ ) {
|
|
|
|
/* scrolling West */
|
|
|
|
for ( i = FG_LOCAL_X - 1; i > 0; i-- ) {
|
|
|
|
tiles[(j*FG_LOCAL_Y) + i] = tiles[(j*FG_LOCAL_Y) + i - 1];
|
|
|
|
}
|
|
|
|
/* load in new column */
|
|
|
|
fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
|
|
|
|
fgTileMgrLoadTile(&p2, &tiles[(j*FG_LOCAL_Y) + 0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-27 00:47:41 +00:00
|
|
|
if ( (p1.lat > p_last.lat) ||
|
|
|
|
( (p1.lat == p_last.lat) && (p1.y > p_last.y) ) ) {
|
1998-01-26 15:55:24 +00:00
|
|
|
for ( i = 0; i < FG_LOCAL_X; i++ ) {
|
|
|
|
/* scrolling North */
|
|
|
|
for ( j = 0; j < FG_LOCAL_Y - 1; j++ ) {
|
|
|
|
tiles[(j * FG_LOCAL_Y) + i] =
|
|
|
|
tiles[((j+1) * FG_LOCAL_Y) + i];
|
|
|
|
}
|
|
|
|
/* load in new column */
|
|
|
|
fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
|
|
|
|
fgTileMgrLoadTile(&p2,
|
|
|
|
&tiles[((FG_LOCAL_Y-1)*FG_LOCAL_Y) + i]);
|
|
|
|
}
|
1998-01-27 00:47:41 +00:00
|
|
|
} else if ( (p1.lat < p_last.lat) ||
|
|
|
|
( (p1.lat == p_last.lat) && (p1.y < p_last.y) ) ) {
|
1998-01-26 15:55:24 +00:00
|
|
|
for ( i = 0; i < FG_LOCAL_X; i++ ) {
|
|
|
|
/* scrolling South */
|
|
|
|
for ( j = FG_LOCAL_Y - 1; j > 0; j-- ) {
|
|
|
|
tiles[(j * FG_LOCAL_Y) + i] =
|
|
|
|
tiles[((j-1) * FG_LOCAL_Y) + i];
|
|
|
|
}
|
|
|
|
/* load in new column */
|
|
|
|
fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
|
|
|
|
fgTileMgrLoadTile(&p2, &tiles[0 + i]);
|
|
|
|
}
|
|
|
|
}
|
1998-01-13 00:23:08 +00:00
|
|
|
}
|
1998-01-26 15:55:24 +00:00
|
|
|
p_last.lon = p1.lon;
|
|
|
|
p_last.lat = p1.lat;
|
|
|
|
p_last.x = p1.x;
|
|
|
|
p_last.y = p1.y;
|
1998-01-13 00:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-01-26 15:55:24 +00:00
|
|
|
/* Render the local tiles */
|
1998-01-19 18:40:15 +00:00
|
|
|
void fgTileMgrRender( void ) {
|
1998-01-13 00:23:08 +00:00
|
|
|
static GLfloat terrain_color[4] = { 0.6, 0.8, 0.4, 1.0 };
|
|
|
|
static GLfloat terrain_ambient[4];
|
|
|
|
static GLfloat terrain_diffuse[4];
|
1998-01-24 00:03:27 +00:00
|
|
|
struct fgCartesianPoint local_ref;
|
|
|
|
GLint display_list;
|
|
|
|
int i;
|
|
|
|
int index;
|
1998-01-13 00:23:08 +00:00
|
|
|
|
|
|
|
for ( i = 0; i < 4; i++ ) {
|
|
|
|
terrain_ambient[i] = terrain_color[i] * 0.5;
|
|
|
|
terrain_diffuse[i] = terrain_color[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
xglMaterialfv(GL_FRONT, GL_AMBIENT, terrain_ambient);
|
|
|
|
xglMaterialfv(GL_FRONT, GL_DIFFUSE, terrain_diffuse);
|
|
|
|
|
1998-01-24 00:03:27 +00:00
|
|
|
for ( i = 0; i < FG_LOCAL_X_Y; i++ ) {
|
|
|
|
index = tiles[i];
|
1998-01-27 03:26:41 +00:00
|
|
|
/* fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index); */
|
1998-01-24 00:03:27 +00:00
|
|
|
fgTileCacheEntryInfo(index, &display_list, &local_ref );
|
|
|
|
|
1998-02-01 03:39:53 +00:00
|
|
|
if ( display_list >= 0 ) {
|
|
|
|
xglPushMatrix();
|
|
|
|
xglTranslatef(local_ref.x - scenery.center.x,
|
|
|
|
local_ref.y - scenery.center.y,
|
|
|
|
local_ref.z - scenery.center.z);
|
|
|
|
xglCallList(display_list);
|
|
|
|
xglPopMatrix();
|
|
|
|
}
|
1998-01-13 00:23:08 +00:00
|
|
|
}
|
1998-01-07 23:50:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* $Log$
|
1998-02-07 15:29:31 +00:00
|
|
|
/* Revision 1.13 1998/02/07 15:29:46 curt
|
|
|
|
/* Incorporated HUD changes and struct/typedef changes from Charlie Hotchkiss
|
|
|
|
/* <chotchkiss@namg.us.anritsu.com>
|
1998-01-07 23:50:01 +00:00
|
|
|
/*
|
1998-02-07 15:29:31 +00:00
|
|
|
* Revision 1.12 1998/02/01 03:39:55 curt
|
|
|
|
* Minor tweaks.
|
|
|
|
*
|
1998-02-01 03:39:53 +00:00
|
|
|
* Revision 1.11 1998/01/31 00:43:27 curt
|
|
|
|
* Added MetroWorks patches from Carmen Volpe.
|
|
|
|
*
|
1998-01-31 00:42:57 +00:00
|
|
|
* Revision 1.10 1998/01/29 00:51:40 curt
|
|
|
|
* First pass at tile cache, dynamic tile loading and tile unloading now works.
|
|
|
|
*
|
1998-01-29 00:51:38 +00:00
|
|
|
* Revision 1.9 1998/01/27 03:26:44 curt
|
|
|
|
* Playing with new fgPrintf command.
|
|
|
|
*
|
1998-01-27 03:26:41 +00:00
|
|
|
* Revision 1.8 1998/01/27 00:48:04 curt
|
|
|
|
* Incorporated Paul Bleisch's <bleisch@chromatic.com> new debug message
|
|
|
|
* system and commandline/config file processing code.
|
|
|
|
*
|
1998-01-27 00:47:41 +00:00
|
|
|
* Revision 1.7 1998/01/26 15:55:25 curt
|
|
|
|
* Progressing on building dynamic scenery system.
|
|
|
|
*
|
1998-01-26 15:55:24 +00:00
|
|
|
* Revision 1.6 1998/01/24 00:03:30 curt
|
|
|
|
* Initial revision.
|
|
|
|
*
|
1998-01-24 00:03:27 +00:00
|
|
|
* Revision 1.5 1998/01/19 19:27:18 curt
|
|
|
|
* Merged in make system changes from Bob Kuehne <rpk@sgi.com>
|
|
|
|
* This should simplify things tremendously.
|
|
|
|
*
|
1998-01-19 19:26:51 +00:00
|
|
|
* 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.
|
|
|
|
*
|
1998-01-19 18:40:15 +00:00
|
|
|
* Revision 1.3 1998/01/13 00:23:11 curt
|
|
|
|
* Initial changes to support loading and management of scenery tiles. Note,
|
|
|
|
* there's still a fair amount of work left to be done.
|
|
|
|
*
|
1998-01-13 00:23:08 +00:00
|
|
|
* Revision 1.2 1998/01/08 02:22:27 curt
|
|
|
|
* Continue working on basic features.
|
|
|
|
*
|
1998-01-08 02:22:26 +00:00
|
|
|
* Revision 1.1 1998/01/07 23:50:51 curt
|
|
|
|
* "area" renamed to "tile"
|
|
|
|
*
|
1998-01-07 23:50:01 +00:00
|
|
|
* Revision 1.2 1998/01/07 03:29:29 curt
|
|
|
|
* Given an arbitrary lat/lon, we can now:
|
|
|
|
* generate a unique index for the chunk containing the lat/lon
|
|
|
|
* generate a path name to the chunk file
|
|
|
|
* build a list of the indexes of all the nearby areas.
|
|
|
|
*
|
|
|
|
* Revision 1.1 1998/01/07 02:05:48 curt
|
|
|
|
* Initial revision.
|
|
|
|
* */
|
|
|
|
|
|
|
|
|