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) {
478 // Apply a random scaling factor and texture index.
479 float scale = (mt_rand(&seed) + mt_rand(&seed)) / 2.0f + 0.5f;
480 int v = (int) (mt_rand(&seed) * mat->get_tree_varieties());
481 if (v == mat->get_tree_varieties()) v--;
482 randomForest.insert(*j, v, scale);
487 void computeRandomObjects(SGMaterialLib* matlib)
489 SGMaterialTriangleMap::iterator i;
491 // generate a repeatable random seed
493 mt_init(&seed, unsigned(123));
495 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
496 SGMaterial *mat = matlib->find(i->first);
500 int group_count = mat->get_object_group_count();
504 for (int j = 0; j < group_count; j++)
506 SGMatModelGroup *object_group = mat->get_object_group(j);
507 int nObjects = object_group->get_object_count();
511 // For each of the random models in the group, determine an appropriate
512 // number of random placements and insert them.
513 for (int k = 0; k < nObjects; k++) {
514 SGMatModel * object = object_group->get_object(k);
516 std::vector<SGVec3f> randomPoints;
518 i->second.addRandomPoints(object->get_coverage_m2(), randomPoints);
519 std::vector<SGVec3f>::iterator l;
520 for (l = randomPoints.begin(); l != randomPoints.end(); ++l) {
521 randomModels.insert(*l, object, (int)object->get_randomized_range_m(&seed));
530 bool insertBinObj(const SGBinObject& obj, SGMaterialLib* matlib)
532 if (!insertPtGeometry(obj, matlib))
534 if (!insertSurfaceGeometry(obj, matlib))
540 typedef std::pair<osg::Node*, int> ModelLOD;
541 struct MakeQuadLeaf {
542 osg::LOD* operator() () const { return new osg::LOD; }
545 void operator() (osg::LOD* leaf, ModelLOD& mlod) const
547 leaf->addChild(mlod.first, 0, mlod.second);
550 struct GetModelLODCoord {
551 GetModelLODCoord() {}
552 GetModelLODCoord(const GetModelLODCoord& rhs)
554 osg::Vec3 operator() (const ModelLOD& mlod) const
556 return mlod.first->getBound().center();
560 typedef QuadTreeBuilder<osg::LOD*, ModelLOD, MakeQuadLeaf, AddModelLOD,
561 GetModelLODCoord> RandomObjectsQuadtree;
564 SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool use_random_objects, bool use_random_vegetation)
567 if (!tile.read_bin(path))
570 SGVec3d center = tile.get_gbs_center2();
571 SGGeod geodPos = SGGeod::fromCart(center);
572 SGQuatd hlOr = SGQuatd::fromLonLat(geodPos)*SGQuatd::fromEulerDeg(0, 0, 180);
574 // rotate the tiles so that the bounding boxes get nearly axis aligned.
575 // this will help the collision tree's bounding boxes a bit ...
576 std::vector<SGVec3d> nodes = tile.get_wgs84_nodes();
577 for (unsigned i = 0; i < nodes.size(); ++i)
578 nodes[i] = hlOr.transform(nodes[i]);
579 tile.set_wgs84_nodes(nodes);
581 SGQuatf hlOrf(hlOr[0], hlOr[1], hlOr[2], hlOr[3]);
582 std::vector<SGVec3f> normals = tile.get_normals();
583 for (unsigned i = 0; i < normals.size(); ++i)
584 normals[i] = hlOrf.transform(normals[i]);
585 tile.set_normals(normals);
587 SGTileGeometryBin tileGeometryBin;
588 if (!tileGeometryBin.insertBinObj(tile, matlib))
592 GroundLightManager* lightManager = GroundLightManager::instance();
594 osg::ref_ptr<osg::Group> lightGroup = new SGOffsetTransform(0.94);
595 osg::ref_ptr<osg::Group> randomObjects;
596 osg::ref_ptr<osg::Group> randomForest;
597 osg::Group* terrainGroup = new osg::Group;
599 osg::Node* node = tileGeometryBin.getSurfaceGeometry(matlib);
601 terrainGroup->addChild(node);
603 if (use_random_objects || use_random_vegetation) {
604 if (use_random_objects) {
606 tileGeometryBin.computeRandomObjects(matlib);
608 if (tileGeometryBin.randomModels.getNumModels() > 0) {
609 // Generate a repeatable random seed
611 mt_init(&seed, unsigned(123));
613 std::vector<ModelLOD> models;
614 for (unsigned int i = 0;
615 i < tileGeometryBin.randomModels.getNumModels(); i++) {
616 SGMatModelBin::MatModel obj
617 = tileGeometryBin.randomModels.getMatModel(i);
618 osg::Node* node = sgGetRandomModel(obj.model);
620 // Create a matrix to place the object in the correct
621 // location, and then apply the rotation matrix created
622 // above, with an additional random heading rotation if appropriate.
623 osg::Matrix transformMat;
624 transformMat = osg::Matrix::translate(obj.position.osg());
625 if (obj.model->get_heading_type() == SGMatModel::HEADING_RANDOM) {
626 // Rotate the object around the z axis.
627 double hdg = mt_rand(&seed) * M_PI * 2;
628 transformMat.preMult(osg::Matrix::rotate(hdg,
629 osg::Vec3d(0.0, 0.0, 1.0)));
631 osg::MatrixTransform* position =
632 new osg::MatrixTransform(transformMat);
633 position->addChild(node);
634 models.push_back(ModelLOD(position, obj.lod));
636 RandomObjectsQuadtree quadtree((GetModelLODCoord()), (AddModelLOD()));
637 quadtree.buildQuadTree(models.begin(), models.end());
638 randomObjects = quadtree.getRoot();
639 randomObjects->setName("random objects");
643 if (use_random_vegetation && matlib) {
644 // Now add some random forest.
645 tileGeometryBin.computeRandomForest(matlib);
647 if (tileGeometryBin.randomForest.getNumTrees() > 0) {
648 randomForest = createForest(tileGeometryBin.randomForest,
649 osg::Matrix::identity());
650 randomForest->setName("random trees");
656 // FIXME: ugly, has a side effect
658 tileGeometryBin.computeRandomSurfaceLights(matlib);
660 if (tileGeometryBin.tileLights.getNumLights() > 0
661 || tileGeometryBin.randomTileLights.getNumLights() > 0) {
662 osg::Group* groundLights0 = new osg::Group;
663 groundLights0->setStateSet(lightManager->getGroundLightStateSet());
664 groundLights0->setNodeMask(GROUNDLIGHTS0_BIT);
665 osg::Geode* geode = new osg::Geode;
666 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.tileLights));
667 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 4, -0.3f));
668 groundLights0->addChild(geode);
669 lightGroup->addChild(groundLights0);
671 if (tileGeometryBin.randomTileLights.getNumLights() > 0) {
672 osg::Group* groundLights1 = new osg::Group;
673 groundLights1->setStateSet(lightManager->getGroundLightStateSet());
674 groundLights1->setNodeMask(GROUNDLIGHTS1_BIT);
675 osg::Group* groundLights2 = new osg::Group;
676 groundLights2->setStateSet(lightManager->getGroundLightStateSet());
677 groundLights2->setNodeMask(GROUNDLIGHTS2_BIT);
678 osg::Geode* geode = new osg::Geode;
679 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 2, -0.15f));
680 groundLights1->addChild(geode);
681 lightGroup->addChild(groundLights1);
682 geode = new osg::Geode;
683 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights));
684 groundLights2->addChild(geode);
685 lightGroup->addChild(groundLights2);
689 if (!tileGeometryBin.vasiLights.empty()) {
690 EffectGeode* vasiGeode = new EffectGeode;
692 = getLightEffect(6, osg::Vec3(1, 0.0001, 0.000001), 1, 6, true);
693 vasiGeode->setEffect(vasiEffect);
694 SGVec4f red(1, 0, 0, 1);
697 mat = matlib->find("RWY_RED_LIGHTS");
699 red = mat->get_light_color();
700 SGVec4f white(1, 1, 1, 1);
703 mat = matlib->find("RWY_WHITE_LIGHTS");
705 white = mat->get_light_color();
706 SGDirectionalLightListBin::const_iterator i;
707 for (i = tileGeometryBin.vasiLights.begin();
708 i != tileGeometryBin.vasiLights.end(); ++i) {
709 vasiGeode->addDrawable(SGLightFactory::getVasi(up, *i, red, white));
711 vasiGeode->setStateSet(lightManager->getRunwayLightStateSet());
712 lightGroup->addChild(vasiGeode);
714 Effect* runwayEffect = 0;
715 if (tileGeometryBin.runwayLights.getNumLights() > 0
716 || !tileGeometryBin.rabitLights.empty()
717 || !tileGeometryBin.reilLights.empty()
718 || !tileGeometryBin.odalLights.empty()
719 || tileGeometryBin.taxiLights.getNumLights() > 0)
720 runwayEffect = getLightEffect(4, osg::Vec3(1, 0.001, 0.0002), 1, 4, true);
721 if (tileGeometryBin.runwayLights.getNumLights() > 0
722 || !tileGeometryBin.rabitLights.empty()
723 || !tileGeometryBin.reilLights.empty()
724 || !tileGeometryBin.odalLights.empty()) {
725 osg::Group* rwyLights = new osg::Group;
726 rwyLights->setStateSet(lightManager->getRunwayLightStateSet());
727 rwyLights->setNodeMask(RUNWAYLIGHTS_BIT);
728 if (tileGeometryBin.runwayLights.getNumLights() != 0) {
729 EffectGeode* geode = new EffectGeode;
730 geode->setEffect(runwayEffect);
731 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin
733 rwyLights->addChild(geode);
735 SGDirectionalLightListBin::const_iterator i;
736 for (i = tileGeometryBin.rabitLights.begin();
737 i != tileGeometryBin.rabitLights.end(); ++i) {
738 rwyLights->addChild(SGLightFactory::getSequenced(*i));
740 for (i = tileGeometryBin.reilLights.begin();
741 i != tileGeometryBin.reilLights.end(); ++i) {
742 rwyLights->addChild(SGLightFactory::getSequenced(*i));
744 SGLightListBin::const_iterator j;
745 for (j = tileGeometryBin.odalLights.begin();
746 j != tileGeometryBin.odalLights.end(); ++j) {
747 rwyLights->addChild(SGLightFactory::getOdal(*j));
749 lightGroup->addChild(rwyLights);
752 if (tileGeometryBin.taxiLights.getNumLights() > 0) {
753 osg::Group* taxiLights = new osg::Group;
754 taxiLights->setStateSet(lightManager->getTaxiLightStateSet());
755 taxiLights->setNodeMask(RUNWAYLIGHTS_BIT);
756 EffectGeode* geode = new EffectGeode;
757 geode->setEffect(runwayEffect);
758 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.taxiLights));
759 taxiLights->addChild(geode);
760 lightGroup->addChild(taxiLights);
763 // The toplevel transform for that tile.
764 osg::MatrixTransform* transform = new osg::MatrixTransform;
765 transform->setName(path);
766 transform->setMatrix(osg::Matrix::rotate(hlOr.osg())*
767 osg::Matrix::translate(center.osg()));
768 transform->addChild(terrainGroup);
769 if (lightGroup->getNumChildren() > 0) {
770 osg::LOD* lightLOD = new osg::LOD;
771 lightLOD->addChild(lightGroup.get(), 0, 30000);
772 // VASI is always on, so doesn't use light bits.
773 lightLOD->setNodeMask(LIGHTS_BITS | MODEL_BIT);
774 transform->addChild(lightLOD);
777 if (randomObjects.valid() || randomForest.valid()) {
779 // Add a LoD node, so we don't try to display anything when the tile center
780 // is more than 20km away.
781 osg::LOD* objectLOD = new osg::LOD;
783 if (randomObjects.valid()) objectLOD->addChild(randomObjects.get(), 0, 20000);
784 if (randomForest.valid()) objectLOD->addChild(randomForest.get(), 0, 20000);
786 unsigned nodeMask = SG_NODEMASK_CASTSHADOW_BIT | SG_NODEMASK_RECIEVESHADOW_BIT | SG_NODEMASK_TERRAIN_BIT;
787 objectLOD->setNodeMask(nodeMask);
788 transform->addChild(objectLOD);