]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/obj.cxx
ocean state set in osg::Geometry
[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 #define CHUNCKED
364 #ifdef CHUNCKED
365       SGMaterial *mat = matlib->find(i->first);
366
367       std::list<SGTexturedTriangleBin::TriangleVector> connectSets;
368       i->second.getConnectedSets(connectSets);
369
370       std::list<SGTexturedTriangleBin::TriangleVector>::iterator j;
371       for (j = connectSets.begin(); j != connectSets.end(); ++j) {
372         osg::Geometry* geometry = i->second.buildGeometry(*j);
373         if (mat)
374           geometry->setStateSet(mat->get_state());
375         geode->addDrawable(geometry);
376       }
377 #else
378       osg::Geometry* geometry = i->second.buildGeometry();
379       SGMaterial *mat = matlib->find(i->first);
380       if (mat)
381         geometry->setStateSet(mat->get_state());
382       geode->addDrawable(geometry);
383 #endif
384     }
385     return geode;
386   }
387
388   void computeRandomSurfaceLights(SGMaterialLib* matlib)
389   {
390     SGMaterialTriangleMap::const_iterator i;
391     for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
392       SGMaterial *mat = matlib->find(i->first);
393       if (!mat)
394         continue;
395
396       float coverage = mat->get_light_coverage();
397       if (coverage <= 0)
398         continue;
399       if (coverage < 10000.0) {
400         SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
401                << coverage << ", pushing up to 10000");
402         coverage = 10000;
403       }
404       
405       // generate a repeatable random seed
406       sg_srandom(unsigned(coverage));
407
408       std::vector<SGVec3f> randomPoints;
409       i->second.addRandomSurfacePoints(coverage, 3, randomPoints);
410       std::vector<SGVec3f>::iterator j;
411       for (j = randomPoints.begin(); j != randomPoints.end(); ++j) {
412         float zombie = sg_random();
413         // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
414         float factor = sg_random();
415         factor *= factor;
416
417         float bright = 1;
418         SGVec4f color;
419         if ( zombie > 0.5 ) {
420           // 50% chance of yellowish
421           color = SGVec4f(0.9f, 0.9f, 0.3f, bright - factor * 0.2f);
422         } else if (zombie > 0.15f) {
423           // 35% chance of whitish
424           color = SGVec4f(0.9, 0.9f, 0.8f, bright - factor * 0.2f);
425         } else if (zombie > 0.05f) {
426           // 10% chance of orangish
427           color = SGVec4f(0.9f, 0.6f, 0.2f, bright - factor * 0.2f);
428         } else {
429           // 5% chance of redish
430           color = SGVec4f(0.9f, 0.2f, 0.2f, bright - factor * 0.2f);
431         }
432         randomTileLights.insert(*j, color);
433       }
434     }
435   }
436
437   bool insertBinObj(const SGBinObject& obj, SGMaterialLib* matlib)
438   {
439     if (!insertPtGeometry(obj, matlib))
440       return false;
441     if (!insertSurfaceGeometry(obj, matlib))
442       return false;
443     return true;
444   }
445 };
446
447
448 class SGTileUpdateCallback : public osg::NodeCallback {
449 public:
450   virtual void operator()(osg::Node* node, osg::NodeVisitor* nv)
451   {
452     assert(dynamic_cast<osg::Switch*>(node));
453     assert(dynamic_cast<SGUpdateVisitor*>(nv));
454
455     osg::Switch* lightSwitch = static_cast<osg::Switch*>(node);
456     SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
457
458     // The current sun angle in degree
459     float sun_angle = updateVisitor->getSunAngleDeg();
460
461     // vasi is always on
462     lightSwitch->setValue(0, true);
463     if (sun_angle > 85 || updateVisitor->getVisibility() < 5000) {
464       // runway and taxi
465       lightSwitch->setValue(1, true);
466       lightSwitch->setValue(2, true);
467     } else {
468       // runway and taxi
469       lightSwitch->setValue(1, false);
470       lightSwitch->setValue(2, false);
471     }
472     
473     // ground lights
474     if ( sun_angle > 95 )
475       lightSwitch->setValue(5, true);
476     else
477       lightSwitch->setValue(5, false);
478     if ( sun_angle > 92 )
479       lightSwitch->setValue(4, true);
480     else
481       lightSwitch->setValue(4, false);
482     if ( sun_angle > 89 )
483       lightSwitch->setValue(3, true);
484     else
485       lightSwitch->setValue(3, false);
486
487     traverse(node, nv);
488   }
489 };
490
491 class SGRunwayLightFogUpdateCallback : public osg::StateAttribute::Callback {
492 public:
493   virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
494   {
495     assert(dynamic_cast<SGUpdateVisitor*>(nv));
496     assert(dynamic_cast<osg::Fog*>(sa));
497     SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
498     osg::Fog* fog = static_cast<osg::Fog*>(sa);
499     fog->setMode(osg::Fog::EXP2);
500     fog->setColor(updateVisitor->getFogColor().osg());
501     fog->setDensity(updateVisitor->getRunwayFogExp2Density());
502   }
503 };
504
505 class SGTaxiLightFogUpdateCallback : public osg::StateAttribute::Callback {
506 public:
507   virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
508   {
509     assert(dynamic_cast<SGUpdateVisitor*>(nv));
510     assert(dynamic_cast<osg::Fog*>(sa));
511     SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
512     osg::Fog* fog = static_cast<osg::Fog*>(sa);
513     fog->setMode(osg::Fog::EXP2);
514     fog->setColor(updateVisitor->getFogColor().osg());
515     fog->setDensity(updateVisitor->getTaxiFogExp2Density());
516   }
517 };
518
519 class SGGroundLightFogUpdateCallback : public osg::StateAttribute::Callback {
520 public:
521   virtual void operator () (osg::StateAttribute* sa, osg::NodeVisitor* nv)
522   {
523     assert(dynamic_cast<SGUpdateVisitor*>(nv));
524     assert(dynamic_cast<osg::Fog*>(sa));
525     SGUpdateVisitor* updateVisitor = static_cast<SGUpdateVisitor*>(nv);
526     osg::Fog* fog = static_cast<osg::Fog*>(sa);
527     fog->setMode(osg::Fog::EXP2);
528     fog->setColor(updateVisitor->getFogColor().osg());
529     fog->setDensity(updateVisitor->getGroundLightsFogExp2Density());
530   }
531 };
532
533 osg::Node*
534 SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool use_random_objects)
535 {
536   SGBinObject obj;
537   if (!obj.read_bin(path))
538     return false;
539
540   SGTileGeometryBin tileGeometryBin;
541   if (!tileGeometryBin.insertBinObj(obj, matlib))
542     return false;
543
544   SGVec3d center = obj.get_gbs_center2();
545   SGGeod geodPos = SGGeod::fromCart(center);
546   SGQuatd hlOr = SGQuatd::fromLonLat(geodPos);
547   SGVec3f up = toVec3f(hlOr.backTransform(SGVec3d(0, 0, -1)));
548
549   osg::Group* vasiLights = new osg::Group;
550   vasiLights->setCullCallback(new SGPointSpriteLightCullCallback(osg::Vec3(1, 0.0001, 0.000001), 6));
551   osg::StateSet* stateSet = vasiLights->getOrCreateStateSet();
552   osg::Fog* fog = new osg::Fog;
553   fog->setUpdateCallback(new SGRunwayLightFogUpdateCallback);
554   stateSet->setAttribute(fog);
555
556   osg::Group* rwyLights = new osg::Group;
557   rwyLights->setCullCallback(new SGPointSpriteLightCullCallback);
558   stateSet = rwyLights->getOrCreateStateSet();
559   fog = new osg::Fog;
560   fog->setUpdateCallback(new SGRunwayLightFogUpdateCallback);
561   stateSet->setAttribute(fog);
562
563   osg::Group* taxiLights = new osg::Group;
564   taxiLights->setCullCallback(new SGPointSpriteLightCullCallback);
565   stateSet = taxiLights->getOrCreateStateSet();
566   fog = new osg::Fog;
567   fog->setUpdateCallback(new SGTaxiLightFogUpdateCallback);
568   stateSet->setAttribute(fog);
569
570   osg::Group* groundLights0 = new osg::Group;
571   stateSet = groundLights0->getOrCreateStateSet();
572   fog = new osg::Fog;
573   fog->setUpdateCallback(new SGGroundLightFogUpdateCallback);
574   stateSet->setAttribute(fog);
575
576   osg::Group* groundLights1 = new osg::Group;
577   stateSet = groundLights1->getOrCreateStateSet();
578   fog = new osg::Fog;
579   fog->setUpdateCallback(new SGGroundLightFogUpdateCallback);
580   stateSet->setAttribute(fog);
581
582   osg::Group* groundLights2 = new osg::Group;
583   stateSet = groundLights2->getOrCreateStateSet();
584   fog = new osg::Fog;
585   fog->setUpdateCallback(new SGGroundLightFogUpdateCallback);
586   stateSet->setAttribute(fog);
587
588   osg::Switch* lightSwitch = new osg::Switch;
589   lightSwitch->setUpdateCallback(new SGTileUpdateCallback);
590   lightSwitch->addChild(vasiLights, true);
591   lightSwitch->addChild(rwyLights, true);
592   lightSwitch->addChild(taxiLights, true);
593   lightSwitch->addChild(groundLights0, true);
594   lightSwitch->addChild(groundLights1, true);
595   lightSwitch->addChild(groundLights2, true);
596
597   osg::Group* lightGroup = new SGOffsetTransform(0.94);
598   lightGroup->addChild(lightSwitch);
599
600   osg::LOD* lightLOD = new osg::LOD;
601   lightLOD->addChild(lightGroup, 0, 30000);
602   unsigned nodeMask = ~0u;
603   nodeMask &= ~SG_NODEMASK_CASTSHADOW_BIT;
604   nodeMask &= ~SG_NODEMASK_RECIEVESHADOW_BIT;
605   nodeMask &= ~SG_NODEMASK_PICK_BIT;
606   nodeMask &= ~SG_NODEMASK_TERRAIN_BIT;
607   lightLOD->setNodeMask(nodeMask);
608
609   osg::Group* terrainGroup = new osg::Group;
610   osg::Node* node = tileGeometryBin.getSurfaceGeometry(matlib);
611   if (node)
612     terrainGroup->addChild(node);
613
614   if (calc_lights) {
615     // FIXME: ugly, has a side effect
616     tileGeometryBin.computeRandomSurfaceLights(matlib);
617
618     osg::Geode* geode = new osg::Geode;
619     groundLights0->addChild(geode);
620     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.tileLights));
621
622     groundLights0->addChild(geode);
623     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 4, -0.3f));
624
625     geode = new osg::Geode;
626     groundLights1->addChild(geode);
627     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 2, -0.15f));
628
629     geode = new osg::Geode;
630     groundLights2->addChild(geode);
631     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights));
632   }
633
634   {
635     SGVec4f red(1, 0, 0, 1);
636     SGMaterial* mat = matlib->find("RWY_RED_LIGHTS");
637     if (mat)
638       red = mat->get_light_color();
639     SGVec4f white(1, 1, 1, 1);
640     mat = matlib->find("RWY_WHITE_LIGHTS");
641     if (mat)
642       white = mat->get_light_color();
643     osg::Geode* geode;
644     geode = new osg::Geode;
645     vasiLights->addChild(geode);
646     SGDirectionalLightListBin::const_iterator i;
647     for (i = tileGeometryBin.vasiLights.begin();
648          i != tileGeometryBin.vasiLights.end(); ++i) {
649       geode->addDrawable(SGLightFactory::getVasi(up, *i, red, white));
650     }
651     
652     geode = new osg::Geode;
653     rwyLights->addChild(geode);
654     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.runwayLights));
655     for (i = tileGeometryBin.rabitLights.begin();
656          i != tileGeometryBin.rabitLights.end(); ++i) {
657       rwyLights->addChild(SGLightFactory::getSequenced(*i));
658     }
659     for (i = tileGeometryBin.reilLights.begin();
660          i != tileGeometryBin.reilLights.end(); ++i) {
661       rwyLights->addChild(SGLightFactory::getSequenced(*i));
662     }
663
664     SGLightListBin::const_iterator j;
665     for (j = tileGeometryBin.odalLights.begin();
666          j != tileGeometryBin.odalLights.end(); ++j) {
667       rwyLights->addChild(SGLightFactory::getOdal(*j));
668     }
669
670     geode = new osg::Geode;
671     taxiLights->addChild(geode);
672     geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.taxiLights));
673   }
674
675   // The toplevel transform for that tile.
676   osg::MatrixTransform* transform = new osg::MatrixTransform;
677   transform->setName(path);
678   transform->setMatrix(osg::Matrix::translate(center.osg()));
679   transform->addChild(terrainGroup);
680   transform->addChild(lightLOD);
681
682   return transform;
683 }