]> git.mxchange.org Git - flightgear.git/blob - src/Objects/obj.cxx
Moved some of the low level scene graph construction code over to simgear.
[flightgear.git] / src / Objects / obj.cxx
1 // obj.cxx -- routines to handle loading scenery and building the plib
2 //            scene graph.
3 //
4 // Written by Curtis Olson, started October 1997.
5 //
6 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <config.h>
27 #endif
28
29 #ifdef SG_MATH_EXCEPTION_CLASH
30 #  include <math.h>
31 #endif
32
33 #include <stdio.h>
34 #include <string.h>
35
36 #include <simgear/compiler.h>
37 #include <simgear/sg_inlines.h>
38 #include <simgear/io/sg_binobj.hxx>
39
40 #include STL_STRING
41 #include <map>                  // STL
42 #include <vector>               // STL
43 #include <ctype.h>              // isdigit()
44
45 #include <simgear/constants.h>
46 #include <simgear/debug/logstream.hxx>
47 #include <simgear/math/point3d.hxx>
48 #include <simgear/math/polar3d.hxx>
49 #include <simgear/math/sg_geodesy.hxx>
50 #include <simgear/math/sg_random.h>
51 #include <simgear/math/vector.hxx>
52 #include <simgear/misc/sgstream.hxx>
53 #include <simgear/misc/stopwatch.hxx>
54 #include <simgear/misc/texcoord.hxx>
55 #include <simgear/scene/material/mat.hxx>
56 #include <simgear/scene/material/matlib.hxx>
57 #include <simgear/scene/tgdb/leaf.hxx>
58 #include <simgear/scene/tgdb/pt_lights.hxx>
59
60 #include <Main/globals.hxx>
61 #include <Main/fg_props.hxx>
62 #include <Time/light.hxx>
63
64
65 #include "obj.hxx"
66
67 SG_USING_STD(string);
68 SG_USING_STD(vector);
69
70
71 typedef vector < int > int_list;
72 typedef int_list::iterator int_list_iterator;
73 typedef int_list::const_iterator int_point_list_iterator;
74
75
76 // not used because plib branches don't honor call backs.
77 static int
78 runway_lights_pretrav (ssgEntity * e, int mask)
79 {
80                                 // Turn on lights only at night
81     float sun_angle = cur_light_params.sun_angle * SGD_RADIANS_TO_DEGREES;
82     return int((sun_angle > 85.0) ||
83                (fgGetDouble("/environment/visibility-m") < 5000.0));
84 }
85
86
87 // Generate an ocean tile
88 bool fgGenTile( const string& path, SGBucket b,
89                 Point3D *center, double *bounding_radius,
90                 SGMaterialLib *matlib, ssgBranch* geometry )
91 {
92     ssgSimpleState *state = NULL;
93
94     geometry->setName( (char *)path.c_str() );
95
96     double tex_width = 1000.0;
97     // double tex_height;
98
99     // find Ocean material in the properties list
100     SGMaterial *mat = matlib->find( "Ocean" );
101     if ( mat != NULL ) {
102         // set the texture width and height values for this
103         // material
104         tex_width = mat->get_xsize();
105         // tex_height = newmat->get_ysize();
106         
107         // set ssgState
108         state = mat->get_state();
109     } else {
110         SG_LOG( SG_TERRAIN, SG_ALERT, 
111                 "Ack! unknown usemtl name = " << "Ocean" 
112                 << " in " << path );
113     }
114
115     // Calculate center point
116     double clon = b.get_center_lon();
117     double clat = b.get_center_lat();
118     double height = b.get_height();
119     double width = b.get_width();
120
121     *center = sgGeodToCart( Point3D(clon*SGD_DEGREES_TO_RADIANS,
122                                     clat*SGD_DEGREES_TO_RADIANS,
123                                     0.0) );
124     // cout << "center = " << center << endl;;
125     
126     // Caculate corner vertices
127     Point3D geod[4];
128     geod[0] = Point3D( clon - width/2.0, clat - height/2.0, 0.0 );
129     geod[1] = Point3D( clon + width/2.0, clat - height/2.0, 0.0 );
130     geod[2] = Point3D( clon + width/2.0, clat + height/2.0, 0.0 );
131     geod[3] = Point3D( clon - width/2.0, clat + height/2.0, 0.0 );
132
133     Point3D rad[4];
134     int i;
135     for ( i = 0; i < 4; ++i ) {
136         rad[i] = Point3D( geod[i].x() * SGD_DEGREES_TO_RADIANS,
137                           geod[i].y() * SGD_DEGREES_TO_RADIANS,
138                           geod[i].z() );
139     }
140
141     Point3D cart[4], rel[4];
142     for ( i = 0; i < 4; ++i ) {
143         cart[i] = sgGeodToCart(rad[i]);
144         rel[i] = cart[i] - *center;
145         // cout << "corner " << i << " = " << cart[i] << endl;
146     }
147
148     // Calculate bounding radius
149     *bounding_radius = center->distance3D( cart[0] );
150     // cout << "bounding radius = " << t->bounding_radius << endl;
151
152     // Calculate normals
153     Point3D normals[4];
154     for ( i = 0; i < 4; ++i ) {
155         double length = cart[i].distance3D( Point3D(0.0) );
156         normals[i] = cart[i] / length;
157         // cout << "normal = " << normals[i] << endl;
158     }
159
160     // Calculate texture coordinates
161     point_list geod_nodes;
162     geod_nodes.clear();
163     geod_nodes.reserve(4);
164     int_list rectangle;
165     rectangle.clear();
166     rectangle.reserve(4);
167     for ( i = 0; i < 4; ++i ) {
168         geod_nodes.push_back( geod[i] );
169         rectangle.push_back( i );
170     }
171     point_list texs = calc_tex_coords( b, geod_nodes, rectangle, 
172                                        1000.0 / tex_width );
173
174     // Allocate ssg structure
175     ssgVertexArray   *vl = new ssgVertexArray( 4 );
176     ssgNormalArray   *nl = new ssgNormalArray( 4 );
177     ssgTexCoordArray *tl = new ssgTexCoordArray( 4 );
178     ssgColourArray   *cl = new ssgColourArray( 1 );
179
180     sgVec4 color;
181     sgSetVec4( color, 1.0, 1.0, 1.0, 1.0 );
182     cl->add( color );
183
184     // sgVec3 *vtlist = new sgVec3 [ 4 ];
185     // t->vec3_ptrs.push_back( vtlist );
186     // sgVec3 *vnlist = new sgVec3 [ 4 ];
187     // t->vec3_ptrs.push_back( vnlist );
188     // sgVec2 *tclist = new sgVec2 [ 4 ];
189     // t->vec2_ptrs.push_back( tclist );
190
191     sgVec2 tmp2;
192     sgVec3 tmp3;
193     for ( i = 0; i < 4; ++i ) {
194         sgSetVec3( tmp3, 
195                    rel[i].x(), rel[i].y(), rel[i].z() );
196         vl->add( tmp3 );
197
198         sgSetVec3( tmp3, 
199                    normals[i].x(), normals[i].y(), normals[i].z() );
200         nl->add( tmp3 );
201
202         sgSetVec2( tmp2, texs[i].x(), texs[i].y());
203         tl->add( tmp2 );
204     }
205     
206     ssgLeaf *leaf = 
207         new ssgVtxTable ( GL_TRIANGLE_FAN, vl, nl, tl, cl );
208
209     leaf->setState( state );
210
211     geometry->addKid( leaf );
212
213     return true;
214 }
215
216
217 static void random_pt_inside_tri( float *res,
218                                   float *n1, float *n2, float *n3 )
219 {
220     double a = sg_random();
221     double b = sg_random();
222     if ( a + b > 1.0 ) {
223         a = 1.0 - a;
224         b = 1.0 - b;
225     }
226     double c = 1 - a - b;
227
228     res[0] = n1[0]*a + n2[0]*b + n3[0]*c;
229     res[1] = n1[1]*a + n2[1]*b + n3[1]*c;
230     res[2] = n1[2]*a + n2[2]*b + n3[2]*c;
231 }
232
233
234 /**
235  * User data for populating leaves when they come in range.
236  */
237 class LeafUserData : public ssgBase
238 {
239 public:
240     bool is_filled_in;
241     ssgLeaf *leaf;
242     SGMaterial *mat;
243     ssgBranch *branch;
244     float sin_lat;
245     float cos_lat;
246     float sin_lon;
247     float cos_lon;
248
249     void setup_triangle( int i );
250 };
251
252
253 /**
254  * User data for populating triangles when they come in range.
255  */
256 class TriUserData : public ssgBase
257 {
258 public:
259   bool is_filled_in;
260   float * p1;
261   float * p2;
262   float * p3;
263     sgVec3 center;
264     double area;
265   SGMaterial::ObjectGroup * object_group;
266   ssgBranch * branch;
267     LeafUserData * leafData;
268   unsigned int seed;
269
270     void fill_in_triangle();
271     void add_object_to_triangle(SGMaterial::Object * object);
272     void makeWorldMatrix (sgMat4 ROT, double hdg_deg );
273 };
274
275
276 /**
277  * Fill in a triangle with randomly-placed objects.
278  *
279  * This method is invoked by a callback when the triangle is in range
280  * but not yet populated.
281  *
282  */
283
284 void TriUserData::fill_in_triangle ()
285 {
286                                 // generate a repeatable random seed
287     sg_srandom(seed);
288
289     int nObjects = object_group->get_object_count();
290
291     for (int i = 0; i < nObjects; i++) {
292       SGMaterial::Object * object = object_group->get_object(i);
293       double num = area / object->get_coverage_m2();
294
295       // place an object each unit of area
296       while ( num > 1.0 ) {
297           add_object_to_triangle(object);
298           num -= 1.0;
299       }
300       // for partial units of area, use a zombie door method to
301       // create the proper random chance of an object being created
302       // for this triangle
303       if ( num > 0.0 ) {
304         if ( sg_random() <= num ) {
305           // a zombie made it through our door
306                 add_object_to_triangle(object);
307         }
308       }
309     }
310 }
311
312 void TriUserData::add_object_to_triangle (SGMaterial::Object * object)
313 {
314     // Set up the random heading if required.
315     double hdg_deg = 0;
316     if (object->get_heading_type() == SGMaterial::Object::HEADING_RANDOM)
317         hdg_deg = sg_random() * 360;
318
319     sgMat4 mat;
320     makeWorldMatrix(mat, hdg_deg);
321
322     ssgTransform * pos = new ssgTransform;
323     pos->setTransform(mat);
324     pos->addKid( object->get_random_model( globals->get_model_loader(),
325                                            globals->get_fg_root(),
326                                            globals->get_props(),
327                                            globals->get_sim_time_sec() )
328                  );
329     branch->addKid(pos);
330 }
331
332 void TriUserData::makeWorldMatrix (sgMat4 mat, double hdg_deg )
333 {
334     if (hdg_deg == 0) {
335         mat[0][0] =  leafData->sin_lat * leafData->cos_lon;
336         mat[0][1] =  leafData->sin_lat * leafData->sin_lon;
337         mat[0][2] = -leafData->cos_lat;
338         mat[0][3] =  SG_ZERO;
339
340         mat[1][0] =  -leafData->sin_lon;
341         mat[1][1] =  leafData->cos_lon;
342         mat[1][2] =  SG_ZERO;
343         mat[1][3] =  SG_ZERO;
344     } else {
345         float sin_hdg = sin( hdg_deg * SGD_DEGREES_TO_RADIANS ) ;
346         float cos_hdg = cos( hdg_deg * SGD_DEGREES_TO_RADIANS ) ;
347         mat[0][0] =  cos_hdg * leafData->sin_lat * leafData->cos_lon - sin_hdg * leafData->sin_lon;
348         mat[0][1] =  cos_hdg * leafData->sin_lat * leafData->sin_lon + sin_hdg * leafData->cos_lon;
349         mat[0][2] = -cos_hdg * leafData->cos_lat;
350         mat[0][3] =  SG_ZERO;
351
352         mat[1][0] = -sin_hdg * leafData->sin_lat * leafData->cos_lon - cos_hdg * leafData->sin_lon;
353         mat[1][1] = -sin_hdg * leafData->sin_lat * leafData->sin_lon + cos_hdg * leafData->cos_lon;
354         mat[1][2] =  sin_hdg * leafData->cos_lat;
355         mat[1][3] =  SG_ZERO;
356     }
357
358     mat[2][0] = leafData->cos_lat * leafData->cos_lon;
359     mat[2][1] = leafData->cos_lat * leafData->sin_lon;
360     mat[2][2] = leafData->sin_lat;
361     mat[2][3] = SG_ZERO;
362
363     // translate to random point in triangle
364     sgVec3 result;
365     random_pt_inside_tri(result, p1, p2, p3);
366     sgSubVec3(mat[3], result, center);
367
368     mat[3][3] = SG_ONE ;
369 }
370
371 /**
372  * SSG callback for an in-range triangle of randomly-placed objects.
373  *
374  * This pretraversal callback is attached to a branch that is traversed
375  * only when a triangle is in range.  If the triangle is not currently
376  * populated with randomly-placed objects, this callback will populate
377  * it.
378  *
379  * @param entity The entity to which the callback is attached (not used).
380  * @param mask The entity's traversal mask (not used).
381  * @return Always 1, to allow traversal and culling to continue.
382  */
383 static int
384 tri_in_range_callback (ssgEntity * entity, int mask)
385 {
386   TriUserData * data = (TriUserData *)entity->getUserData();
387   if (!data->is_filled_in) {
388         data->fill_in_triangle();
389     data->is_filled_in = true;
390   }
391   return 1;
392 }
393
394
395 /**
396  * SSG callback for an out-of-range triangle of randomly-placed objects.
397  *
398  * This pretraversal callback is attached to a branch that is traversed
399  * only when a triangle is out of range.  If the triangle is currently
400  * populated with randomly-placed objects, the objects will be removed.
401  *
402  *
403  * @param entity The entity to which the callback is attached (not used).
404  * @param mask The entity's traversal mask (not used).
405  * @return Always 0, to prevent any further traversal or culling.
406  */
407 static int
408 tri_out_of_range_callback (ssgEntity * entity, int mask)
409 {
410   TriUserData * data = (TriUserData *)entity->getUserData();
411   if (data->is_filled_in) {
412     data->branch->removeAllKids();
413     data->is_filled_in = false;
414   }
415   return 0;
416 }
417
418
419 /**
420  * ssgEntity with a dummy bounding sphere, to fool culling.
421  *
422  * This forces the in-range and out-of-range branches to be visited
423  * when appropriate, even if they have no children.  It's ugly, but
424  * it works and seems fairly efficient (since branches can still
425  * be culled when they're out of the view frustum).
426  */
427 class DummyBSphereEntity : public ssgBranch
428 {
429 public:
430   DummyBSphereEntity (float radius)
431   {
432     bsphere.setCenter(0, 0, 0);
433     bsphere.setRadius(radius);
434   }
435   virtual ~DummyBSphereEntity () {}
436   virtual void recalcBSphere () { bsphere_is_invalid = false; }
437 };
438
439
440 /**
441  * Calculate the bounding radius of a triangle from its center.
442  *
443  * @param center The triangle center.
444  * @param p1 The first point in the triangle.
445  * @param p2 The second point in the triangle.
446  * @param p3 The third point in the triangle.
447  * @return The greatest distance any point lies from the center.
448  */
449 static inline float
450 get_bounding_radius( sgVec3 center, float *p1, float *p2, float *p3)
451 {
452    return sqrt( SG_MAX3( sgDistanceSquaredVec3(center, p1),
453                          sgDistanceSquaredVec3(center, p2),
454                          sgDistanceSquaredVec3(center, p3) ) );
455 }
456
457
458 /**
459  * Set up a triangle for randomly-placed objects.
460  *
461  * No objects will be added unless the triangle comes into range.
462  *
463  */
464
465 void LeafUserData::setup_triangle (int i )
466 {
467     short n1, n2, n3;
468     leaf->getTriangle(i, &n1, &n2, &n3);
469
470     float * p1 = leaf->getVertex(n1);
471     float * p2 = leaf->getVertex(n2);
472     float * p3 = leaf->getVertex(n3);
473
474                                 // Set up a single center point for LOD
475     sgVec3 center;
476     sgSetVec3(center,
477               (p1[0] + p2[0] + p3[0]) / 3.0,
478               (p1[1] + p2[1] + p3[1]) / 3.0,
479               (p1[2] + p2[2] + p3[2]) / 3.0);
480     double area = sgTriArea(p1, p2, p3);
481       
482                                 // maximum radius of an object from center.
483     double bounding_radius = get_bounding_radius(center, p1, p2, p3);
484
485                                 // Set up a transformation to the center
486                                 // point, so that everything else can
487                                 // be specified relative to it.
488     ssgTransform * location = new ssgTransform;
489     sgMat4 TRANS;
490     sgMakeTransMat4(TRANS, center);
491     location->setTransform(TRANS);
492     branch->addKid(location);
493
494                                 // Iterate through all the object types.
495     int num_groups = mat->get_object_group_count();
496     for (int j = 0; j < num_groups; j++) {
497                                 // Look up the random object.
498         SGMaterial::ObjectGroup * group = mat->get_object_group(j);
499
500                                 // Set up the range selector for the entire
501                                 // triangle; note that we use the object
502                                 // range plus the bounding radius here, to
503                                 // allow for objects far from the center.
504         float ranges[] = { 0,
505                           group->get_range_m() + bounding_radius,
506                 SG_MAX };
507         ssgRangeSelector * lod = new ssgRangeSelector;
508         lod->setRanges(ranges, 3);
509         location->addKid(lod);
510
511                                 // Create the in-range and out-of-range
512                                 // branches.
513         ssgBranch * in_range = new ssgBranch;
514         ssgBranch * out_of_range = new ssgBranch;
515
516                                 // Set up the user data for if/when
517                                 // the random objects in this triangle
518                                 // are filled in.
519         TriUserData * data = new TriUserData;
520         data->is_filled_in = false;
521         data->p1 = p1;
522         data->p2 = p2;
523         data->p3 = p3;
524         sgCopyVec3 (data->center, center);
525         data->area = area;
526         data->object_group = group;
527         data->branch = in_range;
528         data->leafData = this;
529         data->seed = (unsigned int)(p1[0] * j);
530
531                                 // Set up the in-range node.
532         in_range->setUserData(data);
533         in_range->setTravCallback(SSG_CALLBACK_PRETRAV,
534                                  tri_in_range_callback);
535         lod->addKid(in_range);
536
537                                 // Set up the out-of-range node.
538         out_of_range->setUserData(data);
539         out_of_range->setTravCallback(SSG_CALLBACK_PRETRAV,
540                                       tri_out_of_range_callback);
541         out_of_range->addKid(new DummyBSphereEntity(bounding_radius));
542         lod->addKid(out_of_range);
543     }
544 }
545
546 /**
547  * SSG callback for an in-range leaf of randomly-placed objects.
548  *
549  * This pretraversal callback is attached to a branch that is
550  * traversed only when a leaf is in range.  If the leaf is not
551  * currently prepared to be populated with randomly-placed objects,
552  * this callback will prepare it (actual population is handled by
553  * the tri_in_range_callback for individual triangles).
554  *
555  * @param entity The entity to which the callback is attached (not used).
556  * @param mask The entity's traversal mask (not used).
557  * @return Always 1, to allow traversal and culling to continue.
558  */
559 static int
560 leaf_in_range_callback (ssgEntity * entity, int mask)
561 {
562   LeafUserData * data = (LeafUserData *)entity->getUserData();
563
564   if (!data->is_filled_in) {
565                                 // Iterate through all the triangles
566                                 // and populate them.
567     int num_tris = data->leaf->getNumTriangles();
568     for ( int i = 0; i < num_tris; ++i ) {
569             data->setup_triangle(i);
570     }
571     data->is_filled_in = true;
572   }
573   return 1;
574 }
575
576
577 /**
578  * SSG callback for an out-of-range leaf of randomly-placed objects.
579  *
580  * This pretraversal callback is attached to a branch that is
581  * traversed only when a leaf is out of range.  If the leaf is
582  * currently prepared to be populated with randomly-placed objects (or
583  * is actually populated), the objects will be removed.
584  *
585  * @param entity The entity to which the callback is attached (not used).
586  * @param mask The entity's traversal mask (not used).
587  * @return Always 0, to prevent any further traversal or culling.
588  */
589 static int
590 leaf_out_of_range_callback (ssgEntity * entity, int mask)
591 {
592   LeafUserData * data = (LeafUserData *)entity->getUserData();
593   if (data->is_filled_in) {
594     data->branch->removeAllKids();
595     data->is_filled_in = false;
596   }
597   return 0;
598 }
599
600
601 /**
602  * Randomly place objects on a surface.
603  *
604  * The leaf node provides the geometry of the surface, while the
605  * material provides the objects and placement density.  Latitude
606  * and longitude are required so that the objects can be rotated
607  * to the world-up vector.  This function does not actually add
608  * any objects; instead, it attaches an ssgRangeSelector to the
609  * branch with callbacks to generate the objects when needed.
610  *
611  * @param leaf The surface where the objects should be placed.
612  * @param branch The branch that will hold the randomly-placed objects.
613  * @param center The center of the leaf in FlightGear coordinates.
614  * @param material_name The name of the surface's material.
615  */
616 static void
617 gen_random_surface_objects (ssgLeaf *leaf,
618                             ssgBranch *branch,
619                             Point3D *center,
620                             SGMaterial *mat )
621 {
622                                 // If the surface has no triangles, return
623                                 // now.
624     int num_tris = leaf->getNumTriangles();
625     if (num_tris < 1)
626         return;
627
628                                 // If the material has no randomly-placed
629                                 // objects, return now.
630     if (mat->get_object_group_count() < 1)
631         return;
632
633                                 // Calculate the geodetic centre of
634                                 // the tile, for aligning automatic
635                                 // objects.
636     double lon_deg, lat_rad, lat_deg, alt_m, sl_radius_m;
637     Point3D geoc = sgCartToPolar3d(*center);
638     lon_deg = geoc.lon() * SGD_RADIANS_TO_DEGREES;
639     sgGeocToGeod(geoc.lat(), geoc.radius(),
640                  &lat_rad, &alt_m, &sl_radius_m);
641     lat_deg = lat_rad * SGD_RADIANS_TO_DEGREES;
642
643                                 // LOD for the leaf
644                                 // max random object range: 20000m
645     float ranges[] = { 0, 20000, 1000000 };
646     ssgRangeSelector * lod = new ssgRangeSelector;
647     lod->setRanges(ranges, 3);
648     branch->addKid(lod);
649
650                                 // Create the in-range and out-of-range
651                                 // branches.
652     ssgBranch * in_range = new ssgBranch;
653     ssgBranch * out_of_range = new ssgBranch;
654     lod->addKid(in_range);
655     lod->addKid(out_of_range);
656
657     LeafUserData * data = new LeafUserData;
658     data->is_filled_in = false;
659     data->leaf = leaf;
660     data->mat = mat;
661     data->branch = in_range;
662     data->sin_lat = sin(lat_deg * SGD_DEGREES_TO_RADIANS);
663     data->cos_lat = cos(lat_deg * SGD_DEGREES_TO_RADIANS);
664     data->sin_lon = sin(lon_deg * SGD_DEGREES_TO_RADIANS);
665     data->cos_lon = cos(lon_deg * SGD_DEGREES_TO_RADIANS);
666
667     in_range->setUserData(data);
668     in_range->setTravCallback(SSG_CALLBACK_PRETRAV, leaf_in_range_callback);
669     out_of_range->setUserData(data);
670     out_of_range->setTravCallback(SSG_CALLBACK_PRETRAV,
671                                    leaf_out_of_range_callback);
672     out_of_range
673       ->addKid(new DummyBSphereEntity(leaf->getBSphere()->getRadius()));
674 }
675
676
677 \f
678 ////////////////////////////////////////////////////////////////////////
679 // Scenery loaders.
680 ////////////////////////////////////////////////////////////////////////
681
682 // Load an Binary obj file
683 bool fgBinObjLoad( const string& path, const bool is_base,
684                    Point3D *center,
685                    double *bounding_radius,
686                    SGMaterialLib *matlib,
687                    ssgBranch* geometry,
688                    ssgBranch* rwy_lights,
689                    ssgBranch* taxi_lights,
690                    ssgVertexArray *ground_lights )
691 {
692     SGBinObject obj;
693     bool use_random_objects =
694       fgGetBool("/sim/rendering/random-objects", true);
695
696     if ( ! obj.read_bin( path ) ) {
697         return false;
698     }
699
700     geometry->setName( (char *)path.c_str() );
701
702     // reference point (center offset/bounding sphere)
703     *center = obj.get_gbs_center();
704     *bounding_radius = obj.get_gbs_radius();
705
706     point_list const& nodes = obj.get_wgs84_nodes();
707     // point_list const& colors = obj.get_colors();
708     point_list const& normals = obj.get_normals();
709     point_list const& texcoords = obj.get_texcoords();
710
711     string material;
712     int_list tex_index;
713
714     group_list::size_type i;
715
716     // generate points
717     string_list const& pt_materials = obj.get_pt_materials();
718     group_list const& pts_v = obj.get_pts_v();
719     group_list const& pts_n = obj.get_pts_n();
720     for ( i = 0; i < pts_v.size(); ++i ) {
721         // cout << "pts_v.size() = " << pts_v.size() << endl;
722         if ( pt_materials[i].substr(0, 3) == "RWY" ) {
723             sgVec3 up;
724             sgSetVec3( up, center->x(), center->y(), center->z() );
725             // returns a transform -> lod -> leaf structure
726             ssgBranch *branch = gen_directional_lights( nodes, normals,
727                                                         pts_v[i], pts_n[i],
728                                                         matlib, pt_materials[i],
729                                                         up );
730             // branches don't honor callbacks as far as I know so I'm
731             // commenting this out to avoid a plib runtime warning.
732             branch->setTravCallback( SSG_CALLBACK_PRETRAV,
733                                      runway_lights_pretrav );
734             if ( pt_materials[i].substr(0, 16) == "RWY_BLUE_TAXIWAY" ) {
735                 taxi_lights->addKid( branch );
736             } else {
737                 rwy_lights->addKid( branch );
738             }
739         } else {
740             material = pt_materials[i];
741             tex_index.clear();
742             ssgLeaf *leaf = sgMakeLeaf( path, GL_POINTS, matlib, material,
743                                         nodes, normals, texcoords,
744                                         pts_v[i], pts_n[i], tex_index,
745                                         false, ground_lights );
746             geometry->addKid( leaf );
747         }
748     }
749
750     // Put all randomly-placed objects under a separate branch
751     // (actually an ssgRangeSelector) named "random-models".
752     ssgBranch * random_object_branch = 0;
753     if (use_random_objects) {
754         float ranges[] = { 0, 20000 }; // Maximum 20km range for random objects
755         ssgRangeSelector * object_lod = new ssgRangeSelector;
756         object_lod->setRanges(ranges, 2);
757         object_lod->setName("random-models");
758         geometry->addKid(object_lod);
759         random_object_branch = new ssgBranch;
760         object_lod->addKid(random_object_branch);
761     }
762
763     // generate triangles
764     string_list const& tri_materials = obj.get_tri_materials();
765     group_list const& tris_v = obj.get_tris_v();
766     group_list const& tris_n = obj.get_tris_n();
767     group_list const& tris_tc = obj.get_tris_tc();
768     for ( i = 0; i < tris_v.size(); ++i ) {
769         ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLES, matlib,
770                                     tri_materials[i],
771                                     nodes, normals, texcoords,
772                                     tris_v[i], tris_n[i], tris_tc[i],
773                                     is_base, ground_lights );
774
775         if ( use_random_objects ) {
776             SGMaterial *mat = matlib->find( tri_materials[i] );
777             if ( mat == NULL ) {
778                 SG_LOG( SG_INPUT, SG_ALERT,
779                         "Unknown material for random surface objects = "
780                         << tri_materials[i] );
781             }
782             gen_random_surface_objects( leaf, random_object_branch,
783                                         center, mat );
784         }
785         geometry->addKid( leaf );
786     }
787
788     // generate strips
789     string_list const& strip_materials = obj.get_strip_materials();
790     group_list const& strips_v = obj.get_strips_v();
791     group_list const& strips_n = obj.get_strips_n();
792     group_list const& strips_tc = obj.get_strips_tc();
793     for ( i = 0; i < strips_v.size(); ++i ) {
794         ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_STRIP,
795                                     matlib, strip_materials[i],
796                                     nodes, normals, texcoords,
797                                     strips_v[i], strips_n[i], strips_tc[i],
798                                     is_base, ground_lights );
799
800         if ( use_random_objects ) {
801             SGMaterial *mat = matlib->find( strip_materials[i] );
802             if ( mat == NULL ) {
803                 SG_LOG( SG_INPUT, SG_ALERT,
804                         "Unknown material for random surface objects = "
805                         << strip_materials[i] );
806             }
807             gen_random_surface_objects( leaf, random_object_branch,
808                                         center, mat );
809         }
810         geometry->addKid( leaf );
811     }
812
813     // generate fans
814     string_list const& fan_materials = obj.get_fan_materials();
815     group_list const& fans_v = obj.get_fans_v();
816     group_list const& fans_n = obj.get_fans_n();
817     group_list const& fans_tc = obj.get_fans_tc();
818     for ( i = 0; i < fans_v.size(); ++i ) {
819         ssgLeaf *leaf = sgMakeLeaf( path, GL_TRIANGLE_FAN,
820                                     matlib, fan_materials[i],
821                                     nodes, normals, texcoords,
822                                     fans_v[i], fans_n[i], fans_tc[i],
823                                     is_base, ground_lights );
824         if ( use_random_objects ) {
825             SGMaterial *mat = matlib->find( fan_materials[i] );
826             if ( mat == NULL ) {
827                 SG_LOG( SG_INPUT, SG_ALERT,
828                         "Unknown material for random surface objects = "
829                         << fan_materials[i] );
830             }
831             gen_random_surface_objects( leaf, random_object_branch,
832                                         center, mat );
833         }
834         geometry->addKid( leaf );
835     }
836
837     return true;
838 }