1 // obj.cxx -- routines to handle loading scenery and building the plib
4 // Written by Curtis Olson, started October 1997.
6 // Copyright (C) 1997 Curtis L. Olson - http://www.flightgear.org/~curt
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.
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.
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.
26 # include <simgear_config.h>
31 #include <simgear/compiler.h>
35 #include <osg/Geometry>
38 #include <osg/MatrixTransform>
40 #include <osg/StateSet>
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/Effect.hxx>
48 #include <simgear/scene/material/EffectGeode.hxx>
49 #include <simgear/scene/material/mat.hxx>
50 #include <simgear/scene/material/matlib.hxx>
51 #include <simgear/scene/model/SGOffsetTransform.hxx>
52 #include <simgear/scene/util/SGUpdateVisitor.hxx>
53 #include <simgear/scene/util/SGNodeMasks.hxx>
54 #include <simgear/scene/util/QuadTreeBuilder.hxx>
56 #include "SGTexturedTriangleBin.hxx"
57 #include "SGLightBin.hxx"
58 #include "SGModelBin.hxx"
59 #include "TreeBin.hxx"
60 #include "SGDirectionalLightBin.hxx"
61 #include "GroundLightManager.hxx"
64 #include "userdata.hxx"
65 #include "pt_lights.hxx"
67 using namespace simgear;
69 typedef std::map<std::string,SGTexturedTriangleBin> SGMaterialTriangleMap;
70 typedef std::list<SGLightBin> SGLightListBin;
71 typedef std::list<SGDirectionalLightBin> SGDirectionalLightListBin;
73 struct SGTileGeometryBin {
74 SGMaterialTriangleMap materialTriangleMap;
75 SGLightBin tileLights;
76 SGLightBin randomTileLights;
78 SGDirectionalLightBin runwayLights;
79 SGDirectionalLightBin taxiLights;
80 SGDirectionalLightListBin vasiLights;
81 SGDirectionalLightListBin rabitLights;
82 SGLightListBin odalLights;
83 SGDirectionalLightListBin reilLights;
84 SGMatModelBin randomModels;
87 getMaterialLightColor(const SGMaterial* material)
90 return SGVec4f(1, 1, 1, 0.8);
91 return material->get_light_color();
95 addPointGeometry(SGLightBin& lights,
96 const std::vector<SGVec3d>& vertices,
98 const int_list& pts_v)
100 for (unsigned i = 0; i < pts_v.size(); ++i)
101 lights.insert(toVec3f(vertices[pts_v[i]]), color);
105 addPointGeometry(SGDirectionalLightBin& lights,
106 const std::vector<SGVec3d>& vertices,
107 const std::vector<SGVec3f>& normals,
108 const SGVec4f& color,
109 const int_list& pts_v,
110 const int_list& pts_n)
112 // If the normal indices match the vertex indices, use seperate
113 // normal indices. Else reuse the vertex indices for the normals.
114 if (pts_v.size() == pts_n.size()) {
115 for (unsigned i = 0; i < pts_v.size(); ++i)
116 lights.insert(toVec3f(vertices[pts_v[i]]), normals[pts_n[i]], color);
118 for (unsigned i = 0; i < pts_v.size(); ++i)
119 lights.insert(toVec3f(vertices[pts_v[i]]), normals[pts_v[i]], color);
124 insertPtGeometry(const SGBinObject& obj, SGMaterialLib* matlib)
126 if (obj.get_pts_v().size() != obj.get_pts_n().size()) {
127 SG_LOG(SG_TERRAIN, SG_ALERT,
128 "Group list sizes for points do not match!");
132 for (unsigned grp = 0; grp < obj.get_pts_v().size(); ++grp) {
133 std::string materialName = obj.get_pt_materials()[grp];
134 SGMaterial* material = 0;
136 material = matlib->find(materialName);
137 SGVec4f color = getMaterialLightColor(material);
139 if (3 <= materialName.size() && materialName.substr(0, 3) != "RWY") {
140 // Just plain lights. Not something for the runway.
141 addPointGeometry(tileLights, obj.get_wgs84_nodes(), color,
142 obj.get_pts_v()[grp]);
143 } else if (materialName == "RWY_BLUE_TAXIWAY_LIGHTS"
144 || materialName == "RWY_GREEN_TAXIWAY_LIGHTS") {
145 addPointGeometry(taxiLights, obj.get_wgs84_nodes(), obj.get_normals(),
146 color, obj.get_pts_v()[grp], obj.get_pts_n()[grp]);
147 } else if (materialName == "RWY_VASI_LIGHTS") {
148 vasiLights.push_back(SGDirectionalLightBin());
149 addPointGeometry(vasiLights.back(), obj.get_wgs84_nodes(),
150 obj.get_normals(), color, obj.get_pts_v()[grp],
151 obj.get_pts_n()[grp]);
152 } else if (materialName == "RWY_SEQUENCED_LIGHTS") {
153 rabitLights.push_back(SGDirectionalLightBin());
154 addPointGeometry(rabitLights.back(), obj.get_wgs84_nodes(),
155 obj.get_normals(), color, obj.get_pts_v()[grp],
156 obj.get_pts_n()[grp]);
157 } else if (materialName == "RWY_ODALS_LIGHTS") {
158 odalLights.push_back(SGLightBin());
159 addPointGeometry(odalLights.back(), obj.get_wgs84_nodes(),
160 color, obj.get_pts_v()[grp]);
161 } else if (materialName == "RWY_REIL_LIGHTS") {
162 reilLights.push_back(SGDirectionalLightBin());
163 addPointGeometry(reilLights.back(), obj.get_wgs84_nodes(),
164 obj.get_normals(), color, obj.get_pts_v()[grp],
165 obj.get_pts_n()[grp]);
167 // what is left must be runway lights
168 addPointGeometry(runwayLights, obj.get_wgs84_nodes(),
169 obj.get_normals(), color, obj.get_pts_v()[grp],
170 obj.get_pts_n()[grp]);
179 getTexCoord(const std::vector<SGVec2f>& texCoords, const int_list& tc,
180 const SGVec2f& tcScale, unsigned i)
184 else if (tc.size() == 1)
185 return mult(texCoords[tc[0]], tcScale);
187 return mult(texCoords[tc[i]], tcScale);
191 addTriangleGeometry(SGTexturedTriangleBin& triangles,
192 const std::vector<SGVec3d>& vertices,
193 const std::vector<SGVec3f>& normals,
194 const std::vector<SGVec2f>& texCoords,
195 const int_list& tris_v,
196 const int_list& tris_n,
197 const int_list& tris_tc,
198 const SGVec2f& tcScale)
200 if (tris_v.size() != tris_n.size()) {
201 // If the normal indices do not match, they should be inmplicitly
202 // the same than the vertex indices. So just call ourselves again
203 // with the matching index vector.
204 addTriangleGeometry(triangles, vertices, normals, texCoords,
205 tris_v, tris_v, tris_tc, tcScale);
209 for (unsigned i = 2; i < tris_v.size(); i += 3) {
211 v0.vertex = toVec3f(vertices[tris_v[i-2]]);
212 v0.normal = normals[tris_n[i-2]];
213 v0.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i-2);
215 v1.vertex = toVec3f(vertices[tris_v[i-1]]);
216 v1.normal = normals[tris_n[i-1]];
217 v1.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i-1);
219 v2.vertex = toVec3f(vertices[tris_v[i]]);
220 v2.normal = normals[tris_n[i]];
221 v2.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i);
222 triangles.insert(v0, v1, v2);
227 addStripGeometry(SGTexturedTriangleBin& triangles,
228 const std::vector<SGVec3d>& vertices,
229 const std::vector<SGVec3f>& normals,
230 const std::vector<SGVec2f>& texCoords,
231 const int_list& strips_v,
232 const int_list& strips_n,
233 const int_list& strips_tc,
234 const SGVec2f& tcScale)
236 if (strips_v.size() != strips_n.size()) {
237 // If the normal indices do not match, they should be inmplicitly
238 // the same than the vertex indices. So just call ourselves again
239 // with the matching index vector.
240 addStripGeometry(triangles, vertices, normals, texCoords,
241 strips_v, strips_v, strips_tc, tcScale);
245 for (unsigned i = 2; i < strips_v.size(); ++i) {
247 v0.vertex = toVec3f(vertices[strips_v[i-2]]);
248 v0.normal = normals[strips_n[i-2]];
249 v0.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i-2);
251 v1.vertex = toVec3f(vertices[strips_v[i-1]]);
252 v1.normal = normals[strips_n[i-1]];
253 v1.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i-1);
255 v2.vertex = toVec3f(vertices[strips_v[i]]);
256 v2.normal = normals[strips_n[i]];
257 v2.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i);
259 triangles.insert(v1, v0, v2);
261 triangles.insert(v0, v1, v2);
266 addFanGeometry(SGTexturedTriangleBin& triangles,
267 const std::vector<SGVec3d>& vertices,
268 const std::vector<SGVec3f>& normals,
269 const std::vector<SGVec2f>& texCoords,
270 const int_list& fans_v,
271 const int_list& fans_n,
272 const int_list& fans_tc,
273 const SGVec2f& tcScale)
275 if (fans_v.size() != fans_n.size()) {
276 // If the normal indices do not match, they should be implicitly
277 // the same than the vertex indices. So just call ourselves again
278 // with the matching index vector.
279 addFanGeometry(triangles, vertices, normals, texCoords,
280 fans_v, fans_v, fans_tc, tcScale);
285 v0.vertex = toVec3f(vertices[fans_v[0]]);
286 v0.normal = normals[fans_n[0]];
287 v0.texCoord = getTexCoord(texCoords, fans_tc, tcScale, 0);
289 v1.vertex = toVec3f(vertices[fans_v[1]]);
290 v1.normal = normals[fans_n[1]];
291 v1.texCoord = getTexCoord(texCoords, fans_tc, tcScale, 1);
292 for (unsigned i = 2; i < fans_v.size(); ++i) {
294 v2.vertex = toVec3f(vertices[fans_v[i]]);
295 v2.normal = normals[fans_n[i]];
296 v2.texCoord = getTexCoord(texCoords, fans_tc, tcScale, i);
297 triangles.insert(v0, v1, v2);
302 SGVec2f getTexCoordScale(const std::string& name, SGMaterialLib* matlib)
305 return SGVec2f(1, 1);
306 SGMaterial* material = matlib->find(name);
308 return SGVec2f(1, 1);
310 return material->get_tex_coord_scale();
314 insertSurfaceGeometry(const SGBinObject& obj, SGMaterialLib* matlib)
316 if (obj.get_tris_n().size() < obj.get_tris_v().size() ||
317 obj.get_tris_tc().size() < obj.get_tris_v().size()) {
318 SG_LOG(SG_TERRAIN, SG_ALERT,
319 "Group list sizes for triangles do not match!");
323 for (unsigned grp = 0; grp < obj.get_tris_v().size(); ++grp) {
324 std::string materialName = obj.get_tri_materials()[grp];
325 SGVec2f tcScale = getTexCoordScale(materialName, matlib);
326 addTriangleGeometry(materialTriangleMap[materialName],
327 obj.get_wgs84_nodes(), obj.get_normals(),
328 obj.get_texcoords(), obj.get_tris_v()[grp],
329 obj.get_tris_n()[grp], obj.get_tris_tc()[grp],
333 if (obj.get_strips_n().size() < obj.get_strips_v().size() ||
334 obj.get_strips_tc().size() < obj.get_strips_v().size()) {
335 SG_LOG(SG_TERRAIN, SG_ALERT,
336 "Group list sizes for strips do not match!");
339 for (unsigned grp = 0; grp < obj.get_strips_v().size(); ++grp) {
340 std::string materialName = obj.get_strip_materials()[grp];
341 SGVec2f tcScale = getTexCoordScale(materialName, matlib);
342 addStripGeometry(materialTriangleMap[materialName],
343 obj.get_wgs84_nodes(), obj.get_normals(),
344 obj.get_texcoords(), obj.get_strips_v()[grp],
345 obj.get_strips_n()[grp], obj.get_strips_tc()[grp],
349 if (obj.get_fans_n().size() < obj.get_fans_v().size() ||
350 obj.get_fans_tc().size() < obj.get_fans_v().size()) {
351 SG_LOG(SG_TERRAIN, SG_ALERT,
352 "Group list sizes for fans do not match!");
355 for (unsigned grp = 0; grp < obj.get_fans_v().size(); ++grp) {
356 std::string materialName = obj.get_fan_materials()[grp];
357 SGVec2f tcScale = getTexCoordScale(materialName, matlib);
358 addFanGeometry(materialTriangleMap[materialName],
359 obj.get_wgs84_nodes(), obj.get_normals(),
360 obj.get_texcoords(), obj.get_fans_v()[grp],
361 obj.get_fans_n()[grp], obj.get_fans_tc()[grp],
367 osg::Node* getSurfaceGeometry(SGMaterialLib* matlib) const
369 if (materialTriangleMap.empty())
373 osg::Group* group = (materialTriangleMap.size() > 1 ? new osg::Group : 0);
374 //osg::Geode* geode = new osg::Geode;
375 SGMaterialTriangleMap::const_iterator i;
376 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
377 osg::Geometry* geometry = i->second.buildGeometry();
380 mat = matlib->find(i->first);
381 eg = new EffectGeode;
383 eg->setEffect(mat->get_effect());
384 eg->addDrawable(geometry);
394 void computeRandomSurfaceLights(SGMaterialLib* matlib)
396 SGMaterialTriangleMap::iterator i;
398 // generate a repeatable random seed
400 mt_init(&seed, unsigned(123));
402 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
403 SGMaterial *mat = matlib->find(i->first);
407 float coverage = mat->get_light_coverage();
410 if (coverage < 10000.0) {
411 SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
412 << coverage << ", pushing up to 10000");
416 std::vector<SGVec3f> randomPoints;
417 i->second.addRandomSurfacePoints(coverage, 3, randomPoints);
418 std::vector<SGVec3f>::iterator j;
419 for (j = randomPoints.begin(); j != randomPoints.end(); ++j) {
420 float zombie = mt_rand(&seed);
421 // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
422 float factor = mt_rand(&seed);
427 if ( zombie > 0.5 ) {
428 // 50% chance of yellowish
429 color = SGVec4f(0.9f, 0.9f, 0.3f, bright - factor * 0.2f);
430 } else if (zombie > 0.15f) {
431 // 35% chance of whitish
432 color = SGVec4f(0.9, 0.9f, 0.8f, bright - factor * 0.2f);
433 } else if (zombie > 0.05f) {
434 // 10% chance of orangish
435 color = SGVec4f(0.9f, 0.6f, 0.2f, bright - factor * 0.2f);
437 // 5% chance of redish
438 color = SGVec4f(0.9f, 0.2f, 0.2f, bright - factor * 0.2f);
440 randomTileLights.insert(*j, color);
445 void computeRandomForest(SGMaterialLib* matlib)
447 SGMaterialTriangleMap::iterator i;
449 // generate a repeatable random seed
451 mt_init(&seed, unsigned(586));
453 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
454 SGMaterial *mat = matlib->find(i->first);
458 float wood_coverage = mat->get_wood_coverage();
459 if (wood_coverage <= 0)
462 // Attributes that don't vary by tree
463 randomForest.texture = mat->get_tree_texture();
464 randomForest.range = mat->get_tree_range();
465 randomForest.width = mat->get_tree_width();
466 randomForest.height = mat->get_tree_height();
467 randomForest.texture_varieties = mat->get_tree_varieties();
469 std::vector<SGVec3f> randomPoints;
470 i->second.addRandomTreePoints(wood_coverage,
471 mat->get_tree_density(),
472 mat->get_wood_size(),
475 std::vector<SGVec3f>::iterator j;
476 for (j = randomPoints.begin(); j != randomPoints.end(); ++j) {
477 randomForest.insert(*j);
482 void computeRandomObjects(SGMaterialLib* matlib)
484 SGMaterialTriangleMap::iterator i;
486 // generate a repeatable random seed
488 mt_init(&seed, unsigned(123));
490 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
491 SGMaterial *mat = matlib->find(i->first);
495 int group_count = mat->get_object_group_count();
499 for (int j = 0; j < group_count; j++)
501 SGMatModelGroup *object_group = mat->get_object_group(j);
502 int nObjects = object_group->get_object_count();
506 // For each of the random models in the group, determine an appropriate
507 // number of random placements and insert them.
508 for (int k = 0; k < nObjects; k++) {
509 SGMatModel * object = object_group->get_object(k);
511 std::vector<SGVec3f> randomPoints;
513 i->second.addRandomPoints(object->get_coverage_m2(), randomPoints);
514 std::vector<SGVec3f>::iterator l;
515 for (l = randomPoints.begin(); l != randomPoints.end(); ++l) {
516 randomModels.insert(*l, object, (int)object->get_randomized_range_m(&seed));
525 bool insertBinObj(const SGBinObject& obj, SGMaterialLib* matlib)
527 if (!insertPtGeometry(obj, matlib))
529 if (!insertSurfaceGeometry(obj, matlib))
535 typedef std::pair<osg::Node*, int> ModelLOD;
536 struct MakeQuadLeaf {
537 osg::LOD* operator() () const { return new osg::LOD; }
540 void operator() (osg::LOD* leaf, ModelLOD& mlod) const
542 leaf->addChild(mlod.first, 0, mlod.second);
545 struct GetModelLODCoord {
546 GetModelLODCoord() {}
547 GetModelLODCoord(const GetModelLODCoord& rhs)
549 osg::Vec3 operator() (const ModelLOD& mlod) const
551 return mlod.first->getBound().center();
555 typedef QuadTreeBuilder<osg::LOD*, ModelLOD, MakeQuadLeaf, AddModelLOD,
556 GetModelLODCoord> RandomObjectsQuadtree;
559 SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool use_random_objects, bool use_random_vegetation)
562 if (!tile.read_bin(path))
565 SGVec3d center = tile.get_gbs_center2();
566 SGGeod geodPos = SGGeod::fromCart(center);
567 SGQuatd hlOr = SGQuatd::fromLonLat(geodPos)*SGQuatd::fromEulerDeg(0, 0, 180);
569 // rotate the tiles so that the bounding boxes get nearly axis aligned.
570 // this will help the collision tree's bounding boxes a bit ...
571 std::vector<SGVec3d> nodes = tile.get_wgs84_nodes();
572 for (unsigned i = 0; i < nodes.size(); ++i)
573 nodes[i] = hlOr.transform(nodes[i]);
574 tile.set_wgs84_nodes(nodes);
576 SGQuatf hlOrf(hlOr[0], hlOr[1], hlOr[2], hlOr[3]);
577 std::vector<SGVec3f> normals = tile.get_normals();
578 for (unsigned i = 0; i < normals.size(); ++i)
579 normals[i] = hlOrf.transform(normals[i]);
580 tile.set_normals(normals);
582 SGTileGeometryBin tileGeometryBin;
583 if (!tileGeometryBin.insertBinObj(tile, matlib))
587 GroundLightManager* lightManager = GroundLightManager::instance();
589 osg::ref_ptr<osg::Group> lightGroup = new SGOffsetTransform(0.94);
590 osg::ref_ptr<osg::Group> randomObjects;
591 osg::ref_ptr<osg::Group> randomForest;
592 osg::Group* terrainGroup = new osg::Group;
594 osg::Node* node = tileGeometryBin.getSurfaceGeometry(matlib);
596 terrainGroup->addChild(node);
598 if (use_random_objects || use_random_vegetation) {
599 if (use_random_objects) {
601 tileGeometryBin.computeRandomObjects(matlib);
603 if (tileGeometryBin.randomModels.getNumModels() > 0) {
604 // Generate a repeatable random seed
606 mt_init(&seed, unsigned(123));
608 std::vector<ModelLOD> models;
609 for (unsigned int i = 0;
610 i < tileGeometryBin.randomModels.getNumModels(); i++) {
611 SGMatModelBin::MatModel obj
612 = tileGeometryBin.randomModels.getMatModel(i);
613 osg::Node* node = sgGetRandomModel(obj.model, seed);
615 // Create a matrix to place the object in the correct
616 // location, and then apply the rotation matrix created
617 // above, with an additional random heading rotation if appropriate.
618 osg::Matrix transformMat;
619 transformMat = osg::Matrix::translate(toOsg(obj.position));
620 if (obj.model->get_heading_type() == SGMatModel::HEADING_RANDOM) {
621 // Rotate the object around the z axis.
622 double hdg = mt_rand(&seed) * M_PI * 2;
623 transformMat.preMult(osg::Matrix::rotate(hdg,
624 osg::Vec3d(0.0, 0.0, 1.0)));
626 osg::MatrixTransform* position =
627 new osg::MatrixTransform(transformMat);
628 position->addChild(node);
629 models.push_back(ModelLOD(position, obj.lod));
631 RandomObjectsQuadtree quadtree((GetModelLODCoord()), (AddModelLOD()));
632 quadtree.buildQuadTree(models.begin(), models.end());
633 randomObjects = quadtree.getRoot();
634 randomObjects->setName("random objects");
638 if (use_random_vegetation && matlib) {
639 // Now add some random forest.
640 tileGeometryBin.computeRandomForest(matlib);
642 if (tileGeometryBin.randomForest.getNumTrees() > 0) {
643 randomForest = createForest(tileGeometryBin.randomForest,
644 osg::Matrix::identity());
645 randomForest->setName("random trees");
651 // FIXME: ugly, has a side effect
653 tileGeometryBin.computeRandomSurfaceLights(matlib);
655 if (tileGeometryBin.tileLights.getNumLights() > 0
656 || tileGeometryBin.randomTileLights.getNumLights() > 0) {
657 osg::Group* groundLights0 = new osg::Group;
658 groundLights0->setStateSet(lightManager->getGroundLightStateSet());
659 groundLights0->setNodeMask(GROUNDLIGHTS0_BIT);
660 osg::Geode* geode = new osg::Geode;
661 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.tileLights));
662 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 4, -0.3f));
663 groundLights0->addChild(geode);
664 lightGroup->addChild(groundLights0);
666 if (tileGeometryBin.randomTileLights.getNumLights() > 0) {
667 osg::Group* groundLights1 = new osg::Group;
668 groundLights1->setStateSet(lightManager->getGroundLightStateSet());
669 groundLights1->setNodeMask(GROUNDLIGHTS1_BIT);
670 osg::Group* groundLights2 = new osg::Group;
671 groundLights2->setStateSet(lightManager->getGroundLightStateSet());
672 groundLights2->setNodeMask(GROUNDLIGHTS2_BIT);
673 osg::Geode* geode = new osg::Geode;
674 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 2, -0.15f));
675 groundLights1->addChild(geode);
676 lightGroup->addChild(groundLights1);
677 geode = new osg::Geode;
678 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights));
679 groundLights2->addChild(geode);
680 lightGroup->addChild(groundLights2);
684 if (!tileGeometryBin.vasiLights.empty()) {
685 EffectGeode* vasiGeode = new EffectGeode;
687 = getLightEffect(6, osg::Vec3(1, 0.0001, 0.000001), 1, 6, true);
688 vasiGeode->setEffect(vasiEffect);
689 SGVec4f red(1, 0, 0, 1);
692 mat = matlib->find("RWY_RED_LIGHTS");
694 red = mat->get_light_color();
695 SGVec4f white(1, 1, 1, 1);
698 mat = matlib->find("RWY_WHITE_LIGHTS");
700 white = mat->get_light_color();
701 SGDirectionalLightListBin::const_iterator i;
702 for (i = tileGeometryBin.vasiLights.begin();
703 i != tileGeometryBin.vasiLights.end(); ++i) {
704 vasiGeode->addDrawable(SGLightFactory::getVasi(up, *i, red, white));
706 vasiGeode->setStateSet(lightManager->getRunwayLightStateSet());
707 lightGroup->addChild(vasiGeode);
709 Effect* runwayEffect = 0;
710 if (tileGeometryBin.runwayLights.getNumLights() > 0
711 || !tileGeometryBin.rabitLights.empty()
712 || !tileGeometryBin.reilLights.empty()
713 || !tileGeometryBin.odalLights.empty()
714 || tileGeometryBin.taxiLights.getNumLights() > 0)
715 runwayEffect = getLightEffect(4, osg::Vec3(1, 0.001, 0.0002), 1, 4, true);
716 if (tileGeometryBin.runwayLights.getNumLights() > 0
717 || !tileGeometryBin.rabitLights.empty()
718 || !tileGeometryBin.reilLights.empty()
719 || !tileGeometryBin.odalLights.empty()) {
720 osg::Group* rwyLights = new osg::Group;
721 rwyLights->setStateSet(lightManager->getRunwayLightStateSet());
722 rwyLights->setNodeMask(RUNWAYLIGHTS_BIT);
723 if (tileGeometryBin.runwayLights.getNumLights() != 0) {
724 EffectGeode* geode = new EffectGeode;
725 geode->setEffect(runwayEffect);
726 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin
728 rwyLights->addChild(geode);
730 SGDirectionalLightListBin::const_iterator i;
731 for (i = tileGeometryBin.rabitLights.begin();
732 i != tileGeometryBin.rabitLights.end(); ++i) {
733 rwyLights->addChild(SGLightFactory::getSequenced(*i));
735 for (i = tileGeometryBin.reilLights.begin();
736 i != tileGeometryBin.reilLights.end(); ++i) {
737 rwyLights->addChild(SGLightFactory::getSequenced(*i));
739 SGLightListBin::const_iterator j;
740 for (j = tileGeometryBin.odalLights.begin();
741 j != tileGeometryBin.odalLights.end(); ++j) {
742 rwyLights->addChild(SGLightFactory::getOdal(*j));
744 lightGroup->addChild(rwyLights);
747 if (tileGeometryBin.taxiLights.getNumLights() > 0) {
748 osg::Group* taxiLights = new osg::Group;
749 taxiLights->setStateSet(lightManager->getTaxiLightStateSet());
750 taxiLights->setNodeMask(RUNWAYLIGHTS_BIT);
751 EffectGeode* geode = new EffectGeode;
752 geode->setEffect(runwayEffect);
753 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.taxiLights));
754 taxiLights->addChild(geode);
755 lightGroup->addChild(taxiLights);
758 // The toplevel transform for that tile.
759 osg::MatrixTransform* transform = new osg::MatrixTransform;
760 transform->setName(path);
761 transform->setMatrix(osg::Matrix::rotate(toOsg(hlOr))*
762 osg::Matrix::translate(toOsg(center)));
763 transform->addChild(terrainGroup);
764 if (lightGroup->getNumChildren() > 0) {
765 osg::LOD* lightLOD = new osg::LOD;
766 lightLOD->addChild(lightGroup.get(), 0, 30000);
767 // VASI is always on, so doesn't use light bits.
768 lightLOD->setNodeMask(LIGHTS_BITS | MODEL_BIT);
769 transform->addChild(lightLOD);
772 if (randomObjects.valid() || randomForest.valid()) {
774 // Add a LoD node, so we don't try to display anything when the tile center
775 // is more than 20km away.
776 osg::LOD* objectLOD = new osg::LOD;
778 if (randomObjects.valid()) objectLOD->addChild(randomObjects.get(), 0, 20000);
779 if (randomForest.valid()) objectLOD->addChild(randomForest.get(), 0, 20000);
781 unsigned nodeMask = SG_NODEMASK_CASTSHADOW_BIT | SG_NODEMASK_RECIEVESHADOW_BIT | SG_NODEMASK_TERRAIN_BIT;
782 objectLOD->setNodeMask(nodeMask);
783 transform->addChild(objectLOD);