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