2 // getapt.cxx -- generate airport scenery from the given definition file
4 // Written by Curtis Olson, started September 1998.
6 // Copyright (C) 1998 Curtis L. Olson - curt@me.umn.edu
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 // (Log is kept at end of this file)
26 #include <string> // Standard C++ string library
28 #include "Include/fg_stl_config.h"
30 #ifdef NEEDNAMESPACESTD
34 #include <Debug/fg_debug.h>
35 #include <Include/fg_types.h>
36 #include <Math/fg_geodesy.h>
37 #include <Math/mat3.h>
38 #include <Math/polar3d.hxx>
39 #include <Misc/fgstream.hxx>
40 #include <Objects/material.hxx>
42 // #include <gpc/gpc.h>
47 typedef vector < fgPoint3d > container;
48 typedef container::iterator iterator;
49 typedef container::const_iterator const_iterator;
52 // Calculate distance between to fgPoint3d's
53 static double calc_dist(const fgPoint3d& p1, const fgPoint3d& p2) {
58 return sqrt(x*x + y*y + z*z);
62 // convert a geodetic point lon(degrees), lat(degrees), elev(meter) to a
64 static fgPoint3d geod_to_cart(fgPoint3d geod) {
69 // printf("A geodetic point is (%.2f, %.2f, %.2f)\n", geod[0],
72 geoc.lon = geod.lon*DEG_TO_RAD;
73 fgGeodToGeoc(geod.lat*DEG_TO_RAD, geod.radius, &sl_radius, &geoc.lat);
75 // printf("A geocentric point is (%.2f, %.2f, %.2f)\n", gc_lon,
76 // gc_lat, sl_radius+geod[2]); */
78 geoc.radius = sl_radius + geod.radius;
79 cart = fgPolarToCart3d(geoc);
81 // printf("A cart point is (%.8f, %.8f, %.8f)\n", cp.x, cp.y, cp.z);
87 #define FG_APT_BASE_TEX_CONSTANT 2000.0
90 // Calculate texture coordinates for a given point.
92 calc_tex_coords(const fgPoint3d& p) {
95 cout << "Texture coordinates = " <<
96 FG_APT_BASE_TEX_CONSTANT * p.lon << " " <<
97 FG_APT_BASE_TEX_CONSTANT * p.lat << "\n";
99 tex.x = fmod(FG_APT_BASE_TEX_CONSTANT * p.lon, 10.0);
100 tex.y = fmod(FG_APT_BASE_TEX_CONSTANT * p.lat, 10.0);
110 cout << "Texture coordinates = " << tex.x << " " << tex.y << "\n";
117 // Calculate texture coordinates for a given point.
118 static fgPoint3d calc_tex_coords(double *node, fgPoint3d *ref) {
122 cp.x = node[0] + ref->x;
123 cp.y = node[1] + ref->y;
124 cp.z = node[2] + ref->z;
126 pp = fgCartToPolar3d(cp);
128 pp.lon = fmod(FG_APT_BASE_TEX_CONSTANT * pp.lon, 10.0);
129 pp.lat = fmod(FG_APT_BASE_TEX_CONSTANT * pp.lat, 10.0);
131 if ( pp.lon < 0.0 ) {
135 if ( pp.lat < 0.0 ) {
143 // generate the actual base area for the airport
145 gen_base( const fgPoint3d& average, const container& perimeter, fgTILE *t)
148 fgPoint3d cart, cart_trans, tex;
150 double dist, max_dist, temp;
157 cout << "generating airport base for size = " << perimeter.size() << "\n";
160 fragment.tile_ptr = t;
162 // find airport base material in the properties list
163 if ( ! material_mgr.find( APT_BASE_MATERIAL, fragment.material_ptr )) {
164 fgPrintf( FG_TERRAIN, FG_ALERT,
165 "Ack! unknown material name = %s in fgAptGenerat()\n",
169 printf(" tile center = %.2f %.2f %.2f\n",
170 t->center.x, t->center.y, t->center.z);
171 printf(" airport center = %.2f %.2f %.2f\n",
172 average.x, average.y, average.z);
173 fragment.center.x = average.x;
174 fragment.center.y = average.y;
175 fragment.center.z = average.z;
177 normal[0] = average.x;
178 normal[1] = average.y;
179 normal[2] = average.z;
180 MAT3_NORMALIZE_VEC(normal, temp);
182 display_list = xglGenLists(1);
183 xglNewList(display_list, GL_COMPILE);
184 xglBegin(GL_TRIANGLE_FAN);
186 // first point center of fan
187 cart_trans.x = average.x - t->center.x;
188 cart_trans.y = average.y - t->center.y;
189 cart_trans.z = average.z - t->center.z;
190 t->nodes[t->ncount][0] = cart_trans.x;
191 t->nodes[t->ncount][1] = cart_trans.y;
192 t->nodes[t->ncount][2] = cart_trans.z;
193 center_num = t->ncount;
196 tex = calc_tex_coords( t->nodes[t->ncount-1], &(t->center) );
197 xglTexCoord2f(tex.x, tex.y);
198 xglNormal3dv(normal);
199 xglVertex3dv(t->nodes[t->ncount-1]);
201 // first point on perimeter
202 iterator current = perimeter.begin();
203 cart = geod_to_cart( *current );
204 cart_trans.x = cart.x - t->center.x;
205 cart_trans.y = cart.y - t->center.y;
206 cart_trans.z = cart.z - t->center.z;
207 t->nodes[t->ncount][0] = cart_trans.x;
208 t->nodes[t->ncount][1] = cart_trans.y;
209 t->nodes[t->ncount][2] = cart_trans.z;
213 tex = calc_tex_coords( t->nodes[i], &(t->center) );
214 dist = calc_dist(average, cart);
215 if ( dist > max_dist ) {
218 xglTexCoord2f(tex.x, tex.y);
219 xglVertex3dv(t->nodes[i]);
223 const_iterator last = perimeter.end();
224 for ( ; current != last; ++current ) {
225 cart = geod_to_cart( *current );
226 cart_trans.x = cart.x - t->center.x;
227 cart_trans.y = cart.y - t->center.y;
228 cart_trans.z = cart.z - t->center.z;
229 t->nodes[t->ncount][0] = cart_trans.x;
230 t->nodes[t->ncount][1] = cart_trans.y;
231 t->nodes[t->ncount][2] = cart_trans.z;
233 fragment.add_face(center_num, i - 1, i);
235 tex = calc_tex_coords( t->nodes[i], &(t->center) );
236 dist = calc_dist(average, cart);
237 if ( dist > max_dist ) {
240 xglTexCoord2f(tex.x, tex.y);
241 xglVertex3dv(t->nodes[i]);
245 // last point (first point in perimeter list)
246 current = perimeter.begin();
247 cart = geod_to_cart( *current );
248 cart_trans.x = cart.x - t->center.x;
249 cart_trans.y = cart.y - t->center.y;
250 cart_trans.z = cart.z - t->center.z;
251 fragment.add_face(center_num, i - 1, 1);
253 tex = calc_tex_coords( t->nodes[1], &(t->center) );
254 xglTexCoord2f(tex.x, tex.y);
255 xglVertex3dv(t->nodes[1]);
260 fragment.bounding_radius = max_dist;
261 fragment.display_list = display_list;
263 t->fragment_list.push_back(fragment);
267 // Load a .apt file and register the GL fragments with the
268 // corresponding tile
270 fgAptGenerate(const string& path, fgTILE *tile)
273 string apt_id, apt_name;
277 // face list (this indexes into the master tile vertex list)
279 fgPoint3d p, average;
282 // gpc_vertex p_2d, list_2d[MAX_PERIMETER];
283 // gpc_vertex_list perimeter_2d;
285 fg_gzifstream in( path );
287 // exit immediately assuming an airport file for this tile
294 // read in each line of the file
298 in.stream() >> token;
300 if ( token == "a" ) {
301 // airport info record (start of airport)
303 if ( apt_id != "" ) {
304 // we have just finished reading and airport record.
306 gen_base(average, perimeter, tile);
309 cout << "Reading airport record\n";
310 in.stream() >> apt_id;
313 average.lon = average.lat = average.radius = 0.0;
314 perimeter.erase( perimeter.begin(), perimeter.end() );
315 // skip to end of line.
316 while ( in.get(c) && c != '\n' ) {
319 cout << "\tID = " + apt_id + " Name = " + apt_name + "\n";
320 } else if ( token == "p" ) {
321 // airport area bounding polygon coordinate. These
322 // specify a convex hull that should already have been cut
323 // out of the base terrain. The points are given in
324 // counter clockwise order and are specified in lon/lat
326 in.stream() >> p.lon >> p.lat >> p.radius;
327 average.x += tile->nodes[i][0];
328 average.y += tile->nodes[i][1];
329 average.z += tile->nodes[i][2];
330 perimeter.push_back(p);
332 } else if ( token == "r" ) {
335 while ( in.get(c) && c != '\n' );
338 // airports.insert(a);
342 if ( apt_id != "" ) {
343 // we have just finished reading and airport record.
345 size = perimeter.size();
346 average.x = average.x / (double)size + tile->center.x;
347 average.y = average.y / (double)size + tile->center.y;
348 average.z = average.z / (double)size + tile->center.z;
350 gen_base(average, perimeter, tile);
358 // Revision 1.3 1998/09/21 20:55:00 curt
359 // Used the cartesian form of the airport area coordinates to determine the
362 // Revision 1.2 1998/09/14 12:44:30 curt
363 // Don't recalculate perimeter points since it is not likely that they will match
364 // exactly with the previously calculated points, which will leave an ugly gap
365 // around the airport area.
367 // Revision 1.1 1998/09/14 02:14:01 curt
368 // Initial revision of genapt.[ch]xx for generating airport scenery.