]> git.mxchange.org Git - flightgear.git/blob - src/Objects/obj.cxx
Work on better texture coordinate handling with the ability to scale textures
[flightgear.git] / src / Objects / obj.cxx
1 // obj.cxx -- routines to handle "sorta" WaveFront .obj format files.
2 //
3 // Written by Curtis Olson, started October 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #ifdef FG_MATH_EXCEPTION_CLASH
29 #  include <math.h>
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34
35 // #if defined ( __sun__ )
36 // extern "C" void *memmove(void *, const void *, size_t);
37 // extern "C" void *memset(void *, int, size_t);
38 // #endif
39
40 #include <Include/compiler.h>
41
42 #include STL_STRING
43 #include <map>                  // STL
44 #include <vector>               // STL
45 #include <ctype.h>              // isdigit()
46
47 #include <Debug/logstream.hxx>
48 #include <Misc/fgstream.hxx>
49 #include <Include/fg_constants.h>
50 #include <Main/options.hxx>
51 #include <Math/mat3.h>
52 #include <Math/fg_geodesy.hxx>
53 #include <Math/fg_random.h>
54 #include <Math/point3d.hxx>
55 #include <Math/polar3d.hxx>
56 #include <Misc/stopwatch.hxx>
57 #include <Scenery/tileentry.hxx>
58
59 #include "materialmgr.hxx"
60 #include "obj.hxx"
61
62 FG_USING_STD(string);
63 FG_USING_STD(vector);
64
65
66 typedef vector < int > int_list;
67 typedef int_list::iterator int_list_iterator;
68 typedef int_list::const_iterator int_point_list_iterator;
69
70
71 static double normals[FG_MAX_NODES][3];
72 static double tex_coords[FG_MAX_NODES*3][3];
73
74
75 // given three points defining a triangle, calculate the normal
76 static void calc_normal(Point3D p1, Point3D p2, 
77                         Point3D p3, double normal[3])
78 {
79     double v1[3], v2[3];
80     double temp;
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     MAT3cross_product(normal, v1, v2);
86     MAT3_NORMALIZE_VEC(normal,temp);
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
96 // Calculate texture coordinates for a given point.
97 static Point3D calc_tex_coords(const Point3D& node, const Point3D& ref) {
98     Point3D cp;
99     Point3D pp;
100     // double tmplon, tmplat;
101
102     // cout << "-> " << node[0] << " " << node[1] << " " << node[2] << endl;
103     // cout << "-> " << ref.x() << " " << ref.y() << " " << ref.z() << endl;
104
105     cp = Point3D( node[0] + ref.x(),
106                   node[1] + ref.y(),
107                   node[2] + ref.z() );
108
109     pp = fgCartToPolar3d(cp);
110
111     // tmplon = pp.lon() * RAD_TO_DEG;
112     // tmplat = pp.lat() * RAD_TO_DEG;
113     // cout << tmplon << " " << tmplat << endl;
114
115     pp.setx( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.x(), 11.0) );
116     pp.sety( fmod(RAD_TO_DEG * FG_TEX_CONSTANT * pp.y(), 11.0) );
117
118     if ( pp.x() < 0.0 ) {
119         pp.setx( pp.x() + 11.0 );
120     }
121
122     if ( pp.y() < 0.0 ) {
123         pp.sety( pp.y() + 11.0 );
124     }
125
126     // cout << pp << endl;
127
128     return(pp);
129 }
130
131
132 // Generate a generic ocean tile on the fly
133 ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
134     fgFRAGMENT fragment;
135     fragment.init();
136     fragment.tile_ptr = t;
137
138     ssgSimpleState *state = NULL;
139
140     ssgBranch *tile = new ssgBranch () ;
141     tile -> setName ( (char *)path.c_str() ) ;
142
143     // find Ocean material in the properties list
144     if ( ! material_mgr.find( "Ocean", fragment.material_ptr )) {
145         FG_LOG( FG_TERRAIN, FG_ALERT, 
146                 "Ack! unknown usemtl name = " << "Ocean" 
147                 << " in " << path );
148     }
149
150     // set the texture width and height values for this
151     // material
152     FGMaterial m = fragment.material_ptr->get_m();
153     // double tex_width = m.get_xsize();
154     // double tex_height = m.get_ysize();
155
156     // set ssgState
157     state = fragment.material_ptr->get_state();
158
159     // Calculate center point
160     FGBucket b = t->tile_bucket;
161     double clon = b.get_center_lon();
162     double clat = b.get_center_lat();
163     double height = b.get_height();
164     double width = b.get_width();
165
166     Point3D center = fgGeodToCart(Point3D(clon*DEG_TO_RAD,clat*DEG_TO_RAD,0.0));
167     t->center = center;
168     fragment.center = center;
169     // cout << "center = " << center << endl;;
170     
171     // Caculate corner vertices
172     Point3D geod[4];
173     geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
174     geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
175     geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
176     geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
177
178     Point3D rad[4];
179     int i;
180     for ( i = 0; i < 4; ++i ) {
181         rad[i] = Point3D( geod[i].x() * DEG_TO_RAD, geod[i].y() * DEG_TO_RAD,
182                           geod[i].z() );
183     }
184
185     Point3D cart[4], rel[4];
186     t->nodes.clear();
187     for ( i = 0; i < 4; ++i ) {
188         cart[i] = fgGeodToCart(rad[i]);
189         rel[i] = cart[i] - center;
190         t->nodes.push_back( rel[i] );
191         // cout << "corner " << i << " = " << cart[i] << endl;
192     }
193
194     t->ncount = 4;
195
196     // Calculate bounding radius
197     t->bounding_radius = center.distance3D( cart[0] );
198     fragment.bounding_radius = t->bounding_radius;
199     // cout << "bounding radius = " << t->bounding_radius << endl;
200
201     // Calculate normals
202     Point3D normals[4];
203     for ( i = 0; i < 4; ++i ) {
204         normals[i] = cart[i];
205         double length = normals[i].distance3D( Point3D(0.0) );
206         normals[i] /= length;
207         // cout << "normal = " << normals[i] << endl;
208     }
209
210     // Calculate texture coordinates
211     Point3D texs[4];
212     for ( i = 0; i < 4; ++i ) {
213         texs[i] = calc_tex_coords( rel[i], center );
214         // cout << "texture coordinate = " << texs[i] << endl;
215     }
216
217     // Build flight gear structure
218     fragment.add_face(0, 1, 2);
219     fragment.add_face(0, 2, 3);
220     t->fragment_list.push_back(fragment);
221
222     // Build ssg structure
223     t->vtlist = new sgVec3 [ 4 ];
224     t->vnlist = new sgVec3 [ 4 ];
225     t->tclist = new sgVec2 [ 4 ];
226
227     for ( i = 0; i < 4; ++i ) {
228         sgSetVec3( t->vtlist[i], 
229                    rel[i].x(), rel[i].y(), rel[i].z() );
230         sgSetVec3( t->vnlist[i], 
231                    normals[i].x(), normals[i].y(), normals[i].z() );
232         sgSetVec2( t->tclist[i], texs[i].x(), texs[i].y() );
233     }
234     
235     unsigned short *vindex = new unsigned short [ 4 ];
236     unsigned short *tindex = new unsigned short [ 4 ];
237     for ( i = 0; i < 4; ++i ) {
238         vindex[i] = i;
239         tindex[i] = i;
240     }
241
242     ssgLeaf *leaf = 
243         new ssgVTable ( GL_TRIANGLE_FAN,
244                         4, vindex, t->vtlist,
245                         4, vindex, t->vnlist,
246                         4, tindex, t->tclist,
247                         0, NULL, NULL ) ;
248     leaf->setState( state );
249
250     tile->addKid( leaf );
251
252     return tile;
253 }
254
255
256 // Load a .obj file and build the fragment list
257 ssgBranch *fgObjLoad( const string& path, FGTileEntry *t) {
258     fgFRAGMENT fragment;
259     Point3D pp;
260     double approx_normal[3] /*, normal[3], scale = 0.0 */;
261     // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
262     // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
263     // GLint display_list = 0;
264     int shading;
265     bool in_fragment = false, in_faces = false;
266     int vncount, vtcount;
267     int n1 = 0, n2 = 0, n3 = 0, n4 = 0;
268     int tex;
269     int last1 = 0, last2 = 0, odd = 0;
270     point_list nodes;
271     Point3D node;
272     Point3D center;
273     double scenery_version = 0.0;
274     double tex_width = 1000.0, tex_height = 1000.0;
275     bool shared_done = false;
276     int_list fan_vertices;
277     int_list fan_tex_coords;
278     int i;
279     ssgSimpleState *state = NULL;
280
281     ssgBranch *tile = new ssgBranch () ;
282     tile -> setName ( (char *)path.c_str() ) ;
283
284     // Attempt to open "path.gz" or "path"
285     fg_gzifstream in( path );
286     if ( ! in.is_open() ) {
287         FG_LOG( FG_TERRAIN, FG_ALERT, "Cannot open file: " << path );
288         FG_LOG( FG_TERRAIN, FG_ALERT, "default to ocean tile: " << path );
289
290         return fgGenTile( path, t );
291     }
292
293     shading = current_options.get_shading();
294
295     in_fragment = false;
296     t->ncount = 0;
297     vncount = 0;
298     vtcount = 0;
299     t->bounding_radius = 0.0;
300     center = t->center;
301
302     StopWatch stopwatch;
303     stopwatch.start();
304
305     // ignore initial comments and blank lines. (priming the pump)
306     // in >> skipcomment;
307     // string line;
308
309     string token;
310     char c;
311
312 #ifdef __MWERKS__
313     while ( in.get(c) && c  != '\0' ) {
314         in.putback(c);
315 #else
316     while ( ! in.eof() ) {
317 #endif
318
319 #if defined( MACOS )
320         in >> ::skipws;
321 #else
322         in >> skipws;
323 #endif
324
325         if ( in.get( c ) && c == '#' ) {
326             // process a comment line
327
328             // getline( in, line );
329             // cout << "comment = " << line << endl;
330
331             in >> token;
332
333             if ( token == "version" ) {
334                 // read scenery versions number
335                 in >> scenery_version;
336                 // cout << "scenery_version = " << scenery_version << endl;
337             } else if ( token == "gbs" ) {
338                 // reference point (center offset)
339                 in >> t->center >> t->bounding_radius;
340                 center = t->center;
341                 // cout << "center = " << center 
342                 //      << " radius = " << t->bounding_radius << endl;
343             } else if ( token == "bs" ) {
344                 // reference point (center offset)
345                 in >> fragment.center;
346                 in >> fragment.bounding_radius;
347
348                 // cout << "center = " << fragment.center 
349                 //      << " radius = " << fragment.bounding_radius << endl;
350             } else if ( token == "usemtl" ) {
351                 // material property specification
352
353                 // if first usemtl with shared_done = false, then set
354                 // shared_done true and build the ssg shared lists
355                 if ( ! shared_done ) {
356                     // sanity check
357                     if ( (int)nodes.size() != vncount ) {
358                         FG_LOG( FG_TERRAIN, FG_ALERT, 
359                                 "Tile has mismatched nodes and normals: " 
360                                 << path );
361                         // exit(-1);
362                     }
363                     shared_done = true;
364
365                     t->vtlist = new sgVec3 [ nodes.size() ];
366                     t->vnlist = new sgVec3 [ vncount ];
367                     t->tclist = new sgVec2 [ vtcount ];
368
369                     for ( i = 0; i < (int)nodes.size(); ++i ) {
370                         sgSetVec3( t->vtlist[i], 
371                                    nodes[i][0], nodes[i][1], nodes[i][2] );
372                     }
373                     for ( i = 0; i < vncount; ++i ) {
374                         sgSetVec3( t->vnlist[i], 
375                                    normals[i][0], 
376                                    normals[i][1],
377                                    normals[i][2] );
378                     }
379                     for ( i = 0; i < vtcount; ++i ) {
380                         sgSetVec2( t->tclist[i],
381                                    tex_coords[i][0],
382                                    tex_coords[i][1] );
383                     }
384                 }
385
386                 // series of individual triangles
387                 // if ( in_faces ) {
388                 //     xglEnd();
389                 // }
390
391                 // this also signals the start of a new fragment
392                 if ( in_fragment ) {
393                     // close out the previous structure and start the next
394                     // xglEndList();
395                     // printf("xglEnd(); xglEndList();\n");
396
397                     // update fragment
398                     // fragment.display_list = display_list;
399
400                     // push this fragment onto the tile's object list
401                     t->fragment_list.push_back(fragment);
402                 } else {
403                     in_fragment = true;
404                 }
405
406                 // printf("start of fragment (usemtl)\n");
407
408                 // display_list = xglGenLists(1);
409                 // xglNewList(display_list, GL_COMPILE);
410                 // printf("xglGenLists(); xglNewList();\n");
411                 in_faces = false;
412
413                 // reset the existing face list
414                 // printf("cleaning a fragment with %d faces\n", 
415                 //        fragment.faces.size());
416                 fragment.init();
417                 
418                 // scan the material line
419                 string material;
420                 in >> material;
421                 fragment.tile_ptr = t;
422                 
423                 // find this material in the properties list
424                 if ( ! material_mgr.find( material, fragment.material_ptr )) {
425                     FG_LOG( FG_TERRAIN, FG_ALERT, 
426                             "Ack! unknown usemtl name = " << material 
427                             << " in " << path );
428                 }
429
430                 // set the texture width and height values for this
431                 // material
432                 FGMaterial m = fragment.material_ptr->get_m();
433                 tex_width = m.get_xsize();
434                 tex_height = m.get_ysize();
435                 state = fragment.material_ptr->get_state();
436                 // cout << "(w) = " << tex_width << " (h) = " 
437                 //      << tex_width << endl;
438
439                 // initialize the fragment transformation matrix
440                 /*
441                  for ( i = 0; i < 16; i++ ) {
442                    fragment.matrix[i] = 0.0;
443                  }
444                  fragment.matrix[0] = fragment.matrix[5] =
445                  fragment.matrix[10] = fragment.matrix[15] = 1.0;
446                 */
447             } else {
448                 // unknown comment, just gobble the input untill the
449                 // end of line
450
451                 in >> skipeol;
452             }
453         } else {
454             in.putback( c );
455         
456             in >> token;
457
458             // cout << "token = " << token << endl;
459
460             if ( token == "vn" ) {
461                 // vertex normal
462                 if ( vncount < FG_MAX_NODES ) {
463                     in >> normals[vncount][0]
464                        >> normals[vncount][1]
465                        >> normals[vncount][2];
466                     vncount++;
467                 } else {
468                     FG_LOG( FG_TERRAIN, FG_ALERT, 
469                             "Read too many vertex normals in " << path 
470                             << " ... dying :-(" );
471                     exit(-1);
472                 }
473             } else if ( token == "vt" ) {
474                 // vertex texture coordinate
475                 if ( vtcount < FG_MAX_NODES*3 ) {
476                     in >> tex_coords[vtcount][0]
477                        >> tex_coords[vtcount][1];
478                     vtcount++;
479                 } else {
480                     FG_LOG( FG_TERRAIN, FG_ALERT, 
481                             "Read too many vertex texture coords in " << path
482                             << " ... dying :-("
483                             );
484                     exit(-1);
485                 }
486             } else if ( token == "v" ) {
487                 // node (vertex)
488                 if ( t->ncount < FG_MAX_NODES ) {
489                     /* in >> nodes[t->ncount][0]
490                        >> nodes[t->ncount][1]
491                        >> nodes[t->ncount][2]; */
492                     in >> node;
493                     nodes.push_back(node);
494                     t->ncount++;
495                 } else {
496                     FG_LOG( FG_TERRAIN, FG_ALERT, 
497                             "Read too many nodes in " << path 
498                             << " ... dying :-(");
499                     exit(-1);
500                 }
501             } else if ( token == "t" ) {
502                 // start a new triangle strip
503
504                 n1 = n2 = n3 = n4 = 0;
505
506                 // fgPrintf( FG_TERRAIN, FG_DEBUG, 
507                 //           "    new tri strip = %s", line);
508                 in >> n1 >> n2 >> n3;
509                 fragment.add_face(n1, n2, n3);
510
511                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "(t) = ");
512
513                 // xglBegin(GL_TRIANGLE_STRIP);
514                 // printf("xglBegin(tristrip) %d %d %d\n", n1, n2, n3);
515
516                 odd = 1; 
517                 // scale = 1.0;
518
519                 if ( shading ) {
520                     // Shading model is "GL_SMOOTH" so use precalculated
521                     // (averaged) normals
522                     // MAT3_SCALE_VEC(normal, normals[n1], scale);
523                     // xglNormal3dv(normal);
524                     pp = calc_tex_coords(nodes[n1], center);
525                     // xglTexCoord2f(pp.lon(), pp.lat());
526                     // xglVertex3dv(nodes[n1].get_n());         
527
528                     // MAT3_SCALE_VEC(normal, normals[n2], scale);
529                     // xglNormal3dv(normal);
530                     pp = calc_tex_coords(nodes[n2], center);
531                     // xglTexCoord2f(pp.lon(), pp.lat());
532                     // xglVertex3dv(nodes[n2].get_n());                         
533
534                     // MAT3_SCALE_VEC(normal, normals[n3], scale);
535                     // xglNormal3dv(normal);
536                     pp = calc_tex_coords(nodes[n3], center);
537                     // xglTexCoord2f(pp.lon(), pp.lat());
538                     // xglVertex3dv(nodes[n3].get_n());
539                 } else {
540                     // Shading model is "GL_FLAT" so calculate per face
541                     // normals on the fly.
542                     if ( odd ) {
543                         calc_normal(nodes[n1], nodes[n2], 
544                                     nodes[n3], approx_normal);
545                     } else {
546                         calc_normal(nodes[n2], nodes[n1], 
547                                     nodes[n3], approx_normal);
548                     }
549                     // MAT3_SCALE_VEC(normal, approx_normal, scale);
550                     // xglNormal3dv(normal);
551
552                     pp = calc_tex_coords(nodes[n1], center);
553                     // xglTexCoord2f(pp.lon(), pp.lat());
554                     // xglVertex3dv(nodes[n1].get_n());         
555
556                     pp = calc_tex_coords(nodes[n2], center);
557                     // xglTexCoord2f(pp.lon(), pp.lat());
558                     // xglVertex3dv(nodes[n2].get_n());         
559                     
560                     pp = calc_tex_coords(nodes[n3], center);
561                     // xglTexCoord2f(pp.lon(), pp.lat());
562                     // xglVertex3dv(nodes[n3].get_n());         
563                 }
564                 // printf("some normals, texcoords, and vertices\n");
565
566                 odd = 1 - odd;
567                 last1 = n2;
568                 last2 = n3;
569
570                 // There can be three or four values 
571                 char c;
572                 while ( in.get(c) ) {
573                     if ( c == '\n' ) {
574                         break; // only the one
575                     }
576                     if ( isdigit(c) ){
577                         in.putback(c);
578                         in >> n4;
579                         break;
580                     }
581                 }
582
583                 if ( n4 > 0 ) {
584                     fragment.add_face(n3, n2, n4);
585                     
586                     if ( shading ) {
587                         // Shading model is "GL_SMOOTH"
588                         // MAT3_SCALE_VEC(normal, normals[n4], scale);
589                     } else {
590                         // Shading model is "GL_FLAT"
591                         calc_normal(nodes[n3], nodes[n2], nodes[n4], 
592                                     approx_normal);
593                         // MAT3_SCALE_VEC(normal, approx_normal, scale);
594                     }
595                     // xglNormal3dv(normal);
596                     pp = calc_tex_coords(nodes[n4], center);
597                     // xglTexCoord2f(pp.lon(), pp.lat());
598                     // xglVertex3dv(nodes[n4].get_n());         
599                     
600                     odd = 1 - odd;
601                     last1 = n3;
602                     last2 = n4;
603                     // printf("a normal, texcoord, and vertex (4th)\n");
604                 }
605             } else if ( token == "tf" ) {
606                 // triangle fan
607                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "new fan");
608
609                 fan_vertices.clear();
610                 fan_tex_coords.clear();
611
612                 // xglBegin(GL_TRIANGLE_FAN);
613
614                 in >> n1;
615                 fan_vertices.push_back( n1 );
616                 // xglNormal3dv(normals[n1]);
617                 if ( in.get( c ) && c == '/' ) {
618                     in >> tex;
619                     fan_tex_coords.push_back( tex );
620                     if ( scenery_version >= 0.4 ) {
621                         t->tclist[tex][0] *= (1000.0 / tex_width);
622                         t->tclist[tex][1] *= (1000.0 / tex_height);
623                     }
624                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
625                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
626                 } else {
627                     in.putback( c );
628                     pp = calc_tex_coords(nodes[n1], center);
629                 }
630                 // xglTexCoord2f(pp.x(), pp.y());
631                 // xglVertex3dv(nodes[n1].get_n());
632
633                 in >> n2;
634                 fan_vertices.push_back( n2 );
635                 // xglNormal3dv(normals[n2]);
636                 if ( in.get( c ) && c == '/' ) {
637                     in >> tex;
638                     fan_tex_coords.push_back( tex );
639                     if ( scenery_version >= 0.4 ) {
640                         t->tclist[tex][0] *= (1000.0 / tex_width);
641                         t->tclist[tex][1] *= (1000.0 / tex_height);
642                     }
643                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
644                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
645                 } else {
646                     in.putback( c );
647                     pp = calc_tex_coords(nodes[n2], center);
648                 }
649                 // xglTexCoord2f(pp.x(), pp.y());
650                 // xglVertex3dv(nodes[n2].get_n());
651                 
652                 // read all subsequent numbers until next thing isn't a number
653                 while ( true ) {
654 #if defined( MACOS )
655                     in >> ::skipws;
656 #else
657                     in >> skipws;
658 #endif
659
660                     char c;
661                     in.get(c);
662                     in.putback(c);
663                     if ( ! isdigit(c) || in.eof() ) {
664                         break;
665                     }
666
667                     in >> n3;
668                     fan_vertices.push_back( n3 );
669                     // cout << "  triangle = " 
670                     //      << n1 << "," << n2 << "," << n3 
671                     //      << endl;
672                     // xglNormal3dv(normals[n3]);
673                     if ( in.get( c ) && c == '/' ) {
674                         in >> tex;
675                         fan_tex_coords.push_back( tex );
676                         if ( scenery_version >= 0.4 ) {
677                             t->tclist[tex][0] *= (1000.0 / tex_width);
678                             t->tclist[tex][1] *= (1000.0 / tex_height);
679                         }
680                         pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
681                         pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
682                     } else {
683                         in.putback( c );
684                         pp = calc_tex_coords(nodes[n3], center);
685                     }
686                     // xglTexCoord2f(pp.x(), pp.y());
687                     // xglVertex3dv(nodes[n3].get_n());
688
689                     fragment.add_face(n1, n2, n3);
690                     n2 = n3;
691                 }
692
693                 // xglEnd();
694
695                 // build the ssg entity
696                 unsigned short *vindex = 
697                     new unsigned short [ fan_vertices.size() ];
698                 unsigned short *tindex = 
699                     new unsigned short [ fan_tex_coords.size() ];
700                 for ( i = 0; i < (int)fan_vertices.size(); ++i ) {
701                     vindex[i] = fan_vertices[i];
702                 }
703                 for ( i = 0; i < (int)fan_tex_coords.size(); ++i ) {
704                     tindex[i] = fan_tex_coords[i];
705                 }
706                 ssgLeaf *leaf = 
707                     new ssgVTable ( GL_TRIANGLE_FAN,
708                                     fan_vertices.size(), vindex, t->vtlist,
709                                     fan_vertices.size(), vindex, t->vnlist,
710                                     fan_tex_coords.size(), tindex, t->tclist,
711                                     0, NULL, NULL ) ;
712                 leaf->setState( state );
713
714                 tile->addKid( leaf );
715
716             } else if ( token == "f" ) {
717                 // unoptimized face
718
719                 if ( !in_faces ) {
720                     // xglBegin(GL_TRIANGLES);
721                     // printf("xglBegin(triangles)\n");
722                     in_faces = true;
723                 }
724
725                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "new triangle = %s", line);*/
726                 in >> n1 >> n2 >> n3;
727                 fragment.add_face(n1, n2, n3);
728
729                 // xglNormal3d(normals[n1][0], normals[n1][1], normals[n1][2]);
730                 // xglNormal3dv(normals[n1]);
731                 pp = calc_tex_coords(nodes[n1], center);
732                 // xglTexCoord2f(pp.lon(), pp.lat());
733                 // xglVertex3dv(nodes[n1].get_n());
734
735                 // xglNormal3dv(normals[n2]);
736                 pp = calc_tex_coords(nodes[n2], center);
737                 // xglTexCoord2f(pp.lon(), pp.lat());
738                 // xglVertex3dv(nodes[n2].get_n());
739                 
740                 // xglNormal3dv(normals[n3]);
741                 pp = calc_tex_coords(nodes[n3], center);
742                 // xglTexCoord2f(pp.lon(), pp.lat());
743                 // xglVertex3dv(nodes[n3].get_n());
744                 // printf("some normals, texcoords, and vertices (tris)\n");
745             } else if ( token == "q" ) {
746                 // continue a triangle strip
747                 n1 = n2 = 0;
748
749                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "continued tri strip = %s ", 
750                 //           line);
751                 in >> n1;
752
753                 // There can be one or two values 
754                 char c;
755                 while ( in.get(c) ) {
756                     if ( c == '\n' ) {
757                         break; // only the one
758                     }
759
760                     if ( isdigit(c) ) {
761                         in.putback(c);
762                         in >> n2;
763                         break;
764                     }
765                 }
766                 // fgPrintf( FG_TERRAIN, FG_DEBUG, "read %d %d\n", n1, n2);
767
768                 if ( odd ) {
769                     fragment.add_face(last1, last2, n1);
770                 } else {
771                     fragment.add_face(last2, last1, n1);
772                 }
773
774                 if ( shading ) {
775                     // Shading model is "GL_SMOOTH"
776                     // MAT3_SCALE_VEC(normal, normals[n1], scale);
777                 } else {
778                     // Shading model is "GL_FLAT"
779                     if ( odd ) {
780                         calc_normal(nodes[last1], nodes[last2], 
781                                     nodes[n1], approx_normal);
782                     } else {
783                         calc_normal(nodes[last2], nodes[last1], 
784                                     nodes[n1], approx_normal);
785                     }
786                     // MAT3_SCALE_VEC(normal, approx_normal, scale);
787                 }
788                 // xglNormal3dv(normal);
789
790                 pp = calc_tex_coords(nodes[n1], center);
791                 // xglTexCoord2f(pp.lon(), pp.lat());
792                 // xglVertex3dv(nodes[n1].get_n());
793                 // printf("a normal, texcoord, and vertex (4th)\n");
794    
795                 odd = 1 - odd;
796                 last1 = last2;
797                 last2 = n1;
798
799                 if ( n2 > 0 ) {
800                     // fgPrintf( FG_TERRAIN, FG_DEBUG, " (cont)\n");
801
802                     if ( odd ) {
803                         fragment.add_face(last1, last2, n2);
804                     } else {
805                         fragment.add_face(last2, last1, n2);
806                     }
807
808                     if ( shading ) {
809                         // Shading model is "GL_SMOOTH"
810                         // MAT3_SCALE_VEC(normal, normals[n2], scale);
811                     } else {
812                         // Shading model is "GL_FLAT"
813                         if ( odd ) {
814                             calc_normal(nodes[last1], nodes[last2], 
815                                         nodes[n2], approx_normal);
816                         } else {
817                             calc_normal(nodes[last2], nodes[last1], 
818                                         nodes[n2], approx_normal);
819                         }
820                         // MAT3_SCALE_VEC(normal, approx_normal, scale);
821                     }
822                     // xglNormal3dv(normal);
823                 
824                     pp = calc_tex_coords(nodes[n2], center);
825                     // xglTexCoord2f(pp.lon(), pp.lat());
826                     // xglVertex3dv(nodes[n2].get_n());         
827                     // printf("a normal, texcoord, and vertex (4th)\n");
828
829                     odd = 1 -odd;
830                     last1 = last2;
831                     last2 = n2;
832                 }
833             } else {
834                 FG_LOG( FG_TERRAIN, FG_WARN, "Unknown token in " 
835                         << path << " = " << token );
836             }
837
838             // eat white space before start of while loop so if we are
839             // done with useful input it is noticed before hand.
840 #if defined( MACOS )
841             in >> ::skipws;
842 #else
843             in >> skipws;
844 #endif
845         }
846     }
847
848     if ( in_fragment ) {
849         // close out the previous structure and start the next
850         // xglEnd();
851         // xglEndList();
852         // printf("xglEnd(); xglEndList();\n");
853         
854         // update fragment
855         // fragment.display_list = display_list;
856         
857         // push this fragment onto the tile's object list
858         t->fragment_list.push_back(fragment);
859     }
860
861 #if 0
862     // Draw normal vectors (for visually verifying normals)
863     xglBegin(GL_LINES);
864     xglColor3f(0.0, 0.0, 0.0);
865     for ( i = 0; i < t->ncount; i++ ) {
866         xglVertex3d(nodes[i][0],
867                     nodes[i][1] ,
868                     nodes[i][2]);
869         xglVertex3d(nodes[i][0] + 500*normals[i][0],
870                     nodes[i][1] + 500*normals[i][1],
871                     nodes[i][2] + 500*normals[i][2]);
872     } 
873     xglEnd();
874 #endif
875
876     t->nodes = nodes;
877
878     stopwatch.stop();
879     FG_LOG( FG_TERRAIN, FG_DEBUG, 
880             "Loaded " << path << " in " 
881             << stopwatch.elapsedSeconds() << " seconds" );
882     
883     return tile;
884 }
885
886