]> git.mxchange.org Git - flightgear.git/blob - src/Objects/obj.cxx
Ssg tweaks. Better handling of missing tiles.
[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     ssgBranch *tile = new ssgBranch () ;
162     tile -> setName ( path.c_str() ) ;
163
164     // Attempt to open "path.gz" or "path"
165     fg_gzifstream in( path );
166     if ( ! in.is_open() ) {
167         FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
168         return NULL;
169     }
170
171     shading = current_options.get_shading();
172
173     in_fragment = false;
174     t->ncount = 0;
175     vncount = 0;
176     vtcount = 0;
177     t->bounding_radius = 0.0;
178     center = t->center;
179
180     StopWatch stopwatch;
181     stopwatch.start();
182
183     // ignore initial comments and blank lines. (priming the pump)
184     // in >> skipcomment;
185     string line;
186
187     while ( ! in.eof() ) {
188         string token;
189         char c;
190
191 #if defined( MACOS )
192         in >> ::skipws;
193 #else
194         in >> skipws;
195 #endif
196
197         if ( in.get( c ) && c == '#' ) {
198             // process a comment line
199
200             // getline( in, line );
201             // cout << "comment = " << line << endl;
202
203             in >> token;
204
205             if ( token == "gbs" ) {
206                 // reference point (center offset)
207                 in >> t->center >> t->bounding_radius;
208                 center = t->center;
209                 // cout << "center = " << center 
210                 //      << " radius = " << t->bounding_radius << endl;
211             } else if ( token == "bs" ) {
212                 // reference point (center offset)
213                 in >> fragment.center;
214                 in >> fragment.bounding_radius;
215
216                 // cout << "center = " << fragment.center 
217                 //      << " radius = " << fragment.bounding_radius << endl;
218             } else if ( token == "usemtl" ) {
219                 // material property specification
220
221                 // if first usemtl with shared_done = false, then set
222                 // shared_done true and build the ssg shared lists
223                 if ( ! shared_done ) {
224                     shared_done = true;
225
226                     t->vtlist = new sgVec3 [ nodes.size() ];
227                     t->vnlist = new sgVec3 [ vncount ];
228                     t->tclist = new sgVec2 [ vtcount ];
229
230                     for ( i = 0; i < (int)nodes.size(); ++i ) {
231                         sgSetVec3( t->vtlist[i], 
232                                    nodes[i][0], nodes[i][1], nodes[i][2] );
233                     }
234                     for ( i = 0; i < vncount; ++i ) {
235                         sgSetVec3( t->vnlist[i], 
236                                    normals[i][0], 
237                                    normals[i][1],
238                                    normals[i][2] );
239                     }
240                     for ( i = 0; i < vtcount; ++i ) {
241                         sgSetVec2( t->tclist[i],
242                                    tex_coords[i][0], tex_coords[i][1] );
243                     }
244                 }
245
246                 // series of individual triangles
247                 if ( in_faces ) {
248                     xglEnd();
249                 }
250
251                 // this also signals the start of a new fragment
252                 if ( in_fragment ) {
253                     // close out the previous structure and start the next
254                     xglEndList();
255                     // printf("xglEnd(); xglEndList();\n");
256
257                     // update fragment
258                     fragment.display_list = display_list;
259
260                     // push this fragment onto the tile's object list
261                     t->fragment_list.push_back(fragment);
262                 } else {
263                     in_fragment = true;
264                 }
265
266                 // printf("start of fragment (usemtl)\n");
267
268                 display_list = xglGenLists(1);
269                 xglNewList(display_list, GL_COMPILE);
270                 // printf("xglGenLists(); xglNewList();\n");
271                 in_faces = false;
272
273                 // reset the existing face list
274                 // printf("cleaning a fragment with %d faces\n", 
275                 //        fragment.faces.size());
276                 fragment.init();
277                 
278                 // scan the material line
279                 string material;
280                 in >> material;
281                 fragment.tile_ptr = t;
282                 
283                 // find this material in the properties list
284                 if ( ! material_mgr.find( material, fragment.material_ptr )) {
285                     FG_LOG( FG_TERRAIN, FG_ALERT, 
286                             "Ack! unknown usemtl name = " << material 
287                             << " in " << path );
288                 }
289
290                 // set the texture width and height values for this
291                 // material
292                 FGMaterial m = fragment.material_ptr->get_m();
293                 tex_width = m.get_xsize();
294                 tex_height = m.get_ysize();
295                 state = fragment.material_ptr->get_state();
296                 // cout << "(w) = " << tex_width << " (h) = " 
297                 //      << tex_width << endl;
298
299                 // initialize the fragment transformation matrix
300                 /*
301                  for ( i = 0; i < 16; i++ ) {
302                    fragment.matrix[i] = 0.0;
303                  }
304                  fragment.matrix[0] = fragment.matrix[5] =
305                  fragment.matrix[10] = fragment.matrix[15] = 1.0;
306                 */
307             } else {
308                 // unknown comment, just gobble the input untill the
309                 // end of line
310
311                 in >> skipeol;
312             }
313         } else {
314             in.putback( c );
315         
316             in >> token;
317
318             // cout << "token = " << token << endl;
319
320             if ( token == "vn" ) {
321                 // vertex normal
322                 if ( vncount < FG_MAX_NODES ) {
323                     in >> normals[vncount][0]
324                        >> normals[vncount][1]
325                        >> normals[vncount][2];
326                     vncount++;
327                 } else {
328                     FG_LOG( FG_TERRAIN, FG_ALERT, 
329                             "Read too many vertex normals in " << path 
330                             << " ... dying :-(" );
331                     exit(-1);
332                 }
333             } else if ( token == "vt" ) {
334                 // vertex texture coordinate
335                 if ( vtcount < FG_MAX_NODES*3 ) {
336                     in >> tex_coords[vtcount][0]
337                        >> tex_coords[vtcount][1];
338                     vtcount++;
339                 } else {
340                     FG_LOG( FG_TERRAIN, FG_ALERT, 
341                             "Read too many vertex texture coords in " << path
342                             << " ... dying :-("
343                             );
344                     exit(-1);
345                 }
346             } else if ( token == "v" ) {
347                 // node (vertex)
348                 if ( t->ncount < FG_MAX_NODES ) {
349                     /* in >> nodes[t->ncount][0]
350                        >> nodes[t->ncount][1]
351                        >> nodes[t->ncount][2]; */
352                     in >> node;
353                     nodes.push_back(node);
354                     t->ncount++;
355                 } else {
356                     FG_LOG( FG_TERRAIN, FG_ALERT, 
357                             "Read too many nodes in " << path 
358                             << " ... dying :-(");
359                     exit(-1);
360                 }
361             } else if ( token == "t" ) {
362                 // start a new triangle strip
363
364                 n1 = n2 = n3 = n4 = 0;
365
366                 // fgPrintf( FG_TERRAIN, FG_DEBUG, 
367                 //           "    new tri strip = %s", line);
368                 in >> n1 >> n2 >> n3;
369                 fragment.add_face(n1, n2, n3);
370
371                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = ");
372
373                 xglBegin(GL_TRIANGLE_STRIP);
374                 // printf("xglBegin(tristrip) %d %d %d\n", n1, n2, n3);
375
376                 odd = 1; 
377                 // scale = 1.0;
378
379                 if ( shading ) {
380                     // Shading model is "GL_SMOOTH" so use precalculated
381                     // (averaged) normals
382                     // MAT3_SCALE_VEC(normal, normals[n1], scale);
383                     xglNormal3dv(normal);
384                     pp = calc_tex_coords(nodes[n1], center);
385                     xglTexCoord2f(pp.lon(), pp.lat());
386                     xglVertex3dv(nodes[n1].get_n());            
387
388                     // MAT3_SCALE_VEC(normal, normals[n2], scale);
389                     xglNormal3dv(normal);
390                     pp = calc_tex_coords(nodes[n2], center);
391                     xglTexCoord2f(pp.lon(), pp.lat());
392                     xglVertex3dv(nodes[n2].get_n());                            
393
394                     // MAT3_SCALE_VEC(normal, normals[n3], scale);
395                     xglNormal3dv(normal);
396                     pp = calc_tex_coords(nodes[n3], center);
397                     xglTexCoord2f(pp.lon(), pp.lat());
398                     xglVertex3dv(nodes[n3].get_n());
399                 } else {
400                     // Shading model is "GL_FLAT" so calculate per face
401                     // normals on the fly.
402                     if ( odd ) {
403                         calc_normal(nodes[n1], nodes[n2], 
404                                     nodes[n3], approx_normal);
405                     } else {
406                         calc_normal(nodes[n2], nodes[n1], 
407                                     nodes[n3], approx_normal);
408                     }
409                     // MAT3_SCALE_VEC(normal, approx_normal, scale);
410                     xglNormal3dv(normal);
411
412                     pp = calc_tex_coords(nodes[n1], center);
413                     xglTexCoord2f(pp.lon(), pp.lat());
414                     xglVertex3dv(nodes[n1].get_n());            
415
416                     pp = calc_tex_coords(nodes[n2], center);
417                     xglTexCoord2f(pp.lon(), pp.lat());
418                     xglVertex3dv(nodes[n2].get_n());            
419                     
420                     pp = calc_tex_coords(nodes[n3], center);
421                     xglTexCoord2f(pp.lon(), pp.lat());
422                     xglVertex3dv(nodes[n3].get_n());            
423                 }
424                 // printf("some normals, texcoords, and vertices\n");
425
426                 odd = 1 - odd;
427                 last1 = n2;
428                 last2 = n3;
429
430                 // There can be three or four values 
431                 char c;
432                 while ( in.get(c) ) {
433                     if ( c == '\n' ) {
434                         break; // only the one
435                     }
436                     if ( isdigit(c) ){
437                         in.putback(c);
438                         in >> n4;
439                         break;
440                     }
441                 }
442
443                 if ( n4 > 0 ) {
444                     fragment.add_face(n3, n2, n4);
445
446                     if ( shading ) {
447                         // Shading model is "GL_SMOOTH"
448                         // MAT3_SCALE_VEC(normal, normals[n4], scale);
449                     } else {
450                         // Shading model is "GL_FLAT"
451                         calc_normal(nodes[n3], nodes[n2], nodes[n4], 
452                                     approx_normal);
453                         // MAT3_SCALE_VEC(normal, approx_normal, scale);
454                     }
455                     xglNormal3dv(normal);
456                     pp = calc_tex_coords(nodes[n4], center);
457                     xglTexCoord2f(pp.lon(), pp.lat());
458                     xglVertex3dv(nodes[n4].get_n());            
459                     
460                     odd = 1 - odd;
461                     last1 = n3;
462                     last2 = n4;
463                     // printf("a normal, texcoord, and vertex (4th)\n");
464                 }
465             } else if ( token == "tf" ) {
466                 // triangle fan
467                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "new fan");
468
469                 fan_vertices.clear();
470                 fan_tex_coords.clear();
471
472                 xglBegin(GL_TRIANGLE_FAN);
473
474                 in >> n1;
475                 fan_vertices.push_back( n1 );
476                 xglNormal3dv(normals[n1]);
477                 if ( in.get( c ) && c == '/' ) {
478                     in >> tex;
479                     fan_tex_coords.push_back( tex );
480                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
481                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
482                 } else {
483                     in.putback( c );
484                     pp = calc_tex_coords(nodes[n1], center);
485                 }
486                 xglTexCoord2f(pp.x(), pp.y());
487                 xglVertex3dv(nodes[n1].get_n());
488
489                 in >> n2;
490                 fan_vertices.push_back( n2 );
491                 xglNormal3dv(normals[n2]);
492                 if ( in.get( c ) && c == '/' ) {
493                     in >> tex;
494                     fan_tex_coords.push_back( tex );
495                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
496                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
497                 } else {
498                     in.putback( c );
499                     pp = calc_tex_coords(nodes[n2], center);
500                 }
501                 xglTexCoord2f(pp.x(), pp.y());
502                 xglVertex3dv(nodes[n2].get_n());
503                 
504                 // read all subsequent numbers until next thing isn't a number
505                 while ( true ) {
506 #if defined( MACOS )
507                     in >> ::skipws;
508 #else
509                     in >> skipws;
510 #endif
511
512                     char c;
513                     in.get(c);
514                     in.putback(c);
515                     if ( ! isdigit(c) || in.eof() ) {
516                         break;
517                     }
518
519                     in >> n3;
520                     fan_vertices.push_back( n3 );
521                     // cout << "  triangle = " 
522                     //      << n1 << "," << n2 << "," << n3 
523                     //      << endl;
524                     xglNormal3dv(normals[n3]);
525                     if ( in.get( c ) && c == '/' ) {
526                         in >> tex;
527                         fan_tex_coords.push_back( tex );
528                         pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
529                         pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
530                     } else {
531                         in.putback( c );
532                         pp = calc_tex_coords(nodes[n3], center);
533                     }
534                     xglTexCoord2f(pp.x(), pp.y());
535                     xglVertex3dv(nodes[n3].get_n());
536
537                     fragment.add_face(n1, n2, n3);
538                     n2 = n3;
539                 }
540
541                 xglEnd();
542
543                 // build the ssg entity
544                 unsigned short *vindex = 
545                     new unsigned short [ fan_vertices.size() ];
546                 unsigned short *tindex = 
547                     new unsigned short [ fan_tex_coords.size() ];
548                 for ( i = 0; i < (int)fan_vertices.size(); ++i ) {
549                     vindex[i] = fan_vertices[i];
550                 }
551                 for ( i = 0; i < (int)fan_tex_coords.size(); ++i ) {
552                     tindex[i] = fan_tex_coords[i];
553                 }
554                 ssgLeaf *leaf = 
555                     new ssgVTable ( GL_TRIANGLE_FAN,
556                                     fan_vertices.size(), vindex, t->vtlist,
557                                     fan_vertices.size(), vindex, t->vnlist,
558                                     fan_tex_coords.size(), tindex, t->tclist,
559                                     0, NULL, NULL ) ;
560                 leaf->setState( state );
561
562                 tile->addKid( leaf );
563
564             } else if ( token == "f" ) {
565                 // unoptimized face
566
567                 if ( !in_faces ) {
568                     xglBegin(GL_TRIANGLES);
569                     // printf("xglBegin(triangles)\n");
570                     in_faces = true;
571                 }
572
573                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
574                 in >> n1 >> n2 >> n3;
575                 fragment.add_face(n1, n2, n3);
576
577                 // xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
578                 xglNormal3dv(normals[n1]);
579                 pp = calc_tex_coords(nodes[n1], center);
580                 xglTexCoord2f(pp.lon(), pp.lat());
581                 xglVertex3dv(nodes[n1].get_n());
582
583                 xglNormal3dv(normals[n2]);
584                 pp = calc_tex_coords(nodes[n2], center);
585                 xglTexCoord2f(pp.lon(), pp.lat());
586                 xglVertex3dv(nodes[n2].get_n());
587                 
588                 xglNormal3dv(normals[n3]);
589                 pp = calc_tex_coords(nodes[n3], center);
590                 xglTexCoord2f(pp.lon(), pp.lat());
591                 xglVertex3dv(nodes[n3].get_n());
592                 // printf("some normals, texcoords, and vertices (tris)\n");
593             } else if ( token == "q" ) {
594                 // continue a triangle strip
595                 n1 = n2 = 0;
596
597                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ", 
598                 //           line);
599                 in >> n1;
600
601                 // There can be one or two values 
602                 char c;
603                 while ( in.get(c) ) {
604                     if ( c == '\n' ) {
605                         break; // only the one
606                     }
607
608                     if ( isdigit(c) ) {
609                         in.putback(c);
610                         in >> n2;
611                         break;
612                     }
613                 }
614                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2);
615
616                 if ( odd ) {
617                     fragment.add_face(last1, last2, n1);
618                 } else {
619                     fragment.add_face(last2, last1, n1);
620                 }
621
622                 if ( shading ) {
623                     // Shading model is "GL_SMOOTH"
624                     // MAT3_SCALE_VEC(normal, normals[n1], scale);
625                 } else {
626                     // Shading model is "GL_FLAT"
627                     if ( odd ) {
628                         calc_normal(nodes[last1], nodes[last2], 
629                                     nodes[n1], approx_normal);
630                     } else {
631                         calc_normal(nodes[last2], nodes[last1], 
632                                     nodes[n1], approx_normal);
633                     }
634                     // MAT3_SCALE_VEC(normal, approx_normal, scale);
635                 }
636                 xglNormal3dv(normal);
637
638                 pp = calc_tex_coords(nodes[n1], center);
639                 xglTexCoord2f(pp.lon(), pp.lat());
640                 xglVertex3dv(nodes[n1].get_n());
641                 // printf("a normal, texcoord, and vertex (4th)\n");
642    
643                 odd = 1 - odd;
644                 last1 = last2;
645                 last2 = n1;
646
647                 if ( n2 > 0 ) {
648                     // fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n");
649
650                     if ( odd ) {
651                         fragment.add_face(last1, last2, n2);
652                     } else {
653                         fragment.add_face(last2, last1, n2);
654                     }
655
656                     if ( shading ) {
657                         // Shading model is "GL_SMOOTH"
658                         // MAT3_SCALE_VEC(normal, normals[n2], scale);
659                     } else {
660                         // Shading model is "GL_FLAT"
661                         if ( odd ) {
662                             calc_normal(nodes[last1], nodes[last2], 
663                                         nodes[n2], approx_normal);
664                         } else {
665                             calc_normal(nodes[last2], nodes[last1], 
666                                         nodes[n2], approx_normal);
667                         }
668                         // MAT3_SCALE_VEC(normal, approx_normal, scale);
669                     }
670                     xglNormal3dv(normal);
671                 
672                     pp = calc_tex_coords(nodes[n2], center);
673                     xglTexCoord2f(pp.lon(), pp.lat());
674                     xglVertex3dv(nodes[n2].get_n());            
675                     // printf("a normal, texcoord, and vertex (4th)\n");
676
677                     odd = 1 -odd;
678                     last1 = last2;
679                     last2 = n2;
680                 }
681             } else {
682                 FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in " 
683                         << path << " = " << token );
684             }
685
686             // eat white space before start of while loop so if we are
687             // done with useful input it is noticed before hand.
688 #if defined( MACOS )
689             in >> ::skipws;
690 #else
691             in >> skipws;
692 #endif
693         }
694     }
695
696     if ( in_fragment ) {
697         // close out the previous structure and start the next
698         xglEnd();
699         xglEndList();
700         // printf("xglEnd(); xglEndList();\n");
701         
702         // update fragment
703         fragment.display_list = display_list;
704         
705         // push this fragment onto the tile's object list
706         t->fragment_list.push_back(fragment);
707     }
708
709 #if 0
710     // Draw normal vectors (for visually verifying normals)
711     xglBegin(GL_LINES);
712     xglColor3f(0.0, 0.0, 0.0);
713     for ( i = 0; i < t->ncount; i++ ) {
714         xglVertex3d(nodes[i][0],
715                     nodes[i][1] ,
716                     nodes[i][2]);
717         xglVertex3d(nodes[i][0] + 500*normals[i][0],
718                     nodes[i][1] + 500*normals[i][1],
719                     nodes[i][2] + 500*normals[i][2]);
720     } 
721     xglEnd();
722 #endif
723
724     t->nodes = nodes;
725
726     stopwatch.stop();
727     FG_LOG( FG_TERRAIN, FG_INFO, 
728             "Loaded " << path << " in " 
729             << stopwatch.elapsedSeconds() << " seconds" );
730     
731     return tile;
732 }
733
734