2 // genapt.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 <Include/compiler.h>
32 # define exception c_exception
36 #ifdef FG_HAVE_NATIVE_SGI_COMPILERS
40 #include <Debug/logstream.hxx>
41 // #include <Include/fg_types.h>
42 #include <Math/fg_geodesy.hxx>
43 #include <Math/mat3.h>
44 #include <Math/point3d.hxx>
45 #include <Math/polar3d.hxx>
46 #include <Misc/fgstream.hxx>
47 #include <Objects/material.hxx>
49 // #include <gpc/gpc.h>
53 FG_USING_NAMESPACE(std);
56 typedef vector < Point3D > container;
57 typedef container::iterator iterator;
58 typedef container::const_iterator const_iterator;
61 #define FG_APT_BASE_TEX_CONSTANT 2000.0
63 // Calculate texture coordinates for a given point.
64 static Point3D calc_tex_coords(double *node, const Point3D& ref) {
68 cp = Point3D( node[0] + ref.x(), node[1] + ref.y(), node[2] + ref.z() );
70 pp = fgCartToPolar3d(cp);
72 pp.setx( fmod(FG_APT_BASE_TEX_CONSTANT * pp.x(), 10.0) );
73 pp.sety( fmod(FG_APT_BASE_TEX_CONSTANT * pp.y(), 10.0) );
76 pp.setx( pp.x() + 10.0 );
80 pp.sety( pp.y() + 10.0 );
87 // generate the actual base area for the airport
89 gen_base( const Point3D& average, const container& perimeter, fgTILE *t)
92 Point3D cart, cart_trans, tex;
94 double dist, max_dist, temp;
101 FG_LOG( FG_TERRAIN, FG_INFO,
102 "generating airport base for size = " << perimeter.size() );
105 fragment.tile_ptr = t;
107 // find airport base material in the properties list
108 if ( ! material_mgr.find( APT_BASE_MATERIAL, fragment.material_ptr )) {
109 FG_LOG( FG_TERRAIN, FG_ALERT,
110 "Ack! unknown material name = " << APT_BASE_MATERIAL
111 << " in fgAptGenerat()" );
114 FG_LOG( FG_TERRAIN, FG_INFO,
116 << t->center.x() << " " << t->center.y() << " " << t->center.z() );
117 FG_LOG( FG_TERRAIN, FG_INFO,
119 << average.x() << " " << average.y() << " " << average.z() );
120 fragment.center = average;
122 normal[0] = average.x();
123 normal[1] = average.y();
124 normal[2] = average.z();
125 MAT3_NORMALIZE_VEC(normal, temp);
127 display_list = xglGenLists(1);
128 xglNewList(display_list, GL_COMPILE);
129 xglBegin(GL_TRIANGLE_FAN);
131 // first point center of fan
132 cart_trans = average - t->center;
133 t->nodes[t->ncount][0] = cart_trans.x();
134 t->nodes[t->ncount][1] = cart_trans.y();
135 t->nodes[t->ncount][2] = cart_trans.z();
136 center_num = t->ncount;
139 tex = calc_tex_coords( t->nodes[t->ncount-1], t->center );
140 xglTexCoord2f(tex.x(), tex.y());
141 xglNormal3dv(normal);
142 xglVertex3dv(t->nodes[t->ncount-1]);
144 // first point on perimeter
145 const_iterator current = perimeter.begin();
146 cart = fgGeodToCart( *current );
147 cart_trans = cart - t->center;
148 t->nodes[t->ncount][0] = cart_trans.x();
149 t->nodes[t->ncount][1] = cart_trans.y();
150 t->nodes[t->ncount][2] = cart_trans.z();
154 tex = calc_tex_coords( t->nodes[i], t->center );
155 dist = cart.distance3Dsquared(average);
156 if ( dist > max_dist ) {
159 xglTexCoord2f(tex.x(), tex.y());
160 xglVertex3dv(t->nodes[i]);
164 const_iterator last = perimeter.end();
165 for ( ; current != last; ++current ) {
166 cart = fgGeodToCart( *current );
167 cart_trans = cart - t->center;
168 t->nodes[t->ncount][0] = cart_trans.x();
169 t->nodes[t->ncount][1] = cart_trans.y();
170 t->nodes[t->ncount][2] = cart_trans.z();
172 fragment.add_face(center_num, i - 1, i);
174 tex = calc_tex_coords( t->nodes[i], t->center );
175 dist = cart.distance3Dsquared(average);
176 if ( dist > max_dist ) {
179 xglTexCoord2f(tex.x(), tex.y());
180 xglVertex3dv(t->nodes[i]);
184 // last point (first point in perimeter list)
185 current = perimeter.begin();
186 cart = fgGeodToCart( *current );
187 cart_trans = cart - t->center;
188 fragment.add_face(center_num, i - 1, 1);
190 tex = calc_tex_coords( t->nodes[1], t->center );
191 xglTexCoord2f(tex.x(), tex.y());
192 xglVertex3dv(t->nodes[1]);
197 fragment.bounding_radius = sqrt(max_dist);
198 fragment.display_list = display_list;
200 t->fragment_list.push_back(fragment);
204 // Load a .apt file and register the GL fragments with the
205 // corresponding tile
207 fgAptGenerate(const string& path, fgTILE *tile)
210 string apt_id, apt_name;
214 // face list (this indexes into the master tile vertex list)
217 double avex = 0.0, avey = 0.0, avez = 0.0;
220 // gpc_vertex p_2d, list_2d[MAX_PERIMETER];
221 // gpc_vertex_list perimeter_2d;
223 fg_gzifstream in( path );
225 // exit immediately assuming an airport file for this tile
232 // read in each line of the file
238 if ( token == "a" ) {
239 // airport info record (start of airport)
241 if ( apt_id.length() > 0 ) {
242 // we have just finished reading and airport record.
244 gen_base(average, perimeter, tile);
247 FG_LOG( FG_TERRAIN, FG_INFO, "Reading airport record" );
251 avex = avey = avez = 0.0;
252 perimeter.erase( perimeter.begin(), perimeter.end() );
253 // skip to end of line.
254 while ( in.get(c) && c != '\n' ) {
257 FG_LOG( FG_TERRAIN, FG_INFO,
258 "\tID = " << apt_id << " Name = " << apt_name );
259 } else if ( token == "p" ) {
260 // airport area bounding polygon coordinate. These
261 // specify a convex hull that should already have been cut
262 // out of the base terrain. The points are given in
263 // counter clockwise order and are specified in lon/lat
266 avex += tile->nodes[i][0];
267 avey += tile->nodes[i][1];
268 avez += tile->nodes[i][2];
269 perimeter.push_back(p);
271 } else if ( token == "r" ) {
274 while ( in.get(c) && c != '\n' );
280 if ( apt_id.length() > 0 ) {
281 // we have just finished reading and airport record.
283 size = perimeter.size();
284 average = Point3D( avex / (double)size + tile->center.x(),
285 avey / (double)size + tile->center.y(),
286 avez / (double)size + tile->center.z() );
288 gen_base(average, perimeter, tile);
296 // Revision 1.13 1999/02/26 22:08:34 curt
297 // Added initial support for native SGI compilers.
299 // Revision 1.12 1999/02/01 21:08:33 curt
300 // Optimizations from Norman Vine.
302 // Revision 1.11 1998/11/23 21:48:09 curt
303 // Borland portability tweaks.
305 // Revision 1.10 1998/11/07 19:07:06 curt
306 // Enable release builds using the --without-logging option to the configure
307 // script. Also a couple log message cleanups, plus some C to C++ comment
310 // Revision 1.9 1998/11/06 21:17:32 curt
311 // Converted to new logstream debugging facility. This allows release
312 // builds with no messages at all (and no performance impact) by using
313 // the -DFG_NDEBUG flag.
315 // Revision 1.8 1998/11/06 14:46:59 curt
316 // Changes to track Bernie's updates to fgstream.
318 // Revision 1.7 1998/10/20 18:26:06 curt
319 // Updates to point3d.hxx
321 // Revision 1.6 1998/10/18 01:17:16 curt
324 // Revision 1.5 1998/10/16 23:27:14 curt
327 // Revision 1.4 1998/10/16 00:51:46 curt
328 // Converted to Point3D class.
330 // Revision 1.3 1998/09/21 20:55:00 curt
331 // Used the cartesian form of the airport area coordinates to determine the
334 // Revision 1.2 1998/09/14 12:44:30 curt
335 // Don't recalculate perimeter points since it is not likely that they will match
336 // exactly with the previously calculated points, which will leave an ugly gap
337 // around the airport area.
339 // Revision 1.1 1998/09/14 02:14:01 curt
340 // Initial revision of genapt.[ch]xx for generating airport scenery.