]> git.mxchange.org Git - flightgear.git/blob - src/Objects/obj.cxx
Add a key mapping (F3) for taking a screen snap shot.
[flightgear.git] / src / Objects / obj.cxx
1 // obj.cxx -- routines to handle "sorta" WaveFront .obj format files.
2 //
3 // Written by Curtis Olson, started October 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef FG_MATH_EXCEPTION_CLASH
29 #  include <math.h>
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34
35 // #if defined ( __sun__ )
36 // extern "C" void *memmove(void *, const void *, size_t);
37 // extern "C" void *memset(void *, int, size_t);
38 // #endif
39
40 #include <Include/compiler.h>
41
42 #include STL_STRING
43 #include <map>                  // STL
44 #include <vector>               // STL
45 #include <ctype.h>              // isdigit()
46
47 #include <Debug/logstream.hxx>
48 #include <Misc/fgstream.hxx>
49 #include <Include/fg_constants.h>
50 #include <Main/options.hxx>
51 #include <Math/mat3.h>
52 #include <Math/fg_geodesy.hxx>
53 #include <Math/fg_random.h>
54 #include <Math/point3d.hxx>
55 #include <Math/polar3d.hxx>
56 #include <Misc/stopwatch.hxx>
57 #include <Scenery/tileentry.hxx>
58 #include <Clouds/cloudobj.hxx>
59
60 #include "materialmgr.hxx"
61 #include "obj.hxx"
62
63 FG_USING_STD(string);
64 FG_USING_STD(vector);
65
66
67 typedef vector < int > int_list;
68 typedef int_list::iterator int_list_iterator;
69 typedef int_list::const_iterator int_point_list_iterator;
70
71
72 static double normals[FG_MAX_NODES][3];
73 static double tex_coords[FG_MAX_NODES*3][3];
74
75
76 // given three points defining a triangle, calculate the normal
77 static void calc_normal(Point3D p1, Point3D p2, 
78                         Point3D p3, double normal[3])
79 {
80     double v1[3], v2[3];
81     double temp;
82
83     v1[0] = p2[0] - p1[0]; v1[1] = p2[1] - p1[1]; v1[2] = p2[2] - p1[2];
84     v2[0] = p3[0] - p1[0]; v2[1] = p3[1] - p1[1]; v2[2] = p3[2] - p1[2];
85
86     MAT3cross_product(normal, v1, v2);
87     MAT3_NORMALIZE_VEC(normal,temp);
88
89     // fgPrintf( FG_TERRAIN, FG_DEBUG, "  Normal = %.2f %.2f %.2f\n", 
90     //           normal[0], normal[1], normal[2]);
91 }
92
93
94 #define FG_TEX_CONSTANT 69.0
95
96
97 // Calculate texture coordinates for a given point.
98 static Point3D calc_tex_coords(const Point3D& node, const Point3D& ref) {
99     Point3D cp;
100     Point3D pp;
101     // double tmplon, tmplat;
102
103     // cout << "-> " << node[0] << " " << node[1] << " " << node[2] << endl;
104     // cout << "-> " << ref.x() << " " << ref.y() << " " << ref.z() << endl;
105
106     cp = Point3D( node[0] + ref.x(),
107                   node[1] + ref.y(),
108                   node[2] + ref.z() );
109
110     pp = fgCartToPolar3d(cp);
111
112     // tmplon = pp.lon() * RAD_TO_DEG;
113     // tmplat = pp.lat() * RAD_TO_DEG;
114     // cout << tmplon << " " << tmplat << endl;
115
116     pp.setx( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.x(), 11.0) );
117     pp.sety( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.y(), 11.0) );
118
119     if ( pp.x() < 0.0 ) {
120         pp.setx( pp.x() + 11.0 );
121     }
122
123     if ( pp.y() < 0.0 ) {
124         pp.sety( pp.y() + 11.0 );
125     }
126
127     // cout << pp << endl;
128
129     return(pp);
130 }
131
132
133 // Generate a generic ocean tile on the fly
134 ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
135     fgFRAGMENT fragment;
136     fragment.init();
137     fragment.tile_ptr = t;
138
139     ssgSimpleState *state = NULL;
140
141     ssgBranch *tile = new ssgBranch () ;
142     tile -> setName ( (char *)path.c_str() ) ;
143
144     // find Ocean material in the properties list
145     if ( ! material_mgr.find( "Ocean", fragment.material_ptr )) {
146         FG_LOG( FG_TERRAIN, FG_ALERT, 
147                 "Ack! unknown usemtl name = " << "Ocean" 
148                 << " in " << path );
149     }
150
151     // set the texture width and height values for this
152     // material
153     FGMaterial m = fragment.material_ptr->get_m();
154     // double tex_width = m.get_xsize();
155     // double tex_height = m.get_ysize();
156
157     // set ssgState
158     state = fragment.material_ptr->get_state();
159
160     // Calculate center point
161     FGBucket b = t->tile_bucket;
162     double clon = b.get_center_lon();
163     double clat = b.get_center_lat();
164     double height = b.get_height();
165     double width = b.get_width();
166
167     Point3D center = fgGeodToCart(Point3D(clon*DEG_TO_RAD,clat*DEG_TO_RAD,0.0));
168     t->center = center;
169     fragment.center = center;
170     // cout << "center = " << center << endl;;
171     
172     // Caculate corner vertices
173     Point3D geod[4];
174     geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
175     geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
176     geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
177     geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
178
179     Point3D rad[4];
180     int i;
181     for ( i = 0; i < 4; ++i ) {
182         rad[i] = Point3D( geod[i].x() * DEG_TO_RAD, geod[i].y() * DEG_TO_RAD,
183                           geod[i].z() );
184     }
185
186     Point3D cart[4], rel[4];
187     t->nodes.clear();
188     for ( i = 0; i < 4; ++i ) {
189         cart[i] = fgGeodToCart(rad[i]);
190         rel[i] = cart[i] - center;
191         t->nodes.push_back( rel[i] );
192         // cout << "corner " << i << " = " << cart[i] << endl;
193     }
194
195     t->ncount = 4;
196
197     // Calculate bounding radius
198     t->bounding_radius = center.distance3D( cart[0] );
199     fragment.bounding_radius = t->bounding_radius;
200     // cout << "bounding radius = " << t->bounding_radius << endl;
201
202     // Calculate normals
203     Point3D normals[4];
204     for ( i = 0; i < 4; ++i ) {
205         normals[i] = cart[i];
206         double length = normals[i].distance3D( Point3D(0.0) );
207         normals[i] /= length;
208         // cout << "normal = " << normals[i] << endl;
209     }
210
211     // Calculate texture coordinates
212     Point3D texs[4];
213     for ( i = 0; i < 4; ++i ) {
214         texs[i] = calc_tex_coords( rel[i], center );
215         // cout << "texture coordinate = " << texs[i] << endl;
216     }
217
218     // Build flight gear structure
219     fragment.add_face(0, 1, 2);
220     fragment.add_face(0, 2, 3);
221     t->fragment_list.push_back(fragment);
222
223     // Build ssg structure
224     t->vtlist = new sgVec3 [ 4 ];
225     t->vnlist = new sgVec3 [ 4 ];
226     t->tclist = new sgVec2 [ 4 ];
227
228     for ( i = 0; i < 4; ++i ) {
229         sgSetVec3( t->vtlist[i], 
230                    rel[i].x(), rel[i].y(), rel[i].z() );
231         sgSetVec3( t->vnlist[i], 
232                    normals[i].x(), normals[i].y(), normals[i].z() );
233         sgSetVec2( t->tclist[i], texs[i].x(), texs[i].y() );
234     }
235     
236     unsigned short *vindex = new unsigned short [ 4 ];
237     t->free_ptrs.push_back( vindex );
238     unsigned short *tindex = new unsigned short [ 4 ];
239     t->free_ptrs.push_back( tindex );
240     for ( i = 0; i < 4; ++i ) {
241         vindex[i] = i;
242         tindex[i] = i;
243     }
244
245     ssgLeaf *leaf = 
246         new ssgVTable ( GL_TRIANGLE_FAN,
247                         4, vindex, t->vtlist,
248                         4, vindex, t->vnlist,
249                         4, tindex, t->tclist,
250                         0, NULL, NULL ) ;
251     leaf->setState( state );
252
253     tile->addKid( leaf );
254     if ( current_options.get_clouds() ) {
255         fgGenCloudTile(path, t, tile);
256     }
257
258     return tile;
259 }
260
261
262 // Load a .obj file and build the fragment list
263 ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
264     fgFRAGMENT fragment;
265     Point3D pp;
266     double approx_normal[3] /*, normal[3], scale = 0.0 */;
267     // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
268     // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
269     // GLint display_list = 0;
270     int shading;
271     bool in_fragment = false, in_faces = false;
272     int vncount, vtcount;
273     int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
274     int tex;
275     int last1 = 0, last2 = 0;
276     bool odd = false;
277     point_list nodes;
278     Point3D node;
279     Point3D center;
280     double scenery_version = 0.0;
281     double tex_width = 1000.0, tex_height = 1000.0;
282     bool shared_done = false;
283     int_list fan_vertices;
284     int_list fan_tex_coords;
285     int i;
286     ssgSimpleState *state = NULL;
287
288     ssgBranch *tile = new ssgBranch () ;
289
290     tile -> setName ( (char *)path.c_str() ) ;
291
292     // Attempt to open "path.gz" or "path"
293     fg_gzifstream in( path );
294     if ( ! in.is_open() ) {
295         FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
296         FG_LOG( FG_TERRAIN, FG_ALERT, "default to ocean tile: " << path );
297
298         return fgGenTile( path, t );
299     }
300
301     shading = current_options.get_shading();
302
303     in_fragment = false;
304     t->ncount = 0;
305     vncount = 0;
306     vtcount = 0;
307     t->bounding_radius = 0.0;
308     center = t->center;
309
310     StopWatch stopwatch;
311     stopwatch.start();
312
313     // ignore initial comments and blank lines. (priming the pump)
314     // in >> skipcomment;
315     // string line;
316
317     string token;
318     char c;
319
320 #ifdef __MWERKS__
321     while ( in.get(c) && c  != '\0' ) {
322         in.putback(c);
323 #else
324     while ( ! in.eof() ) {
325 #endif
326
327 #if defined( MACOS )
328         in >> ::skipws;
329 #else
330         in >> skipws;
331 #endif
332
333         if ( in.get( c ) && c == '#' ) {
334             // process a comment line
335
336             // getline( in, line );
337             // cout << "comment = " << line << endl;
338
339             in >> token;
340
341             if ( token == "Version" ) {
342                 // read scenery versions number
343                 in >> scenery_version;
344                 // cout << "scenery_version = " << scenery_version << endl;
345             } else if ( token == "gbs" ) {
346                 // reference point (center offset)
347                 in >> t->center >> t->bounding_radius;
348                 center = t->center;
349                 // cout << "center = " << center 
350                 //      << " radius = " << t->bounding_radius << endl;
351             } else if ( token == "bs" ) {
352                 // reference point (center offset)
353                 in >> fragment.center;
354                 in >> fragment.bounding_radius;
355
356                 // cout << "center = " << fragment.center 
357                 //      << " radius = " << fragment.bounding_radius << endl;
358             } else if ( token == "usemtl" ) {
359                 // material property specification
360
361                 // if first usemtl with shared_done = false, then set
362                 // shared_done true and build the ssg shared lists
363                 if ( ! shared_done ) {
364                     // sanity check
365                     if ( (int)nodes.size() != vncount ) {
366                         FG_LOG( FG_TERRAIN, FG_ALERT, 
367                                 "Tile has mismatched nodes and normals: " 
368                                 << path );
369                         // exit(-1);
370                     }
371                     shared_done = true;
372
373                     t->vtlist = new sgVec3 [ nodes.size() ];
374                     t->vnlist = new sgVec3 [ vncount ];
375                     t->tclist = new sgVec2 [ vtcount ];
376
377                     for ( i = 0; i < (int)nodes.size(); ++i ) {
378                         sgSetVec3( t->vtlist[i], 
379                                    nodes[i][0], nodes[i][1], nodes[i][2] );
380                     }
381                     for ( i = 0; i < vncount; ++i ) {
382                         sgSetVec3( t->vnlist[i], 
383                                    normals[i][0], 
384                                    normals[i][1],
385                                    normals[i][2] );
386                     }
387                     for ( i = 0; i < vtcount; ++i ) {
388                         sgSetVec2( t->tclist[i],
389                                    tex_coords[i][0],
390                                    tex_coords[i][1] );
391                     }
392                 }
393
394                 // series of individual triangles
395                 // if ( in_faces ) {
396                 //     xglEnd();
397                 // }
398
399                 // this also signals the start of a new fragment
400                 if ( in_fragment ) {
401                     // close out the previous structure and start the next
402                     // xglEndList();
403                     // printf("xglEnd(); xglEndList();\n");
404
405                     // update fragment
406                     // fragment.display_list = display_list;
407
408                     // push this fragment onto the tile's object list
409                     t->fragment_list.push_back(fragment);
410                 } else {
411                     in_fragment = true;
412                 }
413
414                 // printf("start of fragment (usemtl)\n");
415
416                 // display_list = xglGenLists(1);
417                 // xglNewList(display_list, GL_COMPILE);
418                 // printf("xglGenLists(); xglNewList();\n");
419                 in_faces = false;
420
421                 // reset the existing face list
422                 // printf("cleaning a fragment with %d faces\n", 
423                 //        fragment.faces.size());
424                 fragment.init();
425                 
426                 // scan the material line
427                 string material;
428                 in >> material;
429                 fragment.tile_ptr = t;
430                 
431                 // find this material in the properties list
432                 if ( ! material_mgr.find( material, fragment.material_ptr )) {
433                     FG_LOG( FG_TERRAIN, FG_ALERT, 
434                             "Ack! unknown usemtl name = " << material 
435                             << " in " << path );
436                 }
437
438                 // set the texture width and height values for this
439                 // material
440                 FGMaterial m = fragment.material_ptr->get_m();
441                 tex_width = m.get_xsize();
442                 tex_height = m.get_ysize();
443                 state = fragment.material_ptr->get_state();
444                 // cout << "(w) = " << tex_width << " (h) = " 
445                 //      << tex_width << endl;
446
447                 // initialize the fragment transformation matrix
448                 /*
449                  for ( i = 0; i < 16; i++ ) {
450                    fragment.matrix[i] = 0.0;
451                  }
452                  fragment.matrix[0] = fragment.matrix[5] =
453                  fragment.matrix[10] = fragment.matrix[15] = 1.0;
454                 */
455             } else {
456                 // unknown comment, just gobble the input untill the
457                 // end of line
458
459                 in >> skipeol;
460             }
461         } else {
462             in.putback( c );
463         
464             in >> token;
465
466             // cout << "token = " << token << endl;
467
468             if ( token == "vn" ) {
469                 // vertex normal
470                 if ( vncount < FG_MAX_NODES ) {
471                     in >> normals[vncount][0]
472                        >> normals[vncount][1]
473                        >> normals[vncount][2];
474                     vncount++;
475                 } else {
476                     FG_LOG( FG_TERRAIN, FG_ALERT, 
477                             "Read too many vertex normals in " << path 
478                             << " ... dying :-(" );
479                     exit(-1);
480                 }
481             } else if ( token == "vt" ) {
482                 // vertex texture coordinate
483                 if ( vtcount < FG_MAX_NODES*3 ) {
484                     in >> tex_coords[vtcount][0]
485                        >> tex_coords[vtcount][1];
486                     vtcount++;
487                 } else {
488                     FG_LOG( FG_TERRAIN, FG_ALERT, 
489                             "Read too many vertex texture coords in " << path
490                             << " ... dying :-("
491                             );
492                     exit(-1);
493                 }
494             } else if ( token == "v" ) {
495                 // node (vertex)
496                 if ( t->ncount < FG_MAX_NODES ) {
497                     /* in >> nodes[t->ncount][0]
498                        >> nodes[t->ncount][1]
499                        >> nodes[t->ncount][2]; */
500                     in >> node;
501                     nodes.push_back(node);
502                     t->ncount++;
503                 } else {
504                     FG_LOG( FG_TERRAIN, FG_ALERT, 
505                             "Read too many nodes in " << path 
506                             << " ... dying :-(");
507                     exit(-1);
508                 }
509             } else if ( token == "t" ) {
510                 // start a new triangle strip
511
512                 n1 = n2 = n3 = n4 = 0;
513
514                 // fgPrintf( FG_TERRAIN, FG_DEBUG, 
515                 //           "    new tri strip = %s", line);
516                 in >> n1 >> n2 >> n3;
517                 fragment.add_face(n1, n2, n3);
518
519                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = ");
520
521                 // xglBegin(GL_TRIANGLE_STRIP);
522                 // printf("xglBegin(tristrip) %d %d %d\n", n1, n2, n3);
523
524                 odd = true; 
525                 // scale = 1.0;
526
527                 if ( shading ) {
528                     // Shading model is "GL_SMOOTH" so use precalculated
529                     // (averaged) normals
530                     // MAT3_SCALE_VEC(normal, normals[n1], scale);
531                     // xglNormal3dv(normal);
532                     pp = calc_tex_coords(nodes[n1], center);
533                     // xglTexCoord2f(pp.lon(), pp.lat());
534                     // xglVertex3dv(nodes[n1].get_n());         
535
536                     // MAT3_SCALE_VEC(normal, normals[n2], scale);
537                     // xglNormal3dv(normal);
538                     pp = calc_tex_coords(nodes[n2], center);
539                     // xglTexCoord2f(pp.lon(), pp.lat());
540                     // xglVertex3dv(nodes[n2].get_n());                         
541
542                     // MAT3_SCALE_VEC(normal, normals[n3], scale);
543                     // xglNormal3dv(normal);
544                     pp = calc_tex_coords(nodes[n3], center);
545                     // xglTexCoord2f(pp.lon(), pp.lat());
546                     // xglVertex3dv(nodes[n3].get_n());
547                 } else {
548                     // Shading model is "GL_FLAT" so calculate per face
549                     // normals on the fly.
550                     if ( odd ) {
551                         calc_normal(nodes[n1], nodes[n2], 
552                                     nodes[n3], approx_normal);
553                     } else {
554                         calc_normal(nodes[n2], nodes[n1], 
555                                     nodes[n3], approx_normal);
556                     }
557                     // MAT3_SCALE_VEC(normal, approx_normal, scale);
558                     // xglNormal3dv(normal);
559
560                     pp = calc_tex_coords(nodes[n1], center);
561                     // xglTexCoord2f(pp.lon(), pp.lat());
562                     // xglVertex3dv(nodes[n1].get_n());         
563
564                     pp = calc_tex_coords(nodes[n2], center);
565                     // xglTexCoord2f(pp.lon(), pp.lat());
566                     // xglVertex3dv(nodes[n2].get_n());         
567                     
568                     pp = calc_tex_coords(nodes[n3], center);
569                     // xglTexCoord2f(pp.lon(), pp.lat());
570                     // xglVertex3dv(nodes[n3].get_n());         
571                 }
572                 // printf("some normals, texcoords, and vertices\n");
573
574                 odd = !odd;
575                 last1 = n2;
576                 last2 = n3;
577
578                 // There can be three or four values 
579                 char c;
580                 while ( in.get(c) ) {
581                     if ( c == '\n' ) {
582                         break; // only the one
583                     }
584                     if ( isdigit(c) ){
585                         in.putback(c);
586                         in >> n4;
587                         break;
588                     }
589                 }
590
591                 if ( n4 > 0 ) {
592                     fragment.add_face(n3, n2, n4);
593                     
594                     if ( shading ) {
595                         // Shading model is "GL_SMOOTH"
596                         // MAT3_SCALE_VEC(normal, normals[n4], scale);
597                     } else {
598                         // Shading model is "GL_FLAT"
599                         calc_normal(nodes[n3], nodes[n2], nodes[n4], 
600                                     approx_normal);
601                         // MAT3_SCALE_VEC(normal, approx_normal, scale);
602                     }
603                     // xglNormal3dv(normal);
604                     pp = calc_tex_coords(nodes[n4], center);
605                     // xglTexCoord2f(pp.lon(), pp.lat());
606                     // xglVertex3dv(nodes[n4].get_n());         
607                     
608                     odd = !odd;
609                     last1 = n3;
610                     last2 = n4;
611                     // printf("a normal, texcoord, and vertex (4th)\n");
612                 }
613             } else if ( (token == "tf") || (token == "ts") ) {
614                 // triangle fan
615                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "new fan");
616
617                 fan_vertices.clear();
618                 fan_tex_coords.clear();
619                 odd = true;
620
621                 // xglBegin(GL_TRIANGLE_FAN);
622
623                 in >> n1;
624                 fan_vertices.push_back( n1 );
625                 // xglNormal3dv(normals[n1]);
626                 if ( in.get( c ) && c == '/' ) {
627                     in >> tex;
628                     fan_tex_coords.push_back( tex );
629                     if ( scenery_version >= 0.4 ) {
630                         if ( tex_width > 0 ) {
631                             t->tclist[tex][0] *= (1000.0 / tex_width);
632                         }
633                         if ( tex_height > 0 ) {
634                             t->tclist[tex][1] *= (1000.0 / tex_height);
635                         }
636                     }
637                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
638                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
639                 } else {
640                     in.putback( c );
641                     pp = calc_tex_coords(nodes[n1], center);
642                 }
643                 // xglTexCoord2f(pp.x(), pp.y());
644                 // xglVertex3dv(nodes[n1].get_n());
645
646                 in >> n2;
647                 fan_vertices.push_back( n2 );
648                 // xglNormal3dv(normals[n2]);
649                 if ( in.get( c ) && c == '/' ) {
650                     in >> tex;
651                     fan_tex_coords.push_back( tex );
652                     if ( scenery_version >= 0.4 ) {
653                         if ( tex_width > 0 ) {
654                             t->tclist[tex][0] *= (1000.0 / tex_width);
655                         }
656                         if ( tex_height > 0 ) {
657                             t->tclist[tex][1] *= (1000.0 / tex_height);
658                         }
659                     }
660                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
661                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
662                 } else {
663                     in.putback( c );
664                     pp = calc_tex_coords(nodes[n2], center);
665                 }
666                 // xglTexCoord2f(pp.x(), pp.y());
667                 // xglVertex3dv(nodes[n2].get_n());
668                 
669                 // read all subsequent numbers until next thing isn't a number
670                 while ( true ) {
671 #if defined( MACOS )
672                     in >> ::skipws;
673 #else
674                     in >> skipws;
675 #endif
676
677                     char c;
678                     in.get(c);
679                     in.putback(c);
680                     if ( ! isdigit(c) || in.eof() ) {
681                         break;
682                     }
683
684                     in >> n3;
685                     fan_vertices.push_back( n3 );
686                     // cout << "  triangle = " 
687                     //      << n1 << "," << n2 << "," << n3 
688                     //      << endl;
689                     // xglNormal3dv(normals[n3]);
690                     if ( in.get( c ) && c == '/' ) {
691                         in >> tex;
692                         fan_tex_coords.push_back( tex );
693                         if ( scenery_version >= 0.4 ) {
694                             if ( tex_width > 0 ) {
695                                 t->tclist[tex][0] *= (1000.0 / tex_width);
696                             }
697                             if ( tex_height > 0 ) {
698                                 t->tclist[tex][1] *= (1000.0 / tex_height);
699                             }
700                         }
701                         pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
702                         pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
703                     } else {
704                         in.putback( c );
705                         pp = calc_tex_coords(nodes[n3], center);
706                     }
707                     // xglTexCoord2f(pp.x(), pp.y());
708                     // xglVertex3dv(nodes[n3].get_n());
709
710                     if ( token == "tf" ) {
711                         // triangle fan
712                         fragment.add_face(n1, n2, n3);
713                         n2 = n3;
714                     } else {
715                         // triangle strip
716                         if ( odd ) {
717                             fragment.add_face(n1, n2, n3);
718                         } else {
719                             fragment.add_face(n2, n1, n3);
720                         }
721                         odd = !odd;
722                         n1 = n2;
723                         n2 = n3;
724                     }
725                 }
726
727                 // xglEnd();
728
729                 // build the ssg entity
730                 unsigned short *vindex = 
731                     new unsigned short [ fan_vertices.size() ];
732                 t->free_ptrs.push_back( vindex );
733
734                 unsigned short *tindex = 
735                     new unsigned short [ fan_tex_coords.size() ];
736                 t->free_ptrs.push_back( tindex );
737
738                 for ( i = 0; i < (int)fan_vertices.size(); ++i ) {
739                     vindex[i] = fan_vertices[i];
740                 }
741                 for ( i = 0; i < (int)fan_tex_coords.size(); ++i ) {
742                     tindex[i] = fan_tex_coords[i];
743                 }
744                 ssgLeaf *leaf;
745                 if ( token == "tf" ) {
746                     // triangle fan
747                     leaf = 
748                         new ssgVTable ( GL_TRIANGLE_FAN,
749                                         fan_vertices.size(), vindex, t->vtlist,
750                                         fan_vertices.size(), vindex, t->vnlist,
751                                         fan_tex_coords.size(), tindex,t->tclist,
752                                         0, NULL, NULL ) ;
753                 } else {
754                     // triangle strip
755                     leaf = 
756                         new ssgVTable ( GL_TRIANGLE_STRIP,
757                                         fan_vertices.size(), vindex, t->vtlist,
758                                         fan_vertices.size(), vindex, t->vnlist,
759                                         fan_tex_coords.size(), tindex,t->tclist,
760                                         0, NULL, NULL ) ;
761                 }
762                 leaf->setState( state );
763
764                 tile->addKid( leaf );
765
766             } else if ( token == "f" ) {
767                 // unoptimized face
768
769                 if ( !in_faces ) {
770                     // xglBegin(GL_TRIANGLES);
771                     // printf("xglBegin(triangles)\n");
772                     in_faces = true;
773                 }
774
775                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
776                 in >> n1 >> n2 >> n3;
777                 fragment.add_face(n1, n2, n3);
778
779                 // xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
780                 // xglNormal3dv(normals[n1]);
781                 pp = calc_tex_coords(nodes[n1], center);
782                 // xglTexCoord2f(pp.lon(), pp.lat());
783                 // xglVertex3dv(nodes[n1].get_n());
784
785                 // xglNormal3dv(normals[n2]);
786                 pp = calc_tex_coords(nodes[n2], center);
787                 // xglTexCoord2f(pp.lon(), pp.lat());
788                 // xglVertex3dv(nodes[n2].get_n());
789                 
790                 // xglNormal3dv(normals[n3]);
791                 pp = calc_tex_coords(nodes[n3], center);
792                 // xglTexCoord2f(pp.lon(), pp.lat());
793                 // xglVertex3dv(nodes[n3].get_n());
794                 // printf("some normals, texcoords, and vertices (tris)\n");
795             } else if ( token == "q" ) {
796                 // continue a triangle strip
797                 n1 = n2 = 0;
798
799                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ", 
800                 //           line);
801                 in >> n1;
802
803                 // There can be one or two values 
804                 char c;
805                 while ( in.get(c) ) {
806                     if ( c == '\n' ) {
807                         break; // only the one
808                     }
809
810                     if ( isdigit(c) ) {
811                         in.putback(c);
812                         in >> n2;
813                         break;
814                     }
815                 }
816                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2);
817
818                 if ( odd ) {
819                     fragment.add_face(last1, last2, n1);
820                 } else {
821                     fragment.add_face(last2, last1, n1);
822                 }
823
824                 if ( shading ) {
825                     // Shading model is "GL_SMOOTH"
826                     // MAT3_SCALE_VEC(normal, normals[n1], scale);
827                 } else {
828                     // Shading model is "GL_FLAT"
829                     if ( odd ) {
830                         calc_normal(nodes[last1], nodes[last2], 
831                                     nodes[n1], approx_normal);
832                     } else {
833                         calc_normal(nodes[last2], nodes[last1], 
834                                     nodes[n1], approx_normal);
835                     }
836                     // MAT3_SCALE_VEC(normal, approx_normal, scale);
837                 }
838                 // xglNormal3dv(normal);
839
840                 pp = calc_tex_coords(nodes[n1], center);
841                 // xglTexCoord2f(pp.lon(), pp.lat());
842                 // xglVertex3dv(nodes[n1].get_n());
843                 // printf("a normal, texcoord, and vertex (4th)\n");
844    
845                 odd = !odd;
846                 last1 = last2;
847                 last2 = n1;
848
849                 if ( n2 > 0 ) {
850                     // fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n");
851
852                     if ( odd ) {
853                         fragment.add_face(last1, last2, n2);
854                     } else {
855                         fragment.add_face(last2, last1, n2);
856                     }
857
858                     if ( shading ) {
859                         // Shading model is "GL_SMOOTH"
860                         // MAT3_SCALE_VEC(normal, normals[n2], scale);
861                     } else {
862                         // Shading model is "GL_FLAT"
863                         if ( odd ) {
864                             calc_normal(nodes[last1], nodes[last2], 
865                                         nodes[n2], approx_normal);
866                         } else {
867                             calc_normal(nodes[last2], nodes[last1], 
868                                         nodes[n2], approx_normal);
869                         }
870                         // MAT3_SCALE_VEC(normal, approx_normal, scale);
871                     }
872                     // xglNormal3dv(normal);
873                 
874                     pp = calc_tex_coords(nodes[n2], center);
875                     // xglTexCoord2f(pp.lon(), pp.lat());
876                     // xglVertex3dv(nodes[n2].get_n());         
877                     // printf("a normal, texcoord, and vertex (4th)\n");
878
879                     odd = !odd;
880                     last1 = last2;
881                     last2 = n2;
882                 }
883             } else {
884                 FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in " 
885                         << path << " = " << token );
886             }
887
888             // eat white space before start of while loop so if we are
889             // done with useful input it is noticed before hand.
890 #if defined( MACOS )
891             in >> ::skipws;
892 #else
893             in >> skipws;
894 #endif
895         }
896     }
897
898     if ( in_fragment ) {
899         // close out the previous structure and start the next
900         // xglEnd();
901         // xglEndList();
902         // printf("xglEnd(); xglEndList();\n");
903         
904         // update fragment
905         // fragment.display_list = display_list;
906         
907         // push this fragment onto the tile's object list
908         t->fragment_list.push_back(fragment);
909     }
910
911 #if 0
912     // Draw normal vectors (for visually verifying normals)
913     xglBegin(GL_LINES);
914     xglColor3f(0.0, 0.0, 0.0);
915     for ( i = 0; i < t->ncount; i++ ) {
916         xglVertex3d(nodes[i][0],
917                     nodes[i][1] ,
918                     nodes[i][2]);
919         xglVertex3d(nodes[i][0] + 500*normals[i][0],
920                     nodes[i][1] + 500*normals[i][1],
921                     nodes[i][2] + 500*normals[i][2]);
922     } 
923     xglEnd();
924 #endif
925
926     t->nodes = nodes;
927
928     stopwatch.stop();
929     FG_LOG( FG_TERRAIN, FG_DEBUG, 
930             "Loaded " << path << " in " 
931             << stopwatch.elapsedSeconds() << " seconds" );
932
933     // Generate a cloud layer above the tiles
934     if ( current_options.get_clouds() ) {
935         fgGenCloudTile(path, t, tile);
936     }
937     return tile;
938 }
939
940