#include <Include/fg_types.h>
#include <Main/options.hxx>
#include <Main/views.hxx>
+#include <Math/fg_geodesy.h>
#include <Math/mat3.h>
+#include <Math/polar3d.hxx>
+#include <Math/vector.hxx>
+#include <Weather/weather.h>
#include "material.hxx"
#include "obj.hxx"
#define FG_LOCAL_X_Y 81 // max(o->tile_diameter) ** 2
+#define FG_SQUARE( X ) ( (X) * (X) )
+
// closest (potentially viewable) tiles, centered on current tile.
// This is an array of pointers to cache indexes.
// load a tile
-void fgTileMgrLoadTile( struct fgBUCKET *p, int *index) {
+void fgTileMgrLoadTile( fgBUCKET *p, int *index) {
fgTILECACHE *c;
c = &global_tile_cache;
int fgTileMgrUpdate( void ) {
fgTILECACHE *c;
fgFLIGHT *f;
- fgOPTIONS *o;
- struct fgBUCKET p1, p2;
- static struct fgBUCKET p_last = {-1000, 0, 0, 0};
+ fgBUCKET p1, p2;
+ static fgBUCKET p_last = {-1000, 0, 0, 0};
+ int tile_diameter;
int i, j, dw, dh;
c = &global_tile_cache;
f = current_aircraft.flight;
- o = ¤t_options;
+
+ tile_diameter = current_options.get_tile_diameter();
fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p1);
- dw = o->tile_diameter / 2;
- dh = o->tile_diameter / 2;
+ dw = tile_diameter / 2;
+ dh = tile_diameter / 2;
if ( (p1.lon == p_last.lon) && (p1.lat == p_last.lat) &&
(p1.x == p_last.x) && (p1.y == p_last.y) ) {
fgPrintf( FG_TERRAIN, FG_INFO, " Updating Tile list for %d,%d %d,%d\n",
p1.lon, p1.lat, p1.x, p1.y);
fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
- o->tile_diameter * o->tile_diameter);
+ tile_diameter * tile_diameter);
// wipe/initialize tile cache
c->Init();
// build the local area list and update cache
- for ( j = 0; j < o->tile_diameter; j++ ) {
- for ( i = 0; i < o->tile_diameter; i++ ) {
+ for ( j = 0; j < tile_diameter; j++ ) {
+ for ( i = 0; i < tile_diameter; i++ ) {
fgBucketOffset(&p1, &p2, i - dw, j - dh);
- fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + i]);
+ fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) + i]);
}
}
} else {
if ( (p1.lon > p_last.lon) ||
( (p1.lon == p_last.lon) && (p1.x > p_last.x) ) ) {
fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
- o->tile_diameter);
- for ( j = 0; j < o->tile_diameter; j++ ) {
+ tile_diameter);
+ for ( j = 0; j < tile_diameter; j++ ) {
// scrolling East
- for ( i = 0; i < o->tile_diameter - 1; i++ ) {
- tiles[(j*o->tile_diameter) + i] =
- tiles[(j*o->tile_diameter) + i + 1];
+ for ( i = 0; i < tile_diameter - 1; i++ ) {
+ tiles[(j*tile_diameter) + i] =
+ tiles[(j*tile_diameter) + i + 1];
}
// load in new column
fgBucketOffset(&p_last, &p2, dw + 1, j - dh);
- fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) +
- o->tile_diameter - 1]);
+ fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) +
+ tile_diameter - 1]);
}
} else if ( (p1.lon < p_last.lon) ||
( (p1.lon == p_last.lon) && (p1.x < p_last.x) ) ) {
fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
- o->tile_diameter);
- for ( j = 0; j < o->tile_diameter; j++ ) {
+ tile_diameter);
+ for ( j = 0; j < tile_diameter; j++ ) {
// scrolling West
- for ( i = o->tile_diameter - 1; i > 0; i-- ) {
- tiles[(j*o->tile_diameter) + i] =
- tiles[(j*o->tile_diameter) + i - 1];
+ for ( i = tile_diameter - 1; i > 0; i-- ) {
+ tiles[(j*tile_diameter) + i] =
+ tiles[(j*tile_diameter) + i - 1];
}
// load in new column
fgBucketOffset(&p_last, &p2, -dw - 1, j - dh);
- fgTileMgrLoadTile(&p2, &tiles[(j*o->tile_diameter) + 0]);
+ fgTileMgrLoadTile(&p2, &tiles[(j*tile_diameter) + 0]);
}
}
if ( (p1.lat > p_last.lat) ||
( (p1.lat == p_last.lat) && (p1.y > p_last.y) ) ) {
fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
- o->tile_diameter);
- for ( i = 0; i < o->tile_diameter; i++ ) {
+ tile_diameter);
+ for ( i = 0; i < tile_diameter; i++ ) {
// scrolling North
- for ( j = 0; j < o->tile_diameter - 1; j++ ) {
- tiles[(j * o->tile_diameter) + i] =
- tiles[((j+1) * o->tile_diameter) + i];
+ for ( j = 0; j < tile_diameter - 1; j++ ) {
+ tiles[(j * tile_diameter) + i] =
+ tiles[((j+1) * tile_diameter) + i];
}
// load in new column
fgBucketOffset(&p_last, &p2, i - dw, dh + 1);
- fgTileMgrLoadTile(&p2, &tiles[((o->tile_diameter-1) *
- o->tile_diameter) + i]);
+ fgTileMgrLoadTile(&p2, &tiles[((tile_diameter-1) *
+ tile_diameter) + i]);
}
} else if ( (p1.lat < p_last.lat) ||
( (p1.lat == p_last.lat) && (p1.y < p_last.y) ) ) {
fgPrintf( FG_TERRAIN, FG_INFO, " Loading %d tiles\n",
- o->tile_diameter);
- for ( i = 0; i < o->tile_diameter; i++ ) {
+ tile_diameter);
+ for ( i = 0; i < tile_diameter; i++ ) {
// scrolling South
- for ( j = o->tile_diameter - 1; j > 0; j-- ) {
- tiles[(j * o->tile_diameter) + i] =
- tiles[((j-1) * o->tile_diameter) + i];
+ for ( j = tile_diameter - 1; j > 0; j-- ) {
+ tiles[(j * tile_diameter) + i] =
+ tiles[((j-1) * tile_diameter) + i];
}
// load in new column
fgBucketOffset(&p_last, &p2, i - dw, -dh - 1);
}
+// Calculate shortest distance from point to line
+static double point_line_dist_squared( fgPoint3d *tc, fgPoint3d *vp,
+ MAT3vec d )
+{
+ MAT3vec p, p0;
+ double dist;
+
+ p[0] = tc->x; p[1] = tc->y; p[2] = tc->z;
+ p0[0] = vp->x; p0[1] = vp->y; p0[2] = vp->z;
+
+ dist = fgPointLineSquared(p, p0, d);
+
+ // printf("dist = %.2f\n", dist);
+
+ return(dist);
+}
+
+
// Calculate if point/radius is inside view frustum
-static int viewable( fgCartesianPoint3d *cp, double radius ) {
+static int viewable( fgPoint3d *cp, double radius ) {
fgVIEW *v;
MAT3hvec world, eye;
int viewable = 1; // start by assuming it's viewable
return(0);
}
+ // Check far clip plane
+ if ( eye[2] + radius < -current_weather.visibility ) {
+ return(0);
+ }
+
// check right clip plane (from eye perspective)
// y = m * (x - x0) = equation of a line intercepting X axis at x0
x1 = v->cos_fov_x * radius;
}
+// Determine scenery altitude. Normally this just happens when we
+// render the scene, but we'd also like to be able to do this
+// explicitely. lat & lon are in radians. abs_view_pos in meters.
+// Returns result in meters.
+double fgTileMgrCurElev( double lon, double lat, fgPoint3d *abs_view_pos ) {
+ fgTILECACHE *c;
+ fgTILE *t;
+ // fgVIEW *v;
+ fgFRAGMENT *frag_ptr;
+ fgBUCKET p;
+ fgPoint3d earth_center, result;
+ fgPoint3d pp;
+ MAT3vec local_up;
+ list < fgFRAGMENT > :: iterator current;
+ list < fgFRAGMENT > :: iterator last;
+ double dist, min_dist, lat_geod, alt, sea_level_r;
+ double x, y, z;
+ int index, tile_diameter, i;
+
+ c = &global_tile_cache;
+ // v = ¤t_view;
+
+ local_up[0] = abs_view_pos->x;
+ local_up[1] = abs_view_pos->y;
+ local_up[2] = abs_view_pos->z;
+
+ tile_diameter = current_options.get_tile_diameter();
+
+ // Find current translation offset
+ fgBucketFind(lon * RAD_TO_DEG, lat * RAD_TO_DEG, &p);
+ index = c->Exists(&p);
+ t = c->GetTile(index);
+
+ scenery.next_center.x = t->center.x;
+ scenery.next_center.y = t->center.y;
+ scenery.next_center.z = t->center.z;
+
+ earth_center.x = 0.0;
+ earth_center.y = 0.0;
+ earth_center.z = 0.0;
+
+ fgPrintf( FG_TERRAIN, FG_DEBUG,
+ "Pos = (%.2f, %.2f) Current bucket = %d %d %d %d Index = %ld\n",
+ lon * RAD_TO_DEG, lat * RAD_TO_DEG,
+ p.lon, p.lat, p.x, p.y, fgBucketGenIndex(&p) );
+
+ // traverse the potentially viewable tile list
+ for ( i = 0; i < (tile_diameter * tile_diameter); i++ ) {
+ index = tiles[i];
+ // fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index);
+ t = c->GetTile(index);
+
+ // calculate tile offset
+ x = (t->offset.x = t->center.x - scenery.center.x);
+ y = (t->offset.y = t->center.y - scenery.center.y);
+ z = (t->offset.z = t->center.z - scenery.center.z);
+
+ // calc current terrain elevation calculate distance from
+ // vertical tangent line at current position to center of
+ // tile.
+
+ /* printf("distance squared = %.2f, bounding radius = %.2f\n",
+ point_line_dist_squared(&(t->offset), &(v->view_pos),
+ v->local_up), t->bounding_radius); */
+
+ dist = point_line_dist_squared( &(t->center), abs_view_pos,
+ local_up );
+ if ( dist < FG_SQUARE(t->bounding_radius) ) {
+
+ // traverse fragment list for tile
+ current = t->fragment_list.begin();
+ last = t->fragment_list.end();
+
+ while ( current != last ) {
+ frag_ptr = &(*current);
+ current++;
+ /* printf("distance squared = %.2f, bounding radius = %.2f\n",
+ point_line_dist_squared( &(frag_ptr->center),
+ &abs_view_pos), local_up),
+ frag_ptr->bounding_radius); */
+
+ dist = point_line_dist_squared( &(frag_ptr->center),
+ abs_view_pos, local_up);
+ if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
+ if ( frag_ptr->intersect( abs_view_pos,
+ &earth_center, 0, &result ) ) {
+ // compute geocentric coordinates of tile center
+ pp = fgCartToPolar3d(result);
+ // convert to geodetic coordinates
+ fgGeocToGeod(pp.lat, pp.radius, &lat_geod,
+ &alt, &sea_level_r);
+ // printf("alt = %.2f\n", alt);
+ // exit since we found an intersection
+ return(alt);
+ }
+ }
+ }
+ }
+ }
+
+ printf("no terrain intersection found\n");
+ return(0);
+}
+
+
// Render the local tiles
void fgTileMgrRender( void ) {
fgTILECACHE *c;
fgFLIGHT *f;
- fgOPTIONS *o;
- fgTILE *t;
+ fgTILE *t, *last_tile_ptr;
fgVIEW *v;
- struct fgBUCKET p;
- fgCartesianPoint3d frag_offset, last_offset;
+ fgBUCKET p;
+ fgPoint3d frag_offset, pp;
+ fgPoint3d earth_center, result;
fgFRAGMENT *frag_ptr;
fgMATERIAL *mtl_ptr;
+ GLdouble *m;
+ double dist, min_dist, lat_geod, alt, sea_level_r;
+ double x, y, z;
list < fgFRAGMENT > :: iterator current;
list < fgFRAGMENT > :: iterator last;
- int i, size;
+ int i, j, size;
+ int tile_diameter, textures;
int index;
int culled = 0;
int drawn = 0;
+ int total_faces = 0;
c = &global_tile_cache;
f = current_aircraft.flight;
- o = ¤t_options;
v = ¤t_view;
+ tile_diameter = current_options.get_tile_diameter();
+ textures = current_options.get_textures();
+
// Find current translation offset
fgBucketFind(FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG, &p);
index = c->Exists(&p);
t = c->GetTile(index);
+ scenery.next_center.x = t->center.x;
+ scenery.next_center.y = t->center.y;
+ scenery.next_center.z = t->center.z;
+
+ earth_center.x = 0.0;
+ earth_center.y = 0.0;
+ earth_center.z = 0.0;
+
fgPrintf( FG_TERRAIN, FG_DEBUG,
"Pos = (%.2f, %.2f) Current bucket = %d %d %d %d Index = %ld\n",
FG_Longitude * RAD_TO_DEG, FG_Latitude * RAD_TO_DEG,
// initialize the transient per-material fragment lists
material_mgr.init_transient_material_lists();
+ min_dist = 100000.0;
// Pass 1
// traverse the potentially viewable tile list
- for ( i = 0; i < (o->tile_diameter * o->tile_diameter); i++ ) {
+ for ( i = 0; i < (tile_diameter * tile_diameter); i++ ) {
index = tiles[i];
// fgPrintf( FG_TERRAIN, FG_DEBUG, "Index = %d\n", index);
t = c->GetTile(index);
// calculate tile offset
- t->offset.x = t->center.x - scenery.center.x;
- t->offset.y = t->center.y - scenery.center.y;
- t->offset.z = t->center.z - scenery.center.z;
+ x = (t->offset.x = t->center.x - scenery.center.x);
+ y = (t->offset.y = t->center.y - scenery.center.y);
+ z = (t->offset.z = t->center.z - scenery.center.z);
+
+ m = t->model_view;
+ for ( j = 0; j < 16; j++ ) {
+ m[j] = v->MODEL_VIEW[j];
+ }
+
+ // Calculate the model_view transformation matrix for this tile
+ // This is equivalent to doing a glTranslatef(x, y, z);
+ m[12] = m[0] * x + m[4] * y + m[8] * z + m[12];
+ m[13] = m[1] * x + m[5] * y + m[9] * z + m[13];
+ m[14] = m[2] * x + m[6] * y + m[10] * z + m[14];
+ m[15] = m[3] * x + m[7] * y + m[11] * z + m[15];
+
+ // temp ... calc current terrain elevation
+ // calculate distance from vertical tangent line at
+ // current position to center of tile.
+
+ /* printf("distance squared = %.2f, bounding radius = %.2f\n",
+ point_line_dist_squared(&(t->offset), &(v->view_pos),
+ v->local_up), t->bounding_radius); */
+
+ dist = point_line_dist_squared( &(t->center), &(v->abs_view_pos),
+ v->local_up );
+ if ( dist < FG_SQUARE(t->bounding_radius) ) {
+
+ // traverse fragment list for tile
+ current = t->fragment_list.begin();
+ last = t->fragment_list.end();
+
+ while ( current != last ) {
+ frag_ptr = &(*current);
+ current++;
+ /* printf("distance squared = %.2f, bounding radius = %.2f\n",
+ point_line_dist_squared( &(frag_ptr->center),
+ &(v->abs_view_pos), v->local_up),
+ frag_ptr->bounding_radius); */
+
+ dist = point_line_dist_squared( &(frag_ptr->center),
+ &(v->abs_view_pos), v->local_up);
+ if ( dist <= FG_SQUARE(frag_ptr->bounding_radius) ) {
+ if ( frag_ptr->intersect( &(v->abs_view_pos),
+ &earth_center, 0, &result ) ) {
+ // compute geocentric coordinates of tile center
+ pp = fgCartToPolar3d(result);
+ // convert to geodetic coordinates
+ fgGeocToGeod(pp.lat, pp.radius, &lat_geod,
+ &alt, &sea_level_r);
+ // printf("alt = %.2f\n", alt);
+ scenery.cur_elev = alt;
+ // exit this loop since we found an intersection
+ break;
+ }
+ }
+ }
+ }
// Course (tile based) culling
if ( viewable(&(t->offset), t->bounding_radius) ) {
- // at least a portion of this tile is viewable
+ // at least a portion of this tile could be viewable
// xglPushMatrix();
// xglTranslatef(t->offset.x, t->offset.y, t->offset.z);
if ( viewable(&frag_offset, frag_ptr->bounding_radius*2) ) {
// add to transient per-material property fragment list
- frag_ptr->tile_offset.x = t->offset.x;
- frag_ptr->tile_offset.y = t->offset.y;
- frag_ptr->tile_offset.z = t->offset.z;
-
- /*
- frag_ptr->matrix[12] = t->offset.x;
- frag_ptr->matrix[13] = t->offset.y;
- frag_ptr->matrix[14] = t->offset.z;
-
- xglGetFloatv(GL_MODELVIEW_MATRIX, frag_ptr->matrix);
- for ( j = 1; j < 16; j++ ) {
- printf(" a%d = %f\n", j, frag_ptr->matrix[j]);
- }
- */
+ // frag_ptr->tile_offset.x = t->offset.x;
+ // frag_ptr->tile_offset.y = t->offset.y;
+ // frag_ptr->tile_offset.z = t->offset.z;
- mtl_ptr = (fgMATERIAL *)(frag_ptr->material_ptr);
+ mtl_ptr = frag_ptr->material_ptr;
// printf(" lookup = %s\n", mtl_ptr->texture_name);
if ( mtl_ptr->list_size < FG_MAX_MATERIAL_FRAGS ) {
mtl_ptr->list[mtl_ptr->list_size] = frag_ptr;
// (fgMATERIAL)value = (*mapcurrent).second;
mtl_ptr = &(*mapcurrent).second;
- last_offset.x = last_offset.y = last_offset.z = -99999999.0;
+ last_tile_ptr = NULL;
size = mtl_ptr->list_size;
if ( size > 0 ) {
- if ( o->textures ) {
+ if ( textures ) {
+#ifdef GL_VERSION_1_1
xglBindTexture(GL_TEXTURE_2D, mtl_ptr->texture_id);
+#elif GL_EXT_texture_object
+ xglBindTextureEXT(GL_TEXTURE_2D, mtl_ptr->texture_id);
+#else
+# error port me
+#endif
} else {
xglMaterialfv (GL_FRONT, GL_AMBIENT, mtl_ptr->ambient);
xglMaterialfv (GL_FRONT, GL_DIFFUSE, mtl_ptr->diffuse);
for ( i = 0; i < size; i++ ) {
frag_ptr = mtl_ptr->list[i];
- if ( (frag_ptr->tile_offset.x == last_offset.x) &&
- (frag_ptr->tile_offset.y == last_offset.y) &&
- (frag_ptr->tile_offset.z == last_offset.z) ) {
+ // count up the number of polygons we are drawing in
+ // case someone is interested.
+ total_faces += frag_ptr->num_faces;
+
+ if ( frag_ptr->tile_ptr == last_tile_ptr ) {
// same tile as last time, no transform necessary
} else {
// new tile, new translate
// xglLoadMatrixf( frag_ptr->matrix );
- xglPopMatrix();
- xglPushMatrix();
- xglTranslatef( frag_ptr->tile_offset.x,
- frag_ptr->tile_offset.y,
- frag_ptr->tile_offset.z );
+ t = frag_ptr->tile_ptr;
+ xglLoadMatrixd(t->model_view );
}
// Woohoo!!! We finally get to draw something!
// printf(" display_list = %d\n", frag_ptr->display_list);
xglCallList(frag_ptr->display_list);
- last_offset = frag_ptr->tile_offset;
+ last_tile_ptr = frag_ptr->tile_ptr;
}
}
- xglPopMatrix();
-
*mapcurrent++;
}
+
+ xglPopMatrix();
+
+ fgPrintf( FG_TERRAIN, FG_DEBUG, "Rendered %d polygons this frame.\n",
+ total_faces);
}
// $Log$
+// Revision 1.30 1998/08/22 14:49:59 curt
+// Attempting to iron out seg faults and crashes.
+// Did some shuffling to fix a initialization order problem between view
+// position, scenery elevation.
+//
+// Revision 1.29 1998/08/20 15:12:06 curt
+// Used a forward declaration of classes fgTILE and fgMATERIAL to eliminate
+// the need for "void" pointers and casts.
+// Quick hack to count the number of scenery polygons that are being drawn.
+//
+// Revision 1.28 1998/08/12 21:13:06 curt
+// material.cxx: don't load textures if they are disabled
+// obj.cxx: optimizations from Norman Vine
+// tile.cxx: minor tweaks
+// tile.hxx: addition of num_faces
+// tilemgr.cxx: minor tweaks
+//
+// Revision 1.27 1998/07/24 21:42:09 curt
+// material.cxx: whups, double method declaration with no definition.
+// obj.cxx: tweaks to avoid errors in SGI's CC.
+// tile.cxx: optimizations by Norman Vine.
+// tilemgr.cxx: optimizations by Norman Vine.
+//
+// Revision 1.26 1998/07/20 12:51:26 curt
+// Added far clip plane to fragment clipping calculations and tie this to
+// weather->visibility. This way you can increase framerates by increasing
+// for and lowering visibility.
+//
+// Revision 1.25 1998/07/13 21:02:01 curt
+// Wrote access functions for current fgOPTIONS.
+//
+// Revision 1.24 1998/07/12 03:18:29 curt
+// Added ground collision detection. This involved:
+// - saving the entire vertex list for each tile with the tile records.
+// - saving the face list for each fragment with the fragment records.
+// - code to intersect the current vertical line with the proper face in
+// an efficient manner as possible.
+// Fixed a bug where the tiles weren't being shifted to "near" (0,0,0)
+//
+// Revision 1.23 1998/07/08 14:47:23 curt
+// Fix GL_MODULATE vs. GL_DECAL problem introduced by splash screen.
+// polare3d.h renamed to polar3d.hxx
+// fg{Cartesian,Polar}Point3d consolodated.
+// Added some initial support for calculating local current ground elevation.
+//
+// Revision 1.22 1998/07/04 00:54:31 curt
+// Added automatic mipmap generation.
+//
+// When rendering fragments, use saved model view matrix from associated tile
+// rather than recalculating it with push() translate() pop().
+//
+// Revision 1.21 1998/06/27 16:54:59 curt
+// Check for GL_VERSION_1_1 or GL_EXT_texture_object to decide whether to use
+// "EXT" versions of texture management routines.
+//
// Revision 1.20 1998/06/17 21:36:42 curt
// Load and manage multiple textures defined in the Materials library.
// Boost max material fagments for each material property to 800.