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