]> git.mxchange.org Git - flightgear.git/blob - Airports/genapt.cxx
Converted to new logstream debugging facility. This allows release
[flightgear.git] / Airports / genapt.cxx
1 //
2 // getapt.cxx -- generate airport scenery from the given definition file
3 //
4 // Written by Curtis Olson, started September 1998.
5 //
6 // Copyright (C) 1998  Curtis L. Olson  - curt@me.umn.edu
7 //
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.
12 //
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.
17 //
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.
21 //
22 // $Id$
23 // (Log is kept at end of this file)
24
25
26 #include <string>        // Standard C++ string library
27 #include <vector>
28 #include "Include/fg_stl_config.h"
29
30 #ifdef NEEDNAMESPACESTD
31 using namespace std;
32 #endif
33
34 #include <Debug/logstream.hxx>
35 // #include <Include/fg_types.h>
36 #include <Math/fg_geodesy.hxx>
37 #include <Math/mat3.h>
38 #include <Math/point3d.hxx>
39 #include <Math/polar3d.hxx>
40 #include <Misc/fgstream.hxx>
41 #include <Objects/material.hxx>
42
43 // #include <gpc/gpc.h>
44
45 #include "genapt.hxx"
46
47
48 typedef vector < Point3D > container;
49 typedef container::iterator iterator;
50 typedef container::const_iterator const_iterator;
51
52
53 #define FG_APT_BASE_TEX_CONSTANT 2000.0
54
55 // Calculate texture coordinates for a given point.
56 static Point3D calc_tex_coords(double *node, const Point3D& ref) {
57     Point3D cp;
58     Point3D pp;
59
60     cp = Point3D( node[0] + ref.x(), node[1] + ref.y(), node[2] + ref.z() );
61
62     pp = fgCartToPolar3d(cp);
63
64     pp.setx( fmod(FG_APT_BASE_TEX_CONSTANT * pp.x(), 10.0) );
65     pp.sety( fmod(FG_APT_BASE_TEX_CONSTANT * pp.y(), 10.0) );
66
67     if ( pp.x() < 0.0 ) {
68         pp.setx( pp.x() + 10.0 );
69     }
70
71     if ( pp.y() < 0.0 ) {
72         pp.sety( pp.y() + 10.0 );
73     }
74
75     return(pp);
76 }
77
78
79 // generate the actual base area for the airport
80 static void
81 gen_base( const Point3D& average, const container& perimeter, fgTILE *t)
82 {
83     GLint display_list;
84     Point3D cart, cart_trans, tex;
85     MAT3vec normal;
86     double dist, max_dist, temp;
87     int center_num, i;
88
89     fgFRAGMENT fragment;
90
91     max_dist = 0.0;
92
93     cout << "generating airport base for size = " << perimeter.size() << "\n";
94
95     fragment.init();
96     fragment.tile_ptr = t;
97
98     // find airport base material in the properties list
99     if ( ! material_mgr.find( APT_BASE_MATERIAL, fragment.material_ptr )) {
100         FG_LOG( FG_TERRAIN, FG_ALERT, 
101                 "Ack! unknown material name = " << APT_BASE_MATERIAL 
102                 << " in fgAptGenerat()" );
103     }
104
105     printf(" tile center = %.2f %.2f %.2f\n", 
106            t->center.x(), t->center.y(), t->center.z() );
107     printf(" airport center = %.2f %.2f %.2f\n", 
108            average.x(), average.y(), average.z());
109     fragment.center = average;
110
111     normal[0] = average.x();
112     normal[1] = average.y();
113     normal[2] = average.z();
114     MAT3_NORMALIZE_VEC(normal, temp);
115     
116     display_list = xglGenLists(1);
117     xglNewList(display_list, GL_COMPILE);
118     xglBegin(GL_TRIANGLE_FAN);
119
120     // first point center of fan
121     cart_trans = average - t->center;
122     t->nodes[t->ncount][0] = cart_trans.x();
123     t->nodes[t->ncount][1] = cart_trans.y();
124     t->nodes[t->ncount][2] = cart_trans.z();
125     center_num = t->ncount;
126     t->ncount++;
127
128     tex = calc_tex_coords( t->nodes[t->ncount-1], t->center );
129     xglTexCoord2f(tex.x(), tex.y());
130     xglNormal3dv(normal);
131     xglVertex3dv(t->nodes[t->ncount-1]);
132
133     // first point on perimeter
134     const_iterator current = perimeter.begin();
135     cart = fgGeodToCart( *current );
136     cart_trans = cart - t->center;
137     t->nodes[t->ncount][0] = cart_trans.x();
138     t->nodes[t->ncount][1] = cart_trans.y();
139     t->nodes[t->ncount][2] = cart_trans.z();
140     t->ncount++;
141
142     i = 1;
143     tex = calc_tex_coords( t->nodes[i], t->center );
144     dist = cart.distance3D(average);
145     if ( dist > max_dist ) {
146         max_dist = dist;
147     }
148     xglTexCoord2f(tex.x(), tex.y());
149     xglVertex3dv(t->nodes[i]);
150     ++current;
151     ++i;
152
153     const_iterator last = perimeter.end();
154     for ( ; current != last; ++current ) {
155         cart = fgGeodToCart( *current );
156         cart_trans = cart - t->center;
157         t->nodes[t->ncount][0] = cart_trans.x();
158         t->nodes[t->ncount][1] = cart_trans.y();
159         t->nodes[t->ncount][2] = cart_trans.z();
160         t->ncount++;
161         fragment.add_face(center_num, i - 1, i);
162
163         tex = calc_tex_coords( t->nodes[i], t->center );
164         dist = cart.distance3D(average);
165         if ( dist > max_dist ) {
166             max_dist = dist;
167         }
168         xglTexCoord2f(tex.x(), tex.y());
169         xglVertex3dv(t->nodes[i]);
170         i++;
171     }
172
173     // last point (first point in perimeter list)
174     current = perimeter.begin();
175     cart = fgGeodToCart( *current );
176     cart_trans = cart - t->center;
177     fragment.add_face(center_num, i - 1, 1);
178
179     tex = calc_tex_coords( t->nodes[1], t->center );
180     xglTexCoord2f(tex.x(), tex.y());
181     xglVertex3dv(t->nodes[1]);
182
183     xglEnd();
184     xglEndList();
185
186     fragment.bounding_radius = max_dist;
187     fragment.display_list = display_list;
188
189     t->fragment_list.push_back(fragment);
190 }
191
192
193 // Load a .apt file and register the GL fragments with the
194 // corresponding tile
195 int
196 fgAptGenerate(const string& path, fgTILE *tile)
197 {
198     string token;
199     string apt_id, apt_name;
200     char c;
201     int i = 1;
202
203     // face list (this indexes into the master tile vertex list)
204     container perimeter;
205     Point3D p, average;
206     double avex = 0.0, avey = 0.0, avez = 0.0;
207     int size;
208
209     // gpc_vertex p_2d, list_2d[MAX_PERIMETER];
210     // gpc_vertex_list perimeter_2d;
211
212     fg_gzifstream in( path );
213     if ( !in ) {
214         // exit immediately assuming an airport file for this tile
215         // doesn't exist.
216         return 0;
217     }
218
219     apt_id = "";
220
221     // read in each line of the file
222     in >> skipcomment;
223     while ( ! in.eof() )
224     {
225         in >> token;
226
227         if ( token == "a" ) {
228             // airport info record (start of airport)
229
230             if ( apt_id != "" ) {
231                 // we have just finished reading and airport record.
232                 // process the info
233                 gen_base(average, perimeter, tile);
234             }
235
236             cout << "Reading airport record\n";
237             in >> apt_id;
238             apt_name = "";
239             i = 1;
240             avex = avey = avez = 0.0;
241             perimeter.erase( perimeter.begin(), perimeter.end() );
242             // skip to end of line.
243             while ( in.get(c) && c != '\n' ) {
244                 apt_name += c;
245             }
246             cout << "\tID = " + apt_id + "  Name = " + apt_name + "\n";
247         } else if ( token == "p" ) {
248             // airport area bounding polygon coordinate.  These
249             // specify a convex hull that should already have been cut
250             // out of the base terrain.  The points are given in
251             // counter clockwise order and are specified in lon/lat
252             // degrees.
253             in >> p;
254             avex += tile->nodes[i][0];
255             avey += tile->nodes[i][1];
256             avez += tile->nodes[i][2];
257             perimeter.push_back(p);
258             ++i;
259         } else if ( token == "r" ) {
260             // runway record
261             // skip for now
262             while ( in.get(c) && c != '\n' );
263         }
264
265         in >> skipcomment;
266     }
267
268     if ( apt_id != "" ) {
269         // we have just finished reading and airport record.
270         // process the info
271         size = perimeter.size();
272         average = Point3D( avex / (double)size + tile->center.x(),
273                            avey / (double)size + tile->center.y(),
274                            avez / (double)size + tile->center.z() );
275
276         gen_base(average, perimeter, tile);
277     }
278
279     return 1;
280 }
281
282
283 // $Log$
284 // Revision 1.9  1998/11/06 21:17:32  curt
285 // Converted to new logstream debugging facility.  This allows release
286 // builds with no messages at all (and no performance impact) by using
287 // the -DFG_NDEBUG flag.
288 //
289 // Revision 1.8  1998/11/06 14:46:59  curt
290 // Changes to track Bernie's updates to fgstream.
291 //
292 // Revision 1.7  1998/10/20 18:26:06  curt
293 // Updates to point3d.hxx
294 //
295 // Revision 1.6  1998/10/18 01:17:16  curt
296 // Point3D tweaks.
297 //
298 // Revision 1.5  1998/10/16 23:27:14  curt
299 // C++-ifying.
300 //
301 // Revision 1.4  1998/10/16 00:51:46  curt
302 // Converted to Point3D class.
303 //
304 // Revision 1.3  1998/09/21 20:55:00  curt
305 // Used the cartesian form of the airport area coordinates to determine the
306 // center.
307 //
308 // Revision 1.2  1998/09/14 12:44:30  curt
309 // Don't recalculate perimeter points since it is not likely that they will match
310 // exactly with the previously calculated points, which will leave an ugly gap
311 // around the airport area.
312 //
313 // Revision 1.1  1998/09/14 02:14:01  curt
314 // Initial revision of genapt.[ch]xx for generating airport scenery.
315 //
316 //
317