]> git.mxchange.org Git - flightgear.git/blob - src/Objects/obj.cxx
Added a hardcoded "LIGHTING" material entry in the matlib.
[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 void gen_random_surface_points( ssgLeaf *leaf, ssgVertexArray *lights ) {
271     int num = leaf->getNumVertices();
272     float *n1, *n2, *n3;
273     sgVec3 p1, p2, p3;
274     sgVec3 result;
275     float remainder = 1.0;
276     float weight;
277
278     n1 = leaf->getVertex( 1 );
279     n2 = leaf->getVertex( 2 );
280     for ( int i = 3; i < num; ++i ) {
281         n3 = leaf->getVertex( i );
282         weight = sg_random();
283         remainder -= weight;
284         sgScaleVec3( p1, n1, weight );
285
286         weight = sg_random() * remainder;
287         remainder -= weight;
288         sgScaleVec3( p2, n2, weight );
289
290         sgScaleVec3( p3, n3, remainder );
291
292         sgAddVec3( result, p1, p2 );
293         sgAddVec3( result, p3 );
294
295         sgScaleVec3( result, 1.0 / 3.0 );
296         lights->add( result );
297     }
298 }
299
300
301 // Load a .obj file
302 ssgBranch *fgObjLoad( const string& path, FGTileEntry *t, const bool is_base) {
303     FGNewMat *newmat;
304     Point3D pp;
305     // sgVec3 approx_normal;
306     // double normal[3], scale = 0.0;
307     // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
308     // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
309     // GLint display_list = 0;
310     int shading;
311     bool in_faces = false;
312     int vncount, vtcount;
313     int n1 = 0, n2 = 0, n3 = 0;
314     int tex;
315     // int last1 = 0, last2 = 0;
316     bool odd = false;
317     point_list nodes;
318     Point3D node;
319     Point3D center;
320     double scenery_version = 0.0;
321     double tex_width = 1000.0, tex_height = 1000.0;
322     bool shared_done = false;
323     int_list fan_vertices;
324     int_list fan_tex_coords;
325     int i;
326     ssgSimpleState *state = NULL;
327     sgVec3 *vtlist, *vnlist;
328     sgVec2 *tclist;
329
330     ssgBranch *tile = new ssgBranch () ;
331
332     tile -> setName ( (char *)path.c_str() ) ;
333
334     // Attempt to open "path.gz" or "path"
335     fg_gzifstream in( path );
336     if ( ! in.is_open() ) {
337         FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
338         FG_LOG( FG_TERRAIN, FG_ALERT, "default to ocean tile: " << path );
339
340         return fgGenTile( path, t );
341     }
342
343     shading = globals->get_options()->get_shading();
344
345     if ( is_base ) {
346         t->ncount = 0;
347     }
348     vncount = 0;
349     vtcount = 0;
350     if ( is_base ) {
351         t->bounding_radius = 0.0;
352     }
353     center = t->center;
354
355     StopWatch stopwatch;
356     stopwatch.start();
357
358     // ignore initial comments and blank lines. (priming the pump)
359     // in >> skipcomment;
360     // string line;
361
362     string token;
363     char c;
364
365 #ifdef __MWERKS__
366     while ( in.get(c) && c  != '\0' ) {
367         in.putback(c);
368 #else
369     while ( ! in.eof() ) {
370 #endif
371
372 #if defined( macintosh ) || defined( _MSC_VER )
373         in >> ::skipws;
374 #else
375         in >> skipws;
376 #endif
377
378         if ( in.get( c ) && c == '#' ) {
379             // process a comment line
380
381             // getline( in, line );
382             // cout << "comment = " << line << endl;
383
384             in >> token;
385
386             if ( token == "Version" ) {
387                 // read scenery versions number
388                 in >> scenery_version;
389                 // cout << "scenery_version = " << scenery_version << endl;
390             } else if ( token == "gbs" ) {
391                 // reference point (center offset)
392                 if ( is_base ) {
393                     in >> t->center >> t->bounding_radius;
394                 } else {
395                     Point3D junk1;
396                     double junk2;
397                     in >> junk1 >> junk2;
398                 }
399                 center = t->center;
400                 // cout << "center = " << center 
401                 //      << " radius = " << t->bounding_radius << endl;
402             } else if ( token == "bs" ) {
403                 // reference point (center offset)
404                 // (skip past this)
405                 Point3D junk1;
406                 double junk2;
407                 in >> junk1 >> junk2;
408             } else if ( token == "usemtl" ) {
409                 // material property specification
410
411                 // if first usemtl with shared_done = false, then set
412                 // shared_done true and build the ssg shared lists
413                 if ( ! shared_done ) {
414                     // sanity check
415                     if ( (int)nodes.size() != vncount ) {
416                         FG_LOG( FG_TERRAIN, FG_ALERT, 
417                                 "Tile has mismatched nodes = " << nodes.size()
418                                 << " and normals = " << vncount << " : " 
419                                 << path );
420                         // exit(-1);
421                     }
422                     shared_done = true;
423
424                     vtlist = new sgVec3 [ nodes.size() ];
425                     t->vec3_ptrs.push_back( vtlist );
426                     vnlist = new sgVec3 [ vncount ];
427                     t->vec3_ptrs.push_back( vnlist );
428                     tclist = new sgVec2 [ vtcount ];
429                     t->vec2_ptrs.push_back( tclist );
430
431                     for ( i = 0; i < (int)nodes.size(); ++i ) {
432                         sgSetVec3( vtlist[i], 
433                                    nodes[i][0], nodes[i][1], nodes[i][2] );
434                     }
435                     for ( i = 0; i < vncount; ++i ) {
436                         sgSetVec3( vnlist[i], 
437                                    normals[i][0], 
438                                    normals[i][1],
439                                    normals[i][2] );
440                     }
441                     for ( i = 0; i < vtcount; ++i ) {
442                         sgSetVec2( tclist[i],
443                                    tex_coords[i][0],
444                                    tex_coords[i][1] );
445                     }
446                 }
447
448                 // display_list = xglGenLists(1);
449                 // xglNewList(display_list, GL_COMPILE);
450                 // printf("xglGenLists(); xglNewList();\n");
451                 in_faces = false;
452
453                 // scan the material line
454                 string material;
455                 in >> material;
456                 
457                 // find this material in the properties list
458
459                 newmat = material_lib.find( material );
460                 if ( newmat == NULL ) {
461                     // see if this is an on the fly texture
462                     string file = path;
463                     int pos = file.rfind( "/" );
464                     file = file.substr( 0, pos );
465                     cout << "current file = " << file << endl;
466                     file += "/";
467                     file += material;
468                     cout << "current file = " << file << endl;
469                     if ( ! material_lib.add_item( file ) ) {
470                         FG_LOG( FG_TERRAIN, FG_ALERT, 
471                                 "Ack! unknown usemtl name = " << material 
472                                 << " in " << path );
473                     } else {
474                         // locate our newly created material
475                         newmat = material_lib.find( material );
476                         if ( newmat == NULL ) {
477                             FG_LOG( FG_TERRAIN, FG_ALERT, 
478                                     "Ack! bad on the fly materia create = "
479                                     << material << " in " << path );
480                         }
481                     }
482                 }
483
484                 if ( newmat != NULL ) {
485                     // set the texture width and height values for this
486                     // material
487                     tex_width = newmat->get_xsize();
488                     tex_height = newmat->get_ysize();
489                     state = newmat->get_state();
490                     // cout << "(w) = " << tex_width << " (h) = " 
491                     //      << tex_width << endl;
492                 }
493             } else {
494                 // unknown comment, just gobble the input untill the
495                 // end of line
496
497                 in >> skipeol;
498             }
499         } else {
500             in.putback( c );
501         
502             in >> token;
503
504             // cout << "token = " << token << endl;
505
506             if ( token == "vn" ) {
507                 // vertex normal
508                 if ( vncount < FG_MAX_NODES ) {
509                     in >> normals[vncount][0]
510                        >> normals[vncount][1]
511                        >> normals[vncount][2];
512                     vncount++;
513                 } else {
514                     FG_LOG( FG_TERRAIN, FG_ALERT, 
515                             "Read too many vertex normals in " << path 
516                             << " ... dying :-(" );
517                     exit(-1);
518                 }
519             } else if ( token == "vt" ) {
520                 // vertex texture coordinate
521                 if ( vtcount < FG_MAX_NODES*3 ) {
522                     in >> tex_coords[vtcount][0]
523                        >> tex_coords[vtcount][1];
524                     vtcount++;
525                 } else {
526                     FG_LOG( FG_TERRAIN, FG_ALERT, 
527                             "Read too many vertex texture coords in " << path
528                             << " ... dying :-("
529                             );
530                     exit(-1);
531                 }
532             } else if ( token == "v" ) {
533                 // node (vertex)
534                 if ( t->ncount < FG_MAX_NODES ) {
535                     /* in >> nodes[t->ncount][0]
536                        >> nodes[t->ncount][1]
537                        >> nodes[t->ncount][2]; */
538                     in >> node;
539                     nodes.push_back(node);
540                     if ( is_base ) {
541                         t->ncount++;
542                     }
543                 } else {
544                     FG_LOG( FG_TERRAIN, FG_ALERT, 
545                             "Read too many nodes in " << path 
546                             << " ... dying :-(");
547                     exit(-1);
548                 }
549             } else if ( (token == "tf") || (token == "ts") || (token == "f") ) {
550                 // triangle fan, strip, or individual face
551                 // FG_LOG( FG_TERRAIN, FG_INFO, "new fan or strip");
552
553                 fan_vertices.clear();
554                 fan_tex_coords.clear();
555                 odd = true;
556
557                 // xglBegin(GL_TRIANGLE_FAN);
558
559                 in >> n1;
560                 fan_vertices.push_back( n1 );
561                 // xglNormal3dv(normals[n1]);
562                 if ( in.get( c ) && c == '/' ) {
563                     in >> tex;
564                     fan_tex_coords.push_back( tex );
565                     if ( scenery_version >= 0.4 ) {
566                         if ( tex_width > 0 ) {
567                             tclist[tex][0] *= (1000.0 / tex_width);
568                         }
569                         if ( tex_height > 0 ) {
570                             tclist[tex][1] *= (1000.0 / tex_height);
571                         }
572                     }
573                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
574                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
575                 } else {
576                     in.putback( c );
577                     pp = local_calc_tex_coords(nodes[n1], center);
578                 }
579                 // xglTexCoord2f(pp.x(), pp.y());
580                 // xglVertex3dv(nodes[n1].get_n());
581
582                 in >> n2;
583                 fan_vertices.push_back( n2 );
584                 // xglNormal3dv(normals[n2]);
585                 if ( in.get( c ) && c == '/' ) {
586                     in >> tex;
587                     fan_tex_coords.push_back( tex );
588                     if ( scenery_version >= 0.4 ) {
589                         if ( tex_width > 0 ) {
590                             tclist[tex][0] *= (1000.0 / tex_width);
591                         }
592                         if ( tex_height > 0 ) {
593                             tclist[tex][1] *= (1000.0 / tex_height);
594                         }
595                     }
596                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
597                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
598                 } else {
599                     in.putback( c );
600                     pp = local_calc_tex_coords(nodes[n2], center);
601                 }
602                 // xglTexCoord2f(pp.x(), pp.y());
603                 // xglVertex3dv(nodes[n2].get_n());
604                 
605                 // read all subsequent numbers until next thing isn't a number
606                 while ( true ) {
607 #if defined( macintosh ) || defined( _MSC_VER )
608                     in >> ::skipws;
609 #else
610                     in >> skipws;
611 #endif
612
613                     char c;
614                     in.get(c);
615                     in.putback(c);
616                     if ( ! isdigit(c) || in.eof() ) {
617                         break;
618                     }
619
620                     in >> n3;
621                     fan_vertices.push_back( n3 );
622                     // cout << "  triangle = " 
623                     //      << n1 << "," << n2 << "," << n3 
624                     //      << endl;
625                     // xglNormal3dv(normals[n3]);
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                                 tclist[tex][0] *= (1000.0 / tex_width);
632                             }
633                             if ( tex_height > 0 ) {
634                                 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 = local_calc_tex_coords(nodes[n3], center);
642                     }
643                     // xglTexCoord2f(pp.x(), pp.y());
644                     // xglVertex3dv(nodes[n3].get_n());
645
646                     if ( (token == "tf") || (token == "f") ) {
647                         // triangle fan
648                         n2 = n3;
649                     } else {
650                         // triangle strip
651                         odd = !odd;
652                         n1 = n2;
653                         n2 = n3;
654                     }
655                 }
656
657                 // xglEnd();
658
659                 // build the ssg entity
660                 int size = (int)fan_vertices.size();
661                 ssgVertexArray   *vl = new ssgVertexArray( size );
662                 ssgNormalArray   *nl = new ssgNormalArray( size );
663                 ssgTexCoordArray *tl = new ssgTexCoordArray( size );
664                 ssgColourArray   *cl = new ssgColourArray( 1 );
665
666                 sgVec4 color;
667                 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
668                 cl->add( color );
669
670                 sgVec2 tmp2;
671                 sgVec3 tmp3;
672                 for ( i = 0; i < size; ++i ) {
673                     sgCopyVec3( tmp3, vtlist[ fan_vertices[i] ] );
674                     vl -> add( tmp3 );
675
676                     sgCopyVec3( tmp3, vnlist[ fan_vertices[i] ] );
677                     nl -> add( tmp3 );
678
679                     sgCopyVec2( tmp2, tclist[ fan_tex_coords[i] ] );
680                     tl -> add( tmp2 );
681                 }
682
683                 ssgLeaf *leaf = NULL;
684                 if ( token == "tf" ) {
685                     // triangle fan
686                     leaf = 
687                         new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
688                 } else if ( token == "ts" ) {
689                     // triangle strip
690                     leaf = 
691                         new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
692                 } else if ( token == "f" ) {
693                     // triangle
694                     leaf = 
695                         new ssgVtxTable ( GL_TRIANGLES, vl, nl, tl, cl );
696                 }
697                 // leaf->makeDList();
698                 leaf->setState( state );
699
700                 tile->addKid( leaf );
701
702                 /*
703                 ssgVertexArray *lights = NULL;
704                 if ( is_base ) {
705                     // generate lighting
706                     lights = new ssgVertexArray( size );
707                     gen_random_surface_points( leaf, lights );
708                 }
709                 */
710             } else {
711                 FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in " 
712                         << path << " = " << token );
713             }
714
715             // eat white space before start of while loop so if we are
716             // done with useful input it is noticed before hand.
717 #if defined( macintosh ) || defined( _MSC_VER )
718             in >> ::skipws;
719 #else
720             in >> skipws;
721 #endif
722         }
723     }
724
725     if ( is_base ) {
726         t->nodes = nodes;
727     }
728
729     stopwatch.stop();
730     FG_LOG( FG_TERRAIN, FG_DEBUG, 
731             "Loaded " << path << " in " 
732             << stopwatch.elapsedSeconds() << " seconds" );
733
734     // Generate a cloud layer above the tiles
735     // if ( globals->get_options()->get_clouds() ) {
736     //          fgGenCloudTile(path, t, tile);
737     // }
738     return tile;
739 }
740
741