]> git.mxchange.org Git - flightgear.git/blob - src/Objects/obj.cxx
Make objects appear more smoothly and reliably.
[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 <Time/light.hxx>
56 #include <Scenery/tileentry.hxx>
57
58 #include "newmat.hxx"
59 #include "matlib.hxx"
60 #include "obj.hxx"
61
62 SG_USING_STD(string);
63 SG_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 static int
75 runway_lights_predraw (ssgEntity * e)
76 {
77                                 // Turn on lights only at night
78     float sun_angle = cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES;
79     return int(sun_angle > 90.0);
80 }
81
82
83 #define FG_TEX_CONSTANT 69.0
84
85 // Calculate texture coordinates for a given point.
86 static Point3D local_calc_tex_coords(const Point3D& node, const Point3D& ref) {
87     Point3D cp;
88     Point3D pp;
89     // double tmplon, tmplat;
90
91     // cout << "-> " << node[0] << " " << node[1] << " " << node[2] << endl;
92     // cout << "-> " << ref.x() << " " << ref.y() << " " << ref.z() << endl;
93
94     cp = Point3D( node[0] + ref.x(),
95                   node[1] + ref.y(),
96                   node[2] + ref.z() );
97
98     pp = sgCartToPolar3d(cp);
99
100     // tmplon = pp.lon() * SGD_RADIANS_TO_DEGREES;
101     // tmplat = pp.lat() * SGD_RADIANS_TO_DEGREES;
102     // cout << tmplon << " " << tmplat << endl;
103
104     pp.setx( fmod(SGD_RADIANS_TO_DEGREES * FG_TEX_CONSTANT * pp.x(), 11.0) );
105     pp.sety( fmod(SGD_RADIANS_TO_DEGREES * FG_TEX_CONSTANT * pp.y(), 11.0) );
106
107     if ( pp.x() < 0.0 ) {
108         pp.setx( pp.x() + 11.0 );
109     }
110
111     if ( pp.y() < 0.0 ) {
112         pp.sety( pp.y() + 11.0 );
113     }
114
115     // cout << pp << endl;
116
117     return(pp);
118 }
119
120
121 // Generate an ocean tile
122 bool fgGenTile( const string& path, SGBucket b,
123                       Point3D *center,
124                       double *bounding_radius,
125                       ssgBranch* geometry )
126 {
127     FGNewMat *newmat;
128
129     ssgSimpleState *state = NULL;
130
131     geometry -> setName ( (char *)path.c_str() ) ;
132
133     double tex_width = 1000.0;
134     // double tex_height;
135
136     // find Ocean material in the properties list
137     newmat = material_lib.find( "Ocean" );
138     if ( newmat != NULL ) {
139         // set the texture width and height values for this
140         // material
141         tex_width = newmat->get_xsize();
142         // tex_height = newmat->get_ysize();
143         
144         // set ssgState
145         state = newmat->get_state();
146     } else {
147         SG_LOG( SG_TERRAIN, SG_ALERT, 
148                 "Ack! unknown usemtl name = " << "Ocean" 
149                 << " in " << path );
150     }
151
152     // Calculate center point
153     double clon = b.get_center_lon();
154     double clat = b.get_center_lat();
155     double height = b.get_height();
156     double width = b.get_width();
157
158     *center = sgGeodToCart( Point3D(clon*SGD_DEGREES_TO_RADIANS,
159                                     clat*SGD_DEGREES_TO_RADIANS,
160                                     0.0) );
161     // cout << "center = " << center << endl;;
162     
163     // Caculate corner vertices
164     Point3D geod[4];
165     geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
166     geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
167     geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
168     geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
169
170     Point3D rad[4];
171     int i;
172     for ( i = 0; i < 4; ++i ) {
173         rad[i] = Point3D( geod[i].x() * SGD_DEGREES_TO_RADIANS,
174                           geod[i].y() * SGD_DEGREES_TO_RADIANS,
175                           geod[i].z() );
176     }
177
178     Point3D cart[4], rel[4];
179     for ( i = 0; i < 4; ++i ) {
180         cart[i] = sgGeodToCart(rad[i]);
181         rel[i] = cart[i] - *center;
182         // cout << "corner " << i << " = " << cart[i] << endl;
183     }
184
185     // Calculate bounding radius
186     *bounding_radius = center->distance3D( cart[0] );
187     // cout << "bounding radius = " << t->bounding_radius << endl;
188
189     // Calculate normals
190     Point3D normals[4];
191     for ( i = 0; i < 4; ++i ) {
192         double length = cart[i].distance3D( Point3D(0.0) );
193         normals[i] = cart[i] / length;
194         // cout << "normal = " << normals[i] << endl;
195     }
196
197     // Calculate texture coordinates
198     point_list geod_nodes;
199     geod_nodes.clear();
200     int_list rectangle;
201     rectangle.clear();
202     for ( i = 0; i < 4; ++i ) {
203         geod_nodes.push_back( geod[i] );
204         rectangle.push_back( i );
205     }
206     point_list texs = calc_tex_coords( b, geod_nodes, rectangle, 
207                                        1000.0 / tex_width );
208
209     // Allocate ssg structure
210     ssgVertexArray   *vl = new ssgVertexArray( 4 );
211     ssgNormalArray   *nl = new ssgNormalArray( 4 );
212     ssgTexCoordArray *tl = new ssgTexCoordArray( 4 );
213     ssgColourArray   *cl = new ssgColourArray( 1 );
214
215     sgVec4 color;
216     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
217     cl->add( color );
218
219     // sgVec3 *vtlist = new sgVec3 [ 4 ];
220     // t->vec3_ptrs.push_back( vtlist );
221     // sgVec3 *vnlist = new sgVec3 [ 4 ];
222     // t->vec3_ptrs.push_back( vnlist );
223     // sgVec2 *tclist = new sgVec2 [ 4 ];
224     // t->vec2_ptrs.push_back( tclist );
225
226     sgVec2 tmp2;
227     sgVec3 tmp3;
228     for ( i = 0; i < 4; ++i ) {
229         sgSetVec3( tmp3, 
230                    rel[i].x(), rel[i].y(), rel[i].z() );
231         vl->add( tmp3 );
232
233         sgSetVec3( tmp3, 
234                    normals[i].x(), normals[i].y(), normals[i].z() );
235         nl->add( tmp3 );
236
237         sgSetVec2( tmp2, texs[i].x(), texs[i].y());
238         tl->add( tmp2 );
239     }
240     
241     ssgLeaf *leaf = 
242         new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
243
244     leaf->setState( state );
245
246     geometry->addKid( leaf );
247
248     return true;
249 }
250
251
252 static void random_pt_inside_tri( float *res,
253                                   float *n1, float *n2, float *n3 )
254 {
255     sgVec3 p1, p2, p3;
256
257     double a = sg_random();
258     double b = sg_random();
259     if ( a + b > 1.0 ) {
260         a = 1.0 - a;
261         b = 1.0 - b;
262     }
263     double c = 1 - a - b;
264
265     sgScaleVec3( p1, n1, a );
266     sgScaleVec3( p2, n2, b );
267     sgScaleVec3( p3, n3, c );
268
269     sgAddVec3( res, p1, p2 );
270     sgAddVec3( res, p3 );
271 }
272
273
274 static void gen_random_surface_points( ssgLeaf *leaf, ssgVertexArray *lights,
275                                        double factor ) {
276     int num = leaf->getNumTriangles();
277     if ( num > 0 ) {
278         short int n1, n2, n3;
279         float *p1, *p2, *p3;
280         sgVec3 result;
281
282         // generate a repeatable random seed
283         p1 = leaf->getVertex( 0 );
284         unsigned int seed = (unsigned int)p1[0];
285         sg_srandom( seed );
286
287         for ( int i = 0; i < num; ++i ) {
288             leaf->getTriangle( i, &n1, &n2, &n3 );
289             p1 = leaf->getVertex(n1);
290             p2 = leaf->getVertex(n2);
291             p3 = leaf->getVertex(n3);
292             double area = sgTriArea( p1, p2, p3 );
293             double num = area / factor;
294
295             // generate a light point for each unit of area
296             while ( num > 1.0 ) {
297                 random_pt_inside_tri( result, p1, p2, p3 );
298                 lights->add( result );
299                 num -= 1.0;
300             }
301             // for partial units of area, use a zombie door method to
302             // create the proper random chance of a light being created
303             // for this triangle
304             if ( num > 0.0 ) {
305                 if ( sg_random() <= num ) {
306                     // a zombie made it through our door
307                     random_pt_inside_tri( result, p1, p2, p3 );
308                     lights->add( result );
309                 }
310             }
311         }
312     }
313 }
314
315
316 /**
317  * Add an object to a random location inside a triangle.
318  *
319  * @param p1 The first vertex of the triangle.
320  * @param p2 The second vertex of the triangle.
321  * @param p3 The third vertex of the triangle.
322  * @param center The center of the triangle.
323  * @param ROT The world-up rotation matrix.
324  * @param mat The material object.
325  * @param object_index The index of the dynamically-placed object in
326  *        the material.
327  * @param branch The branch where the object should be added to the
328  *        scene graph.
329  */
330 static void
331 add_object_to_triangle (sgVec3 p1, sgVec3 p2, sgVec3 p3, sgVec3 center,
332                         sgMat4 ROT, FGNewMat * mat, int object_index,
333                         ssgBranch * branch)
334 {
335     sgVec3 result;
336
337     random_pt_inside_tri(result, p1, p2, p3);
338     sgSubVec3(result, center);
339     sgMat4 OBJ_pos, OBJ;
340     sgMakeTransMat4(OBJ_pos, result);
341     sgCopyMat4(OBJ, ROT);
342     sgPostMultMat4(OBJ, OBJ_pos);
343     ssgTransform * pos = new ssgTransform;
344     pos->setTransform(OBJ);
345     pos->addKid(mat->get_object(object_index));
346     branch->addKid(pos);
347 }
348
349 class RandomObjectUserData : public ssgBase
350 {
351 public:
352   bool is_filled_in;
353   float * p1;
354   float * p2;
355   float * p3;
356   FGNewMat * mat;
357   int object_index;
358   ssgBranch * branch;
359   sgMat4 ROT;
360 };
361
362
363 /**
364  * Fill in a triangle with randomly-placed objects.
365  *
366  * This method is invoked by a callback when the triangle is in range
367  * but not yet populated.
368  *
369  * @param p1 The first vertex of the triangle.
370  * @param p2 The second vertex of the triangle.
371  * @param p3 The third vertex of the triangle.
372  * @param mat The triangle's material.
373  * @param object_index The index of the random object in the triangle.
374  * @param branch The branch where the objects should be added.
375  * @param ROT The rotation matrix to align objects with the earth's
376  *        surface.
377  */
378 static void
379 fill_in_triangle (float * p1, float * p2, float * p3, FGNewMat * mat,
380                   int object_index, ssgBranch * branch, sgMat4 ROT)
381 {
382     sgVec3 center;
383     sgSetVec3(center,
384               (p1[0] + p2[0] + p3[0]) / 3.0,
385               (p1[1] + p2[1] + p3[1]) / 3.0,
386               (p1[2] + p2[2] + p3[2]) / 3.0);
387     double area = sgTriArea(p1, p2, p3);
388     double num = area / mat->get_object_coverage(object_index);
389
390     // place an object each unit of area
391     while ( num > 1.0 ) {
392       add_object_to_triangle(p1, p2, p3, center,
393                              ROT, mat, object_index, branch);
394       num -= 1.0;
395     }
396     // for partial units of area, use a zombie door method to
397     // create the proper random chance of an object being created
398     // for this triangle
399     if ( num > 0.0 ) {
400       if ( sg_random() <= num ) {
401         // a zombie made it through our door
402         add_object_to_triangle(p1, p2, p3, center,
403                                ROT, mat, object_index, branch);
404       }
405     }
406 }
407
408
409 /**
410  * SSG callback for an in-range triangle of randomly-placed objects.
411  *
412  * This pretraversal callback is attached to a branch that is traversed
413  * only when a triangle is in range.  If the triangle is not currently
414  * populated with randomly-placed objects, this callback will populate
415  * it.
416  *
417  * @param entity The entity to which the callback is attached (not used).
418  * @param mask The entity's traversal mask (not used).
419  * @return Always 1, to allow traversal and culling to continue.
420  */
421 static int
422 in_range_callback (ssgEntity * entity, int mask)
423 {
424   RandomObjectUserData * data = (RandomObjectUserData *)entity->getUserData();
425   if (!data->is_filled_in) {
426     fill_in_triangle(data->p1, data->p2, data->p3, data->mat,
427                      data->object_index, data->branch, data->ROT);
428     data->is_filled_in = true;
429   }
430   return 1;
431 }
432
433
434 /**
435  * SSG callback for an out-of-range triangle of randomly-placed objects.
436  *
437  * This pretraversal callback is attached to a branch that is traversed
438  * only when a triangle is out of range.  If the triangle is currently
439  * populated with randomly-placed objects, the objects will be removed.
440  *
441  *
442  * @param entity The entity to which the callback is attached (not used).
443  * @param mask The entity's traversal mask (not used).
444  * @return Always 0, to prevent any further traversal or culling.
445  */
446 static int
447 out_of_range_callback (ssgEntity * entity, int mask)
448 {
449   RandomObjectUserData * data = (RandomObjectUserData *)entity->getUserData();
450   if (data->is_filled_in) {
451     data->branch->removeAllKids();
452     data->is_filled_in = false;
453   }
454   return 0;
455 }
456
457
458 /**
459  * Singleton ssgEntity with a dummy bounding sphere, to fool culling.
460  *
461  * This forces the in-range and out-of-range branches to be visited
462  * when appropriate, even if they have no children.  It's ugly, but
463  * it works and seems fairly efficient (since branches can still
464  * be culled when they're out of the view frustum).
465  */
466 class DummyBSphereEntity : public ssgEntity
467 {
468 public:
469   virtual ~DummyBSphereEntity () {}
470   virtual void recalcBSphere () { bsphere_is_invalid = false; }
471   virtual void cull (sgFrustum *f, sgMat4 m, int test_needed) {}
472   virtual void isect (sgSphere *s, sgMat4 m, int test_needed) {}
473   virtual void hot (sgVec3 s, sgMat4 m, int test_needed) {}
474   virtual void los (sgVec3 s, sgMat4 m, int test_needed) {}
475   static ssgEntity * get_entity ();
476 private:
477   DummyBSphereEntity ()
478   {
479     bsphere.setCenter(0, 0, 0);
480     bsphere.setRadius(10);
481   }
482   static DummyBSphereEntity * entity;
483 };
484
485
486 DummyBSphereEntity * DummyBSphereEntity::entity = 0;
487
488
489 /**
490  * Ensure that only one copy of the dummy entity exists.
491  *
492  * @return The singleton copy of the DummyBSphereEntity.
493  */
494 ssgEntity *
495 DummyBSphereEntity::get_entity ()
496 {
497   if (entity == 0) {
498     entity = new DummyBSphereEntity;
499     entity->ref();
500   }
501   return entity;
502 }
503
504
505 /**
506  * Calculate the bounding radius of a triangle from its center.
507  *
508  * @param center The triangle center.
509  * @param p1 The first point in the triangle.
510  * @param p2 The second point in the triangle.
511  * @param p3 The third point in the triangle.
512  * @return The greatest distance any point lies from the center.
513  */
514 static float
515 get_bounding_radius (sgVec3 center, float *p1, float *p2, float *p3)
516 {
517   float result = sgDistanceVec3(center, p1);
518   float length = sgDistanceVec3(center, p2);
519   if (length > result)
520     result = length;
521   length = sgDistanceVec3(center, p3);
522   if (length > result)
523     result = length;
524   return result;
525 }
526
527
528 /**
529  * Set up a triangle for randomly-placed objects.
530  *
531  * No objects will be added unless the triangle comes into range.
532  *
533  * @param leaf The leaf containing the data for the terrain surface.
534  * @param tri_index The index of the triangle in the leaf.
535  * @param mat The material data for the triangle.
536  * @param branch The branch to which the randomly-placed objects
537  *        should be added.
538  * @param ROT A rotation matrix to align the objects with the earth's
539  *        surface at the current lat/lon.
540  */
541 static void
542 setup_triangle (float * p1, float * p2, float * p3,
543                    FGNewMat * mat, ssgBranch * branch, sgMat4 ROT)
544 {
545                                 // Set up a single center point for LOD
546     sgVec3 center;
547     sgSetVec3(center,
548               (p1[0] + p2[0] + p3[0]) / 3.0,
549               (p1[1] + p2[1] + p3[1]) / 3.0,
550               (p1[2] + p2[2] + p3[2]) / 3.0);
551       
552                                 // maximum radius of an object from center.
553     double bounding_radius = get_bounding_radius(center, p1, p2, p3);
554
555                                 // Set up a transformation to the center
556                                 // point, so that everything else can
557                                 // be specified relative to it.
558     ssgTransform * location = new ssgTransform;
559     sgMat4 TRANS;
560     sgMakeTransMat4(TRANS, center);
561     location->setTransform(TRANS);
562     branch->addKid(location);
563
564                                 // Iterate through all the object types.
565     int num_objects = mat->get_object_count();
566     for (int i = 0; i < num_objects; i++) {
567
568                                 // Set up the range selector for the entire
569                                 // triangle; note that we use the object
570                                 // range plus the bounding radius here, to
571                                 // allow for objects far from the center.
572         float ranges[] = {0,
573                           mat->get_object_lod(i) + bounding_radius,
574                           500000};
575         ssgRangeSelector * lod = new ssgRangeSelector;
576         lod->setRanges(ranges, 3);
577         location->addKid(lod);
578
579                                 // Create the in-range and out-of-range
580                                 // branches.
581         ssgBranch * in_range = new ssgBranch;
582         ssgBranch * out_of_range = new ssgBranch;
583
584                                 // Set up the user data for if/when
585                                 // the random objects in this triangle
586                                 // are filled in.
587         RandomObjectUserData * data = new RandomObjectUserData;
588         data->is_filled_in = false;
589         data->p1 = p1;
590         data->p2 = p2;
591         data->p3 = p3;
592         data->mat = mat;
593         data->object_index = i;
594         data->branch = in_range;
595         sgCopyMat4(data->ROT, ROT);
596
597                                 // Set up the in-range node.
598         in_range->setUserData(data);
599         in_range->setTravCallback(SSG_CALLBACK_PRETRAV,
600                                  in_range_callback);
601         lod->addKid(in_range);
602
603                                 // Set up the out-of-range node.
604         out_of_range->setUserData(data);
605         out_of_range->setTravCallback(SSG_CALLBACK_PRETRAV,
606                                       out_of_range_callback);
607         out_of_range->addKid(DummyBSphereEntity::get_entity());
608         lod->addKid(out_of_range);
609     }
610 }
611
612
613 /**
614  * Create a rotation matrix to align an object for the current lat/lon.
615  *
616  * By default, objects are aligned for the north pole.  This code
617  * calculates a matrix to rotate them for the surface of the earth in
618  * the current location.
619  *
620  * TODO: there should be a single version of this method somewhere
621  * for all of SimGear.
622  *
623  * @param ROT The resulting rotation matrix.
624  * @param hdg_deg The object heading in degrees.
625  * @param lon_deg The longitude in degrees.
626  * @param lat_deg The latitude in degrees.
627  */
628 void
629 makeWorldUpRotationMatrix (sgMat4 ROT, double hdg_deg,
630                            double lon_deg, double lat_deg)
631 {
632         SGfloat sin_lat = sin( lat_deg * SGD_DEGREES_TO_RADIANS );
633         SGfloat cos_lat = cos( lat_deg * SGD_DEGREES_TO_RADIANS );
634         SGfloat sin_lon = sin( lon_deg * SGD_DEGREES_TO_RADIANS );
635         SGfloat cos_lon = cos( lon_deg * SGD_DEGREES_TO_RADIANS );
636         SGfloat sin_hdg = sin( hdg_deg * SGD_DEGREES_TO_RADIANS ) ;
637         SGfloat cos_hdg = cos( hdg_deg * SGD_DEGREES_TO_RADIANS ) ;
638
639         ROT[0][0] =  cos_hdg * sin_lat * cos_lon - sin_hdg * sin_lon;
640         ROT[0][1] =  cos_hdg * sin_lat * sin_lon + sin_hdg * cos_lon;
641         ROT[0][2] = -cos_hdg * cos_lat;
642         ROT[0][3] =  SG_ZERO;
643
644         ROT[1][0] = -sin_hdg * sin_lat * cos_lon - cos_hdg * sin_lon;
645         ROT[1][1] = -sin_hdg * sin_lat * sin_lon + cos_hdg * cos_lon;
646         ROT[1][2] =  sin_hdg * cos_lat;
647         ROT[1][3] =  SG_ZERO;
648
649         ROT[2][0] = cos_lat * cos_lon;
650         ROT[2][1] = cos_lat * sin_lon;
651         ROT[2][2] = sin_lat;
652         ROT[2][3] = SG_ZERO;
653
654         ROT[3][0] = SG_ZERO;
655         ROT[3][1] = SG_ZERO;
656         ROT[3][2] = SG_ZERO;
657         ROT[3][3] = SG_ONE ;
658 }
659
660
661 /**
662  * Randomly place objects on a surface.
663  *
664  * The leaf node provides the geometry of the surface, while the
665  * material provides the objects and placement density.  Latitude
666  * and longitude are required so that the objects can be rotated
667  * to the world-up vector.
668  *
669  * @param leaf The surface where the objects should be placed.
670  * @param branch The branch that will hold the randomly-placed objects.
671  * @param lon_deg The longitude of the surface center, in degrees.
672  * @param lat_deg The latitude of the surface center, in degrees.
673  * @param material_name The name of the surface's material.
674  */
675 static void
676 gen_random_surface_objects (ssgLeaf *leaf,
677                             ssgBranch *branch,
678                             float lon_deg,
679                             float lat_deg,
680                             const string &material_name)
681 {
682     float hdg_deg = 0.0;        // do something here later
683
684                                 // First, look up the material
685                                 // for this surface.
686     FGNewMat * mat = material_lib.find(material_name);
687     if (mat == 0) {
688       SG_LOG(SG_INPUT, SG_ALERT, "Unknown material " << material_name);
689       return;
690     }
691
692                                 // If the material has no randomly-placed
693                                 // objects, return now.
694     int num_objects = mat->get_object_count();
695     if (num_objects < 1)
696       return;
697
698                                 // If the surface has no triangles, return
699                                 // now.
700     int num_tris = leaf->getNumTriangles();
701     if (num_tris < 1)
702       return;
703
704                                 // Make a rotation matrix to align the
705                                 // object for this point on the earth's
706                                 // surface.
707     sgMat4 ROT;
708     makeWorldUpRotationMatrix(ROT, hdg_deg, lon_deg, lat_deg);
709
710                                 // generate a repeatable random seed
711     sg_srandom((unsigned int)(leaf->getVertex(0)[0]));
712
713                                 // Iterate through all the triangles
714                                 // and populate them.
715     for ( int i = 0; i < num_tris; ++i ) {
716       short n1, n2, n3;
717       leaf->getTriangle(i, &n1, &n2, &n3);
718       setup_triangle(leaf->getVertex(n1),
719                      leaf->getVertex(n2),
720                      leaf->getVertex(n3),
721                      mat, branch, ROT);
722     }
723 }
724
725
726 \f
727 ////////////////////////////////////////////////////////////////////////
728 // Scenery loaders.
729 ////////////////////////////////////////////////////////////////////////
730
731
732 // Load an Ascii obj file
733 ssgBranch *fgAsciiObjLoad( const string& path, FGTileEntry *t,
734                            ssgVertexArray *lights, const bool is_base)
735 {
736     FGNewMat *newmat = NULL;
737     string material;
738     float coverage = -1;
739     Point3D pp;
740     // sgVec3 approx_normal;
741     // double normal[3], scale = 0.0;
742     // double x, y, z, xmax, xmin, ymax, ymin, zmax, zmin;
743     // GLfloat sgenparams[] = { 1.0, 0.0, 0.0, 0.0 };
744     // GLint display_list = 0;
745     int shading;
746     bool in_faces = false;
747     int vncount, vtcount;
748     int n1 = 0, n2 = 0, n3 = 0;
749     int tex;
750     // int last1 = 0, last2 = 0;
751     bool odd = false;
752     point_list nodes;
753     Point3D node;
754     Point3D center;
755     double scenery_version = 0.0;
756     double tex_width = 1000.0, tex_height = 1000.0;
757     bool shared_done = false;
758     int_list fan_vertices;
759     int_list fan_tex_coords;
760     int i;
761     ssgSimpleState *state = NULL;
762     sgVec3 *vtlist, *vnlist;
763     sgVec2 *tclist;
764
765     ssgBranch *tile = new ssgBranch () ;
766
767     tile -> setName ( (char *)path.c_str() ) ;
768
769     // Attempt to open "path.gz" or "path"
770     sg_gzifstream in( path );
771     if ( ! in.is_open() ) {
772         SG_LOG( SG_TERRAIN, SG_DEBUG, "Cannot open file: " << path );
773         SG_LOG( SG_TERRAIN, SG_DEBUG, "default to ocean tile: " << path );
774
775         delete tile;
776
777         return NULL;
778     }
779
780     shading = fgGetBool("/sim/rendering/shading");
781
782     if ( is_base ) {
783         t->ncount = 0;
784     }
785     vncount = 0;
786     vtcount = 0;
787     if ( is_base ) {
788         t->bounding_radius = 0.0;
789     }
790     center = t->center;
791
792     // StopWatch stopwatch;
793     // stopwatch.start();
794
795     // ignore initial comments and blank lines. (priming the pump)
796     // in >> skipcomment;
797     // string line;
798
799     string token;
800     char c;
801
802 #ifdef __MWERKS__
803     while ( in.get(c) && c  != '\0' ) {
804         in.putback(c);
805 #else
806     while ( ! in.eof() ) {
807 #endif
808
809         in >> ::skipws;
810
811         if ( in.get( c ) && c == '#' ) {
812             // process a comment line
813
814             // getline( in, line );
815             // cout << "comment = " << line << endl;
816
817             in >> token;
818
819             if ( token == "Version" ) {
820                 // read scenery versions number
821                 in >> scenery_version;
822                 // cout << "scenery_version = " << scenery_version << endl;
823                 if ( scenery_version > 0.4 ) {
824                     SG_LOG( SG_TERRAIN, SG_ALERT, 
825                             "\nYou are attempting to load a tile format that\n"
826                             << "is newer than this version of flightgear can\n"
827                             << "handle.  You should upgrade your copy of\n"
828                             << "FlightGear to the newest version.  For\n"
829                             << "details, please see:\n"
830                             << "\n    http://www.flightgear.org\n" );
831                     exit(-1);
832                 }
833             } else if ( token == "gbs" ) {
834                 // reference point (center offset)
835                 if ( is_base ) {
836                     in >> t->center >> t->bounding_radius;
837                 } else {
838                     Point3D junk1;
839                     double junk2;
840                     in >> junk1 >> junk2;
841                 }
842                 center = t->center;
843                 // cout << "center = " << center 
844                 //      << " radius = " << t->bounding_radius << endl;
845             } else if ( token == "bs" ) {
846                 // reference point (center offset)
847                 // (skip past this)
848                 Point3D junk1;
849                 double junk2;
850                 in >> junk1 >> junk2;
851             } else if ( token == "usemtl" ) {
852                 // material property specification
853
854                 // if first usemtl with shared_done = false, then set
855                 // shared_done true and build the ssg shared lists
856                 if ( ! shared_done ) {
857                     // sanity check
858                     if ( (int)nodes.size() != vncount ) {
859                         SG_LOG( SG_TERRAIN, SG_ALERT, 
860                                 "Tile has mismatched nodes = " << nodes.size()
861                                 << " and normals = " << vncount << " : " 
862                                 << path );
863                         // exit(-1);
864                     }
865                     shared_done = true;
866
867                     vtlist = new sgVec3 [ nodes.size() ];
868                     t->vec3_ptrs.push_back( vtlist );
869                     vnlist = new sgVec3 [ vncount ];
870                     t->vec3_ptrs.push_back( vnlist );
871                     tclist = new sgVec2 [ vtcount ];
872                     t->vec2_ptrs.push_back( tclist );
873
874                     for ( i = 0; i < (int)nodes.size(); ++i ) {
875                         sgSetVec3( vtlist[i], 
876                                    nodes[i][0], nodes[i][1], nodes[i][2] );
877                     }
878                     for ( i = 0; i < vncount; ++i ) {
879                         sgSetVec3( vnlist[i], 
880                                    normals[i][0], 
881                                    normals[i][1],
882                                    normals[i][2] );
883                     }
884                     for ( i = 0; i < vtcount; ++i ) {
885                         sgSetVec2( tclist[i],
886                                    tex_coords[i][0],
887                                    tex_coords[i][1] );
888                     }
889                 }
890
891                 // display_list = xglGenLists(1);
892                 // xglNewList(display_list, GL_COMPILE);
893                 // printf("xglGenLists(); xglNewList();\n");
894                 in_faces = false;
895
896                 // scan the material line
897                 in >> material;
898                 
899                 // find this material in the properties list
900
901                 newmat = material_lib.find( material );
902                 if ( newmat == NULL ) {
903                     // see if this is an on the fly texture
904                     string file = path;
905                     int pos = file.rfind( "/" );
906                     file = file.substr( 0, pos );
907                     // cout << "current file = " << file << endl;
908                     file += "/";
909                     file += material;
910                     // cout << "current file = " << file << endl;
911                     if ( ! material_lib.add_item( file ) ) {
912                         SG_LOG( SG_TERRAIN, SG_ALERT, 
913                                 "Ack! unknown usemtl name = " << material 
914                                 << " in " << path );
915                     } else {
916                         // locate our newly created material
917                         newmat = material_lib.find( material );
918                         if ( newmat == NULL ) {
919                             SG_LOG( SG_TERRAIN, SG_ALERT, 
920                                     "Ack! bad on the fly materia create = "
921                                     << material << " in " << path );
922                         }
923                     }
924                 }
925
926                 if ( newmat != NULL ) {
927                     // set the texture width and height values for this
928                     // material
929                     tex_width = newmat->get_xsize();
930                     tex_height = newmat->get_ysize();
931                     state = newmat->get_state();
932                     coverage = newmat->get_light_coverage();
933                     // cout << "(w) = " << tex_width << " (h) = "
934                     //      << tex_width << endl;
935                 } else {
936                     coverage = -1;
937                 }
938             } else {
939                 // unknown comment, just gobble the input until the
940                 // end of line
941
942                 in >> skipeol;
943             }
944         } else {
945             in.putback( c );
946         
947             in >> token;
948
949             // cout << "token = " << token << endl;
950
951             if ( token == "vn" ) {
952                 // vertex normal
953                 if ( vncount < FG_MAX_NODES ) {
954                     in >> normals[vncount][0]
955                        >> normals[vncount][1]
956                        >> normals[vncount][2];
957                     vncount++;
958                 } else {
959                     SG_LOG( SG_TERRAIN, SG_ALERT, 
960                             "Read too many vertex normals in " << path 
961                             << " ... dying :-(" );
962                     exit(-1);
963                 }
964             } else if ( token == "vt" ) {
965                 // vertex texture coordinate
966                 if ( vtcount < FG_MAX_NODES*3 ) {
967                     in >> tex_coords[vtcount][0]
968                        >> tex_coords[vtcount][1];
969                     vtcount++;
970                 } else {
971                     SG_LOG( SG_TERRAIN, SG_ALERT, 
972                             "Read too many vertex texture coords in " << path
973                             << " ... dying :-("
974                             );
975                     exit(-1);
976                 }
977             } else if ( token == "v" ) {
978                 // node (vertex)
979                 if ( t->ncount < FG_MAX_NODES ) {
980                     /* in >> nodes[t->ncount][0]
981                        >> nodes[t->ncount][1]
982                        >> nodes[t->ncount][2]; */
983                     in >> node;
984                     nodes.push_back(node);
985                     if ( is_base ) {
986                         t->ncount++;
987                     }
988                 } else {
989                     SG_LOG( SG_TERRAIN, SG_ALERT, 
990                             "Read too many nodes in " << path 
991                             << " ... dying :-(");
992                     exit(-1);
993                 }
994             } else if ( (token == "tf") || (token == "ts") || (token == "f") ) {
995                 // triangle fan, strip, or individual face
996                 // SG_LOG( SG_TERRAIN, SG_INFO, "new fan or strip");
997
998                 fan_vertices.clear();
999                 fan_tex_coords.clear();
1000                 odd = true;
1001
1002                 // xglBegin(GL_TRIANGLE_FAN);
1003
1004                 in >> n1;
1005                 fan_vertices.push_back( n1 );
1006                 // xglNormal3dv(normals[n1]);
1007                 if ( in.get( c ) && c == '/' ) {
1008                     in >> tex;
1009                     fan_tex_coords.push_back( tex );
1010                     if ( scenery_version >= 0.4 ) {
1011                         if ( tex_width > 0 ) {
1012                             tclist[tex][0] *= (1000.0 / tex_width);
1013                         }
1014                         if ( tex_height > 0 ) {
1015                             tclist[tex][1] *= (1000.0 / tex_height);
1016                         }
1017                     }
1018                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
1019                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
1020                 } else {
1021                     in.putback( c );
1022                     pp = local_calc_tex_coords(nodes[n1], center);
1023                 }
1024                 // xglTexCoord2f(pp.x(), pp.y());
1025                 // xglVertex3dv(nodes[n1].get_n());
1026
1027                 in >> n2;
1028                 fan_vertices.push_back( n2 );
1029                 // xglNormal3dv(normals[n2]);
1030                 if ( in.get( c ) && c == '/' ) {
1031                     in >> tex;
1032                     fan_tex_coords.push_back( tex );
1033                     if ( scenery_version >= 0.4 ) {
1034                         if ( tex_width > 0 ) {
1035                             tclist[tex][0] *= (1000.0 / tex_width);
1036                         }
1037                         if ( tex_height > 0 ) {
1038                             tclist[tex][1] *= (1000.0 / tex_height);
1039                         }
1040                     }
1041                     pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
1042                     pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
1043                 } else {
1044                     in.putback( c );
1045                     pp = local_calc_tex_coords(nodes[n2], center);
1046                 }
1047                 // xglTexCoord2f(pp.x(), pp.y());
1048                 // xglVertex3dv(nodes[n2].get_n());
1049                 
1050                 // read all subsequent numbers until next thing isn't a number
1051                 while ( true ) {
1052                     in >> ::skipws;
1053
1054                     char c;
1055                     in.get(c);
1056                     in.putback(c);
1057                     if ( ! isdigit(c) || in.eof() ) {
1058                         break;
1059                     }
1060
1061                     in >> n3;
1062                     fan_vertices.push_back( n3 );
1063                     // cout << "  triangle = "
1064                     //      << n1 << "," << n2 << "," << n3
1065                     //      << endl;
1066                     // xglNormal3dv(normals[n3]);
1067                     if ( in.get( c ) && c == '/' ) {
1068                         in >> tex;
1069                         fan_tex_coords.push_back( tex );
1070                         if ( scenery_version >= 0.4 ) {
1071                             if ( tex_width > 0 ) {
1072                                 tclist[tex][0] *= (1000.0 / tex_width);
1073                             }
1074                             if ( tex_height > 0 ) {
1075                                 tclist[tex][1] *= (1000.0 / tex_height);
1076                             }
1077                         }
1078                         pp.setx( tex_coords[tex][0] * (1000.0 / tex_width) );
1079                         pp.sety( tex_coords[tex][1] * (1000.0 / tex_height) );
1080                     } else {
1081                         in.putback( c );
1082                         pp = local_calc_tex_coords(nodes[n3], center);
1083                     }
1084                     // xglTexCoord2f(pp.x(), pp.y());
1085                     // xglVertex3dv(nodes[n3].get_n());
1086
1087                     if ( (token == "tf") || (token == "f") ) {
1088                         // triangle fan
1089                         n2 = n3;
1090                     } else {
1091                         // triangle strip
1092                         odd = !odd;
1093                         n1 = n2;
1094                         n2 = n3;
1095                     }
1096                 }
1097
1098                 // xglEnd();
1099
1100                 // build the ssg entity
1101                 int size = (int)fan_vertices.size();
1102                 ssgVertexArray   *vl = new ssgVertexArray( size );
1103                 ssgNormalArray   *nl = new ssgNormalArray( size );
1104                 ssgTexCoordArray *tl = new ssgTexCoordArray( size );
1105                 ssgColourArray   *cl = new ssgColourArray( 1 );
1106
1107                 sgVec4 color;
1108                 sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
1109                 cl->add( color );
1110
1111                 sgVec2 tmp2;
1112                 sgVec3 tmp3;
1113                 for ( i = 0; i < size; ++i ) {
1114                     sgCopyVec3( tmp3, vtlist[ fan_vertices[i] ] );
1115                     vl -> add( tmp3 );
1116
1117                     sgCopyVec3( tmp3, vnlist[ fan_vertices[i] ] );
1118                     nl -> add( tmp3 );
1119
1120                     sgCopyVec2( tmp2, tclist[ fan_tex_coords[i] ] );
1121                     tl -> add( tmp2 );
1122                 }
1123
1124                 ssgLeaf *leaf = NULL;
1125                 if ( token == "tf" ) {
1126                     // triangle fan
1127                     leaf = 
1128                         new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
1129                 } else if ( token == "ts" ) {
1130                     // triangle strip
1131                     leaf = 
1132                         new ssgVtxTable ( GL_TRIANGLE_STRIP, vl, nl, tl, cl );
1133                 } else if ( token == "f" ) {
1134                     // triangle
1135                     leaf = 
1136                         new ssgVtxTable ( GL_TRIANGLES, vl, nl, tl, cl );
1137                 }
1138                 // leaf->makeDList();
1139                 leaf->setState( state );
1140
1141                 tile->addKid( leaf );
1142
1143                 if ( is_base ) {
1144                     if ( coverage > 0.0 ) {
1145                         if ( coverage < 10000.0 ) {
1146                             SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
1147                                    << coverage << ", pushing up to 10000");
1148                             coverage = 10000;
1149                         }
1150                         gen_random_surface_points(leaf, lights, coverage);
1151                     }
1152                 }
1153             } else {
1154                 SG_LOG( SG_TERRAIN, SG_WARN, "Unknown token in " 
1155                         << path << " = " << token );
1156             }
1157
1158             // eat white space before start of while loop so if we are
1159             // done with useful input it is noticed before hand.
1160             in >> ::skipws;
1161         }
1162     }
1163
1164     if ( is_base ) {
1165         t->nodes = nodes;
1166     }
1167
1168     // stopwatch.stop();
1169     // SG_LOG( SG_TERRAIN, SG_DEBUG, 
1170     //     "Loaded " << path << " in " 
1171     //     << stopwatch.elapsedSeconds() << " seconds" );
1172
1173     return tile;
1174 }
1175
1176
1177 ssgLeaf *gen_leaf( const string& path,
1178                    const GLenum ty, const string& material,
1179                    const point_list& nodes, const point_list& normals,
1180                    const point_list& texcoords,
1181                    const int_list node_index,
1182                    const int_list normal_index,
1183                    const int_list& tex_index,
1184                    const bool calc_lights, ssgVertexArray *lights )
1185 {
1186     double tex_width = 1000.0, tex_height = 1000.0;
1187     ssgSimpleState *state = NULL;
1188     float coverage = -1;
1189
1190     FGNewMat *newmat = material_lib.find( material );
1191     if ( newmat == NULL ) {
1192         // see if this is an on the fly texture
1193         string file = path;
1194         int pos = file.rfind( "/" );
1195         file = file.substr( 0, pos );
1196         // cout << "current file = " << file << endl;
1197         file += "/";
1198         file += material;
1199         // cout << "current file = " << file << endl;
1200         if ( ! material_lib.add_item( file ) ) {
1201             SG_LOG( SG_TERRAIN, SG_ALERT, 
1202                     "Ack! unknown usemtl name = " << material 
1203                     << " in " << path );
1204         } else {
1205             // locate our newly created material
1206             newmat = material_lib.find( material );
1207             if ( newmat == NULL ) {
1208                 SG_LOG( SG_TERRAIN, SG_ALERT, 
1209                         "Ack! bad on the fly material create = "
1210                         << material << " in " << path );
1211             }
1212         }
1213     }
1214
1215     if ( newmat != NULL ) {
1216         // set the texture width and height values for this
1217         // material
1218         tex_width = newmat->get_xsize();
1219         tex_height = newmat->get_ysize();
1220         state = newmat->get_state();
1221         coverage = newmat->get_light_coverage();
1222         // cout << "(w) = " << tex_width << " (h) = "
1223         //      << tex_width << endl;
1224     } else {
1225         coverage = -1;
1226     }
1227
1228     sgVec2 tmp2;
1229     sgVec3 tmp3;
1230     sgVec4 tmp4;
1231     int i;
1232
1233     // vertices
1234     int size = node_index.size();
1235     if ( size < 1 ) {
1236         SG_LOG( SG_TERRAIN, SG_ALERT, "Woh! node list size < 1" );
1237         exit(-1);
1238     }
1239     ssgVertexArray *vl = new ssgVertexArray( size );
1240     Point3D node;
1241     for ( i = 0; i < size; ++i ) {
1242         node = nodes[ node_index[i] ];
1243         sgSetVec3( tmp3, node[0], node[1], node[2] );
1244         vl -> add( tmp3 );
1245     }
1246
1247     // normals
1248     Point3D normal;
1249     ssgNormalArray *nl = new ssgNormalArray( size );
1250     if ( normal_index.size() ) {
1251         // object file specifies normal indices (i.e. normal indices
1252         // aren't 'implied'
1253         for ( i = 0; i < size; ++i ) {
1254             normal = normals[ normal_index[i] ];
1255             sgSetVec3( tmp3, normal[0], normal[1], normal[2] );
1256             nl -> add( tmp3 );
1257         }
1258     } else {
1259         // use implied normal indices.  normal index = vertex index.
1260         for ( i = 0; i < size; ++i ) {
1261             normal = normals[ node_index[i] ];
1262             sgSetVec3( tmp3, normal[0], normal[1], normal[2] );
1263             nl -> add( tmp3 );
1264         }
1265     }
1266
1267     // colors
1268     ssgColourArray *cl = new ssgColourArray( 1 );
1269     sgSetVec4( tmp4, 1.0, 1.0, 1.0, 1.0 );
1270     cl->add( tmp4 );
1271
1272     // texture coordinates
1273     size = tex_index.size();
1274     Point3D texcoord;
1275     ssgTexCoordArray *tl = new ssgTexCoordArray( size );
1276     if ( size == 1 ) {
1277         texcoord = texcoords[ tex_index[0] ];
1278         sgSetVec2( tmp2, texcoord[0], texcoord[1] );
1279         sgSetVec2( tmp2, texcoord[0], texcoord[1] );
1280         if ( tex_width > 0 ) {
1281             tmp2[0] *= (1000.0 / tex_width);
1282         }
1283         if ( tex_height > 0 ) {
1284             tmp2[1] *= (1000.0 / tex_height);
1285         }
1286         tl -> add( tmp2 );
1287     } else if ( size > 1 ) {
1288         for ( i = 0; i < size; ++i ) {
1289             texcoord = texcoords[ tex_index[i] ];
1290             sgSetVec2( tmp2, texcoord[0], texcoord[1] );
1291             if ( tex_width > 0 ) {
1292                 tmp2[0] *= (1000.0 / tex_width);
1293             }
1294             if ( tex_height > 0 ) {
1295                 tmp2[1] *= (1000.0 / tex_height);
1296             }
1297             tl -> add( tmp2 );
1298         }
1299     }
1300
1301     ssgLeaf *leaf = new ssgVtxTable ( ty, vl, nl, tl, cl );
1302
1303     // lookup the state record
1304
1305     leaf->setState( state );
1306
1307     if ( calc_lights ) {
1308         if ( coverage > 0.0 ) {
1309             if ( coverage < 10000.0 ) {
1310                 SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
1311                        << coverage << ", pushing up to 10000");
1312                 coverage = 10000;
1313             }
1314             gen_random_surface_points(leaf, lights, coverage);
1315         }
1316     }
1317
1318     return leaf;
1319 }
1320
1321
1322 // Load an Binary obj file
1323 bool fgBinObjLoad( const string& path, const bool is_base,
1324                    Point3D *center,
1325                    double *bounding_radius,
1326                    ssgBranch* geometry,
1327                    ssgBranch* rwy_lights,
1328                    ssgVertexArray *ground_lights )
1329 {
1330     SGBinObject obj;
1331     bool use_dynamic_objects =
1332       fgGetBool("/sim/rendering/dynamic-objects", false);
1333
1334     if ( ! obj.read_bin( path ) ) {
1335         return false;
1336     }
1337
1338     geometry->setName( (char *)path.c_str() );
1339    
1340     double geod_lon = 0.0, geod_lat = 0.0, geod_alt = 0.0,
1341       geod_sl_radius = 0.0;
1342     if ( is_base ) {
1343         // reference point (center offset/bounding sphere)
1344         *center = obj.get_gbs_center();
1345         *bounding_radius = obj.get_gbs_radius();
1346
1347                                 // Calculate the geodetic centre of
1348                                 // the tile, for aligning automatic
1349                                 // objects.
1350         Point3D geoc = sgCartToPolar3d(*center);
1351         geod_lon = geoc.lon();
1352         sgGeocToGeod(geoc.lat(), geoc.radius(),
1353                      &geod_lat, &geod_alt, &geod_sl_radius);
1354         geod_lon *= SGD_RADIANS_TO_DEGREES;
1355         geod_lat *= SGD_RADIANS_TO_DEGREES;
1356     }
1357
1358     point_list nodes = obj.get_wgs84_nodes();
1359     point_list colors = obj.get_colors();
1360     point_list normals = obj.get_normals();
1361     point_list texcoords = obj.get_texcoords();
1362
1363     string material, tmp_mat;
1364     int_list vertex_index;
1365     int_list normal_index;
1366     int_list tex_index;
1367
1368     int i;
1369     bool is_lighting = false;
1370
1371     // generate points
1372     string_list pt_materials = obj.get_pt_materials();
1373     group_list pts_v = obj.get_pts_v();
1374     group_list pts_n = obj.get_pts_n();
1375     for ( i = 0; i < (int)pts_v.size(); ++i ) {
1376         // cout << "pts_v.size() = " << pts_v.size() << endl;
1377         tmp_mat = pt_materials[i];
1378         if ( tmp_mat.substr(0, 3) == "RWY" ) {
1379             material = "LIGHTS";
1380             is_lighting = true;
1381         } else {
1382             material = tmp_mat;
1383         }
1384         vertex_index = pts_v[i];
1385         normal_index = pts_n[i];
1386         tex_index.clear();
1387         ssgLeaf *leaf = gen_leaf( path, GL_POINTS, material,
1388                                   nodes, normals, texcoords,
1389                                   vertex_index, normal_index, tex_index,
1390                                   false, ground_lights );
1391
1392         if ( is_lighting ) {
1393             float ranges[] = { 0, 12000 };
1394             leaf->setCallback(SSG_CALLBACK_PREDRAW, runway_lights_predraw);
1395             ssgRangeSelector * lod = new ssgRangeSelector;
1396             lod->setRanges(ranges, 2);
1397             lod->addKid(leaf);
1398             rwy_lights->addKid(lod);
1399         } else {
1400             geometry->addKid( leaf );
1401         }
1402     }
1403
1404     // generate triangles
1405     string_list tri_materials = obj.get_tri_materials();
1406     group_list tris_v = obj.get_tris_v();
1407     group_list tris_n = obj.get_tris_n();
1408     group_list tris_tc = obj.get_tris_tc();
1409     for ( i = 0; i < (int)tris_v.size(); ++i ) {
1410         material = tri_materials[i];
1411         vertex_index = tris_v[i];
1412         normal_index = tris_n[i];
1413         tex_index = tris_tc[i];
1414         ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLES, material,
1415                                   nodes, normals, texcoords,
1416                                   vertex_index, normal_index, tex_index,
1417                                   is_base, ground_lights );
1418
1419         if (use_dynamic_objects)
1420           gen_random_surface_objects(leaf, geometry, geod_lon, geod_lat,
1421                                      material);
1422         geometry->addKid( leaf );
1423     }
1424
1425     // generate strips
1426     string_list strip_materials = obj.get_strip_materials();
1427     group_list strips_v = obj.get_strips_v();
1428     group_list strips_n = obj.get_strips_n();
1429     group_list strips_tc = obj.get_strips_tc();
1430     for ( i = 0; i < (int)strips_v.size(); ++i ) {
1431         material = strip_materials[i];
1432         vertex_index = strips_v[i];
1433         normal_index = strips_n[i];
1434         tex_index = strips_tc[i];
1435         ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_STRIP, material,
1436                                   nodes, normals, texcoords,
1437                                   vertex_index, normal_index, tex_index,
1438                                   is_base, ground_lights );
1439
1440         if (use_dynamic_objects)
1441           gen_random_surface_objects(leaf, geometry, geod_lon, geod_lat,
1442                                      material);
1443         geometry->addKid( leaf );
1444     }
1445
1446     // generate fans
1447     string_list fan_materials = obj.get_fan_materials();
1448     group_list fans_v = obj.get_fans_v();
1449     group_list fans_n = obj.get_fans_n();
1450     group_list fans_tc = obj.get_fans_tc();
1451     for ( i = 0; i < (int)fans_v.size(); ++i ) {
1452         material = fan_materials[i];
1453         vertex_index = fans_v[i];
1454         normal_index = fans_n[i];
1455         tex_index = fans_tc[i];
1456         ssgLeaf *leaf = gen_leaf( path, GL_TRIANGLE_FAN, material,
1457                                   nodes, normals, texcoords,
1458                                   vertex_index, normal_index, tex_index,
1459                                   is_base, ground_lights );
1460         if (use_dynamic_objects)
1461           gen_random_surface_objects(leaf, geometry, geod_lon, geod_lat,
1462                                      material);
1463         geometry->addKid( leaf );
1464     }
1465
1466     return true;
1467 }