]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/obj.cxx
hla: Provide a directly property based api for property data element.
[simgear.git] / simgear / scene / tgdb / 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  - http://www.flightgear.org/~curt
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 //
22 // $Id$
23
24
25 #ifdef HAVE_CONFIG_H
26 #  include <simgear_config.h>
27 #endif
28
29 #include "obj.hxx"
30
31 #include <simgear/compiler.h>
32
33 #include <osg/Fog>
34 #include <osg/Geode>
35 #include <osg/Geometry>
36 #include <osg/Group>
37 #include <osg/LOD>
38 #include <osg/MatrixTransform>
39 #include <osg/Point>
40 #include <osg/StateSet>
41 #include <osg/Switch>
42
43 #include <boost/foreach.hpp>
44
45 #include <simgear/debug/logstream.hxx>
46 #include <simgear/io/sg_binobj.hxx>
47 #include <simgear/math/sg_geodesy.hxx>
48 #include <simgear/math/sg_random.h>
49 #include <simgear/scene/material/Effect.hxx>
50 #include <simgear/scene/material/EffectGeode.hxx>
51 #include <simgear/scene/material/mat.hxx>
52 #include <simgear/scene/material/matlib.hxx>
53 #include <simgear/scene/model/SGOffsetTransform.hxx>
54 #include <simgear/scene/util/SGUpdateVisitor.hxx>
55 #include <simgear/scene/util/SGNodeMasks.hxx>
56 #include <simgear/scene/util/QuadTreeBuilder.hxx>
57
58 #include "SGTexturedTriangleBin.hxx"
59 #include "SGLightBin.hxx"
60 #include "SGModelBin.hxx"
61 #include "TreeBin.hxx"
62 #include "SGDirectionalLightBin.hxx"
63 #include "GroundLightManager.hxx"
64
65
66 #include "userdata.hxx"
67 #include "pt_lights.hxx"
68
69 using namespace simgear;
70
71 typedef std::map<std::string,SGTexturedTriangleBin> SGMaterialTriangleMap;
72 typedef std::list<SGLightBin> SGLightListBin;
73 typedef std::list<SGDirectionalLightBin> SGDirectionalLightListBin;
74
75 struct SGTileGeometryBin {
76   SGMaterialTriangleMap materialTriangleMap;
77   SGLightBin tileLights;
78   SGLightBin randomTileLights;
79   SGTreeBinList randomForest;
80   SGDirectionalLightBin runwayLights;
81   SGDirectionalLightBin taxiLights;
82   SGDirectionalLightListBin vasiLights;
83   SGDirectionalLightListBin rabitLights;
84   SGLightListBin odalLights;
85   SGDirectionalLightListBin reilLights;
86   SGMatModelBin randomModels;
87
88   static SGVec4f
89   getMaterialLightColor(const SGMaterial* material)
90   {
91     if (!material)
92       return SGVec4f(1, 1, 1, 0.8);
93     return material->get_light_color();
94   }
95
96   static void
97   addPointGeometry(SGLightBin& lights,
98                    const std::vector<SGVec3d>& vertices,
99                    const SGVec4f& color,
100                    const int_list& pts_v)
101   {
102     for (unsigned i = 0; i < pts_v.size(); ++i)
103       lights.insert(toVec3f(vertices[pts_v[i]]), color);
104   }
105
106   static void
107   addPointGeometry(SGDirectionalLightBin& lights,
108                    const std::vector<SGVec3d>& vertices,
109                    const std::vector<SGVec3f>& normals,
110                    const SGVec4f& color,
111                    const int_list& pts_v,
112                    const int_list& pts_n)
113   {
114     // If the normal indices match the vertex indices, use seperate
115     // normal indices. Else reuse the vertex indices for the normals.
116     if (pts_v.size() == pts_n.size()) {
117       for (unsigned i = 0; i < pts_v.size(); ++i)
118         lights.insert(toVec3f(vertices[pts_v[i]]), normals[pts_n[i]], color);
119     } else {
120       for (unsigned i = 0; i < pts_v.size(); ++i)
121         lights.insert(toVec3f(vertices[pts_v[i]]), normals[pts_v[i]], color);
122     }
123   }
124
125   bool
126   insertPtGeometry(const SGBinObject& obj, SGMaterialLib* matlib)
127   {
128     if (obj.get_pts_v().size() != obj.get_pts_n().size()) {
129       SG_LOG(SG_TERRAIN, SG_ALERT,
130              "Group list sizes for points do not match!");
131       return false;
132     }
133
134     for (unsigned grp = 0; grp < obj.get_pts_v().size(); ++grp) {
135       std::string materialName = obj.get_pt_materials()[grp];
136       SGMaterial* material = 0;
137       if (matlib)
138           material = matlib->find(materialName);
139       SGVec4f color = getMaterialLightColor(material);
140
141       if (3 <= materialName.size() && materialName.substr(0, 3) != "RWY") {
142         // Just plain lights. Not something for the runway.
143         addPointGeometry(tileLights, obj.get_wgs84_nodes(), color,
144                          obj.get_pts_v()[grp]);
145       } else if (materialName == "RWY_BLUE_TAXIWAY_LIGHTS"
146                  || materialName == "RWY_GREEN_TAXIWAY_LIGHTS") {
147         addPointGeometry(taxiLights, obj.get_wgs84_nodes(), obj.get_normals(),
148                          color, obj.get_pts_v()[grp], obj.get_pts_n()[grp]);
149       } else if (materialName == "RWY_VASI_LIGHTS") {
150         vasiLights.push_back(SGDirectionalLightBin());
151         addPointGeometry(vasiLights.back(), obj.get_wgs84_nodes(),
152                          obj.get_normals(), color, obj.get_pts_v()[grp],
153                          obj.get_pts_n()[grp]);
154       } else if (materialName == "RWY_SEQUENCED_LIGHTS") {
155         rabitLights.push_back(SGDirectionalLightBin());
156         addPointGeometry(rabitLights.back(), obj.get_wgs84_nodes(),
157                          obj.get_normals(), color, obj.get_pts_v()[grp],
158                          obj.get_pts_n()[grp]);
159       } else if (materialName == "RWY_ODALS_LIGHTS") {
160         odalLights.push_back(SGLightBin());
161         addPointGeometry(odalLights.back(), obj.get_wgs84_nodes(),
162                          color, obj.get_pts_v()[grp]);
163       } else if (materialName == "RWY_REIL_LIGHTS") {
164         reilLights.push_back(SGDirectionalLightBin());
165         addPointGeometry(reilLights.back(), obj.get_wgs84_nodes(),
166                          obj.get_normals(), color, obj.get_pts_v()[grp],
167                          obj.get_pts_n()[grp]);
168       } else {
169         // what is left must be runway lights
170         addPointGeometry(runwayLights, obj.get_wgs84_nodes(),
171                          obj.get_normals(), color, obj.get_pts_v()[grp],
172                          obj.get_pts_n()[grp]);
173       }
174     }
175
176     return true;
177   }
178
179
180   static SGVec2f
181   getTexCoord(const std::vector<SGVec2f>& texCoords, const int_list& tc,
182               const SGVec2f& tcScale, unsigned i)
183   {
184     if (tc.empty())
185       return tcScale;
186     else if (tc.size() == 1)
187       return mult(texCoords[tc[0]], tcScale);
188     else
189       return mult(texCoords[tc[i]], tcScale);
190   }
191
192   static void
193   addTriangleGeometry(SGTexturedTriangleBin& triangles,
194                       const std::vector<SGVec3d>& vertices,
195                       const std::vector<SGVec3f>& normals,
196                       const std::vector<SGVec2f>& texCoords,
197                       const int_list& tris_v,
198                       const int_list& tris_n,
199                       const int_list& tris_tc,
200                       const SGVec2f& tcScale)
201   {
202     if (tris_v.size() != tris_n.size()) {
203       // If the normal indices do not match, they should be inmplicitly
204       // the same than the vertex indices. So just call ourselves again
205       // with the matching index vector.
206       addTriangleGeometry(triangles, vertices, normals, texCoords,
207                           tris_v, tris_v, tris_tc, tcScale);
208       return;
209     }
210
211     for (unsigned i = 2; i < tris_v.size(); i += 3) {
212       SGVertNormTex v0;
213       v0.vertex = toVec3f(vertices[tris_v[i-2]]);
214       v0.normal = normals[tris_n[i-2]];
215       v0.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i-2);
216       SGVertNormTex v1;
217       v1.vertex = toVec3f(vertices[tris_v[i-1]]);
218       v1.normal = normals[tris_n[i-1]];
219       v1.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i-1);
220       SGVertNormTex v2;
221       v2.vertex = toVec3f(vertices[tris_v[i]]);
222       v2.normal = normals[tris_n[i]];
223       v2.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i);
224       triangles.insert(v0, v1, v2);
225     }
226   }
227
228   static void
229   addStripGeometry(SGTexturedTriangleBin& triangles,
230                    const std::vector<SGVec3d>& vertices,
231                    const std::vector<SGVec3f>& normals,
232                    const std::vector<SGVec2f>& texCoords,
233                    const int_list& strips_v,
234                    const int_list& strips_n,
235                    const int_list& strips_tc,
236                    const SGVec2f& tcScale)
237   {
238     if (strips_v.size() != strips_n.size()) {
239       // If the normal indices do not match, they should be inmplicitly
240       // the same than the vertex indices. So just call ourselves again
241       // with the matching index vector.
242       addStripGeometry(triangles, vertices, normals, texCoords,
243                        strips_v, strips_v, strips_tc, tcScale);
244       return;
245     }
246
247     for (unsigned i = 2; i < strips_v.size(); ++i) {
248       SGVertNormTex v0;
249       v0.vertex = toVec3f(vertices[strips_v[i-2]]);
250       v0.normal = normals[strips_n[i-2]];
251       v0.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i-2);
252       SGVertNormTex v1;
253       v1.vertex = toVec3f(vertices[strips_v[i-1]]);
254       v1.normal = normals[strips_n[i-1]];
255       v1.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i-1);
256       SGVertNormTex v2;
257       v2.vertex = toVec3f(vertices[strips_v[i]]);
258       v2.normal = normals[strips_n[i]];
259       v2.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i);
260       if (i%2)
261         triangles.insert(v1, v0, v2);
262       else
263         triangles.insert(v0, v1, v2);
264     }
265   }
266   
267   static void
268   addFanGeometry(SGTexturedTriangleBin& triangles,
269                  const std::vector<SGVec3d>& vertices,
270                  const std::vector<SGVec3f>& normals,
271                  const std::vector<SGVec2f>& texCoords,
272                  const int_list& fans_v,
273                  const int_list& fans_n,
274                  const int_list& fans_tc,
275                  const SGVec2f& tcScale)
276   {
277     if (fans_v.size() != fans_n.size()) {
278       // If the normal indices do not match, they should be implicitly
279       // the same than the vertex indices. So just call ourselves again
280       // with the matching index vector.
281       addFanGeometry(triangles, vertices, normals, texCoords,
282                      fans_v, fans_v, fans_tc, tcScale);
283       return;
284     }
285
286     SGVertNormTex v0;
287     v0.vertex = toVec3f(vertices[fans_v[0]]);
288     v0.normal = normals[fans_n[0]];
289     v0.texCoord = getTexCoord(texCoords, fans_tc, tcScale, 0);
290     SGVertNormTex v1;
291     v1.vertex = toVec3f(vertices[fans_v[1]]);
292     v1.normal = normals[fans_n[1]];
293     v1.texCoord = getTexCoord(texCoords, fans_tc, tcScale, 1);
294     for (unsigned i = 2; i < fans_v.size(); ++i) {
295       SGVertNormTex v2;
296       v2.vertex = toVec3f(vertices[fans_v[i]]);
297       v2.normal = normals[fans_n[i]];
298       v2.texCoord = getTexCoord(texCoords, fans_tc, tcScale, i);
299       triangles.insert(v0, v1, v2);
300       v1 = v2;
301     }
302   }
303
304   SGVec2f getTexCoordScale(const std::string& name, SGMaterialLib* matlib)
305   {
306     if (!matlib)
307       return SGVec2f(1, 1);
308     SGMaterial* material = matlib->find(name);
309     if (!material)
310       return SGVec2f(1, 1);
311
312     return material->get_tex_coord_scale();
313   }
314
315   bool
316   insertSurfaceGeometry(const SGBinObject& obj, SGMaterialLib* matlib)
317   {
318     if (obj.get_tris_n().size() < obj.get_tris_v().size() ||
319         obj.get_tris_tc().size() < obj.get_tris_v().size()) {
320       SG_LOG(SG_TERRAIN, SG_ALERT,
321              "Group list sizes for triangles do not match!");
322       return false;
323     }
324
325     for (unsigned grp = 0; grp < obj.get_tris_v().size(); ++grp) {
326       std::string materialName = obj.get_tri_materials()[grp];
327       SGVec2f tcScale = getTexCoordScale(materialName, matlib);
328       addTriangleGeometry(materialTriangleMap[materialName],
329                           obj.get_wgs84_nodes(), obj.get_normals(),
330                           obj.get_texcoords(), obj.get_tris_v()[grp],
331                           obj.get_tris_n()[grp], obj.get_tris_tc()[grp],
332                           tcScale);
333     }
334
335     if (obj.get_strips_n().size() < obj.get_strips_v().size() ||
336         obj.get_strips_tc().size() < obj.get_strips_v().size()) {
337       SG_LOG(SG_TERRAIN, SG_ALERT,
338              "Group list sizes for strips do not match!");
339       return false;
340     }
341     for (unsigned grp = 0; grp < obj.get_strips_v().size(); ++grp) {
342       std::string materialName = obj.get_strip_materials()[grp];
343       SGVec2f tcScale = getTexCoordScale(materialName, matlib);
344       addStripGeometry(materialTriangleMap[materialName],
345                        obj.get_wgs84_nodes(), obj.get_normals(),
346                        obj.get_texcoords(), obj.get_strips_v()[grp],
347                        obj.get_strips_n()[grp], obj.get_strips_tc()[grp],
348                        tcScale);
349     }
350
351     if (obj.get_fans_n().size() < obj.get_fans_v().size() ||
352         obj.get_fans_tc().size() < obj.get_fans_v().size()) {
353       SG_LOG(SG_TERRAIN, SG_ALERT,
354              "Group list sizes for fans do not match!");
355       return false;
356     }
357     for (unsigned grp = 0; grp < obj.get_fans_v().size(); ++grp) {
358       std::string materialName = obj.get_fan_materials()[grp];
359       SGVec2f tcScale = getTexCoordScale(materialName, matlib);
360       addFanGeometry(materialTriangleMap[materialName],
361                      obj.get_wgs84_nodes(), obj.get_normals(),
362                      obj.get_texcoords(), obj.get_fans_v()[grp],
363                      obj.get_fans_n()[grp], obj.get_fans_tc()[grp],
364                      tcScale);
365     }
366     return true;
367   }
368
369   osg::Node* getSurfaceGeometry(SGMaterialLib* matlib) const
370   {
371     if (materialTriangleMap.empty())
372       return 0;
373
374     EffectGeode* eg = 0;
375     osg::Group* group = (materialTriangleMap.size() > 1 ? new osg::Group : 0);
376     //osg::Geode* geode = new osg::Geode;
377     SGMaterialTriangleMap::const_iterator i;
378     for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
379       osg::Geometry* geometry = i->second.buildGeometry();
380       SGMaterial *mat = 0;
381       if (matlib)
382         mat = matlib->find(i->first);
383       eg = new EffectGeode;
384       if (mat)
385         eg->setEffect(mat->get_effect());
386       eg->addDrawable(geometry);
387       eg->runGenerators(geometry);  // Generate extra data needed by effect
388       if (group)
389         group->addChild(eg);
390     }
391     if (group)
392         return group;
393     else
394         return eg;
395   }
396
397   void computeRandomSurfaceLights(SGMaterialLib* matlib)
398   {
399     SGMaterialTriangleMap::iterator i;
400         
401     // generate a repeatable random seed
402     mt seed;
403     mt_init(&seed, unsigned(123));
404     
405     for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
406       SGMaterial *mat = matlib->find(i->first);
407       if (!mat)
408         continue;
409
410       float coverage = mat->get_light_coverage();
411       if (coverage <= 0)
412         continue;
413       if (coverage < 10000.0) {
414         SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
415                << coverage << ", pushing up to 10000");
416         coverage = 10000;
417       }
418       
419       std::vector<SGVec3f> randomPoints;
420       i->second.addRandomSurfacePoints(coverage, 3, randomPoints);
421       std::vector<SGVec3f>::iterator j;
422       for (j = randomPoints.begin(); j != randomPoints.end(); ++j) {
423         float zombie = mt_rand(&seed);
424         // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
425         float factor = mt_rand(&seed);
426         factor *= factor;
427
428         float bright = 1;
429         SGVec4f color;
430         if ( zombie > 0.5 ) {
431           // 50% chance of yellowish
432           color = SGVec4f(0.9f, 0.9f, 0.3f, bright - factor * 0.2f);
433         } else if (zombie > 0.15f) {
434           // 35% chance of whitish
435           color = SGVec4f(0.9, 0.9f, 0.8f, bright - factor * 0.2f);
436         } else if (zombie > 0.05f) {
437           // 10% chance of orangish
438           color = SGVec4f(0.9f, 0.6f, 0.2f, bright - factor * 0.2f);
439         } else {
440           // 5% chance of redish
441           color = SGVec4f(0.9f, 0.2f, 0.2f, bright - factor * 0.2f);
442         }
443         randomTileLights.insert(*j, color);
444       }
445     }
446   }
447
448   void computeRandomForest(SGMaterialLib* matlib)
449   {
450     SGMaterialTriangleMap::iterator i;
451
452     // generate a repeatable random seed
453     mt seed;
454     mt_init(&seed, unsigned(586));
455
456     for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
457       SGMaterial *mat = matlib->find(i->first);
458       if (!mat)
459         continue;
460
461       float wood_coverage = mat->get_wood_coverage();
462       if (wood_coverage <= 0)
463         continue;
464               
465       // Attributes that don't vary by tree but do vary by material
466       bool found = false;
467       TreeBin* bin = NULL;
468       
469       BOOST_FOREACH(bin, randomForest)
470       {
471         if ((bin->texture           == mat->get_tree_texture()  ) &&
472             (bin->texture_varieties == mat->get_tree_varieties()) &&
473             (bin->range             == mat->get_tree_range()    ) &&
474             (bin->width             == mat->get_tree_width()    ) &&
475             (bin->height            == mat->get_tree_height()   )   ) {
476             found = true;
477             break;
478         }
479       }
480       
481       if (!found) {
482         bin = new TreeBin();
483         bin->texture = mat->get_tree_texture();
484           SG_LOG(SG_INPUT, SG_DEBUG, "Tree texture " << bin->texture);
485         bin->range   = mat->get_tree_range();
486         bin->width   = mat->get_tree_width();
487         bin->height  = mat->get_tree_height();
488         bin->texture_varieties = mat->get_tree_varieties();
489         randomForest.push_back(bin);
490       }
491
492       std::vector<SGVec3f> randomPoints;
493       i->second.addRandomTreePoints(wood_coverage,
494                                     mat->get_tree_density(),
495                                     mat->get_wood_size(),
496                                     randomPoints);
497       
498       std::vector<SGVec3f>::iterator k;
499       for (k = randomPoints.begin(); k != randomPoints.end(); ++k) {
500         bin->insert(*k);
501       }
502     }
503   }
504
505   void computeRandomObjects(SGMaterialLib* matlib)
506   {
507     SGMaterialTriangleMap::iterator i;
508
509     // generate a repeatable random seed
510     mt seed;
511     mt_init(&seed, unsigned(123));
512
513     for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
514       SGMaterial *mat = matlib->find(i->first);
515       if (!mat)
516         continue;
517
518       int group_count = mat->get_object_group_count();
519
520       if (group_count > 0)
521       {
522         for (int j = 0; j < group_count; j++)
523         {
524           SGMatModelGroup *object_group =  mat->get_object_group(j);
525           int nObjects = object_group->get_object_count();
526
527           if (nObjects > 0)
528           {
529             // For each of the random models in the group, determine an appropriate
530             // number of random placements and insert them.
531             for (int k = 0; k < nObjects; k++) {
532               SGMatModel * object = object_group->get_object(k);
533
534               std::vector<SGVec3f> randomPoints;
535
536               i->second.addRandomPoints(object->get_coverage_m2(), randomPoints);
537               std::vector<SGVec3f>::iterator l;
538               for (l = randomPoints.begin(); l != randomPoints.end(); ++l) {
539                 randomModels.insert(*l, object, (int)object->get_randomized_range_m(&seed));
540               }
541             }
542           }
543         }
544       }
545     }
546   }
547
548   bool insertBinObj(const SGBinObject& obj, SGMaterialLib* matlib)
549   {
550     if (!insertPtGeometry(obj, matlib))
551       return false;
552     if (!insertSurfaceGeometry(obj, matlib))
553       return false;
554     return true;
555   }
556 };
557
558 typedef std::pair<osg::Node*, int> ModelLOD;
559 struct MakeQuadLeaf {
560     osg::LOD* operator() () const { return new osg::LOD; }
561 };
562 struct AddModelLOD {
563     void operator() (osg::LOD* leaf, ModelLOD& mlod) const
564     {
565         leaf->addChild(mlod.first, 0, mlod.second);
566     }
567 };
568 struct GetModelLODCoord {
569     GetModelLODCoord() {}
570     GetModelLODCoord(const GetModelLODCoord& rhs)
571     {}
572     osg::Vec3 operator() (const ModelLOD& mlod) const
573     {
574         return mlod.first->getBound().center();
575     }
576 };
577
578 typedef QuadTreeBuilder<osg::LOD*, ModelLOD, MakeQuadLeaf, AddModelLOD,
579                         GetModelLODCoord>  RandomObjectsQuadtree;
580
581 osg::Node*
582 SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool use_random_objects, bool use_random_vegetation)
583 {
584   SGBinObject tile;
585   if (!tile.read_bin(path))
586     return false;
587
588   SGVec3d center = tile.get_gbs_center();
589   SGGeod geodPos = SGGeod::fromCart(center);
590   SGQuatd hlOr = SGQuatd::fromLonLat(geodPos)*SGQuatd::fromEulerDeg(0, 0, 180);
591
592   // rotate the tiles so that the bounding boxes get nearly axis aligned.
593   // this will help the collision tree's bounding boxes a bit ...
594   std::vector<SGVec3d> nodes = tile.get_wgs84_nodes();
595   for (unsigned i = 0; i < nodes.size(); ++i)
596     nodes[i] = hlOr.transform(nodes[i]);
597   tile.set_wgs84_nodes(nodes);
598
599   SGQuatf hlOrf(hlOr[0], hlOr[1], hlOr[2], hlOr[3]);
600   std::vector<SGVec3f> normals = tile.get_normals();
601   for (unsigned i = 0; i < normals.size(); ++i)
602     normals[i] = hlOrf.transform(normals[i]);
603   tile.set_normals(normals);
604
605   SGTileGeometryBin tileGeometryBin;
606   if (!tileGeometryBin.insertBinObj(tile, matlib))
607     return false;
608
609   SGVec3f up(0, 0, 1);
610   GroundLightManager* lightManager = GroundLightManager::instance();
611
612   osg::ref_ptr<osg::Group> lightGroup = new SGOffsetTransform(0.94);
613   osg::ref_ptr<osg::Group> randomObjects;
614   osg::ref_ptr<osg::Group> forestNode;
615   osg::Group* terrainGroup = new osg::Group;
616
617   osg::Node* node = tileGeometryBin.getSurfaceGeometry(matlib);
618   if (node)
619     terrainGroup->addChild(node);
620
621   if (use_random_objects || use_random_vegetation) {
622     if (use_random_objects) {
623       if (matlib)
624         tileGeometryBin.computeRandomObjects(matlib);
625     
626       if (tileGeometryBin.randomModels.getNumModels() > 0) {
627         // Generate a repeatable random seed
628         mt seed;
629         mt_init(&seed, unsigned(123));
630
631         std::vector<ModelLOD> models;
632         for (unsigned int i = 0;
633              i < tileGeometryBin.randomModels.getNumModels(); i++) {
634           SGMatModelBin::MatModel obj
635             = tileGeometryBin.randomModels.getMatModel(i);
636           osg::Node* node = sgGetRandomModel(obj.model, seed);
637         
638           // Create a matrix to place the object in the correct
639           // location, and then apply the rotation matrix created
640           // above, with an additional random heading rotation if appropriate.
641           osg::Matrix transformMat;
642           transformMat = osg::Matrix::translate(toOsg(obj.position));
643           if (obj.model->get_heading_type() == SGMatModel::HEADING_RANDOM) {
644             // Rotate the object around the z axis.
645             double hdg = mt_rand(&seed) * M_PI * 2;
646             transformMat.preMult(osg::Matrix::rotate(hdg,
647                                                      osg::Vec3d(0.0, 0.0, 1.0)));
648           }
649           osg::MatrixTransform* position =
650             new osg::MatrixTransform(transformMat);
651           position->addChild(node);
652           models.push_back(ModelLOD(position, obj.lod));
653         }
654         RandomObjectsQuadtree quadtree((GetModelLODCoord()), (AddModelLOD()));
655         quadtree.buildQuadTree(models.begin(), models.end());
656         randomObjects = quadtree.getRoot();
657         randomObjects->setName("random objects");
658       }
659     }
660
661     if (use_random_vegetation && matlib) {
662       // Now add some random forest.
663       tileGeometryBin.computeRandomForest(matlib);
664       
665       if (tileGeometryBin.randomForest.size() > 0) {
666         forestNode = createForest(tileGeometryBin.randomForest, osg::Matrix::identity());
667         forestNode->setName("Random trees");
668       }
669     } 
670   }
671
672   if (calc_lights) {
673     // FIXME: ugly, has a side effect
674     if (matlib)
675       tileGeometryBin.computeRandomSurfaceLights(matlib);
676
677     if (tileGeometryBin.tileLights.getNumLights() > 0
678         || tileGeometryBin.randomTileLights.getNumLights() > 0) {
679       osg::Group* groundLights0 = new osg::Group;
680       groundLights0->setStateSet(lightManager->getGroundLightStateSet());
681       groundLights0->setNodeMask(GROUNDLIGHTS0_BIT);
682       osg::Geode* geode = new osg::Geode;
683       geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.tileLights));
684       geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 4, -0.3f));
685       groundLights0->addChild(geode);
686       lightGroup->addChild(groundLights0);
687     }
688     if (tileGeometryBin.randomTileLights.getNumLights() > 0) {
689       osg::Group* groundLights1 = new osg::Group;
690       groundLights1->setStateSet(lightManager->getGroundLightStateSet());
691       groundLights1->setNodeMask(GROUNDLIGHTS1_BIT);
692       osg::Group* groundLights2 = new osg::Group;
693       groundLights2->setStateSet(lightManager->getGroundLightStateSet());
694       groundLights2->setNodeMask(GROUNDLIGHTS2_BIT);
695       osg::Geode* geode = new osg::Geode;
696       geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 2, -0.15f));
697       groundLights1->addChild(geode);
698       lightGroup->addChild(groundLights1);
699       geode = new osg::Geode;
700       geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights));
701       groundLights2->addChild(geode);
702       lightGroup->addChild(groundLights2);
703     }
704   }
705
706   if (!tileGeometryBin.vasiLights.empty()) {
707     EffectGeode* vasiGeode = new EffectGeode;
708     Effect* vasiEffect
709         = getLightEffect(6, osg::Vec3(1, 0.0001, 0.000001), 1, 6, true);
710     vasiGeode->setEffect(vasiEffect);
711     SGVec4f red(1, 0, 0, 1);
712     SGMaterial* mat = 0;
713     if (matlib)
714       mat = matlib->find("RWY_RED_LIGHTS");
715     if (mat)
716       red = mat->get_light_color();
717     SGVec4f white(1, 1, 1, 1);
718     mat = 0;
719     if (matlib)
720       mat = matlib->find("RWY_WHITE_LIGHTS");
721     if (mat)
722       white = mat->get_light_color();
723     SGDirectionalLightListBin::const_iterator i;
724     for (i = tileGeometryBin.vasiLights.begin();
725          i != tileGeometryBin.vasiLights.end(); ++i) {
726       vasiGeode->addDrawable(SGLightFactory::getVasi(up, *i, red, white));
727     }
728     vasiGeode->setStateSet(lightManager->getRunwayLightStateSet());
729     lightGroup->addChild(vasiGeode);
730   }
731   Effect* runwayEffect = 0;
732   if (tileGeometryBin.runwayLights.getNumLights() > 0
733       || !tileGeometryBin.rabitLights.empty()
734       || !tileGeometryBin.reilLights.empty()
735       || !tileGeometryBin.odalLights.empty()
736       || tileGeometryBin.taxiLights.getNumLights() > 0)
737       runwayEffect = getLightEffect(4, osg::Vec3(1, 0.001, 0.0002), 1, 4, true);
738   if (tileGeometryBin.runwayLights.getNumLights() > 0
739       || !tileGeometryBin.rabitLights.empty()
740       || !tileGeometryBin.reilLights.empty()
741       || !tileGeometryBin.odalLights.empty()) {
742     osg::Group* rwyLights = new osg::Group;
743     rwyLights->setStateSet(lightManager->getRunwayLightStateSet());
744     rwyLights->setNodeMask(RUNWAYLIGHTS_BIT);
745     if (tileGeometryBin.runwayLights.getNumLights() != 0) {
746       EffectGeode* geode = new EffectGeode;
747       geode->setEffect(runwayEffect);
748       geode->addDrawable(SGLightFactory::getLights(tileGeometryBin
749                                                    .runwayLights));
750       rwyLights->addChild(geode);
751     }
752     SGDirectionalLightListBin::const_iterator i;
753     for (i = tileGeometryBin.rabitLights.begin();
754          i != tileGeometryBin.rabitLights.end(); ++i) {
755       rwyLights->addChild(SGLightFactory::getSequenced(*i));
756     }
757     for (i = tileGeometryBin.reilLights.begin();
758          i != tileGeometryBin.reilLights.end(); ++i) {
759       rwyLights->addChild(SGLightFactory::getSequenced(*i));
760     }
761     SGLightListBin::const_iterator j;
762     for (j = tileGeometryBin.odalLights.begin();
763          j != tileGeometryBin.odalLights.end(); ++j) {
764       rwyLights->addChild(SGLightFactory::getOdal(*j));
765     }
766     lightGroup->addChild(rwyLights);
767   }
768
769   if (tileGeometryBin.taxiLights.getNumLights() > 0) {
770     osg::Group* taxiLights = new osg::Group;
771     taxiLights->setStateSet(lightManager->getTaxiLightStateSet());
772     taxiLights->setNodeMask(RUNWAYLIGHTS_BIT);
773     EffectGeode* geode = new EffectGeode;
774     geode->setEffect(runwayEffect);
775     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.taxiLights));
776     taxiLights->addChild(geode);
777     lightGroup->addChild(taxiLights);
778   }
779
780   // The toplevel transform for that tile.
781   osg::MatrixTransform* transform = new osg::MatrixTransform;
782   transform->setName(path);
783   transform->setMatrix(osg::Matrix::rotate(toOsg(hlOr))*
784                        osg::Matrix::translate(toOsg(center)));
785   transform->addChild(terrainGroup);
786   if (lightGroup->getNumChildren() > 0) {
787     osg::LOD* lightLOD = new osg::LOD;
788     lightLOD->addChild(lightGroup.get(), 0, 30000);
789     // VASI is always on, so doesn't use light bits.
790     lightLOD->setNodeMask(LIGHTS_BITS | MODEL_BIT); 
791     transform->addChild(lightLOD);
792   }
793   
794   if (randomObjects.valid() || forestNode.valid()) {
795   
796     // Add a LoD node, so we don't try to display anything when the tile center
797     // is more than 20km away.
798     osg::LOD* objectLOD = new osg::LOD;
799     
800     if (randomObjects.valid()) objectLOD->addChild(randomObjects.get(), 0, 20000);
801     if (forestNode.valid())  objectLOD->addChild(forestNode.get(), 0, 20000);
802     
803     unsigned nodeMask = SG_NODEMASK_CASTSHADOW_BIT | SG_NODEMASK_RECIEVESHADOW_BIT | SG_NODEMASK_TERRAIN_BIT;
804     objectLOD->setNodeMask(nodeMask);
805     transform->addChild(objectLOD);
806   }
807   
808   return transform;
809 }