]> git.mxchange.org Git - flightgear.git/blob - src/Objects/obj.cxx
Attempt to fix a segfault on exit, although I don't think this quite
[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 SG_MATH_EXCEPTION_CLASH
29 #  include <math.h>
30 #endif
31
32 #include <stdio.h>
33 #include <string.h>
34
35 #include <simgear/compiler.h>
36 #include <simgear/io/sg_binobj.hxx>
37
38 #include STL_STRING
39 #include <map>                  // STL
40 #include <vector>               // STL
41 #include <ctype.h>              // isdigit()
42
43 #include <simgear/constants.h>
44 #include <simgear/debug/logstream.hxx>
45 #include <simgear/math/point3d.hxx>
46 #include <simgear/math/polar3d.hxx>
47 #include <simgear/math/sg_geodesy.hxx>
48 #include <simgear/math/sg_random.h>
49 #include <simgear/misc/sgstream.hxx>
50 #include <simgear/misc/stopwatch.hxx>
51 #include <simgear/misc/texcoord.hxx>
52
53 #include <Main/globals.hxx>
54 #include <Main/fg_props.hxx>
55 #include <Scenery/tileentry.hxx>
56
57 #include "matlib.hxx"
58 #include "obj.hxx"
59
60 SG_USING_STD(string);
61 SG_USING_STD(vector);
62
63
64 typedef vector < int > int_list;
65 typedef int_list::iterator int_list_iterator;
66 typedef int_list::const_iterator int_point_list_iterator;
67
68
69 static double normals[FG_MAX_NODES][3];
70 static double tex_coords[FG_MAX_NODES*3][3];
71
72
73 #define FG_TEX_CONSTANT 69.0
74
75 // Calculate texture coordinates for a given point.
76 static Point3D local_calc_tex_coords(const Point3D& node, const Point3D& ref) {
77     Point3D cp;
78     Point3D pp;
79     // double tmplon, tmplat;
80
81     // cout << "-> " << node[0] << " " << node[1] << " " << node[2] << endl;
82     // cout << "-> " << ref.x() << " " << ref.y() << " " << ref.z() << endl;
83
84     cp = Point3D( node[0] + ref.x(),
85                   node[1] + ref.y(),
86                   node[2] + ref.z() );
87
88     pp = sgCartToPolar3d(cp);
89
90     // tmplon = pp.lon() * SGD_RADIANS_TO_DEGREES;
91     // tmplat = pp.lat() * SGD_RADIANS_TO_DEGREES;
92     // cout << tmplon << " " << tmplat << endl;
93
94     pp.setx( fmod(SGD_RADIANS_TO_DEGREES * FG_TEX_CONSTANT * pp.x(), 11.0) );
95     pp.sety( fmod(SGD_RADIANS_TO_DEGREES * FG_TEX_CONSTANT * pp.y(), 11.0) );
96
97     if ( pp.x() < 0.0 ) {
98         pp.setx( pp.x() + 11.0 );
99     }
100
101     if ( pp.y() < 0.0 ) {
102         pp.sety( pp.y() + 11.0 );
103     }
104
105     // cout << pp << endl;
106
107     return(pp);
108 }
109
110
111 // Generate a generic ocean tile on the fly
112 ssgBranch *fgGenTile( const string& path, FGTileEntry *t) {
113     FGNewMat *newmat;
114
115     ssgSimpleState *state = NULL;
116
117     ssgBranch *tile = new ssgBranch () ;
118     tile -> setName ( (char *)path.c_str() ) ;
119
120     double tex_width = 1000.0;
121     // double tex_height;
122
123     // find Ocean material in the properties list
124     newmat = material_lib.find( "Ocean" );
125     if ( newmat != NULL ) {
126         // set the texture width and height values for this
127         // material
128         tex_width = newmat->get_xsize();
129         // tex_height = newmat->get_ysize();
130         
131         // set ssgState
132         state = newmat->get_state();
133     } else {
134         SG_LOG( SG_TERRAIN, SG_ALERT, 
135                 "Ack! unknown usemtl name = " << "Ocean" 
136                 << " in " << path );
137     }
138
139     // Calculate center point
140     SGBucket b = t->tile_bucket;
141     double clon = b.get_center_lon();
142     double clat = b.get_center_lat();
143     double height = b.get_height();
144     double width = b.get_width();
145
146     Point3D center = sgGeodToCart(Point3D(clon*SGD_DEGREES_TO_RADIANS,clat*SGD_DEGREES_TO_RADIANS,0.0));
147     t->center = center;
148     // cout << "center = " << center << endl;;
149     
150     // Caculate corner vertices
151     Point3D geod[4];
152     geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
153     geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
154     geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
155     geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
156
157     Point3D rad[4];
158     int i;
159     for ( i = 0; i < 4; ++i ) {
160         rad[i] = Point3D( geod[i].x() * SGD_DEGREES_TO_RADIANS, geod[i].y() * SGD_DEGREES_TO_RADIANS,
161                           geod[i].z() );
162     }
163
164     Point3D cart[4], rel[4];
165     t->nodes.clear();
166     for ( i = 0; i < 4; ++i ) {
167         cart[i] = sgGeodToCart(rad[i]);
168         rel[i] = cart[i] - center;
169         t->nodes.push_back( rel[i] );
170         // cout << "corner " << i << " = " << cart[i] << endl;
171     }
172
173     t->ncount = 4;
174
175     // Calculate bounding radius
176     t->bounding_radius = center.distance3D( cart[0] );
177     // cout << "bounding radius = " << t->bounding_radius << endl;
178
179     // Calculate normals
180     Point3D normals[4];
181     for ( i = 0; i < 4; ++i ) {
182         normals[i] = cart[i];
183         double length = normals[i].distance3D( Point3D(0.0) );
184         normals[i] /= length;
185         // cout << "normal = " << normals[i] << endl;
186     }
187
188     // Calculate texture coordinates
189     point_list geod_nodes;
190     geod_nodes.clear();
191     for ( i = 0; i < 4; ++i ) {
192         geod_nodes.push_back( geod[i] );
193     }
194     int_list rectangle;
195     rectangle.clear();
196     for ( i = 0; i < 4; ++i ) {
197         rectangle.push_back( i );
198     }
199     point_list texs = calc_tex_coords( b, geod_nodes, rectangle, 
200                                        1000.0 / tex_width );
201
202     // Allocate ssg structure
203     ssgVertexArray   *vl = new ssgVertexArray( 4 );
204     ssgNormalArray   *nl = new ssgNormalArray( 4 );
205     ssgTexCoordArray *tl = new ssgTexCoordArray( 4 );
206     ssgColourArray   *cl = new ssgColourArray( 1 );
207
208     sgVec4 color;
209     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
210     cl->add( color );
211
212     // sgVec3 *vtlist = new sgVec3 [ 4 ];
213     // t->vec3_ptrs.push_back( vtlist );
214     // sgVec3 *vnlist = new sgVec3 [ 4 ];
215     // t->vec3_ptrs.push_back( vnlist );
216     // sgVec2 *tclist = new sgVec2 [ 4 ];
217     // t->vec2_ptrs.push_back( tclist );
218
219     sgVec2 tmp2;
220     sgVec3 tmp3;
221     for ( i = 0; i < 4; ++i ) {
222         sgSetVec3( tmp3, 
223                    rel[i].x(), rel[i].y(), rel[i].z() );
224         vl->add( tmp3 );
225
226         sgSetVec3( tmp3, 
227                    normals[i].x(), normals[i].y(), normals[i].z() );
228         nl->add( tmp3 );
229
230         sgSetVec2( tmp2, texs[i].x(), texs[i].y());
231         tl->add( tmp2 );
232     }
233     
234     ssgLeaf *leaf = 
235         new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
236
237     leaf->setState( state );
238
239     tile->addKid( leaf );
240
241     return tile;
242 }
243
244
245 static void random_pt_inside_tri( float *res,
246                                   float *n1, float *n2, float *n3 )
247 {
248     sgVec3 p1, p2, p3;
249
250     double a = sg_random();
251     double b = sg_random();
252     if ( a + b > 1.0 ) {
253         a = 1.0 - a;
254         b = 1.0 - b;
255     }
256     double c = 1 - a - b;
257
258     sgScaleVec3( p1, n1, a );
259     sgScaleVec3( p2, n2, b );
260     sgScaleVec3( p3, n3, c );
261
262     sgAddVec3( res, p1, p2 );
263     sgAddVec3( res, p3 );
264 }
265
266
267 static void gen_random_surface_points( ssgLeaf *leaf, ssgVertexArray *lights,
268                                        double factor ) {
269     int num = leaf->getNumTriangles();
270     if ( num > 0 ) {
271         short int n1, n2, n3;
272         float *p1, *p2, *p3;
273         sgVec3 result;
274
275         // generate a repeatable random seed
276         p1 = leaf->getVertex( 0 );
277         unsigned int seed = (unsigned int)p1[0];
278         sg_srandom( seed );
279
280         for ( int i = 0; i < num; ++i ) {
281             leaf->getTriangle( i, &n1, &n2, &n3 );
282             p1 = leaf->getVertex(n1);
283             p2 = leaf->getVertex(n2);
284             p3 = leaf->getVertex(n3);
285             double area = sgTriArea( p1, p2, p3 );
286             double num = area / factor;
287
288             // generate a light point for each unit of area
289             while ( num > 1.0 ) {
290                 random_pt_inside_tri( result, p1, p2, p3 );
291                 lights->add( result );
292                 num -= 1.0;
293             }
294             // for partial units of area, use a zombie door method to
295             // create the proper random chance of a light being created
296             // for this triangle
297             if ( num > 0.0 ) {
298                 if ( sg_random() <= num ) {
299                     // a zombie made it through our door
300                     random_pt_inside_tri( result, p1, p2, p3 );
301                     lights->add( result );
302                 }
303             }
304         }
305     }
306 }
307
308
309 // Load an Ascii obj file
310 ssgBranch *fgAsciiObjLoad( const string& path, FGTileEntry *t,
311                                   ssgVertexArray *lights, const bool is_base)
312 {
313     FGNewMat *newmat = NULL;
314     string material;
315     float coverage = -1;
316     Point3D pp;
317     // sgVec3 approx_normal;
318     // double normal[3], scale = 0.0;
319     // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
320     // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
321     // GLint display_list = 0;
322     int shading;
323     bool in_faces = false;
324     int vncount, vtcount;
325     int n1 = 0, n2 = 0, n3 = 0;
326     int tex;
327     // int last1 = 0, last2 = 0;
328     bool odd = false;
329     point_list nodes;
330     Point3D node;
331     Point3D center;
332     double scenery_version = 0.0;
333     double tex_width = 1000.0, tex_height = 1000.0;
334     bool shared_done = false;
335     int_list fan_vertices;
336     int_list fan_tex_coords;
337     int i;
338     ssgSimpleState *state = NULL;
339     sgVec3 *vtlist, *vnlist;
340     sgVec2 *tclist;
341
342     ssgBranch *tile = new ssgBranch () ;
343
344     tile -> setName ( (char *)path.c_str() ) ;
345
346     // Attempt to open "path.gz" or "path"
347     sg_gzifstream in( path );
348     if ( ! in.is_open() ) {
349         SG_LOG( SG_TERRAIN, SG_DEBUG, "Cannot open file: " << path );
350         SG_LOG( SG_TERRAIN, SG_DEBUG, "default to ocean tile: " << path );
351
352         return NULL;
353     }
354
355     shading = fgGetBool("/sim/rendering/shading");
356
357     if ( is_base ) {
358         t->ncount = 0;
359     }
360     vncount = 0;
361     vtcount = 0;
362     if ( is_base ) {
363         t->bounding_radius = 0.0;
364     }
365     center = t->center;
366
367     StopWatch stopwatch;
368     stopwatch.start();
369
370     // ignore initial comments and blank lines. (priming the pump)
371     // in >> skipcomment;
372     // string line;
373
374     string token;
375     char c;
376
377 #ifdef __MWERKS__
378     while ( in.get(c) && c  != '\0' ) {
379         in.putback(c);
380 #else
381     while ( ! in.eof() ) {
382 #endif
383
384 #if defined( macintosh ) || defined( _MSC_VER )
385         in >> ::skipws;
386 #else
387         in >> skipws;
388 #endif
389
390         if ( in.get( c ) && c == '#' ) {
391             // process a comment line
392
393             // getline( in, line );
394             // cout << "comment = " << line << endl;
395
396             in >> token;
397
398             if ( token == "Version" ) {
399                 // read scenery versions number
400                 in >> scenery_version;
401                 // cout << "scenery_version = " << scenery_version << endl;
402                 if ( scenery_version > 0.4 ) {
403                     SG_LOG( SG_TERRAIN, SG_ALERT, 
404                             "\nYou are attempting to load a tile format that\n"
405                             << "is newer than this version of flightgear can\n"
406                             << "handle.  You should upgrade your copy of\n"
407                             << "FlightGear to the newest version.  For\n"
408                             << "details, please see:\n"
409                             << "\n    http://www.flightgear.org\n" );
410                     exit(-1);
411                 }
412             } else if ( token == "gbs" ) {
413                 // reference point (center offset)
414                 if ( is_base ) {
415                     in >> t->center >> t->bounding_radius;
416                 } else {
417                     Point3D junk1;
418                     double junk2;
419                     in >> junk1 >> junk2;
420                 }
421                 center = t->center;
422                 // cout << "center = " << center 
423                 //      << " radius = " << t->bounding_radius << endl;
424             } else if ( token == "bs" ) {
425                 // reference point (center offset)
426                 // (skip past this)
427                 Point3D junk1;
428                 double junk2;
429                 in >> junk1 >> junk2;
430             } else if ( token == "usemtl" ) {
431                 // material property specification
432
433                 // if first usemtl with shared_done = false, then set
434                 // shared_done true and build the ssg shared lists
435                 if ( ! shared_done ) {
436                     // sanity check
437                     if ( (int)nodes.size() != vncount ) {
438                         SG_LOG( SG_TERRAIN, SG_ALERT, 
439                                 "Tile has mismatched nodes = " << nodes.size()
440                                 << " and normals = " << vncount << " : " 
441                                 << path );
442                         // exit(-1);
443                     }
444                     shared_done = true;
445
446                     vtlist = new sgVec3 [ nodes.size() ];
447                     t->vec3_ptrs.push_back( vtlist );
448                     vnlist = new sgVec3 [ vncount ];
449                     t->vec3_ptrs.push_back( vnlist );
450                     tclist = new sgVec2 [ vtcount ];
451                     t->vec2_ptrs.push_back( tclist );
452
453                     for ( i = 0; i < (int)nodes.size(); ++i ) {
454                         sgSetVec3( vtlist[i], 
455                                    nodes[i][0], nodes[i][1], nodes[i][2] );
456                     }
457                     for ( i = 0; i < vncount; ++i ) {
458                         sgSetVec3( vnlist[i], 
459                                    normals[i][0], 
460                                    normals[i][1],
461                                    normals[i][2] );
462                     }
463                     for ( i = 0; i < vtcount; ++i ) {
464                         sgSetVec2( tclist[i],
465                                    tex_coords[i][0],
466                                    tex_coords[i][1] );
467                     }
468                 }
469
470                 // display_list = xglGenLists(1);
471                 // xglNewList(display_list, GL_COMPILE);
472                 // printf("xglGenLists(); xglNewList();\n");
473                 in_faces = false;
474
475                 // scan the material line
476                 in >> material;
477                 
478                 // find this material in the properties list
479
480                 newmat = material_lib.find( material );
481                 if ( newmat == NULL ) {
482                     // see if this is an on the fly texture
483                     string file = path;
484                     int pos = file.rfind( "/" );
485                     file = file.substr( 0, pos );
486                     cout << "current file = " << file << endl;
487                     file += "/";
488                     file += material;
489                     cout << "current file = " << file << endl;
490                     if ( ! material_lib.add_item( file ) ) {
491                         SG_LOG( SG_TERRAIN, SG_ALERT, 
492                                 "Ack! unknown usemtl name = " << material 
493                                 << " in " << path );
494                     } else {
495                         // locate our newly created material
496                         newmat = material_lib.find( material );
497                         if ( newmat == NULL ) {
498                             SG_LOG( SG_TERRAIN, SG_ALERT, 
499                                     "Ack! bad on the fly materia create = "
500                                     << material << " in " << path );
501                         }
502                     }
503                 }
504
505                 if ( newmat != NULL ) {
506                     // set the texture width and height values for this
507                     // material
508                     tex_width = newmat->get_xsize();
509                     tex_height = newmat->get_ysize();
510                     state = newmat->get_state();
511                     coverage = newmat->get_light_coverage();
512                     // cout << "(w) = " << tex_width << " (h) = " 
513                     //      << tex_width << endl;
514                 } else {
515                     coverage = -1;
516                 }
517             } else {
518                 // unknown comment, just gobble the input until the
519                 // end of line
520
521                 in >> skipeol;
522             }
523         } else {
524             in.putback( c );
525         
526             in >> token;
527
528             // cout << "token = " << token << endl;
529
530             if ( token == "vn" ) {
531                 // vertex normal
532                 if ( vncount < FG_MAX_NODES ) {
533                     in >> normals[vncount][0]
534                        >> normals[vncount][1]
535                        >> normals[vncount][2];
536                     vncount++;
537                 } else {
538                     SG_LOG( SG_TERRAIN, SG_ALERT, 
539                             "Read too many vertex normals in " << path 
540                             << " ... dying :-(" );
541                     exit(-1);
542                 }
543             } else if ( token == "vt" ) {
544                 // vertex texture coordinate
545                 if ( vtcount < FG_MAX_NODES*3 ) {
546                     in >> tex_coords[vtcount][0]
547                        >> tex_coords[vtcount][1];
548                     vtcount++;
549                 } else {
550                     SG_LOG( SG_TERRAIN, SG_ALERT, 
551                             "Read too many vertex texture coords in " << path
552                             << " ... dying :-("
553                             );
554                     exit(-1);
555                 }
556             } else if ( token == "v" ) {
557                 // node (vertex)
558                 if ( t->ncount < FG_MAX_NODES ) {
559                     /* in >> nodes[t->ncount][0]
560                        >> nodes[t->ncount][1]
561                        >> nodes[t->ncount][2]; */
562                     in >> node;
563                     nodes.push_back(node);
564                     if ( is_base ) {
565                         t->ncount++;
566                     }
567                 } else {
568                     SG_LOG( SG_TERRAIN, SG_ALERT, 
569                             "Read too many nodes in " << path 
570                             << " ... dying :-(");
571                     exit(-1);
572                 }
573             } else if ( (token == "tf") || (token == "ts") || (token == "f") ) {
574                 // triangle fan, strip, or individual face
575                 // SG_LOG( SG_TERRAIN, SG_INFO, "new fan or strip");
576
577                 fan_vertices.clear();
578                 fan_tex_coords.clear();
579                 odd = true;
580
581                 // xglBegin(GL_TRIANGLE_FAN);
582
583                 in >> n1;
584                 fan_vertices.push_back( n1 );
585                 // xglNormal3dv(normals[n1]);
586                 if ( in.get( c ) && c == '/' ) {
587                     in >> tex;
588                     fan_tex_coords.push_back( tex );
589                     if ( scenery_version >= 0.4 ) {
590                         if ( tex_width > 0 ) {
591                             tclist[tex][0] *= (1000.0 / tex_width);
592                         }
593                         if ( tex_height > 0 ) {
594                             tclist[tex][1] *= (1000.0 / tex_height);
595                         }
596                     }
597                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
598                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
599                 } else {
600                     in.putback( c );
601                     pp = local_calc_tex_coords(nodes[n1], center);
602                 }
603                 // xglTexCoord2f(pp.x(), pp.y());
604                 // xglVertex3dv(nodes[n1].get_n());
605
606                 in >> n2;
607                 fan_vertices.push_back( n2 );
608                 // xglNormal3dv(normals[n2]);
609                 if ( in.get( c ) && c == '/' ) {
610                     in >> tex;
611                     fan_tex_coords.push_back( tex );
612                     if ( scenery_version >= 0.4 ) {
613                         if ( tex_width > 0 ) {
614                             tclist[tex][0] *= (1000.0 / tex_width);
615                         }
616                         if ( tex_height > 0 ) {
617                             tclist[tex][1] *= (1000.0 / tex_height);
618                         }
619                     }
620                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
621                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
622                 } else {
623                     in.putback( c );
624                     pp = local_calc_tex_coords(nodes[n2], center);
625                 }
626                 // xglTexCoord2f(pp.x(), pp.y());
627                 // xglVertex3dv(nodes[n2].get_n());
628                 
629                 // read all subsequent numbers until next thing isn't a number
630                 while ( true ) {
631 #if defined( macintosh ) || defined( _MSC_VER )
632                     in >> ::skipws;
633 #else
634                     in >> skipws;
635 #endif
636
637                     char c;
638                     in.get(c);
639                     in.putback(c);
640                     if ( ! isdigit(c) || in.eof() ) {
641                         break;
642                     }
643
644                     in >> n3;
645                     fan_vertices.push_back( n3 );
646                     // cout << "  triangle = " 
647                     //      << n1 << "," << n2 << "," << n3 
648                     //      << endl;
649                     // xglNormal3dv(normals[n3]);
650                     if ( in.get( c ) && c == '/' ) {
651                         in >> tex;
652                         fan_tex_coords.push_back( tex );
653                         if ( scenery_version >= 0.4 ) {
654                             if ( tex_width > 0 ) {
655                                 tclist[tex][0] *= (1000.0 / tex_width);
656                             }
657                             if ( tex_height > 0 ) {
658                                 tclist[tex][1] *= (1000.0 / tex_height);
659                             }
660                         }
661                         pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
662                         pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
663                     } else {
664                         in.putback( c );
665                         pp = local_calc_tex_coords(nodes[n3], center);
666                     }
667                     // xglTexCoord2f(pp.x(), pp.y());
668                     // xglVertex3dv(nodes[n3].get_n());
669
670                     if ( (token == "tf") || (token == "f") ) {
671                         // triangle fan
672                         n2 = n3;
673                     } else {
674                         // triangle strip
675                         odd = !odd;
676                         n1 = n2;
677                         n2 = n3;
678                     }
679                 }
680
681                 // xglEnd();
682
683                 // build the ssg entity
684                 int size = (int)fan_vertices.size();
685                 ssgVertexArray   *vl = new ssgVertexArray( size );
686                 ssgNormalArray   *nl = new ssgNormalArray( size );
687                 ssgTexCoordArray *tl = new ssgTexCoordArray( size );
688                 ssgColourArray   *cl = new ssgColourArray( 1 );
689
690                 sgVec4 color;
691                 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
692                 cl->add( color );
693
694                 sgVec2 tmp2;
695                 sgVec3 tmp3;
696                 for ( i = 0; i < size; ++i ) {
697                     sgCopyVec3( tmp3, vtlist[ fan_vertices[i] ] );
698                     vl -> add( tmp3 );
699
700                     sgCopyVec3( tmp3, vnlist[ fan_vertices[i] ] );
701                     nl -> add( tmp3 );
702
703                     sgCopyVec2( tmp2, tclist[ fan_tex_coords[i] ] );
704                     tl -> add( tmp2 );
705                 }
706
707                 ssgLeaf *leaf = NULL;
708                 if ( token == "tf" ) {
709                     // triangle fan
710                     leaf = 
711                         new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
712                 } else if ( token == "ts" ) {
713                     // triangle strip
714                     leaf = 
715                         new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
716                 } else if ( token == "f" ) {
717                     // triangle
718                     leaf = 
719                         new ssgVtxTable ( GL_TRIANGLES, vl, nl, tl, cl );
720                 }
721                 // leaf->makeDList();
722                 leaf->setState( state );
723
724                 tile->addKid( leaf );
725
726                 if ( is_base ) {
727                     if ( coverage > 0.0 ) {
728                         if ( coverage < 10000.0 ) {
729                             SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
730                                    << coverage << ", pushing up to 10000");
731                             coverage = 10000;
732                         }
733                         gen_random_surface_points(leaf, lights, coverage);
734                     }
735                 }
736             } else {
737                 SG_LOG( SG_TERRAIN, SG_WARN, "Unknown token in " 
738                         << path << " = " << token );
739             }
740
741             // eat white space before start of while loop so if we are
742             // done with useful input it is noticed before hand.
743 #if defined( macintosh ) || defined( _MSC_VER )
744             in >> ::skipws;
745 #else
746             in >> skipws;
747 #endif
748         }
749     }
750
751     if ( is_base ) {
752         t->nodes = nodes;
753     }
754
755     stopwatch.stop();
756     SG_LOG( SG_TERRAIN, SG_DEBUG, 
757             "Loaded " << path << " in " 
758             << stopwatch.elapsedSeconds() << " seconds" );
759
760     return tile;
761 }
762
763
764 static ssgLeaf *gen_leaf( const string& path,
765                           const GLenum ty, const string& material,
766                           const point_list& nodes, const point_list& normals,
767                           const point_list& texcoords,
768                           const int_list node_index,
769                           const int_list& tex_index,
770                           const bool calc_lights, ssgVertexArray *lights )
771 {
772     double tex_width = 1000.0, tex_height = 1000.0;
773     ssgSimpleState *state = NULL;
774     float coverage = -1;
775
776     FGNewMat *newmat = material_lib.find( material );
777     if ( newmat == NULL ) {
778         // see if this is an on the fly texture
779         string file = path;
780         int pos = file.rfind( "/" );
781         file = file.substr( 0, pos );
782         cout << "current file = " << file << endl;
783         file += "/";
784         file += material;
785         cout << "current file = " << file << endl;
786         if ( ! material_lib.add_item( file ) ) {
787             SG_LOG( SG_TERRAIN, SG_ALERT, 
788                     "Ack! unknown usemtl name = " << material 
789                     << " in " << path );
790         } else {
791             // locate our newly created material
792             newmat = material_lib.find( material );
793             if ( newmat == NULL ) {
794                 SG_LOG( SG_TERRAIN, SG_ALERT, 
795                         "Ack! bad on the fly material create = "
796                         << material << " in " << path );
797             }
798         }
799     }
800
801     if ( newmat != NULL ) {
802         // set the texture width and height values for this
803         // material
804         tex_width = newmat->get_xsize();
805         tex_height = newmat->get_ysize();
806         state = newmat->get_state();
807         coverage = newmat->get_light_coverage();
808         // cout << "(w) = " << tex_width << " (h) = " 
809         //      << tex_width << endl;
810     } else {
811         coverage = -1;
812     }
813
814     // cout << "before list allocs" << endl;
815
816     int size = node_index.size();
817
818     if ( size < 1 ) {
819         SG_LOG( SG_TERRAIN, SG_ALERT, "Woh! list size < 1" );
820         exit(-1);
821     }
822
823     // cout << "before vl, size = " << size << endl;
824     ssgVertexArray   *vl = new ssgVertexArray( size );
825     // cout << "before nl" << endl;
826     ssgNormalArray   *nl = new ssgNormalArray( size );
827     // cout << "before tl" << endl;
828     ssgTexCoordArray *tl = new ssgTexCoordArray( size );
829     // cout << "before cl" << endl;
830     ssgColourArray   *cl = new ssgColourArray( 1 );
831
832     sgVec4 color;
833     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
834     cl->add( color );
835
836     sgVec2 tmp2;
837     sgVec3 tmp3;
838     int i;
839     for ( i = 0; i < size; ++i ) {
840         Point3D node = nodes[ node_index[i] ];
841         sgSetVec3( tmp3, node[0], node[1], node[2] );
842         vl -> add( tmp3 );
843
844         Point3D normal = normals[ node_index[i] ];
845         sgSetVec3( tmp3, normal[0], normal[1], normal[2] );
846         nl -> add( tmp3 );
847
848         Point3D texcoord = texcoords[ tex_index[i] ];
849         sgSetVec2( tmp2, texcoord[0], texcoord[1] );
850         if ( tex_width > 0 ) {
851             tmp2[0] *= (1000.0 / tex_width);
852         }
853         if ( tex_height > 0 ) {
854             tmp2[1] *= (1000.0 / tex_height);
855         }
856         tl -> add( tmp2 );
857     }
858
859     // cout << "before leaf create" << endl;
860     ssgLeaf *leaf = new ssgVtxTable ( ty, vl, nl, tl, cl );
861     // cout << "after leaf create" << endl;
862
863     // lookup the state record
864     // cout << "looking up material = " << endl;
865     // cout << material << endl;
866     // cout << "'" << endl;
867
868     leaf->setState( state );
869
870     if ( calc_lights ) {
871         if ( coverage > 0.0 ) {
872             if ( coverage < 10000.0 ) {
873                 SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
874                        << coverage << ", pushing up to 10000");
875                 coverage = 10000;
876             }
877             gen_random_surface_points(leaf, lights, coverage);
878         }
879     }
880
881     return leaf;
882 }
883
884
885 // Load an Binary obj file
886 ssgBranch *fgBinObjLoad( const string& path, FGTileEntry *t,
887                          ssgVertexArray *lights, const bool is_base)
888 {
889     int i;
890
891     SGBinObject obj;
892     bool result = obj.read_bin( path );
893
894     if ( !result ) {
895         return NULL;
896     }
897
898     // cout << "fans size = " << obj.get_fans_v().size()
899     //      << " fan_mats size = " << obj.get_fan_materials().size() << endl;
900
901     ssgBranch *object = new ssgBranch();
902     object->setName( (char *)path.c_str() );
903    
904     if ( is_base && t != NULL ) {
905         // reference point (center offset/bounding sphere)
906         t->center = obj.get_gbs_center();
907         t->bounding_radius = obj.get_gbs_radius();
908     }
909
910     point_list nodes = obj.get_wgs84_nodes();
911     point_list normals = obj.get_normals();
912     point_list texcoords = obj.get_texcoords();
913
914     string material;
915     int_list vertex_index;
916     int_list tex_index;
917
918     // generate triangles
919     string_list tri_materials = obj.get_tri_materials();
920     group_list tris_v = obj.get_tris_v();
921     group_list tris_tc = obj.get_tris_tc();
922     for ( i = 0; i < (int)tris_v.size(); ++i ) {
923         material = tri_materials[i];
924         vertex_index = tris_v[i];
925         tex_index = tris_tc[i];
926         ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLES, material,
927                                   nodes, normals, texcoords,
928                                   vertex_index, tex_index,
929                                   is_base, lights );
930
931         object->addKid( leaf );
932     }
933
934     // generate strips
935     string_list strip_materials = obj.get_strip_materials();
936     group_list strips_v = obj.get_strips_v();
937     group_list strips_tc = obj.get_strips_tc();
938     for ( i = 0; i < (int)strips_v.size(); ++i ) {
939         material = strip_materials[i];
940         vertex_index = strips_v[i];
941         tex_index = strips_tc[i];
942         ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_STRIP, material,
943                                   nodes, normals, texcoords,
944                                   vertex_index, tex_index,
945                                   is_base, lights );
946
947         object->addKid( leaf );
948     }
949
950     // generate fans
951     string_list fan_materials = obj.get_fan_materials();
952     group_list fans_v = obj.get_fans_v();
953     group_list fans_tc = obj.get_fans_tc();
954     for ( i = 0; i < (int)fans_v.size(); ++i ) {
955         material = fan_materials[i];
956         vertex_index = fans_v[i];
957         tex_index = fans_tc[i];
958         ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_FAN, material,
959                                   nodes, normals, texcoords,
960                                   vertex_index, tex_index,
961                                   is_base, lights );
962
963         object->addKid( leaf );
964     }
965
966     return object;
967 }
968
969
970 ssgBranch *gen_taxi_sign( const string path, const string content ) {
971     // for demo purposes we assume each element (letter) is 1x1 meter.
972     // Sign is placed 0.25 meters above the ground
973
974     ssgBranch *object = new ssgBranch();
975     object->setName( (char *)content.c_str() );
976
977     double offset = content.length() / 2.0;
978
979     for ( unsigned int i = 0; i < content.length(); ++i ) {
980         string material;
981
982         char item = content[i];
983         if ( item == '<' ) {
984             material = "ArrowL.rgb";
985         } else if ( item == '>' ) {
986             material = "ArrowR.rgb";
987         } else if ( item >= 'A' && item <= 'Z' ) {
988             material = "Letter";
989             material += item;
990             material += ".rgb";
991         } else if ( item >= 'a' && item <= 'z' ) {
992             int tmp = item - 'a';
993             char c = 'A' + tmp;
994             material = "Black";
995             material += c;
996             material += ".rgb";
997         } else {
998             cout << "Unknown taxi sign code = '" << item << "' !!!!" << endl;
999             return NULL;
1000         }
1001
1002         point_list nodes; nodes.clear();
1003         point_list normals; normals.clear();
1004         point_list texcoords; texcoords.clear();
1005         int_list vertex_index; vertex_index.clear();
1006         int_list tex_index; tex_index.clear();
1007
1008         nodes.push_back( Point3D( -offset + i, 0, 0.25 ) );
1009         nodes.push_back( Point3D( -offset + i + 1, 0, 0.25 ) );
1010         nodes.push_back( Point3D( -offset + i, 0, 1.25 ) );
1011         nodes.push_back( Point3D( -offset + i + 1, 0, 1.25 ) );
1012
1013         normals.push_back( Point3D( 0, -1, 0 ) );
1014         normals.push_back( Point3D( 0, -1, 0 ) );
1015         normals.push_back( Point3D( 0, -1, 0 ) );
1016         normals.push_back( Point3D( 0, -1, 0 ) );
1017
1018         texcoords.push_back( Point3D( 0, 0, 0 ) );
1019         texcoords.push_back( Point3D( 1, 0, 0 ) );
1020         texcoords.push_back( Point3D( 0, 1, 0 ) );
1021         texcoords.push_back( Point3D( 1, 1, 0 ) );
1022
1023         vertex_index.push_back( 0 );
1024         vertex_index.push_back( 1 );
1025         vertex_index.push_back( 2 );
1026         vertex_index.push_back( 3 );
1027
1028         tex_index.push_back( 0 );
1029         tex_index.push_back( 1 );
1030         tex_index.push_back( 2 );
1031         tex_index.push_back( 3 );
1032
1033         ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_STRIP, material,
1034                                   nodes, normals, texcoords,
1035                                   vertex_index, tex_index,
1036                                   false, NULL );
1037
1038         object->addKid( leaf );
1039     }
1040
1041     return object;
1042 }
1043
1044
1045 ssgBranch *gen_runway_sign( const string path, const string name ) {
1046     // for demo purposes we assume each element (letter) is 1x1 meter.
1047     // Sign is placed 0.25 meters above the ground
1048
1049     ssgBranch *object = new ssgBranch();
1050     object->setName( (char *)name.c_str() );
1051
1052     double width = name.length() / 3.0;
1053
1054     string material = name + ".rgb";
1055
1056     point_list nodes; nodes.clear();
1057     point_list normals; normals.clear();
1058     point_list texcoords; texcoords.clear();
1059     int_list vertex_index; vertex_index.clear();
1060     int_list tex_index; tex_index.clear();
1061
1062     nodes.push_back( Point3D( -width, 0, 0.25 ) );
1063     nodes.push_back( Point3D( width + 1, 0, 0.25 ) );
1064     nodes.push_back( Point3D( -width, 0, 1.25 ) );
1065     nodes.push_back( Point3D( width + 1, 0, 1.25 ) );
1066
1067     normals.push_back( Point3D( 0, -1, 0 ) );
1068     normals.push_back( Point3D( 0, -1, 0 ) );
1069     normals.push_back( Point3D( 0, -1, 0 ) );
1070     normals.push_back( Point3D( 0, -1, 0 ) );
1071
1072     texcoords.push_back( Point3D( 0, 0, 0 ) );
1073     texcoords.push_back( Point3D( 1, 0, 0 ) );
1074     texcoords.push_back( Point3D( 0, 1, 0 ) );
1075     texcoords.push_back( Point3D( 1, 1, 0 ) );
1076
1077     vertex_index.push_back( 0 );
1078     vertex_index.push_back( 1 );
1079     vertex_index.push_back( 2 );
1080     vertex_index.push_back( 3 );
1081
1082     tex_index.push_back( 0 );
1083     tex_index.push_back( 1 );
1084     tex_index.push_back( 2 );
1085     tex_index.push_back( 3 );
1086
1087     ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_STRIP, material,
1088                               nodes, normals, texcoords,
1089                               vertex_index, tex_index,
1090                               false, NULL );
1091
1092     object->addKid( leaf );
1093
1094     return object;
1095 }