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