]> git.mxchange.org Git - flightgear.git/blob - src/Objects/obj.cxx
Changes to begin incorporating plib support for managing and rendering the
[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 #ifdef HAVE_WINDOWS_H
33 #  include <windows.h>
34 #endif
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <GL/glut.h>
39 #include <XGL/xgl.h>
40
41 // #if defined ( __sun__ )
42 // extern "C" void *memmove(void *, const void *, size_t);
43 // extern "C" void *memset(void *, int, size_t);
44 // #endif
45
46 #include <Include/compiler.h>
47
48 #include STL_STRING
49 #include <map>                  // STL
50 #include <vector>               // STL
51 #include <ctype.h>              // isdigit()
52
53 #include <Debug/logstream.hxx>
54 #include <Misc/fgstream.hxx>
55 #include <Include/fg_constants.h>
56 #include <Main/options.hxx>
57 #include <Math/mat3.h>
58 #include <Math/fg_random.h>
59 #include <Math/point3d.hxx>
60 #include <Math/polar3d.hxx>
61 #include <Misc/stopwatch.hxx>
62 #include <Scenery/tileentry.hxx>
63
64 #include "materialmgr.hxx"
65 #include "obj.hxx"
66
67 FG_USING_STD(string);
68 FG_USING_STD(vector);
69
70
71 typedef vector < int > int_list;
72 typedef int_list::iterator int_list_iterator;
73 typedef int_list::const_iterator int_point_list_iterator;
74
75
76 static double normals[FG_MAX_NODES][3];
77 static double tex_coords[FG_MAX_NODES*3][3];
78
79
80 // given three points defining a triangle, calculate the normal
81 static void calc_normal(Point3D p1, Point3D p2, 
82                         Point3D p3, double normal[3])
83 {
84     double v1[3], v2[3];
85     double temp;
86
87     v1[0] = p2[0] - p1[0]; v1[1] = p2[1] - p1[1]; v1[2] = p2[2] - p1[2];
88     v2[0] = p3[0] - p1[0]; v2[1] = p3[1] - p1[1]; v2[2] = p3[2] - p1[2];
89
90     MAT3cross_product(normal, v1, v2);
91     MAT3_NORMALIZE_VEC(normal,temp);
92
93     // fgPrintf( FG_TERRAIN, FG_DEBUG, "  Normal = %.2f %.2f %.2f\n", 
94     //           normal[0], normal[1], normal[2]);
95 }
96
97
98 #define FG_TEX_CONSTANT 69.0
99
100
101 // Calculate texture coordinates for a given point.
102 static Point3D calc_tex_coords(const Point3D& node, const Point3D& ref) {
103     Point3D cp;
104     Point3D pp;
105     // double tmplon, tmplat;
106
107     // cout << "-> " << node[0] << " " << node[1] << " " << node[2] << endl;
108     // cout << "-> " << ref.x() << " " << ref.y() << " " << ref.z() << endl;
109
110     cp = Point3D( node[0] + ref.x(),
111                   node[1] + ref.y(),
112                   node[2] + ref.z() );
113
114     pp = fgCartToPolar3d(cp);
115
116     // tmplon = pp.lon() * RAD_TO_DEG;
117     // tmplat = pp.lat() * RAD_TO_DEG;
118     // cout << tmplon << " " << tmplat << endl;
119
120     pp.setx( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.x(), 11.0) );
121     pp.sety( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.y(), 11.0) );
122
123     if ( pp.x() < 0.0 ) {
124         pp.setx( pp.x() + 11.0 );
125     }
126
127     if ( pp.y() < 0.0 ) {
128         pp.sety( pp.y() + 11.0 );
129     }
130
131     // cout << pp << endl;
132
133     return(pp);
134 }
135
136
137 // Load a .obj file and build the GL fragment list
138 ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
139     fgFRAGMENT fragment;
140     Point3D pp;
141     double approx_normal[3], normal[3] /*, scale = 0.0 */;
142     // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
143     // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
144     GLint display_list = 0;
145     int shading;
146     bool in_fragment = false, in_faces = false;
147     int vncount, vtcount;
148     int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
149     int tex;
150     int last1 = 0, last2 = 0, odd = 0;
151     point_list nodes;
152     Point3D node;
153     Point3D center;
154     double tex_width = 1000.0, tex_height = 1000.0;
155     bool shared_done = false;
156     int_list fan_vertices;
157     int_list fan_tex_coords;
158     int i;
159     ssgSimpleState *state = NULL;
160
161     // printf("loading %s\n", path.c_str() );
162
163     // Attempt to open "path.gz" or "path"
164     fg_gzifstream in( path );
165     if ( ! in.is_open() ) {
166         FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
167         return 0;
168     }
169
170     shading = current_options.get_shading();
171
172     in_fragment = false;
173     t->ncount = 0;
174     vncount = 0;
175     vtcount = 0;
176     t->bounding_radius = 0.0;
177     center = t->center;
178
179     StopWatch stopwatch;
180     stopwatch.start();
181
182     ssgBranch *tile = new ssgBranch () ;
183     tile -> setName ( path.c_str() ) ;
184
185     // ignore initial comments and blank lines. (priming the pump)
186     // in >> skipcomment;
187     string line;
188
189     while ( ! in.eof() ) {
190         string token;
191         char c;
192
193 #if defined( MACOS )
194         in >> ::skipws;
195 #else
196         in >> skipws;
197 #endif
198
199         if ( in.get( c ) && c == '#' ) {
200             // process a comment line
201
202             // getline( in, line );
203             // cout << "comment = " << line << endl;
204
205             in >> token;
206
207             if ( token == "gbs" ) {
208                 // reference point (center offset)
209                 in >> t->center >> t->bounding_radius;
210                 center = t->center;
211                 // cout << "center = " << center 
212                 //      << " radius = " << t->bounding_radius << endl;
213             } else if ( token == "bs" ) {
214                 // reference point (center offset)
215                 in >> fragment.center;
216                 in >> fragment.bounding_radius;
217
218                 // cout << "center = " << fragment.center 
219                 //      << " radius = " << fragment.bounding_radius << endl;
220             } else if ( token == "usemtl" ) {
221                 // material property specification
222
223                 // if first usemtl with shared_done = false, then set
224                 // shared_done true and build the ssg shared lists
225                 if ( ! shared_done ) {
226                     shared_done = true;
227
228                     t->vtlist = new sgVec3 [ nodes.size() ];
229                     t->vnlist = new sgVec3 [ vncount ];
230                     t->tclist = new sgVec2 [ vtcount ];
231
232                     for ( i = 0; i < (int)nodes.size(); ++i ) {
233                         sgSetVec3( t->vtlist[i], 
234                                    nodes[i][0], nodes[i][1], nodes[i][2] );
235                     }
236                     for ( i = 0; i < vncount; ++i ) {
237                         sgSetVec3( t->vnlist[i], 
238                                    normals[i][0], 
239                                    normals[i][1],
240                                    normals[i][2] );
241                     }
242                     for ( i = 0; i < vtcount; ++i ) {
243                         sgSetVec2( t->tclist[i],
244                                    tex_coords[i][0], tex_coords[i][1] );
245                     }
246                 }
247
248                 // series of individual triangles
249                 if ( in_faces ) {
250                     xglEnd();
251                 }
252
253                 // this also signals the start of a new fragment
254                 if ( in_fragment ) {
255                     // close out the previous structure and start the next
256                     xglEndList();
257                     // printf("xglEnd(); xglEndList();\n");
258
259                     // update fragment
260                     fragment.display_list = display_list;
261
262                     // push this fragment onto the tile's object list
263                     t->fragment_list.push_back(fragment);
264                 } else {
265                     in_fragment = true;
266                 }
267
268                 // printf("start of fragment (usemtl)\n");
269
270                 display_list = xglGenLists(1);
271                 xglNewList(display_list, GL_COMPILE);
272                 // printf("xglGenLists(); xglNewList();\n");
273                 in_faces = false;
274
275                 // reset the existing face list
276                 // printf("cleaning a fragment with %d faces\n", 
277                 //        fragment.faces.size());
278                 fragment.init();
279                 
280                 // scan the material line
281                 string material;
282                 in >> material;
283                 fragment.tile_ptr = t;
284                 
285                 // find this material in the properties list
286                 if ( ! material_mgr.find( material, fragment.material_ptr )) {
287                     FG_LOG( FG_TERRAIN, FG_ALERT, 
288                             "Ack! unknown usemtl name = " << material 
289                             << " in " << path );
290                 }
291
292                 // set the texture width and height values for this
293                 // material
294                 FGMaterial m = fragment.material_ptr->get_m();
295                 tex_width = m.get_xsize();
296                 tex_height = m.get_ysize();
297                 state = fragment.material_ptr->get_state();
298                 // cout << "(w) = " << tex_width << " (h) = " 
299                 //      << tex_width << endl;
300
301                 // initialize the fragment transformation matrix
302                 /*
303                  for ( i = 0; i < 16; i++ ) {
304                    fragment.matrix[i] = 0.0;
305                  }
306                  fragment.matrix[0] = fragment.matrix[5] =
307                  fragment.matrix[10] = fragment.matrix[15] = 1.0;
308                 */
309             } else {
310                 // unknown comment, just gobble the input untill the
311                 // end of line
312
313                 in >> skipeol;
314             }
315         } else {
316             in.putback( c );
317         
318             in >> token;
319
320             // cout << "token = " << token << endl;
321
322             if ( token == "vn" ) {
323                 // vertex normal
324                 if ( vncount < FG_MAX_NODES ) {
325                     in >> normals[vncount][0]
326                        >> normals[vncount][1]
327                        >> normals[vncount][2];
328                     vncount++;
329                 } else {
330                     FG_LOG( FG_TERRAIN, FG_ALERT, 
331                             "Read too many vertex normals in " << path 
332                             << " ... dying :-(" );
333                     exit(-1);
334                 }
335             } else if ( token == "vt" ) {
336                 // vertex texture coordinate
337                 if ( vtcount < FG_MAX_NODES*3 ) {
338                     in >> tex_coords[vtcount][0]
339                        >> tex_coords[vtcount][1];
340                     vtcount++;
341                 } else {
342                     FG_LOG( FG_TERRAIN, FG_ALERT, 
343                             "Read too many vertex texture coords in " << path
344                             << " ... dying :-("
345                             );
346                     exit(-1);
347                 }
348             } else if ( token == "v" ) {
349                 // node (vertex)
350                 if ( t->ncount < FG_MAX_NODES ) {
351                     /* in >> nodes[t->ncount][0]
352                        >> nodes[t->ncount][1]
353                        >> nodes[t->ncount][2]; */
354                     in >> node;
355                     nodes.push_back(node);
356                     t->ncount++;
357                 } else {
358                     FG_LOG( FG_TERRAIN, FG_ALERT, 
359                             "Read too many nodes in " << path 
360                             << " ... dying :-(");
361                     exit(-1);
362                 }
363             } else if ( token == "t" ) {
364                 // start a new triangle strip
365
366                 n1 = n2 = n3 = n4 = 0;
367
368                 // fgPrintf( FG_TERRAIN, FG_DEBUG, 
369                 //           "    new tri strip = %s", line);
370                 in >> n1 >> n2 >> n3;
371                 fragment.add_face(n1, n2, n3);
372
373                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = ");
374
375                 xglBegin(GL_TRIANGLE_STRIP);
376                 // printf("xglBegin(tristrip) %d %d %d\n", n1, n2, n3);
377
378                 odd = 1; 
379                 // scale = 1.0;
380
381                 if ( shading ) {
382                     // Shading model is "GL_SMOOTH" so use precalculated
383                     // (averaged) normals
384                     // MAT3_SCALE_VEC(normal, normals[n1], scale);
385                     xglNormal3dv(normal);
386                     pp = calc_tex_coords(nodes[n1], center);
387                     xglTexCoord2f(pp.lon(), pp.lat());
388                     xglVertex3dv(nodes[n1].get_n());            
389
390                     // MAT3_SCALE_VEC(normal, normals[n2], scale);
391                     xglNormal3dv(normal);
392                     pp = calc_tex_coords(nodes[n2], center);
393                     xglTexCoord2f(pp.lon(), pp.lat());
394                     xglVertex3dv(nodes[n2].get_n());                            
395
396                     // MAT3_SCALE_VEC(normal, normals[n3], scale);
397                     xglNormal3dv(normal);
398                     pp = calc_tex_coords(nodes[n3], center);
399                     xglTexCoord2f(pp.lon(), pp.lat());
400                     xglVertex3dv(nodes[n3].get_n());
401                 } else {
402                     // Shading model is "GL_FLAT" so calculate per face
403                     // normals on the fly.
404                     if ( odd ) {
405                         calc_normal(nodes[n1], nodes[n2], 
406                                     nodes[n3], approx_normal);
407                     } else {
408                         calc_normal(nodes[n2], nodes[n1], 
409                                     nodes[n3], approx_normal);
410                     }
411                     // MAT3_SCALE_VEC(normal, approx_normal, scale);
412                     xglNormal3dv(normal);
413
414                     pp = calc_tex_coords(nodes[n1], center);
415                     xglTexCoord2f(pp.lon(), pp.lat());
416                     xglVertex3dv(nodes[n1].get_n());            
417
418                     pp = calc_tex_coords(nodes[n2], center);
419                     xglTexCoord2f(pp.lon(), pp.lat());
420                     xglVertex3dv(nodes[n2].get_n());            
421                     
422                     pp = calc_tex_coords(nodes[n3], center);
423                     xglTexCoord2f(pp.lon(), pp.lat());
424                     xglVertex3dv(nodes[n3].get_n());            
425                 }
426                 // printf("some normals, texcoords, and vertices\n");
427
428                 odd = 1 - odd;
429                 last1 = n2;
430                 last2 = n3;
431
432                 // There can be three or four values 
433                 char c;
434                 while ( in.get(c) ) {
435                     if ( c == '\n' ) {
436                         break; // only the one
437                     }
438                     if ( isdigit(c) ){
439                         in.putback(c);
440                         in >> n4;
441                         break;
442                     }
443                 }
444
445                 if ( n4 > 0 ) {
446                     fragment.add_face(n3, n2, n4);
447
448                     if ( shading ) {
449                         // Shading model is "GL_SMOOTH"
450                         // MAT3_SCALE_VEC(normal, normals[n4], scale);
451                     } else {
452                         // Shading model is "GL_FLAT"
453                         calc_normal(nodes[n3], nodes[n2], nodes[n4], 
454                                     approx_normal);
455                         // MAT3_SCALE_VEC(normal, approx_normal, scale);
456                     }
457                     xglNormal3dv(normal);
458                     pp = calc_tex_coords(nodes[n4], center);
459                     xglTexCoord2f(pp.lon(), pp.lat());
460                     xglVertex3dv(nodes[n4].get_n());            
461                     
462                     odd = 1 - odd;
463                     last1 = n3;
464                     last2 = n4;
465                     // printf("a normal, texcoord, and vertex (4th)\n");
466                 }
467             } else if ( token == "tf" ) {
468                 // triangle fan
469                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "new fan");
470
471                 fan_vertices.clear();
472                 fan_tex_coords.clear();
473
474                 xglBegin(GL_TRIANGLE_FAN);
475
476                 in >> n1;
477                 fan_vertices.push_back( n1 );
478                 xglNormal3dv(normals[n1]);
479                 if ( in.get( c ) && c == '/' ) {
480                     in >> tex;
481                     fan_tex_coords.push_back( tex );
482                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
483                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
484                 } else {
485                     in.putback( c );
486                     pp = calc_tex_coords(nodes[n1], center);
487                 }
488                 xglTexCoord2f(pp.x(), pp.y());
489                 xglVertex3dv(nodes[n1].get_n());
490
491                 in >> n2;
492                 fan_vertices.push_back( n2 );
493                 xglNormal3dv(normals[n2]);
494                 if ( in.get( c ) && c == '/' ) {
495                     in >> tex;
496                     fan_tex_coords.push_back( tex );
497                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
498                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
499                 } else {
500                     in.putback( c );
501                     pp = calc_tex_coords(nodes[n2], center);
502                 }
503                 xglTexCoord2f(pp.x(), pp.y());
504                 xglVertex3dv(nodes[n2].get_n());
505                 
506                 // read all subsequent numbers until next thing isn't a number
507                 while ( true ) {
508 #if defined( MACOS )
509                     in >> ::skipws;
510 #else
511                     in >> skipws;
512 #endif
513
514                     char c;
515                     in.get(c);
516                     in.putback(c);
517                     if ( ! isdigit(c) || in.eof() ) {
518                         break;
519                     }
520
521                     in >> n3;
522                     fan_vertices.push_back( n3 );
523                     // cout << "  triangle = " 
524                     //      << n1 << "," << n2 << "," << n3 
525                     //      << endl;
526                     xglNormal3dv(normals[n3]);
527                     if ( in.get( c ) && c == '/' ) {
528                         in >> tex;
529                         fan_tex_coords.push_back( tex );
530                         pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
531                         pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
532                     } else {
533                         in.putback( c );
534                         pp = calc_tex_coords(nodes[n3], center);
535                     }
536                     xglTexCoord2f(pp.x(), pp.y());
537                     xglVertex3dv(nodes[n3].get_n());
538
539                     fragment.add_face(n1, n2, n3);
540                     n2 = n3;
541                 }
542
543                 xglEnd();
544
545                 // build the ssg entity
546                 unsigned short *vindex = 
547                     new unsigned short [ fan_vertices.size() ];
548                 unsigned short *tindex = 
549                     new unsigned short [ fan_tex_coords.size() ];
550                 for ( i = 0; i < (int)fan_vertices.size(); ++i ) {
551                     vindex[i] = fan_vertices[i];
552                 }
553                 for ( i = 0; i < (int)fan_tex_coords.size(); ++i ) {
554                     tindex[i] = fan_tex_coords[i];
555                 }
556                 ssgLeaf *leaf = 
557                     new ssgVTable ( GL_TRIANGLE_FAN,
558                                     fan_vertices.size(), vindex, t->vtlist,
559                                     fan_vertices.size(), vindex, t->vnlist,
560                                     fan_tex_coords.size(), tindex, t->tclist,
561                                     0, NULL, NULL ) ;
562                 leaf->setState( state );
563
564                 tile->addKid( leaf );
565
566             } else if ( token == "f" ) {
567                 // unoptimized face
568
569                 if ( !in_faces ) {
570                     xglBegin(GL_TRIANGLES);
571                     // printf("xglBegin(triangles)\n");
572                     in_faces = true;
573                 }
574
575                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
576                 in >> n1 >> n2 >> n3;
577                 fragment.add_face(n1, n2, n3);
578
579                 // xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
580                 xglNormal3dv(normals[n1]);
581                 pp = calc_tex_coords(nodes[n1], center);
582                 xglTexCoord2f(pp.lon(), pp.lat());
583                 xglVertex3dv(nodes[n1].get_n());
584
585                 xglNormal3dv(normals[n2]);
586                 pp = calc_tex_coords(nodes[n2], center);
587                 xglTexCoord2f(pp.lon(), pp.lat());
588                 xglVertex3dv(nodes[n2].get_n());
589                 
590                 xglNormal3dv(normals[n3]);
591                 pp = calc_tex_coords(nodes[n3], center);
592                 xglTexCoord2f(pp.lon(), pp.lat());
593                 xglVertex3dv(nodes[n3].get_n());
594                 // printf("some normals, texcoords, and vertices (tris)\n");
595             } else if ( token == "q" ) {
596                 // continue a triangle strip
597                 n1 = n2 = 0;
598
599                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ", 
600                 //           line);
601                 in >> n1;
602
603                 // There can be one or two values 
604                 char c;
605                 while ( in.get(c) ) {
606                     if ( c == '\n' ) {
607                         break; // only the one
608                     }
609
610                     if ( isdigit(c) ) {
611                         in.putback(c);
612                         in >> n2;
613                         break;
614                     }
615                 }
616                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2);
617
618                 if ( odd ) {
619                     fragment.add_face(last1, last2, n1);
620                 } else {
621                     fragment.add_face(last2, last1, n1);
622                 }
623
624                 if ( shading ) {
625                     // Shading model is "GL_SMOOTH"
626                     // MAT3_SCALE_VEC(normal, normals[n1], scale);
627                 } else {
628                     // Shading model is "GL_FLAT"
629                     if ( odd ) {
630                         calc_normal(nodes[last1], nodes[last2], 
631                                     nodes[n1], approx_normal);
632                     } else {
633                         calc_normal(nodes[last2], nodes[last1], 
634                                     nodes[n1], approx_normal);
635                     }
636                     // MAT3_SCALE_VEC(normal, approx_normal, scale);
637                 }
638                 xglNormal3dv(normal);
639
640                 pp = calc_tex_coords(nodes[n1], center);
641                 xglTexCoord2f(pp.lon(), pp.lat());
642                 xglVertex3dv(nodes[n1].get_n());
643                 // printf("a normal, texcoord, and vertex (4th)\n");
644    
645                 odd = 1 - odd;
646                 last1 = last2;
647                 last2 = n1;
648
649                 if ( n2 > 0 ) {
650                     // fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n");
651
652                     if ( odd ) {
653                         fragment.add_face(last1, last2, n2);
654                     } else {
655                         fragment.add_face(last2, last1, n2);
656                     }
657
658                     if ( shading ) {
659                         // Shading model is "GL_SMOOTH"
660                         // MAT3_SCALE_VEC(normal, normals[n2], scale);
661                     } else {
662                         // Shading model is "GL_FLAT"
663                         if ( odd ) {
664                             calc_normal(nodes[last1], nodes[last2], 
665                                         nodes[n2], approx_normal);
666                         } else {
667                             calc_normal(nodes[last2], nodes[last1], 
668                                         nodes[n2], approx_normal);
669                         }
670                         // MAT3_SCALE_VEC(normal, approx_normal, scale);
671                     }
672                     xglNormal3dv(normal);
673                 
674                     pp = calc_tex_coords(nodes[n2], center);
675                     xglTexCoord2f(pp.lon(), pp.lat());
676                     xglVertex3dv(nodes[n2].get_n());            
677                     // printf("a normal, texcoord, and vertex (4th)\n");
678
679                     odd = 1 -odd;
680                     last1 = last2;
681                     last2 = n2;
682                 }
683             } else {
684                 FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in " 
685                         << path << " = " << token );
686             }
687
688             // eat white space before start of while loop so if we are
689             // done with useful input it is noticed before hand.
690 #if defined( MACOS )
691             in >> ::skipws;
692 #else
693             in >> skipws;
694 #endif
695         }
696     }
697
698     if ( in_fragment ) {
699         // close out the previous structure and start the next
700         xglEnd();
701         xglEndList();
702         // printf("xglEnd(); xglEndList();\n");
703         
704         // update fragment
705         fragment.display_list = display_list;
706         
707         // push this fragment onto the tile's object list
708         t->fragment_list.push_back(fragment);
709     }
710
711 #if 0
712     // Draw normal vectors (for visually verifying normals)
713     xglBegin(GL_LINES);
714     xglColor3f(0.0, 0.0, 0.0);
715     for ( i = 0; i < t->ncount; i++ ) {
716         xglVertex3d(nodes[i][0],
717                     nodes[i][1] ,
718                     nodes[i][2]);
719         xglVertex3d(nodes[i][0] + 500*normals[i][0],
720                     nodes[i][1] + 500*normals[i][1],
721                     nodes[i][2] + 500*normals[i][2]);
722     } 
723     xglEnd();
724 #endif
725
726     t->nodes = nodes;
727
728     stopwatch.stop();
729     FG_LOG( FG_TERRAIN, FG_INFO, 
730             "Loaded " << path << " in " 
731             << stopwatch.elapsedSeconds() << " seconds" );
732     
733     return tile;
734 }
735
736