]> git.mxchange.org Git - flightgear.git/blob - src/Objects/obj.cxx
6d3336b54141801f273d93e0334bb497261e13c8
[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     FGBucket 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 sgTriArea( 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 sgTriArea() 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     short int n1, n2, n3;
341     float *p1, *p2, *p3;
342     sgVec3 result;
343
344     for ( int i = 0; i < num; ++i ) {
345         leaf->getTriangle( i, &n1, &n2, &n3 );
346         p1 = leaf->getVertex(n1);
347         p2 = leaf->getVertex(n2);
348         p3 = leaf->getVertex(n3);
349         double area = sgTriArea( p1, p2, p3 );
350         double num = area / factor;
351
352         // generate a light point for each unit of area
353         while ( num > 1.0 ) {
354             random_pt_inside_tri( result, p1, p2, p3 );
355             lights->add( result );
356             num -= 1.0;
357         }
358         // for partial units of area, use a zombie door method to
359         // create the proper random chance of a light being created
360         // for this triangle
361         if ( num > 0.0 ) {
362             if ( sg_random() <= num ) {
363                 // a zombie made it through our door
364                 random_pt_inside_tri( result, p1, p2, p3 );
365                 lights->add( result );
366             }
367         }
368     }
369 }
370
371
372 // Load a .obj file
373 ssgBranch *fgObjLoad( const string& path, FGTileEntry *t,
374                       ssgVertexArray *lights, const bool is_base)
375 {
376     FGNewMat *newmat;
377     string material;
378     Point3D pp;
379     // sgVec3 approx_normal;
380     // double normal[3], scale = 0.0;
381     // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
382     // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
383     // GLint display_list = 0;
384     int shading;
385     bool in_faces = false;
386     int vncount, vtcount;
387     int n1 = 0, n2 = 0, n3 = 0;
388     int tex;
389     // int last1 = 0, last2 = 0;
390     bool odd = false;
391     point_list nodes;
392     Point3D node;
393     Point3D center;
394     double scenery_version = 0.0;
395     double tex_width = 1000.0, tex_height = 1000.0;
396     bool shared_done = false;
397     int_list fan_vertices;
398     int_list fan_tex_coords;
399     int i;
400     ssgSimpleState *state = NULL;
401     sgVec3 *vtlist, *vnlist;
402     sgVec2 *tclist;
403
404     ssgBranch *tile = new ssgBranch () ;
405
406     tile -> setName ( (char *)path.c_str() ) ;
407
408     // Attempt to open "path.gz" or "path"
409     fg_gzifstream in( path );
410     if ( ! in.is_open() ) {
411         FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
412         FG_LOG( FG_TERRAIN, FG_ALERT, "default to ocean tile: " << path );
413
414         return fgGenTile( path, t );
415     }
416
417     shading = globals->get_options()->get_shading();
418
419     if ( is_base ) {
420         t->ncount = 0;
421     }
422     vncount = 0;
423     vtcount = 0;
424     if ( is_base ) {
425         t->bounding_radius = 0.0;
426     }
427     center = t->center;
428
429     StopWatch stopwatch;
430     stopwatch.start();
431
432     // ignore initial comments and blank lines. (priming the pump)
433     // in >> skipcomment;
434     // string line;
435
436     string token;
437     char c;
438
439 #ifdef __MWERKS__
440     while ( in.get(c) && c  != '\0' ) {
441         in.putback(c);
442 #else
443     while ( ! in.eof() ) {
444 #endif
445
446 #if defined( macintosh ) || defined( _MSC_VER )
447         in >> ::skipws;
448 #else
449         in >> skipws;
450 #endif
451
452         if ( in.get( c ) && c == '#' ) {
453             // process a comment line
454
455             // getline( in, line );
456             // cout << "comment = " << line << endl;
457
458             in >> token;
459
460             if ( token == "Version" ) {
461                 // read scenery versions number
462                 in >> scenery_version;
463                 // cout << "scenery_version = " << scenery_version << endl;
464             } else if ( token == "gbs" ) {
465                 // reference point (center offset)
466                 if ( is_base ) {
467                     in >> t->center >> t->bounding_radius;
468                 } else {
469                     Point3D junk1;
470                     double junk2;
471                     in >> junk1 >> junk2;
472                 }
473                 center = t->center;
474                 // cout << "center = " << center 
475                 //      << " radius = " << t->bounding_radius << endl;
476             } else if ( token == "bs" ) {
477                 // reference point (center offset)
478                 // (skip past this)
479                 Point3D junk1;
480                 double junk2;
481                 in >> junk1 >> junk2;
482             } else if ( token == "usemtl" ) {
483                 // material property specification
484
485                 // if first usemtl with shared_done = false, then set
486                 // shared_done true and build the ssg shared lists
487                 if ( ! shared_done ) {
488                     // sanity check
489                     if ( (int)nodes.size() != vncount ) {
490                         FG_LOG( FG_TERRAIN, FG_ALERT, 
491                                 "Tile has mismatched nodes = " << nodes.size()
492                                 << " and normals = " << vncount << " : " 
493                                 << path );
494                         // exit(-1);
495                     }
496                     shared_done = true;
497
498                     vtlist = new sgVec3 [ nodes.size() ];
499                     t->vec3_ptrs.push_back( vtlist );
500                     vnlist = new sgVec3 [ vncount ];
501                     t->vec3_ptrs.push_back( vnlist );
502                     tclist = new sgVec2 [ vtcount ];
503                     t->vec2_ptrs.push_back( tclist );
504
505                     for ( i = 0; i < (int)nodes.size(); ++i ) {
506                         sgSetVec3( vtlist[i], 
507                                    nodes[i][0], nodes[i][1], nodes[i][2] );
508                     }
509                     for ( i = 0; i < vncount; ++i ) {
510                         sgSetVec3( vnlist[i], 
511                                    normals[i][0], 
512                                    normals[i][1],
513                                    normals[i][2] );
514                     }
515                     for ( i = 0; i < vtcount; ++i ) {
516                         sgSetVec2( tclist[i],
517                                    tex_coords[i][0],
518                                    tex_coords[i][1] );
519                     }
520                 }
521
522                 // display_list = xglGenLists(1);
523                 // xglNewList(display_list, GL_COMPILE);
524                 // printf("xglGenLists(); xglNewList();\n");
525                 in_faces = false;
526
527                 // scan the material line
528                 in >> material;
529                 
530                 // find this material in the properties list
531
532                 newmat = material_lib.find( material );
533                 if ( newmat == NULL ) {
534                     // see if this is an on the fly texture
535                     string file = path;
536                     int pos = file.rfind( "/" );
537                     file = file.substr( 0, pos );
538                     cout << "current file = " << file << endl;
539                     file += "/";
540                     file += material;
541                     cout << "current file = " << file << endl;
542                     if ( ! material_lib.add_item( file ) ) {
543                         FG_LOG( FG_TERRAIN, FG_ALERT, 
544                                 "Ack! unknown usemtl name = " << material 
545                                 << " in " << path );
546                     } else {
547                         // locate our newly created material
548                         newmat = material_lib.find( material );
549                         if ( newmat == NULL ) {
550                             FG_LOG( FG_TERRAIN, FG_ALERT, 
551                                     "Ack! bad on the fly materia create = "
552                                     << material << " in " << path );
553                         }
554                     }
555                 }
556
557                 if ( newmat != NULL ) {
558                     // set the texture width and height values for this
559                     // material
560                     tex_width = newmat->get_xsize();
561                     tex_height = newmat->get_ysize();
562                     state = newmat->get_state();
563                     // cout << "(w) = " << tex_width << " (h) = " 
564                     //      << tex_width << endl;
565                 }
566             } else {
567                 // unknown comment, just gobble the input untill the
568                 // end of line
569
570                 in >> skipeol;
571             }
572         } else {
573             in.putback( c );
574         
575             in >> token;
576
577             // cout << "token = " << token << endl;
578
579             if ( token == "vn" ) {
580                 // vertex normal
581                 if ( vncount < FG_MAX_NODES ) {
582                     in >> normals[vncount][0]
583                        >> normals[vncount][1]
584                        >> normals[vncount][2];
585                     vncount++;
586                 } else {
587                     FG_LOG( FG_TERRAIN, FG_ALERT, 
588                             "Read too many vertex normals in " << path 
589                             << " ... dying :-(" );
590                     exit(-1);
591                 }
592             } else if ( token == "vt" ) {
593                 // vertex texture coordinate
594                 if ( vtcount < FG_MAX_NODES*3 ) {
595                     in >> tex_coords[vtcount][0]
596                        >> tex_coords[vtcount][1];
597                     vtcount++;
598                 } else {
599                     FG_LOG( FG_TERRAIN, FG_ALERT, 
600                             "Read too many vertex texture coords in " << path
601                             << " ... dying :-("
602                             );
603                     exit(-1);
604                 }
605             } else if ( token == "v" ) {
606                 // node (vertex)
607                 if ( t->ncount < FG_MAX_NODES ) {
608                     /* in >> nodes[t->ncount][0]
609                        >> nodes[t->ncount][1]
610                        >> nodes[t->ncount][2]; */
611                     in >> node;
612                     nodes.push_back(node);
613                     if ( is_base ) {
614                         t->ncount++;
615                     }
616                 } else {
617                     FG_LOG( FG_TERRAIN, FG_ALERT, 
618                             "Read too many nodes in " << path 
619                             << " ... dying :-(");
620                     exit(-1);
621                 }
622             } else if ( (token == "tf") || (token == "ts") || (token == "f") ) {
623                 // triangle fan, strip, or individual face
624                 // FG_LOG( FG_TERRAIN, FG_INFO, "new fan or strip");
625
626                 fan_vertices.clear();
627                 fan_tex_coords.clear();
628                 odd = true;
629
630                 // xglBegin(GL_TRIANGLE_FAN);
631
632                 in >> n1;
633                 fan_vertices.push_back( n1 );
634                 // xglNormal3dv(normals[n1]);
635                 if ( in.get( c ) && c == '/' ) {
636                     in >> tex;
637                     fan_tex_coords.push_back( tex );
638                     if ( scenery_version >= 0.4 ) {
639                         if ( tex_width > 0 ) {
640                             tclist[tex][0] *= (1000.0 / tex_width);
641                         }
642                         if ( tex_height > 0 ) {
643                             tclist[tex][1] *= (1000.0 / tex_height);
644                         }
645                     }
646                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
647                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
648                 } else {
649                     in.putback( c );
650                     pp = local_calc_tex_coords(nodes[n1], center);
651                 }
652                 // xglTexCoord2f(pp.x(), pp.y());
653                 // xglVertex3dv(nodes[n1].get_n());
654
655                 in >> n2;
656                 fan_vertices.push_back( n2 );
657                 // xglNormal3dv(normals[n2]);
658                 if ( in.get( c ) && c == '/' ) {
659                     in >> tex;
660                     fan_tex_coords.push_back( tex );
661                     if ( scenery_version >= 0.4 ) {
662                         if ( tex_width > 0 ) {
663                             tclist[tex][0] *= (1000.0 / tex_width);
664                         }
665                         if ( tex_height > 0 ) {
666                             tclist[tex][1] *= (1000.0 / tex_height);
667                         }
668                     }
669                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
670                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
671                 } else {
672                     in.putback( c );
673                     pp = local_calc_tex_coords(nodes[n2], center);
674                 }
675                 // xglTexCoord2f(pp.x(), pp.y());
676                 // xglVertex3dv(nodes[n2].get_n());
677                 
678                 // read all subsequent numbers until next thing isn't a number
679                 while ( true ) {
680 #if defined( macintosh ) || defined( _MSC_VER )
681                     in >> ::skipws;
682 #else
683                     in >> skipws;
684 #endif
685
686                     char c;
687                     in.get(c);
688                     in.putback(c);
689                     if ( ! isdigit(c) || in.eof() ) {
690                         break;
691                     }
692
693                     in >> n3;
694                     fan_vertices.push_back( n3 );
695                     // cout << "  triangle = " 
696                     //      << n1 << "," << n2 << "," << n3 
697                     //      << endl;
698                     // xglNormal3dv(normals[n3]);
699                     if ( in.get( c ) && c == '/' ) {
700                         in >> tex;
701                         fan_tex_coords.push_back( tex );
702                         if ( scenery_version >= 0.4 ) {
703                             if ( tex_width > 0 ) {
704                                 tclist[tex][0] *= (1000.0 / tex_width);
705                             }
706                             if ( tex_height > 0 ) {
707                                 tclist[tex][1] *= (1000.0 / tex_height);
708                             }
709                         }
710                         pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
711                         pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
712                     } else {
713                         in.putback( c );
714                         pp = local_calc_tex_coords(nodes[n3], center);
715                     }
716                     // xglTexCoord2f(pp.x(), pp.y());
717                     // xglVertex3dv(nodes[n3].get_n());
718
719                     if ( (token == "tf") || (token == "f") ) {
720                         // triangle fan
721                         n2 = n3;
722                     } else {
723                         // triangle strip
724                         odd = !odd;
725                         n1 = n2;
726                         n2 = n3;
727                     }
728                 }
729
730                 // xglEnd();
731
732                 // build the ssg entity
733                 int size = (int)fan_vertices.size();
734                 ssgVertexArray   *vl = new ssgVertexArray( size );
735                 ssgNormalArray   *nl = new ssgNormalArray( size );
736                 ssgTexCoordArray *tl = new ssgTexCoordArray( size );
737                 ssgColourArray   *cl = new ssgColourArray( 1 );
738
739                 sgVec4 color;
740                 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
741                 cl->add( color );
742
743                 sgVec2 tmp2;
744                 sgVec3 tmp3;
745                 for ( i = 0; i < size; ++i ) {
746                     sgCopyVec3( tmp3, vtlist[ fan_vertices[i] ] );
747                     vl -> add( tmp3 );
748
749                     sgCopyVec3( tmp3, vnlist[ fan_vertices[i] ] );
750                     nl -> add( tmp3 );
751
752                     sgCopyVec2( tmp2, tclist[ fan_tex_coords[i] ] );
753                     tl -> add( tmp2 );
754                 }
755
756                 ssgLeaf *leaf = NULL;
757                 if ( token == "tf" ) {
758                     // triangle fan
759                     leaf = 
760                         new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
761                 } else if ( token == "ts" ) {
762                     // triangle strip
763                     leaf = 
764                         new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
765                 } else if ( token == "f" ) {
766                     // triangle
767                     leaf = 
768                         new ssgVtxTable ( GL_TRIANGLES, vl, nl, tl, cl );
769                 }
770                 // leaf->makeDList();
771                 leaf->setState( state );
772
773                 tile->addKid( leaf );
774
775                 if ( is_base ) {
776                     // generate lighting
777                     if ( material == "Urban" || material == "BuiltUpCover" ) {
778                         gen_random_surface_points( leaf, lights, 100000.0 );
779                     } else if ( material == "EvergreenBroadCover" ||
780                                 material == "Default" || material == "Island" ||
781                                 material == "SomeSort" ||
782                                 material == "DeciduousBroadCover" ||
783                                 material == "EvergreenNeedleCover" ||
784                                 material == "DeciduousNeedleCover" ) {
785                         gen_random_surface_points( leaf, lights, 10000000.0 );
786                     } else if ( material == "MixedForestCover" ) {
787                         gen_random_surface_points( leaf, lights, 5000000.0 );
788                     } else if ( material == "WoodedTundraCover" ||
789                                 material == "BareTundraCover" ||
790                                 material == "HerbTundraCover" ||
791                                 material == "MixedTundraCover" ||
792                                 material == "Marsh" ||
793                                 material == "HerbWetlandCover" ||
794                                 material == "WoodedWetlandCover" ) {
795                         gen_random_surface_points( leaf, lights, 20000000.0 );
796                     } else if ( material == "ShrubCover" ||
797                                 material == "ShrubGrassCover" ) {
798                         gen_random_surface_points( leaf, lights, 4000000.0 );
799                     } else if ( material == "GrassCover" ||
800                                 material == "SavannaCover" ) {
801                         gen_random_surface_points( leaf, lights, 4000000.0 );
802                     } else if ( material == "MixedCropPastureCover" ||
803                                 material == "IrrCropPastureCover" ||
804                                 material == "DryCropPastureCover" ||
805                                 material == "CropGrassCover" ||
806                                 material == "CropWoodCover" ) {
807                         gen_random_surface_points( leaf, lights, 2000000.0 );
808                     }
809                 }
810             } else {
811                 FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in " 
812                         << path << " = " << token );
813             }
814
815             // eat white space before start of while loop so if we are
816             // done with useful input it is noticed before hand.
817 #if defined( macintosh ) || defined( _MSC_VER )
818             in >> ::skipws;
819 #else
820             in >> skipws;
821 #endif
822         }
823     }
824
825     if ( is_base ) {
826         t->nodes = nodes;
827     }
828
829     stopwatch.stop();
830     FG_LOG( FG_TERRAIN, FG_DEBUG, 
831             "Loaded " << path << " in " 
832             << stopwatch.elapsedSeconds() << " seconds" );
833
834     // Generate a cloud layer above the tiles
835     // if ( globals->get_options()->get_clouds() ) {
836     //          fgGenCloudTile(path, t, tile);
837     // }
838     return tile;
839 }
840
841