]> git.mxchange.org Git - flightgear.git/blob - Airports/genapt.cxx
C++-ifying.
[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/fg_debug.h>
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 /*
54 // Calculate distance between to Point3D's
55 static double calc_dist(const Point3D& p1, const Point3D& p2) {
56     double x, y, z;
57     x = p1.x() - p2.x();
58     y = p1.y() - p2.y();
59     z = p1.z() - p2.z();
60     return sqrt(x*x + y*y + z*z);
61 }
62 */
63
64
65 #define FG_APT_BASE_TEX_CONSTANT 2000.0
66
67 #ifdef OLD_TEX_COORDS
68 // Calculate texture coordinates for a given point.
69 static fgPoint3d
70 calc_tex_coords(const fgPoint3d& p) {
71     fgPoint3d tex;
72
73     cout << "Texture coordinates = " << 
74         FG_APT_BASE_TEX_CONSTANT * p.lon << "  " << 
75         FG_APT_BASE_TEX_CONSTANT * p.lat << "\n";
76
77     tex.x = fmod(FG_APT_BASE_TEX_CONSTANT * p.lon, 10.0);
78     tex.y = fmod(FG_APT_BASE_TEX_CONSTANT * p.lat, 10.0);
79
80     if ( tex.x < 0.0 ) {
81         tex.x += 10.0;
82     }
83
84     if ( tex.y < 0.0 ) {
85         tex.y += 10.0;
86     }
87
88     cout << "Texture coordinates = " << tex.x << "  " << tex.y << "\n";
89
90     return tex;
91 }
92 #endif
93
94
95 // Calculate texture coordinates for a given point.
96 static Point3D calc_tex_coords(double *node, const Point3D& ref) {
97     Point3D cp;
98     Point3D pp;
99
100     cp.setvals( node[0] + ref.x(), node[1] + ref.y(), node[2] + ref.z() );
101
102     pp = fgCartToPolar3d(cp);
103
104     pp.setx( fmod(FG_APT_BASE_TEX_CONSTANT * pp.x(), 10.0) );
105     pp.sety( fmod(FG_APT_BASE_TEX_CONSTANT * pp.y(), 10.0) );
106
107     if ( pp.x() < 0.0 ) {
108         pp.setx( pp.x() + 10.0 );
109     }
110
111     if ( pp.y() < 0.0 ) {
112         pp.sety( pp.y() + 10.0 );
113     }
114
115     return(pp);
116 }
117
118
119 // generate the actual base area for the airport
120 static void
121 gen_base( const Point3D& average, const container& perimeter, fgTILE *t)
122 {
123     GLint display_list;
124     Point3D cart, cart_trans, tex;
125     MAT3vec normal;
126     double dist, max_dist, temp;
127     int center_num, i;
128
129     fgFRAGMENT fragment;
130
131     max_dist = 0.0;
132
133     cout << "generating airport base for size = " << perimeter.size() << "\n";
134
135     fragment.init();
136     fragment.tile_ptr = t;
137
138     // find airport base material in the properties list
139     if ( ! material_mgr.find( APT_BASE_MATERIAL, fragment.material_ptr )) {
140         fgPrintf( FG_TERRAIN, FG_ALERT, 
141                   "Ack! unknown material name = %s in fgAptGenerat()\n",
142                   APT_BASE_MATERIAL );
143     }
144
145     printf(" tile center = %.2f %.2f %.2f\n", 
146            t->center.x(), t->center.y(), t->center.z() );
147     printf(" airport center = %.2f %.2f %.2f\n", 
148            average.x(), average.y(), average.z());
149     fragment.center = average;
150
151     normal[0] = average.x();
152     normal[1] = average.y();
153     normal[2] = average.z();
154     MAT3_NORMALIZE_VEC(normal, temp);
155     
156     display_list = xglGenLists(1);
157     xglNewList(display_list, GL_COMPILE);
158     xglBegin(GL_TRIANGLE_FAN);
159
160     // first point center of fan
161     cart_trans = average - t->center;
162     t->nodes[t->ncount][0] = cart_trans.x();
163     t->nodes[t->ncount][1] = cart_trans.y();
164     t->nodes[t->ncount][2] = cart_trans.z();
165     center_num = t->ncount;
166     t->ncount++;
167
168     tex = calc_tex_coords( t->nodes[t->ncount-1], t->center );
169     xglTexCoord2f(tex.x(), tex.y());
170     xglNormal3dv(normal);
171     xglVertex3dv(t->nodes[t->ncount-1]);
172
173     // first point on perimeter
174     const_iterator current = perimeter.begin();
175     cart = fgGeodToCart( *current );
176     cart_trans = cart - t->center;
177     t->nodes[t->ncount][0] = cart_trans.x();
178     t->nodes[t->ncount][1] = cart_trans.y();
179     t->nodes[t->ncount][2] = cart_trans.z();
180     t->ncount++;
181
182     i = 1;
183     tex = calc_tex_coords( t->nodes[i], t->center );
184     dist = distance3D(average, cart);
185     if ( dist > max_dist ) {
186         max_dist = dist;
187     }
188     xglTexCoord2f(tex.x(), tex.y());
189     xglVertex3dv(t->nodes[i]);
190     ++current;
191     ++i;
192
193     const_iterator last = perimeter.end();
194     for ( ; current != last; ++current ) {
195         cart = fgGeodToCart( *current );
196         cart_trans = cart - t->center;
197         t->nodes[t->ncount][0] = cart_trans.x();
198         t->nodes[t->ncount][1] = cart_trans.y();
199         t->nodes[t->ncount][2] = cart_trans.z();
200         t->ncount++;
201         fragment.add_face(center_num, i - 1, i);
202
203         tex = calc_tex_coords( t->nodes[i], t->center );
204         dist = distance3D(average, cart);
205         if ( dist > max_dist ) {
206             max_dist = dist;
207         }
208         xglTexCoord2f(tex.x(), tex.y());
209         xglVertex3dv(t->nodes[i]);
210         i++;
211     }
212
213     // last point (first point in perimeter list)
214     current = perimeter.begin();
215     cart = fgGeodToCart( *current );
216     cart_trans = cart - t->center;
217     fragment.add_face(center_num, i - 1, 1);
218
219     tex = calc_tex_coords( t->nodes[1], t->center );
220     xglTexCoord2f(tex.x(), tex.y());
221     xglVertex3dv(t->nodes[1]);
222
223     xglEnd();
224     xglEndList();
225
226     fragment.bounding_radius = max_dist;
227     fragment.display_list = display_list;
228
229     t->fragment_list.push_back(fragment);
230 }
231
232
233 // Load a .apt file and register the GL fragments with the
234 // corresponding tile
235 int
236 fgAptGenerate(const string& path, fgTILE *tile)
237 {
238     string token;
239     string apt_id, apt_name;
240     char c;
241     int i = 1;
242
243     // face list (this indexes into the master tile vertex list)
244     container perimeter;
245     Point3D p, average;
246     double avex = 0.0, avey = 0.0, avez = 0.0;
247     int size;
248
249     // gpc_vertex p_2d, list_2d[MAX_PERIMETER];
250     // gpc_vertex_list perimeter_2d;
251
252     fg_gzifstream in( path );
253     if ( !in ) {
254         // exit immediately assuming an airport file for this tile
255         // doesn't exist.
256         return 0;
257     }
258
259     apt_id = "";
260
261     // read in each line of the file
262     in.eat_comments();
263     while ( ! in.eof() )
264     {
265         in.stream() >> token;
266
267         if ( token == "a" ) {
268             // airport info record (start of airport)
269
270             if ( apt_id != "" ) {
271                 // we have just finished reading and airport record.
272                 // process the info
273                 gen_base(average, perimeter, tile);
274             }
275
276             cout << "Reading airport record\n";
277             in.stream() >> apt_id;
278             apt_name = "";
279             i = 1;
280             avex = avey = avez = 0.0;
281             perimeter.erase( perimeter.begin(), perimeter.end() );
282             // skip to end of line.
283             while ( in.get(c) && c != '\n' ) {
284                 apt_name += c;
285             }
286             cout << "\tID = " + apt_id + "  Name = " + apt_name + "\n";
287         } else if ( token == "p" ) {
288             // airport area bounding polygon coordinate.  These
289             // specify a convex hull that should already have been cut
290             // out of the base terrain.  The points are given in
291             // counter clockwise order and are specified in lon/lat
292             // degrees.
293             in.stream() >> p;
294             avex += tile->nodes[i][0];
295             avey += tile->nodes[i][1];
296             avez += tile->nodes[i][2];
297             perimeter.push_back(p);
298             ++i;
299         } else if ( token == "r" ) {
300             // runway record
301             // skip for now
302             while ( in.get(c) && c != '\n' );
303         }
304
305         // airports.insert(a);
306         in.eat_comments();
307     }
308
309     if ( apt_id != "" ) {
310         // we have just finished reading and airport record.
311         // process the info
312         size = perimeter.size();
313         average.setvals( avex / (double)size + tile->center.x(),
314                          avey / (double)size + tile->center.y(),
315                          avez / (double)size + tile->center.z() );
316
317         gen_base(average, perimeter, tile);
318     }
319
320     return 1;
321 }
322
323
324 // $Log$
325 // Revision 1.5  1998/10/16 23:27:14  curt
326 // C++-ifying.
327 //
328 // Revision 1.4  1998/10/16 00:51:46  curt
329 // Converted to Point3D class.
330 //
331 // Revision 1.3  1998/09/21 20:55:00  curt
332 // Used the cartesian form of the airport area coordinates to determine the
333 // center.
334 //
335 // Revision 1.2  1998/09/14 12:44:30  curt
336 // Don't recalculate perimeter points since it is not likely that they will match
337 // exactly with the previously calculated points, which will leave an ugly gap
338 // around the airport area.
339 //
340 // Revision 1.1  1998/09/14 02:14:01  curt
341 // Initial revision of genapt.[ch]xx for generating airport scenery.
342 //
343 //
344