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 <boost/foreach.hpp>
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>
58 #include "SGTexturedTriangleBin.hxx"
59 #include "SGLightBin.hxx"
60 #include "SGModelBin.hxx"
61 #include "TreeBin.hxx"
62 #include "SGDirectionalLightBin.hxx"
63 #include "GroundLightManager.hxx"
66 #include "userdata.hxx"
67 #include "pt_lights.hxx"
69 using namespace simgear;
71 typedef std::map<std::string,SGTexturedTriangleBin> SGMaterialTriangleMap;
72 typedef std::list<SGLightBin> SGLightListBin;
73 typedef std::list<SGDirectionalLightBin> SGDirectionalLightListBin;
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;
89 getMaterialLightColor(const SGMaterial* material)
92 return SGVec4f(1, 1, 1, 0.8);
93 return material->get_light_color();
97 addPointGeometry(SGLightBin& lights,
98 const std::vector<SGVec3d>& vertices,
100 const int_list& pts_v)
102 for (unsigned i = 0; i < pts_v.size(); ++i)
103 lights.insert(toVec3f(vertices[pts_v[i]]), color);
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)
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);
120 for (unsigned i = 0; i < pts_v.size(); ++i)
121 lights.insert(toVec3f(vertices[pts_v[i]]), normals[pts_v[i]], color);
126 insertPtGeometry(const SGBinObject& obj, SGMaterialLib* matlib)
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!");
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;
138 material = matlib->find(materialName);
139 SGVec4f color = getMaterialLightColor(material);
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]);
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]);
181 getTexCoord(const std::vector<SGVec2f>& texCoords, const int_list& tc,
182 const SGVec2f& tcScale, unsigned i)
186 else if (tc.size() == 1)
187 return mult(texCoords[tc[0]], tcScale);
189 return mult(texCoords[tc[i]], tcScale);
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)
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);
211 for (unsigned i = 2; i < tris_v.size(); i += 3) {
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);
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);
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);
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)
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);
247 for (unsigned i = 2; i < strips_v.size(); ++i) {
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);
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);
257 v2.vertex = toVec3f(vertices[strips_v[i]]);
258 v2.normal = normals[strips_n[i]];
259 v2.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i);
261 triangles.insert(v1, v0, v2);
263 triangles.insert(v0, v1, v2);
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)
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);
287 v0.vertex = toVec3f(vertices[fans_v[0]]);
288 v0.normal = normals[fans_n[0]];
289 v0.texCoord = getTexCoord(texCoords, fans_tc, tcScale, 0);
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) {
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);
304 SGVec2f getTexCoordScale(const std::string& name, SGMaterialLib* matlib)
307 return SGVec2f(1, 1);
308 SGMaterial* material = matlib->find(name);
310 return SGVec2f(1, 1);
312 return material->get_tex_coord_scale();
316 insertSurfaceGeometry(const SGBinObject& obj, SGMaterialLib* matlib)
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!");
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],
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!");
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],
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!");
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],
369 osg::Node* getSurfaceGeometry(SGMaterialLib* matlib) const
371 if (materialTriangleMap.empty())
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();
382 mat = matlib->find(i->first);
383 eg = new EffectGeode;
385 eg->setEffect(mat->get_effect());
386 eg->addDrawable(geometry);
387 eg->runGenerators(geometry); // Generate extra data needed by effect
397 void computeRandomSurfaceLights(SGMaterialLib* matlib)
399 SGMaterialTriangleMap::iterator i;
401 // generate a repeatable random seed
403 mt_init(&seed, unsigned(123));
405 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
406 SGMaterial *mat = matlib->find(i->first);
410 float coverage = mat->get_light_coverage();
413 if (coverage < 10000.0) {
414 SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
415 << coverage << ", pushing up to 10000");
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);
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);
440 // 5% chance of redish
441 color = SGVec4f(0.9f, 0.2f, 0.2f, bright - factor * 0.2f);
443 randomTileLights.insert(*j, color);
448 void computeRandomForest(SGMaterialLib* matlib)
450 SGMaterialTriangleMap::iterator i;
452 // generate a repeatable random seed
454 mt_init(&seed, unsigned(586));
456 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
457 SGMaterial *mat = matlib->find(i->first);
461 float wood_coverage = mat->get_wood_coverage();
462 if (wood_coverage <= 0)
465 // Attributes that don't vary by tree but do vary by material
469 BOOST_FOREACH(bin, randomForest)
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() ) ) {
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);
492 std::vector<SGVec3f> randomPoints;
493 i->second.addRandomTreePoints(wood_coverage,
494 mat->get_tree_density(),
495 mat->get_wood_size(),
498 std::vector<SGVec3f>::iterator k;
499 for (k = randomPoints.begin(); k != randomPoints.end(); ++k) {
505 void computeRandomObjects(SGMaterialLib* matlib)
507 SGMaterialTriangleMap::iterator i;
509 // generate a repeatable random seed
511 mt_init(&seed, unsigned(123));
513 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
514 SGMaterial *mat = matlib->find(i->first);
518 int group_count = mat->get_object_group_count();
522 for (int j = 0; j < group_count; j++)
524 SGMatModelGroup *object_group = mat->get_object_group(j);
525 int nObjects = object_group->get_object_count();
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);
534 std::vector<SGVec3f> randomPoints;
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));
548 bool insertBinObj(const SGBinObject& obj, SGMaterialLib* matlib)
550 if (!insertPtGeometry(obj, matlib))
552 if (!insertSurfaceGeometry(obj, matlib))
558 typedef std::pair<osg::Node*, int> ModelLOD;
559 struct MakeQuadLeaf {
560 osg::LOD* operator() () const { return new osg::LOD; }
563 void operator() (osg::LOD* leaf, ModelLOD& mlod) const
565 leaf->addChild(mlod.first, 0, mlod.second);
568 struct GetModelLODCoord {
569 GetModelLODCoord() {}
570 GetModelLODCoord(const GetModelLODCoord& rhs)
572 osg::Vec3 operator() (const ModelLOD& mlod) const
574 return mlod.first->getBound().center();
578 typedef QuadTreeBuilder<osg::LOD*, ModelLOD, MakeQuadLeaf, AddModelLOD,
579 GetModelLODCoord> RandomObjectsQuadtree;
582 SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool use_random_objects, bool use_random_vegetation)
585 if (!tile.read_bin(path))
588 SGVec3d center = tile.get_gbs_center();
589 SGGeod geodPos = SGGeod::fromCart(center);
590 SGQuatd hlOr = SGQuatd::fromLonLat(geodPos)*SGQuatd::fromEulerDeg(0, 0, 180);
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);
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);
605 SGTileGeometryBin tileGeometryBin;
606 if (!tileGeometryBin.insertBinObj(tile, matlib))
610 GroundLightManager* lightManager = GroundLightManager::instance();
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;
617 osg::Node* node = tileGeometryBin.getSurfaceGeometry(matlib);
619 terrainGroup->addChild(node);
621 if (use_random_objects || use_random_vegetation) {
622 if (use_random_objects) {
624 tileGeometryBin.computeRandomObjects(matlib);
626 if (tileGeometryBin.randomModels.getNumModels() > 0) {
627 // Generate a repeatable random seed
629 mt_init(&seed, unsigned(123));
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);
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)));
649 osg::MatrixTransform* position =
650 new osg::MatrixTransform(transformMat);
651 position->addChild(node);
652 models.push_back(ModelLOD(position, obj.lod));
654 RandomObjectsQuadtree quadtree((GetModelLODCoord()), (AddModelLOD()));
655 quadtree.buildQuadTree(models.begin(), models.end());
656 randomObjects = quadtree.getRoot();
657 randomObjects->setName("random objects");
661 if (use_random_vegetation && matlib) {
662 // Now add some random forest.
663 tileGeometryBin.computeRandomForest(matlib);
665 if (tileGeometryBin.randomForest.size() > 0) {
666 forestNode = createForest(tileGeometryBin.randomForest, osg::Matrix::identity());
667 forestNode->setName("Random trees");
673 // FIXME: ugly, has a side effect
675 tileGeometryBin.computeRandomSurfaceLights(matlib);
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);
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);
706 if (!tileGeometryBin.vasiLights.empty()) {
707 EffectGeode* vasiGeode = new EffectGeode;
709 = getLightEffect(6, osg::Vec3(1, 0.0001, 0.000001), 1, 6, true);
710 vasiGeode->setEffect(vasiEffect);
711 SGVec4f red(1, 0, 0, 1);
714 mat = matlib->find("RWY_RED_LIGHTS");
716 red = mat->get_light_color();
717 SGVec4f white(1, 1, 1, 1);
720 mat = matlib->find("RWY_WHITE_LIGHTS");
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));
728 vasiGeode->setStateSet(lightManager->getRunwayLightStateSet());
729 lightGroup->addChild(vasiGeode);
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
750 rwyLights->addChild(geode);
752 SGDirectionalLightListBin::const_iterator i;
753 for (i = tileGeometryBin.rabitLights.begin();
754 i != tileGeometryBin.rabitLights.end(); ++i) {
755 rwyLights->addChild(SGLightFactory::getSequenced(*i));
757 for (i = tileGeometryBin.reilLights.begin();
758 i != tileGeometryBin.reilLights.end(); ++i) {
759 rwyLights->addChild(SGLightFactory::getSequenced(*i));
761 SGLightListBin::const_iterator j;
762 for (j = tileGeometryBin.odalLights.begin();
763 j != tileGeometryBin.odalLights.end(); ++j) {
764 rwyLights->addChild(SGLightFactory::getOdal(*j));
766 lightGroup->addChild(rwyLights);
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);
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);
794 if (randomObjects.valid() || forestNode.valid()) {
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;
800 if (randomObjects.valid()) objectLOD->addChild(randomObjects.get(), 0, 20000);
801 if (forestNode.valid()) objectLOD->addChild(forestNode.get(), 0, 20000);
803 unsigned nodeMask = SG_NODEMASK_CASTSHADOW_BIT | SG_NODEMASK_RECIEVESHADOW_BIT | SG_NODEMASK_TERRAIN_BIT;
804 objectLOD->setNodeMask(nodeMask);
805 transform->addChild(objectLOD);