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