]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/obj.cxx
Some scene graph optimizations
[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 <simgear/debug/logstream.hxx>
44 #include <simgear/io/sg_binobj.hxx>
45 #include <simgear/math/sg_geodesy.hxx>
46 #include <simgear/math/sg_random.h>
47 #include <simgear/scene/material/mat.hxx>
48 #include <simgear/scene/material/matlib.hxx>
49 #include <simgear/scene/model/SGOffsetTransform.hxx>
50 #include <simgear/scene/util/SGUpdateVisitor.hxx>
51 #include <simgear/scene/util/SGNodeMasks.hxx>
52 #include <simgear/threads/SGThread.hxx>
53 #include <simgear/threads/SGGuard.hxx>
54
55 #include "SGTexturedTriangleBin.hxx"
56 #include "SGLightBin.hxx"
57 #include "SGDirectionalLightBin.hxx"
58
59 #include "pt_lights.hxx"
60
61 typedef std::map<std::string,SGTexturedTriangleBin> SGMaterialTriangleMap;
62 typedef std::list<SGLightBin> SGLightListBin;
63 typedef std::list<SGDirectionalLightBin> SGDirectionalLightListBin;
64
65 struct SGTileGeometryBin {
66   SGMaterialTriangleMap materialTriangleMap;
67   SGLightBin tileLights;
68   SGLightBin randomTileLights;
69   SGDirectionalLightBin runwayLights;
70   SGDirectionalLightBin taxiLights;
71   SGDirectionalLightListBin vasiLights;
72   SGDirectionalLightListBin rabitLights;
73   SGLightListBin odalLights;
74   SGDirectionalLightListBin reilLights;
75
76   static SGVec4f
77   getMaterialLightColor(const SGMaterial* material)
78   {
79     if (!material)
80       return SGVec4f(1, 1, 1, 0.8);
81     return material->get_light_color();
82   }
83
84   static void
85   addPointGeometry(SGLightBin& lights,
86                    const std::vector<SGVec3d>& vertices,
87                    const SGVec4f& color,
88                    const int_list& pts_v)
89   {
90     for (unsigned i = 0; i < pts_v.size(); ++i)
91       lights.insert(toVec3f(vertices[pts_v[i]]), color);
92   }
93
94   static void
95   addPointGeometry(SGDirectionalLightBin& lights,
96                    const std::vector<SGVec3d>& vertices,
97                    const std::vector<SGVec3f>& normals,
98                    const SGVec4f& color,
99                    const int_list& pts_v,
100                    const int_list& pts_n)
101   {
102     // If the normal indices match the vertex indices, use seperate
103     // normal indices. Else reuse the vertex indices for the normals.
104     if (pts_v.size() == pts_n.size()) {
105       for (unsigned i = 0; i < pts_v.size(); ++i)
106         lights.insert(toVec3f(vertices[pts_v[i]]), normals[pts_n[i]], color);
107     } else {
108       for (unsigned i = 0; i < pts_v.size(); ++i)
109         lights.insert(toVec3f(vertices[pts_v[i]]), normals[pts_v[i]], color);
110     }
111   }
112
113   bool
114   insertPtGeometry(const SGBinObject& obj, SGMaterialLib* matlib)
115   {
116     if (obj.get_pts_v().size() != obj.get_pts_n().size()) {
117       SG_LOG(SG_TERRAIN, SG_ALERT,
118              "Group list sizes for points do not match!");
119       return false;
120     }
121
122     for (unsigned grp = 0; grp < obj.get_pts_v().size(); ++grp) {
123       std::string materialName = obj.get_pt_materials()[grp];
124       SGMaterial* material = matlib->find(materialName);
125       SGVec4f color = getMaterialLightColor(material);
126       
127       if (3 <= materialName.size() && materialName.substr(0, 3) != "RWY") {
128         // Just plain lights. Not something for the runway.
129         addPointGeometry(tileLights, obj.get_wgs84_nodes(), color,
130                          obj.get_pts_v()[grp]);
131       } else if (materialName == "RWY_BLUE_TAXIWAY_LIGHTS"
132                  || materialName == "RWY_GREEN_TAXIWAY_LIGHTS") {
133         addPointGeometry(taxiLights, obj.get_wgs84_nodes(), obj.get_normals(),
134                          color, obj.get_pts_v()[grp], obj.get_pts_n()[grp]);
135       } else if (materialName == "RWY_VASI_LIGHTS") {
136         vasiLights.push_back(SGDirectionalLightBin());
137         addPointGeometry(vasiLights.back(), obj.get_wgs84_nodes(),
138                          obj.get_normals(), color, obj.get_pts_v()[grp],
139                          obj.get_pts_n()[grp]);
140       } else if (materialName == "RWY_SEQUENCED_LIGHTS") {
141         rabitLights.push_back(SGDirectionalLightBin());
142         addPointGeometry(rabitLights.back(), obj.get_wgs84_nodes(),
143                          obj.get_normals(), color, obj.get_pts_v()[grp],
144                          obj.get_pts_n()[grp]);
145       } else if (materialName == "RWY_ODALS_LIGHTS") {
146         odalLights.push_back(SGLightBin());
147         addPointGeometry(odalLights.back(), obj.get_wgs84_nodes(),
148                          color, obj.get_pts_v()[grp]);
149       } else if (materialName == "RWY_REIL_LIGHTS") {
150         reilLights.push_back(SGDirectionalLightBin());
151         addPointGeometry(reilLights.back(), obj.get_wgs84_nodes(),
152                          obj.get_normals(), color, obj.get_pts_v()[grp],
153                          obj.get_pts_n()[grp]);
154       } else {
155         // what is left must be runway lights
156         addPointGeometry(runwayLights, obj.get_wgs84_nodes(),
157                          obj.get_normals(), color, obj.get_pts_v()[grp],
158                          obj.get_pts_n()[grp]);
159       }
160     }
161
162     return true;
163   }
164
165
166   static SGVec2f
167   getTexCoord(const std::vector<SGVec2f>& texCoords, const int_list& tc,
168               const SGVec2f& tcScale, unsigned i)
169   {
170     if (tc.empty())
171       return tcScale;
172     else if (tc.size() == 1)
173       return mult(texCoords[tc[0]], tcScale);
174     else
175       return mult(texCoords[tc[i]], tcScale);
176   }
177
178   static void
179   addTriangleGeometry(SGTexturedTriangleBin& triangles,
180                       const std::vector<SGVec3d>& vertices,
181                       const std::vector<SGVec3f>& normals,
182                       const std::vector<SGVec2f>& texCoords,
183                       const int_list& tris_v,
184                       const int_list& tris_n,
185                       const int_list& tris_tc,
186                       const SGVec2f& tcScale)
187   {
188     if (tris_v.size() != tris_n.size()) {
189       // If the normal indices do not match, they should be inmplicitly
190       // the same than the vertex indices. So just call ourselves again
191       // with the matching index vector.
192       addTriangleGeometry(triangles, vertices, normals, texCoords,
193                           tris_v, tris_v, tris_tc, tcScale);
194       return;
195     }
196
197     for (unsigned i = 2; i < tris_v.size(); i += 3) {
198       SGVertNormTex v0;
199       v0.vertex = toVec3f(vertices[tris_v[i-2]]);
200       v0.normal = normals[tris_n[i-2]];
201       v0.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i-2);
202       SGVertNormTex v1;
203       v1.vertex = toVec3f(vertices[tris_v[i-1]]);
204       v1.normal = normals[tris_n[i-1]];
205       v1.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i-1);
206       SGVertNormTex v2;
207       v2.vertex = toVec3f(vertices[tris_v[i]]);
208       v2.normal = normals[tris_n[i]];
209       v2.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i);
210       triangles.insert(v0, v1, v2);
211     }
212   }
213
214   static void
215   addStripGeometry(SGTexturedTriangleBin& triangles,
216                    const std::vector<SGVec3d>& vertices,
217                    const std::vector<SGVec3f>& normals,
218                    const std::vector<SGVec2f>& texCoords,
219                    const int_list& strips_v,
220                    const int_list& strips_n,
221                    const int_list& strips_tc,
222                    const SGVec2f& tcScale)
223   {
224     if (strips_v.size() != strips_n.size()) {
225       // If the normal indices do not match, they should be inmplicitly
226       // the same than the vertex indices. So just call ourselves again
227       // with the matching index vector.
228       addStripGeometry(triangles, vertices, normals, texCoords,
229                        strips_v, strips_v, strips_tc, tcScale);
230       return;
231     }
232
233     for (unsigned i = 2; i < strips_v.size(); ++i) {
234       SGVertNormTex v0;
235       v0.vertex = toVec3f(vertices[strips_v[i-2]]);
236       v0.normal = normals[strips_n[i-2]];
237       v0.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i-2);
238       SGVertNormTex v1;
239       v1.vertex = toVec3f(vertices[strips_v[i-1]]);
240       v1.normal = normals[strips_n[i-1]];
241       v1.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i-1);
242       SGVertNormTex v2;
243       v2.vertex = toVec3f(vertices[strips_v[i]]);
244       v2.normal = normals[strips_n[i]];
245       v2.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i);
246       if (i%2)
247         triangles.insert(v1, v0, v2);
248       else
249         triangles.insert(v0, v1, v2);
250     }
251   }
252   
253   static void
254   addFanGeometry(SGTexturedTriangleBin& triangles,
255                  const std::vector<SGVec3d>& vertices,
256                  const std::vector<SGVec3f>& normals,
257                  const std::vector<SGVec2f>& texCoords,
258                  const int_list& fans_v,
259                  const int_list& fans_n,
260                  const int_list& fans_tc,
261                  const SGVec2f& tcScale)
262   {
263     if (fans_v.size() != fans_n.size()) {
264       // If the normal indices do not match, they should be implicitly
265       // the same than the vertex indices. So just call ourselves again
266       // with the matching index vector.
267       addFanGeometry(triangles, vertices, normals, texCoords,
268                      fans_v, fans_v, fans_tc, tcScale);
269       return;
270     }
271
272     SGVertNormTex v0;
273     v0.vertex = toVec3f(vertices[fans_v[0]]);
274     v0.normal = normals[fans_n[0]];
275     v0.texCoord = getTexCoord(texCoords, fans_tc, tcScale, 0);
276     SGVertNormTex v1;
277     v1.vertex = toVec3f(vertices[fans_v[1]]);
278     v1.normal = normals[fans_n[1]];
279     v1.texCoord = getTexCoord(texCoords, fans_tc, tcScale, 1);
280     for (unsigned i = 2; i < fans_v.size(); ++i) {
281       SGVertNormTex v2;
282       v2.vertex = toVec3f(vertices[fans_v[i]]);
283       v2.normal = normals[fans_n[i]];
284       v2.texCoord = getTexCoord(texCoords, fans_tc, tcScale, i);
285       triangles.insert(v0, v1, v2);
286       v1 = v2;
287     }
288   }
289
290   SGVec2f getTexCoordScale(const std::string& name, SGMaterialLib* matlib)
291   {
292     if (!matlib)
293       return SGVec2f(1, 1);
294     SGMaterial* material = matlib->find(name);
295     if (!material)
296       return SGVec2f(1, 1);
297
298     return material->get_tex_coord_scale();
299   }
300
301   bool
302   insertSurfaceGeometry(const SGBinObject& obj, SGMaterialLib* matlib)
303   {
304     if (obj.get_tris_n().size() < obj.get_tris_v().size() ||
305         obj.get_tris_tc().size() < obj.get_tris_v().size()) {
306       SG_LOG(SG_TERRAIN, SG_ALERT,
307              "Group list sizes for triangles do not match!");
308       return false;
309     }
310
311     for (unsigned grp = 0; grp < obj.get_tris_v().size(); ++grp) {
312       std::string materialName = obj.get_tri_materials()[grp];
313       SGVec2f tcScale = getTexCoordScale(materialName, matlib);
314       addTriangleGeometry(materialTriangleMap[materialName],
315                           obj.get_wgs84_nodes(), obj.get_normals(),
316                           obj.get_texcoords(), obj.get_tris_v()[grp],
317                           obj.get_tris_n()[grp], obj.get_tris_tc()[grp],
318                           tcScale);
319     }
320
321     if (obj.get_strips_n().size() < obj.get_strips_v().size() ||
322         obj.get_strips_tc().size() < obj.get_strips_v().size()) {
323       SG_LOG(SG_TERRAIN, SG_ALERT,
324              "Group list sizes for strips do not match!");
325       return false;
326     }
327     for (unsigned grp = 0; grp < obj.get_strips_v().size(); ++grp) {
328       std::string materialName = obj.get_strip_materials()[grp];
329       SGVec2f tcScale = getTexCoordScale(materialName, matlib);
330       addStripGeometry(materialTriangleMap[materialName],
331                        obj.get_wgs84_nodes(), obj.get_normals(),
332                        obj.get_texcoords(), obj.get_strips_v()[grp],
333                        obj.get_strips_n()[grp], obj.get_strips_tc()[grp],
334                        tcScale);
335     }
336
337     if (obj.get_fans_n().size() < obj.get_fans_v().size() ||
338         obj.get_fans_tc().size() < obj.get_fans_v().size()) {
339       SG_LOG(SG_TERRAIN, SG_ALERT,
340              "Group list sizes for fans do not match!");
341       return false;
342     }
343     for (unsigned grp = 0; grp < obj.get_fans_v().size(); ++grp) {
344       std::string materialName = obj.get_fan_materials()[grp];
345       SGVec2f tcScale = getTexCoordScale(materialName, matlib);
346       addFanGeometry(materialTriangleMap[materialName],
347                      obj.get_wgs84_nodes(), obj.get_normals(),
348                      obj.get_texcoords(), obj.get_fans_v()[grp],
349                      obj.get_fans_n()[grp], obj.get_fans_tc()[grp],
350                      tcScale);
351     }
352     return true;
353   }
354
355   osg::Node* getSurfaceGeometry(SGMaterialLib* matlib) const
356   {
357     if (materialTriangleMap.empty())
358       return 0;
359
360     osg::Geode* geode = new osg::Geode;
361     SGMaterialTriangleMap::const_iterator i;
362     for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
363       // CHUNCKED (sic) here splits up unconnected triangles parts of
364       // the mesh into different Geometry sets, presumably for better
365       // culling. I (timoore) believe it is more performant to build
366       // the biggest indexed sets possible at the expense of tight
367       // culling.
368 //#define CHUNCKED
369 #ifdef CHUNCKED
370       SGMaterial *mat = matlib->find(i->first);
371
372       std::list<SGTexturedTriangleBin::TriangleVector> connectSets;
373       i->second.getConnectedSets(connectSets);
374
375       std::list<SGTexturedTriangleBin::TriangleVector>::iterator j;
376       for (j = connectSets.begin(); j != connectSets.end(); ++j) {
377         osg::Geometry* geometry = i->second.buildGeometry(*j);
378         if (mat)
379           geometry->setStateSet(mat->get_state());
380         geode->addDrawable(geometry);
381       }
382 #else
383       osg::Geometry* geometry = i->second.buildGeometry();
384       SGMaterial *mat = matlib->find(i->first);
385       if (mat)
386         geometry->setStateSet(mat->get_state());
387       geode->addDrawable(geometry);
388 #endif
389     }
390     return geode;
391   }
392
393   void computeRandomSurfaceLights(SGMaterialLib* matlib)
394   {
395     SGMaterialTriangleMap::const_iterator i;
396     for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
397       SGMaterial *mat = matlib->find(i->first);
398       if (!mat)
399         continue;
400
401       float coverage = mat->get_light_coverage();
402       if (coverage <= 0)
403         continue;
404       if (coverage < 10000.0) {
405         SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
406                << coverage << ", pushing up to 10000");
407         coverage = 10000;
408       }
409       
410       // generate a repeatable random seed
411       sg_srandom(unsigned(coverage));
412
413       std::vector<SGVec3f> randomPoints;
414       i->second.addRandomSurfacePoints(coverage, 3, randomPoints);
415       std::vector<SGVec3f>::iterator j;
416       for (j = randomPoints.begin(); j != randomPoints.end(); ++j) {
417         float zombie = sg_random();
418         // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
419         float factor = sg_random();
420         factor *= factor;
421
422         float bright = 1;
423         SGVec4f color;
424         if ( zombie > 0.5 ) {
425           // 50% chance of yellowish
426           color = SGVec4f(0.9f, 0.9f, 0.3f, bright - factor * 0.2f);
427         } else if (zombie > 0.15f) {
428           // 35% chance of whitish
429           color = SGVec4f(0.9, 0.9f, 0.8f, bright - factor * 0.2f);
430         } else if (zombie > 0.05f) {
431           // 10% chance of orangish
432           color = SGVec4f(0.9f, 0.6f, 0.2f, bright - factor * 0.2f);
433         } else {
434           // 5% chance of redish
435           color = SGVec4f(0.9f, 0.2f, 0.2f, bright - factor * 0.2f);
436         }
437         randomTileLights.insert(*j, color);
438       }
439     }
440   }
441
442   bool insertBinObj(const SGBinObject& obj, SGMaterialLib* matlib)
443   {
444     if (!insertPtGeometry(obj, matlib))
445       return false;
446     if (!insertSurfaceGeometry(obj, matlib))
447       return false;
448     return true;
449   }
450 };
451
452
453 class SGTileUpdateCallback : public osg::NodeCallback {
454 public:
455   virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
456   {
457     assert(dynamic_cast<osg::Switch*>(node));
458     assert(dynamic_cast<SGUpdateVisitor*>(nv));
459
460     osg::Switch* lightSwitch = static_cast<osg::Switch*>(node);
461     SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
462
463     // The current sun angle in degree
464     float sun_angle = updateVisitor->getSunAngleDeg();
465
466     // vasi is always on
467     lightSwitch->setValue(0, true);
468     if (sun_angle > 85 || updateVisitor->getVisibility() < 5000) {
469       // runway and taxi
470       lightSwitch->setValue(1, true);
471       lightSwitch->setValue(2, true);
472     } else {
473       // runway and taxi
474       lightSwitch->setValue(1, false);
475       lightSwitch->setValue(2, false);
476     }
477     
478     // ground lights
479     if ( sun_angle > 95 )
480       lightSwitch->setValue(5, true);
481     else
482       lightSwitch->setValue(5, false);
483     if ( sun_angle > 92 )
484       lightSwitch->setValue(4, true);
485     else
486       lightSwitch->setValue(4, false);
487     if ( sun_angle > 89 )
488       lightSwitch->setValue(3, true);
489     else
490       lightSwitch->setValue(3, false);
491
492     traverse(node, nv);
493   }
494 };
495
496 class SGRunwayLightFogUpdateCallback : public osg::StateAttribute::Callback {
497 public:
498   virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
499   {
500     assert(dynamic_cast<SGUpdateVisitor*>(nv));
501     assert(dynamic_cast<osg::Fog*>(sa));
502     SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
503     osg::Fog* fog = static_cast<osg::Fog*>(sa);
504     fog->setMode(osg::Fog::EXP2);
505     fog->setColor(updateVisitor->getFogColor().osg());
506     fog->setDensity(updateVisitor->getRunwayFogExp2Density());
507   }
508 };
509
510 class SGTaxiLightFogUpdateCallback : public osg::StateAttribute::Callback {
511 public:
512   virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
513   {
514     assert(dynamic_cast<SGUpdateVisitor*>(nv));
515     assert(dynamic_cast<osg::Fog*>(sa));
516     SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
517     osg::Fog* fog = static_cast<osg::Fog*>(sa);
518     fog->setMode(osg::Fog::EXP2);
519     fog->setColor(updateVisitor->getFogColor().osg());
520     fog->setDensity(updateVisitor->getTaxiFogExp2Density());
521   }
522 };
523
524 class SGGroundLightFogUpdateCallback : public osg::StateAttribute::Callback {
525 public:
526   virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
527   {
528     assert(dynamic_cast<SGUpdateVisitor*>(nv));
529     assert(dynamic_cast<osg::Fog*>(sa));
530     SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
531     osg::Fog* fog = static_cast<osg::Fog*>(sa);
532     fog->setMode(osg::Fog::EXP2);
533     fog->setColor(updateVisitor->getFogColor().osg());
534     fog->setDensity(updateVisitor->getGroundLightsFogExp2Density());
535   }
536 };
537
538 osg::Node*
539 SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool use_random_objects)
540 {
541   SGBinObject obj;
542   if (!obj.read_bin(path))
543     return false;
544
545   SGTileGeometryBin tileGeometryBin;
546   if (!tileGeometryBin.insertBinObj(obj, matlib))
547     return false;
548
549   SGVec3d center = obj.get_gbs_center2();
550   SGGeod geodPos = SGGeod::fromCart(center);
551   SGQuatd hlOr = SGQuatd::fromLonLat(geodPos);
552   SGVec3f up = toVec3f(hlOr.backTransform(SGVec3d(0, 0, -1)));
553
554   osg::Group* vasiLights = new osg::Group;
555   vasiLights->setCullCallback(new SGPointSpriteLightCullCallback(osg::Vec3(1, 0.0001, 0.000001), 6));
556   osg::StateSet* stateSet = vasiLights->getOrCreateStateSet();
557   osg::Fog* fog = new osg::Fog;
558   fog->setUpdateCallback(new SGRunwayLightFogUpdateCallback);
559   stateSet->setAttribute(fog);
560
561   osg::Group* rwyLights = new osg::Group;
562   rwyLights->setCullCallback(new SGPointSpriteLightCullCallback);
563   stateSet = rwyLights->getOrCreateStateSet();
564   fog = new osg::Fog;
565   fog->setUpdateCallback(new SGRunwayLightFogUpdateCallback);
566   stateSet->setAttribute(fog);
567
568   osg::Group* taxiLights = new osg::Group;
569   taxiLights->setCullCallback(new SGPointSpriteLightCullCallback);
570   stateSet = taxiLights->getOrCreateStateSet();
571   fog = new osg::Fog;
572   fog->setUpdateCallback(new SGTaxiLightFogUpdateCallback);
573   stateSet->setAttribute(fog);
574
575   osg::Group* groundLights0 = new osg::Group;
576   stateSet = groundLights0->getOrCreateStateSet();
577   fog = new osg::Fog;
578   fog->setUpdateCallback(new SGGroundLightFogUpdateCallback);
579   stateSet->setAttribute(fog);
580
581   osg::Group* groundLights1 = new osg::Group;
582   stateSet = groundLights1->getOrCreateStateSet();
583   fog = new osg::Fog;
584   fog->setUpdateCallback(new SGGroundLightFogUpdateCallback);
585   stateSet->setAttribute(fog);
586
587   osg::Group* groundLights2 = new osg::Group;
588   stateSet = groundLights2->getOrCreateStateSet();
589   fog = new osg::Fog;
590   fog->setUpdateCallback(new SGGroundLightFogUpdateCallback);
591   stateSet->setAttribute(fog);
592
593   osg::Switch* lightSwitch = new osg::Switch;
594   lightSwitch->setUpdateCallback(new SGTileUpdateCallback);
595   lightSwitch->addChild(vasiLights, true);
596   lightSwitch->addChild(rwyLights, true);
597   lightSwitch->addChild(taxiLights, true);
598   lightSwitch->addChild(groundLights0, true);
599   lightSwitch->addChild(groundLights1, true);
600   lightSwitch->addChild(groundLights2, true);
601
602   osg::Group* lightGroup = new SGOffsetTransform(0.94);
603   lightGroup->addChild(lightSwitch);
604
605   osg::LOD* lightLOD = new osg::LOD;
606   lightLOD->addChild(lightGroup, 0, 30000);
607   unsigned nodeMask = ~0u;
608   nodeMask &= ~SG_NODEMASK_CASTSHADOW_BIT;
609   nodeMask &= ~SG_NODEMASK_RECIEVESHADOW_BIT;
610   nodeMask &= ~SG_NODEMASK_PICK_BIT;
611   nodeMask &= ~SG_NODEMASK_TERRAIN_BIT;
612   lightLOD->setNodeMask(nodeMask);
613
614   osg::Group* terrainGroup = new osg::Group;
615   osg::Node* node = tileGeometryBin.getSurfaceGeometry(matlib);
616   if (node)
617     terrainGroup->addChild(node);
618
619   if (calc_lights) {
620     // FIXME: ugly, has a side effect
621     tileGeometryBin.computeRandomSurfaceLights(matlib);
622
623     osg::Geode* geode = new osg::Geode;
624     groundLights0->addChild(geode);
625     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.tileLights));
626
627     groundLights0->addChild(geode);
628     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 4, -0.3f));
629
630     geode = new osg::Geode;
631     groundLights1->addChild(geode);
632     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 2, -0.15f));
633
634     geode = new osg::Geode;
635     groundLights2->addChild(geode);
636     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights));
637   }
638
639   {
640     SGVec4f red(1, 0, 0, 1);
641     SGMaterial* mat = matlib->find("RWY_RED_LIGHTS");
642     if (mat)
643       red = mat->get_light_color();
644     SGVec4f white(1, 1, 1, 1);
645     mat = matlib->find("RWY_WHITE_LIGHTS");
646     if (mat)
647       white = mat->get_light_color();
648     osg::Geode* geode;
649     geode = new osg::Geode;
650     vasiLights->addChild(geode);
651     SGDirectionalLightListBin::const_iterator i;
652     for (i = tileGeometryBin.vasiLights.begin();
653          i != tileGeometryBin.vasiLights.end(); ++i) {
654       geode->addDrawable(SGLightFactory::getVasi(up, *i, red, white));
655     }
656     
657     geode = new osg::Geode;
658     rwyLights->addChild(geode);
659     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.runwayLights));
660     for (i = tileGeometryBin.rabitLights.begin();
661          i != tileGeometryBin.rabitLights.end(); ++i) {
662       rwyLights->addChild(SGLightFactory::getSequenced(*i));
663     }
664     for (i = tileGeometryBin.reilLights.begin();
665          i != tileGeometryBin.reilLights.end(); ++i) {
666       rwyLights->addChild(SGLightFactory::getSequenced(*i));
667     }
668
669     SGLightListBin::const_iterator j;
670     for (j = tileGeometryBin.odalLights.begin();
671          j != tileGeometryBin.odalLights.end(); ++j) {
672       rwyLights->addChild(SGLightFactory::getOdal(*j));
673     }
674
675     geode = new osg::Geode;
676     taxiLights->addChild(geode);
677     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.taxiLights));
678   }
679
680   // The toplevel transform for that tile.
681   osg::MatrixTransform* transform = new osg::MatrixTransform;
682   transform->setName(path);
683   transform->setMatrix(osg::Matrix::translate(center.osg()));
684   transform->addChild(terrainGroup);
685   transform->addChild(lightLOD);
686
687   return transform;
688 }