Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

TerrainTile.cpp

Go to the documentation of this file.
00001 /*
00002  * TerrainTile.cpp
00003  *
00004  * Part of Fly! Legacy project
00005  *
00006  * Copyright 2003 Chris Wallace
00007  *
00008  * Fly! Legacy is free software; you can redistribute it and/or modify
00009  *   it under the terms of the GNU General Public License as published by
00010  *   the Free Software Foundation; either version 2 of the License, or
00011  *   (at your option) any later version.
00012  *
00013  * Fly! Legacy is distributed in the hope that it will be useful,
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  *   GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  *   along with Fly! Legacy; if not, write to the Free Software
00020  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00031 #include "../Include/Globals.h"
00032 #include "../Include/Terrain.h"
00033 #include "../Include/Utility.h"
00034 #include "../Include/Ui.h"
00035 
00036 
00037 //
00038 // CTerrainTile
00039 //
00040 CTerrainTile::CTerrainTile (void)
00041 {
00042   memset (&sw, 0, sizeof(SPosition));
00043   memset (&nw, 0, sizeof(SPosition));
00044   memset (&ne, 0, sizeof(SPosition));
00045   memset (&se, 0, sizeof(SPosition));
00046   memset (&c, 0, sizeof(SPosition));
00047   strcpy (swBound, "");
00048   strcpy (nwBound, "");
00049   strcpy (neBound, "");
00050   strcpy (seBound, "");
00051   texid = 0;
00052   created = false;
00053 }
00054 
00055 CTerrainTile::~CTerrainTile (void)
00056 {
00057   if (texid != 0) {
00058     glDeleteTextures (1, &texid);
00059   }
00060 }
00061 
00062 //
00063 // Calculate center latitude/longitude position for the tile by simply
00064 //   dividing the lat and lon size of the tile in half
00065 //
00066 // For this function to be meaningful, the boundary positions must have been
00067 //   set already
00068 //
00069 void CTerrainTile::CalculateCenter (void)
00070 {
00071   // Calculate center position
00072   c.lat = sw.lat + (ne.lat - sw.lat) / 2;
00073   c.lon = sw.lon + (ne.lon - sw.lon) / 2;
00074   c.alt = 0;
00075 }
00076 
00077 //
00078 // Set the corner bounds of the tile
00079 //
00080 void CTerrainTile::SetBounds (SPosition sw, SPosition nw, SPosition ne, SPosition se)
00081 {
00082   this->sw = sw;
00083   this->nw = nw;
00084   this->ne = ne;
00085   this->se = se;
00086 
00087   CalculateCenter ();
00088   FormatBounds ();
00089 }
00090 
00091 //
00092 // Set tile bounds by specifying only the SW and NE corner latitude/longitude values
00093 //
00094 void CTerrainTile::SetCorners (double sw_lat, double sw_lon,
00095                                double ne_lat, double ne_lon)
00096 {
00097   sw.lon = sw_lon;
00098   sw.lat = sw_lat;
00099 
00100   ne.lon = ne_lon;
00101   ne.lat = ne_lat;
00102 
00103   nw.lon = sw.lon;
00104   nw.lat = ne.lat;
00105 
00106   se.lon = ne.lon;
00107   se.lat = sw.lat;
00108 
00109   CalculateCenter ();
00110   FormatBounds ();
00111 }
00112 
00113 //
00114 // Format the corner positions into human-readable text strings for debugging
00115 //
00116 // For this function to be meaningful, the boundary positions must have been
00117 //   set already
00118 //
00119 void CTerrainTile::FormatBounds (void)
00120 {
00121   FormatPosition (sw, swBound);
00122   FormatPosition (nw, nwBound);
00123   FormatPosition (ne, neBound);
00124   FormatPosition (se, seBound);
00125 }
00126 
00127 //
00128 // Determine if tile is visible from the given position.  This is only a rough,
00129 //   fast check against the corners.
00130 //
00131 // Parameters:
00132 //   from     Position from which tile distance is measured
00133 //   vis      Visibility in feet
00134 //
00135 // Return code:
00136 //   bool     true if the tile is within visibility range of the position
00137 //
00138 /*bool CTerrainTile::CheckVisibility (SPosition from, float vis)
00139 {
00140   bool rc = false;
00141 
00142   // Check distance to closest edges of tile
00143   bool nsInRange = false;
00144   bool ewInRange = false;
00145   float distance;
00146 
00147   // Check viewing position relative to tile edges
00148   if (from.lat > nw.lat) {
00149     // Viewing position is north of the tile
00150     distance = NmToFeet ((from.lat - nw.lat) / 60.0);
00151     nsInRange = distance <= vis;
00152   } else if (from.lat < sw.lat) {
00153     // Viewing position is south of the tile
00154     distance = NmToFeet ((sw.lat - from.lat) / 60.0);
00155     nsInRange = distance <= vis;
00156   } else {
00157     // Viewing position is within latitude bounds of tile
00158     nsInRange = true;
00159   }
00160 
00161   if (from.lon > ne.lon) {
00162     // Viewing position is east of the tile
00163     distance = (from.lon - ne.lon) * FeetPerLonArcsec (from.lat);
00164     ewInRange = distance <= vis;
00165   } else if (from.lon < nw.lon) {
00166     // Viewing position is west of the tile
00167     distance = (nw.lon - from.lon) * FeetPerLonArcsec (from.lat);
00168     ewInRange = distance <= vis;
00169   } else {
00170     // Viewing position is within longitude bounds of tile
00171     ewInRange = true;
00172   }
00173 
00174   // Tile is in range if both N/S and E/W distances are in range
00175   rc = nsInRange && ewInRange;
00176 
00177   return rc;
00178 }
00179 */
00180 
00181 void CTerrainTile::Create (void)
00182 {
00183   created = true;
00184 }
00185 
00186 void CTerrainTile::Destroy (void)
00187 {
00188   created = false;
00189 }
00190 
00191 bool CTerrainTile::IsCreated (void)
00192 {
00193   return created;
00194 }
00195 
00196 //
00197 // Debug print
00198 //
00199 void CTerrainTile::print (FILE *fd, char *indent, int how_much)
00200 {
00201   ssgTransform::print (fd, indent, how_much);
00202 
00203   // Print 
00204   FormatBounds();
00205   fprintf (fd, "%s  CTerrainTile:\n", indent);
00206   fprintf (fd, "%s    SW Extent = %s\n", indent, swBound);
00207   fprintf (fd, "%s    NW Extent = %s\n", indent, nwBound);
00208   fprintf (fd, "%s    NE Extent = %s\n", indent, neBound);
00209   fprintf (fd, "%s    SE Extent = %s\n", indent, seBound);
00210 }
00211 
SourceForge.net Logo Documentation generated by doxygen