]> git.mxchange.org Git - flightgear.git/blob - src/Objects/obj.cxx
7eca9a6bf863e094b87a44a8b05c0d5b0424beb6
[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 <simgear/compiler.h>
41
42 #include STL_STRING
43 #include <map>                  // STL
44 #include <vector>               // STL
45 #include <ctype.h>              // isdigit()
46
47 #include <simgear/constants.h>
48 #include <simgear/debug/logstream.hxx>
49 #include <simgear/math/point3d.hxx>
50 #include <simgear/math/polar3d.hxx>
51 #include <simgear/math/sg_geodesy.hxx>
52 #include <simgear/math/sg_random.h>
53 #include <simgear/misc/fgstream.hxx>
54 #include <simgear/misc/stopwatch.hxx>
55 #include <simgear/misc/texcoord.hxx>
56
57 #include <Main/globals.hxx>
58 #include <Scenery/tileentry.hxx>
59
60 #include "matlib.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 #if 0
77 // given three points defining a triangle, calculate the normal
78 static void calc_normal(Point3D p1, Point3D p2, 
79                         Point3D p3, sgVec3 normal)
80 {
81     sgVec3 v1, v2;
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     sgVectorProductVec3( normal, v1, v2 );
87     sgNormalizeVec3( normal );
88
89     // fgPrintf( FG_TERRAIN, FG_DEBUG, "  Normal = %.2f %.2f %.2f\n", 
90     //           normal[0], normal[1], normal[2]);
91 }
92 #endif
93
94
95 #define FG_TEX_CONSTANT 69.0
96
97 // Calculate texture coordinates for a given point.
98 static Point3D local_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 = sgCartToPolar3d(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     FGNewMat *newmat;
136
137     ssgSimpleState *state = NULL;
138
139     ssgBranch *tile = new ssgBranch () ;
140     tile -> setName ( (char *)path.c_str() ) ;
141
142     double tex_width = 1000.0;
143     // double tex_height;
144
145     // find Ocean material in the properties list
146     newmat = material_lib.find( "Ocean" );
147     if ( newmat != NULL ) {
148         // set the texture width and height values for this
149         // material
150         tex_width = newmat->get_xsize();
151         // tex_height = newmat->get_ysize();
152         
153         // set ssgState
154         state = newmat->get_state();
155     } else {
156         FG_LOG( FG_TERRAIN, FG_ALERT, 
157                 "Ack! unknown usemtl name = " << "Ocean" 
158                 << " in " << path );
159     }
160
161     // Calculate center point
162     SGBucket b = t->tile_bucket;
163     double clon = b.get_center_lon();
164     double clat = b.get_center_lat();
165     double height = b.get_height();
166     double width = b.get_width();
167
168     Point3D center = sgGeodToCart(Point3D(clon*DEG_TO_RAD,clat*DEG_TO_RAD,0.0));
169     t->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] = sgGeodToCart(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     // cout << "bounding radius = " << t->bounding_radius << endl;
200
201     // Calculate normals
202     Point3D normals[4];
203     for ( i = 0; i < 4; ++i ) {
204         normals[i] = cart[i];
205         double length = normals[i].distance3D( Point3D(0.0) );
206         normals[i] /= length;
207         // cout << "normal = " << normals[i] << endl;
208     }
209
210     // Calculate texture coordinates
211     point_list geod_nodes;
212     geod_nodes.clear();
213     for ( i = 0; i < 4; ++i ) {
214         geod_nodes.push_back( geod[i] );
215     }
216     int_list rectangle;
217     rectangle.clear();
218     for ( i = 0; i < 4; ++i ) {
219         rectangle.push_back( i );
220     }
221     point_list texs = calc_tex_coords( b, geod_nodes, rectangle, 
222                                        1000.0 / tex_width );
223
224     // Allocate ssg structure
225     ssgVertexArray   *vl = new ssgVertexArray( 4 );
226     ssgNormalArray   *nl = new ssgNormalArray( 4 );
227     ssgTexCoordArray *tl = new ssgTexCoordArray( 4 );
228     ssgColourArray   *cl = new ssgColourArray( 1 );
229
230     sgVec4 color;
231     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
232     cl->add( color );
233
234     // sgVec3 *vtlist = new sgVec3 [ 4 ];
235     // t->vec3_ptrs.push_back( vtlist );
236     // sgVec3 *vnlist = new sgVec3 [ 4 ];
237     // t->vec3_ptrs.push_back( vnlist );
238     // sgVec2 *tclist = new sgVec2 [ 4 ];
239     // t->vec2_ptrs.push_back( tclist );
240
241     sgVec2 tmp2;
242     sgVec3 tmp3;
243     for ( i = 0; i < 4; ++i ) {
244         sgSetVec3( tmp3, 
245                    rel[i].x(), rel[i].y(), rel[i].z() );
246         vl->add( tmp3 );
247
248         sgSetVec3( tmp3, 
249                    normals[i].x(), normals[i].y(), normals[i].z() );
250         nl->add( tmp3 );
251
252         sgSetVec2( tmp2, texs[i].x(), texs[i].y());
253         tl->add( tmp2 );
254     }
255     
256     ssgLeaf *leaf = 
257         new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
258
259     leaf->setState( state );
260
261     tile->addKid( leaf );
262     // if ( globals->get_options()->get_clouds() ) {
263     //    fgGenCloudTile(path, t, tile);
264     // }
265
266     return tile;
267 }
268
269
270 static float fgTriArea( sgVec3 p0, sgVec3 p1, sgVec3 p2 ) {
271     /* 
272        From comp.graph.algorithms FAQ
273        2A(P) = abs(N.(sum_{i=0}^{n-1}(v_i x v_{i+1})))
274      */
275     sgVec3 sum;
276     sgZeroVec3( sum );
277         
278     sgVec3 norm;
279     sgMakeNormal( norm, p0, p1, p2 );
280         
281     float *vv[3];
282     vv[0] = p0;
283     vv[1] = p1;
284     vv[2] = p2;
285         
286     for( int i=0; i<3; i++ ) {
287         int ii = (i+1) % 3;
288         sum[0] += (vv[i][1] * vv[ii][2] - vv[i][2] * vv[ii][1]) ;
289         sum[1] += (vv[i][2] * vv[ii][0] - vv[i][0] * vv[ii][2]) ;
290         sum[2] += (vv[i][0] * vv[ii][1] - vv[i][1] * vv[ii][0]) ;
291     }
292
293     return( sgAbs(sgScalarProductVec3( norm, sum )) * SG_HALF );
294 }
295
296
297 #if 0
298 // this works too, but Norman claims fgTriArea() is more efficient :-)
299 static double triangle_area_3d( float *p1, float *p2, float *p3 ) {
300     // Heron's formula: A^2 = s(s-a)(s-b)(s-c) where A is the area,
301     // a,b,c are the side lengths, s=(a+b+c)/2. In R^3 you can compute
302     // the lengths of the sides with the distance formula, of course.
303
304     double a = sgDistanceVec3( p1, p2 );
305     double b = sgDistanceVec3( p2, p3 );
306     double c = sgDistanceVec3( p3, p1 );
307
308     double s = (a + b + c) / 2.0;
309
310     return sqrt( s * ( s - a ) * ( s - b ) * ( s - c ) );
311 }
312 #endif
313
314
315 static void random_pt_inside_tri( float *res,
316                                   float *n1, float *n2, float *n3 )
317 {
318     sgVec3 p1, p2, p3;
319
320     double a = sg_random();
321     double b = sg_random();
322     if ( a + b > 1.0 ) {
323         a = 1.0 - a;
324         b = 1.0 - b;
325     }
326     double c = 1 - a - b;
327
328     sgScaleVec3( p1, n1, a );
329     sgScaleVec3( p2, n2, b );
330     sgScaleVec3( p3, n3, c );
331
332     sgAddVec3( res, p1, p2 );
333     sgAddVec3( res, p3 );
334 }
335
336
337 static void gen_random_surface_points( ssgLeaf *leaf, ssgVertexArray *lights,
338                                        double factor ) {
339     int num = leaf->getNumTriangles();
340     if ( num > 0 ) {
341         short int n1, n2, n3;
342         float *p1, *p2, *p3;
343         sgVec3 result;
344
345         // generate a repeatable random seed
346         p1 = leaf->getVertex( 0 );
347         unsigned int *seed = (unsigned int *)p1;
348         sg_srandom( *seed );
349
350         for ( int i = 0; i < num; ++i ) {
351             leaf->getTriangle( i, &n1, &n2, &n3 );
352             p1 = leaf->getVertex(n1);
353             p2 = leaf->getVertex(n2);
354             p3 = leaf->getVertex(n3);
355             double area = fgTriArea( p1, p2, p3 );
356             double num = area / factor;
357
358             // generate a light point for each unit of area
359             while ( num > 1.0 ) {
360                 random_pt_inside_tri( result, p1, p2, p3 );
361                 lights->add( result );
362                 num -= 1.0;
363             }
364             // for partial units of area, use a zombie door method to
365             // create the proper random chance of a light being created
366             // for this triangle
367             if ( num > 0.0 ) {
368                 if ( sg_random() <= num ) {
369                     // a zombie made it through our door
370                     random_pt_inside_tri( result, p1, p2, p3 );
371                     lights->add( result );
372                 }
373             }
374         }
375     }
376 }
377
378
379 // Load a .obj file
380 ssgBranch *fgObjLoad( const string& path, FGTileEntry *t,
381                       ssgVertexArray *lights, const bool is_base)
382 {
383     FGNewMat *newmat = NULL;
384     string material;
385     float coverage = -1;
386     Point3D pp;
387     // sgVec3 approx_normal;
388     // double normal[3], scale = 0.0;
389     // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
390     // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
391     // GLint display_list = 0;
392     int shading;
393     bool in_faces = false;
394     int vncount, vtcount;
395     int n1 = 0, n2 = 0, n3 = 0;
396     int tex;
397     // int last1 = 0, last2 = 0;
398     bool odd = false;
399     point_list nodes;
400     Point3D node;
401     Point3D center;
402     double scenery_version = 0.0;
403     double tex_width = 1000.0, tex_height = 1000.0;
404     bool shared_done = false;
405     int_list fan_vertices;
406     int_list fan_tex_coords;
407     int i;
408     ssgSimpleState *state = NULL;
409     sgVec3 *vtlist, *vnlist;
410     sgVec2 *tclist;
411
412     ssgBranch *tile = new ssgBranch () ;
413
414     tile -> setName ( (char *)path.c_str() ) ;
415
416     // Attempt to open "path.gz" or "path"
417     fg_gzifstream in( path );
418     if ( ! in.is_open() ) {
419         FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
420         FG_LOG( FG_TERRAIN, FG_ALERT, "default to ocean tile: " << path );
421
422         return fgGenTile( path, t );
423     }
424
425     shading = globals->get_options()->get_shading();
426
427     if ( is_base ) {
428         t->ncount = 0;
429     }
430     vncount = 0;
431     vtcount = 0;
432     if ( is_base ) {
433         t->bounding_radius = 0.0;
434     }
435     center = t->center;
436
437     StopWatch stopwatch;
438     stopwatch.start();
439
440     // ignore initial comments and blank lines. (priming the pump)
441     // in >> skipcomment;
442     // string line;
443
444     string token;
445     char c;
446
447 #ifdef __MWERKS__
448     while ( in.get(c) && c  != '\0' ) {
449         in.putback(c);
450 #else
451     while ( ! in.eof() ) {
452 #endif
453
454 #if defined( macintosh ) || defined( _MSC_VER )
455         in >> ::skipws;
456 #else
457         in >> skipws;
458 #endif
459
460         if ( in.get( c ) && c == '#' ) {
461             // process a comment line
462
463             // getline( in, line );
464             // cout << "comment = " << line << endl;
465
466             in >> token;
467
468             if ( token == "Version" ) {
469                 // read scenery versions number
470                 in >> scenery_version;
471                 // cout << "scenery_version = " << scenery_version << endl;
472             } else if ( token == "gbs" ) {
473                 // reference point (center offset)
474                 if ( is_base ) {
475                     in >> t->center >> t->bounding_radius;
476                 } else {
477                     Point3D junk1;
478                     double junk2;
479                     in >> junk1 >> junk2;
480                 }
481                 center = t->center;
482                 // cout << "center = " << center 
483                 //      << " radius = " << t->bounding_radius << endl;
484             } else if ( token == "bs" ) {
485                 // reference point (center offset)
486                 // (skip past this)
487                 Point3D junk1;
488                 double junk2;
489                 in >> junk1 >> junk2;
490             } else if ( token == "usemtl" ) {
491                 // material property specification
492
493                 // if first usemtl with shared_done = false, then set
494                 // shared_done true and build the ssg shared lists
495                 if ( ! shared_done ) {
496                     // sanity check
497                     if ( (int)nodes.size() != vncount ) {
498                         FG_LOG( FG_TERRAIN, FG_ALERT, 
499                                 "Tile has mismatched nodes = " << nodes.size()
500                                 << " and normals = " << vncount << " : " 
501                                 << path );
502                         // exit(-1);
503                     }
504                     shared_done = true;
505
506                     vtlist = new sgVec3 [ nodes.size() ];
507                     t->vec3_ptrs.push_back( vtlist );
508                     vnlist = new sgVec3 [ vncount ];
509                     t->vec3_ptrs.push_back( vnlist );
510                     tclist = new sgVec2 [ vtcount ];
511                     t->vec2_ptrs.push_back( tclist );
512
513                     for ( i = 0; i < (int)nodes.size(); ++i ) {
514                         sgSetVec3( vtlist[i], 
515                                    nodes[i][0], nodes[i][1], nodes[i][2] );
516                     }
517                     for ( i = 0; i < vncount; ++i ) {
518                         sgSetVec3( vnlist[i], 
519                                    normals[i][0], 
520                                    normals[i][1],
521                                    normals[i][2] );
522                     }
523                     for ( i = 0; i < vtcount; ++i ) {
524                         sgSetVec2( tclist[i],
525                                    tex_coords[i][0],
526                                    tex_coords[i][1] );
527                     }
528                 }
529
530                 // display_list = xglGenLists(1);
531                 // xglNewList(display_list, GL_COMPILE);
532                 // printf("xglGenLists(); xglNewList();\n");
533                 in_faces = false;
534
535                 // scan the material line
536                 in >> material;
537                 
538                 // find this material in the properties list
539
540                 newmat = material_lib.find( material );
541                 if ( newmat == NULL ) {
542                     // see if this is an on the fly texture
543                     string file = path;
544                     int pos = file.rfind( "/" );
545                     file = file.substr( 0, pos );
546                     cout << "current file = " << file << endl;
547                     file += "/";
548                     file += material;
549                     cout << "current file = " << file << endl;
550                     if ( ! material_lib.add_item( file ) ) {
551                         FG_LOG( FG_TERRAIN, FG_ALERT, 
552                                 "Ack! unknown usemtl name = " << material 
553                                 << " in " << path );
554                     } else {
555                         // locate our newly created material
556                         newmat = material_lib.find( material );
557                         if ( newmat == NULL ) {
558                             FG_LOG( FG_TERRAIN, FG_ALERT, 
559                                     "Ack! bad on the fly materia create = "
560                                     << material << " in " << path );
561                         }
562                     }
563                 }
564
565                 if ( newmat != NULL ) {
566                     // set the texture width and height values for this
567                     // material
568                     tex_width = newmat->get_xsize();
569                     tex_height = newmat->get_ysize();
570                     state = newmat->get_state();
571                     coverage = newmat->get_light_coverage();
572                     // cout << "(w) = " << tex_width << " (h) = " 
573                     //      << tex_width << endl;
574                 } else {
575                     coverage = -1;
576                 }
577             } else {
578                 // unknown comment, just gobble the input until the
579                 // end of line
580
581                 in >> skipeol;
582             }
583         } else {
584             in.putback( c );
585         
586             in >> token;
587
588             // cout << "token = " << token << endl;
589
590             if ( token == "vn" ) {
591                 // vertex normal
592                 if ( vncount < FG_MAX_NODES ) {
593                     in >> normals[vncount][0]
594                        >> normals[vncount][1]
595                        >> normals[vncount][2];
596                     vncount++;
597                 } else {
598                     FG_LOG( FG_TERRAIN, FG_ALERT, 
599                             "Read too many vertex normals in " << path 
600                             << " ... dying :-(" );
601                     exit(-1);
602                 }
603             } else if ( token == "vt" ) {
604                 // vertex texture coordinate
605                 if ( vtcount < FG_MAX_NODES*3 ) {
606                     in >> tex_coords[vtcount][0]
607                        >> tex_coords[vtcount][1];
608                     vtcount++;
609                 } else {
610                     FG_LOG( FG_TERRAIN, FG_ALERT, 
611                             "Read too many vertex texture coords in " << path
612                             << " ... dying :-("
613                             );
614                     exit(-1);
615                 }
616             } else if ( token == "v" ) {
617                 // node (vertex)
618                 if ( t->ncount < FG_MAX_NODES ) {
619                     /* in >> nodes[t->ncount][0]
620                        >> nodes[t->ncount][1]
621                        >> nodes[t->ncount][2]; */
622                     in >> node;
623                     nodes.push_back(node);
624                     if ( is_base ) {
625                         t->ncount++;
626                     }
627                 } else {
628                     FG_LOG( FG_TERRAIN, FG_ALERT, 
629                             "Read too many nodes in " << path 
630                             << " ... dying :-(");
631                     exit(-1);
632                 }
633             } else if ( (token == "tf") || (token == "ts") || (token == "f") ) {
634                 // triangle fan, strip, or individual face
635                 // FG_LOG( FG_TERRAIN, FG_INFO, "new fan or strip");
636
637                 fan_vertices.clear();
638                 fan_tex_coords.clear();
639                 odd = true;
640
641                 // xglBegin(GL_TRIANGLE_FAN);
642
643                 in >> n1;
644                 fan_vertices.push_back( n1 );
645                 // xglNormal3dv(normals[n1]);
646                 if ( in.get( c ) && c == '/' ) {
647                     in >> tex;
648                     fan_tex_coords.push_back( tex );
649                     if ( scenery_version >= 0.4 ) {
650                         if ( tex_width > 0 ) {
651                             tclist[tex][0] *= (1000.0 / tex_width);
652                         }
653                         if ( tex_height > 0 ) {
654                             tclist[tex][1] *= (1000.0 / tex_height);
655                         }
656                     }
657                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
658                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
659                 } else {
660                     in.putback( c );
661                     pp = local_calc_tex_coords(nodes[n1], center);
662                 }
663                 // xglTexCoord2f(pp.x(), pp.y());
664                 // xglVertex3dv(nodes[n1].get_n());
665
666                 in >> n2;
667                 fan_vertices.push_back( n2 );
668                 // xglNormal3dv(normals[n2]);
669                 if ( in.get( c ) && c == '/' ) {
670                     in >> tex;
671                     fan_tex_coords.push_back( tex );
672                     if ( scenery_version >= 0.4 ) {
673                         if ( tex_width > 0 ) {
674                             tclist[tex][0] *= (1000.0 / tex_width);
675                         }
676                         if ( tex_height > 0 ) {
677                             tclist[tex][1] *= (1000.0 / tex_height);
678                         }
679                     }
680                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
681                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
682                 } else {
683                     in.putback( c );
684                     pp = local_calc_tex_coords(nodes[n2], center);
685                 }
686                 // xglTexCoord2f(pp.x(), pp.y());
687                 // xglVertex3dv(nodes[n2].get_n());
688                 
689                 // read all subsequent numbers until next thing isn't a number
690                 while ( true ) {
691 #if defined( macintosh ) || defined( _MSC_VER )
692                     in >> ::skipws;
693 #else
694                     in >> skipws;
695 #endif
696
697                     char c;
698                     in.get(c);
699                     in.putback(c);
700                     if ( ! isdigit(c) || in.eof() ) {
701                         break;
702                     }
703
704                     in >> n3;
705                     fan_vertices.push_back( n3 );
706                     // cout << "  triangle = " 
707                     //      << n1 << "," << n2 << "," << n3 
708                     //      << endl;
709                     // xglNormal3dv(normals[n3]);
710                     if ( in.get( c ) && c == '/' ) {
711                         in >> tex;
712                         fan_tex_coords.push_back( tex );
713                         if ( scenery_version >= 0.4 ) {
714                             if ( tex_width > 0 ) {
715                                 tclist[tex][0] *= (1000.0 / tex_width);
716                             }
717                             if ( tex_height > 0 ) {
718                                 tclist[tex][1] *= (1000.0 / tex_height);
719                             }
720                         }
721                         pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
722                         pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
723                     } else {
724                         in.putback( c );
725                         pp = local_calc_tex_coords(nodes[n3], center);
726                     }
727                     // xglTexCoord2f(pp.x(), pp.y());
728                     // xglVertex3dv(nodes[n3].get_n());
729
730                     if ( (token == "tf") || (token == "f") ) {
731                         // triangle fan
732                         n2 = n3;
733                     } else {
734                         // triangle strip
735                         odd = !odd;
736                         n1 = n2;
737                         n2 = n3;
738                     }
739                 }
740
741                 // xglEnd();
742
743                 // build the ssg entity
744                 int size = (int)fan_vertices.size();
745                 ssgVertexArray   *vl = new ssgVertexArray( size );
746                 ssgNormalArray   *nl = new ssgNormalArray( size );
747                 ssgTexCoordArray *tl = new ssgTexCoordArray( size );
748                 ssgColourArray   *cl = new ssgColourArray( 1 );
749
750                 sgVec4 color;
751                 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
752                 cl->add( color );
753
754                 sgVec2 tmp2;
755                 sgVec3 tmp3;
756                 for ( i = 0; i < size; ++i ) {
757                     sgCopyVec3( tmp3, vtlist[ fan_vertices[i] ] );
758                     vl -> add( tmp3 );
759
760                     sgCopyVec3( tmp3, vnlist[ fan_vertices[i] ] );
761                     nl -> add( tmp3 );
762
763                     sgCopyVec2( tmp2, tclist[ fan_tex_coords[i] ] );
764                     tl -> add( tmp2 );
765                 }
766
767                 ssgLeaf *leaf = NULL;
768                 if ( token == "tf" ) {
769                     // triangle fan
770                     leaf = 
771                         new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
772                 } else if ( token == "ts" ) {
773                     // triangle strip
774                     leaf = 
775                         new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
776                 } else if ( token == "f" ) {
777                     // triangle
778                     leaf = 
779                         new ssgVtxTable ( GL_TRIANGLES, vl, nl, tl, cl );
780                 }
781                 // leaf->makeDList();
782                 leaf->setState( state );
783
784                 tile->addKid( leaf );
785
786                 if ( is_base ) {
787                     if ( coverage > 0.0 ) {
788                         if ( coverage < 10000.0 ) {
789                             FG_LOG(FG_INPUT, FG_ALERT, "Light coverage is "
790                                    << coverage << ", pushing up to 10000");
791                             coverage = 10000;
792                         }
793                         gen_random_surface_points(leaf, lights, coverage);
794                     }
795 //                  // generate lighting
796 //                  if ( material == "Urban" || material == "BuiltUpCover" ) {
797 //                      gen_random_surface_points( leaf, lights, 100000.0 );
798 //                  } else if ( material == "EvergreenBroadCover" ||
799 //                              material == "Default" || material == "Island" ||
800 //                              material == "SomeSort" ||
801 //                              material == "DeciduousBroadCover" ||
802 //                              material == "EvergreenNeedleCover" ||
803 //                              material == "DeciduousNeedleCover" ) {
804 //                      gen_random_surface_points( leaf, lights, 10000000.0 );
805 //                  } else if ( material == "Road") {
806 //                      gen_random_surface_points( leaf, lights,  10000.0);
807 //                  } else if ( material == "MixedForestCover" ) {
808 //                      gen_random_surface_points( leaf, lights, 5000000.0 );
809 //                  } else if ( material == "WoodedTundraCover" ||
810 //                              material == "BareTundraCover" ||
811 //                              material == "HerbTundraCover" ||
812 //                              material == "MixedTundraCover" ||
813 //                              material == "Marsh" ||
814 //                              material == "HerbWetlandCover" ||
815 //                              material == "WoodedWetlandCover" ) {
816 //                      gen_random_surface_points( leaf, lights, 20000000.0 );
817 //                  } else if ( material == "ShrubCover" ||
818 //                              material == "ShrubGrassCover" ) {
819 //                      gen_random_surface_points( leaf, lights, 4000000.0 );
820 //                  } else if ( material == "GrassCover" ||
821 //                              material == "SavannaCover" ) {
822 //                      gen_random_surface_points( leaf, lights, 4000000.0 );
823 //                  } else if ( material == "MixedCropPastureCover" ||
824 //                              material == "IrrCropPastureCover" ||
825 //                              material == "DryCropPastureCover" ||
826 //                              material == "CropGrassCover" ||
827 //                              material == "CropWoodCover" ) {
828 //                      gen_random_surface_points( leaf, lights, 2000000.0 );
829 //                  }
830                 }
831             } else {
832                 FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in " 
833                         << path << " = " << token );
834             }
835
836             // eat white space before start of while loop so if we are
837             // done with useful input it is noticed before hand.
838 #if defined( macintosh ) || defined( _MSC_VER )
839             in >> ::skipws;
840 #else
841             in >> skipws;
842 #endif
843         }
844     }
845
846     if ( is_base ) {
847         t->nodes = nodes;
848     }
849
850     stopwatch.stop();
851     FG_LOG( FG_TERRAIN, FG_DEBUG, 
852             "Loaded " << path << " in " 
853             << stopwatch.elapsedSeconds() << " seconds" );
854
855     // Generate a cloud layer above the tiles
856     // if ( globals->get_options()->get_clouds() ) {
857     //          fgGenCloudTile(path, t, tile);
858     // }
859     return tile;
860 }
861
862