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