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