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/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/scene/util/QuadTreeBuilder.hxx>
54 #include "SGTexturedTriangleBin.hxx"
55 #include "SGLightBin.hxx"
56 #include "SGModelBin.hxx"
57 #include "TreeBin.hxx"
58 #include "SGDirectionalLightBin.hxx"
59 #include "GroundLightManager.hxx"
62 #include "userdata.hxx"
63 #include "pt_lights.hxx"
65 using namespace simgear;
67 typedef std::map<std::string,SGTexturedTriangleBin> SGMaterialTriangleMap;
68 typedef std::list<SGLightBin> SGLightListBin;
69 typedef std::list<SGDirectionalLightBin> SGDirectionalLightListBin;
71 struct SGTileGeometryBin {
72 SGMaterialTriangleMap materialTriangleMap;
73 SGLightBin tileLights;
74 SGLightBin randomTileLights;
76 SGDirectionalLightBin runwayLights;
77 SGDirectionalLightBin taxiLights;
78 SGDirectionalLightListBin vasiLights;
79 SGDirectionalLightListBin rabitLights;
80 SGLightListBin odalLights;
81 SGDirectionalLightListBin reilLights;
82 SGMatModelBin randomModels;
85 getMaterialLightColor(const SGMaterial* material)
88 return SGVec4f(1, 1, 1, 0.8);
89 return material->get_light_color();
93 addPointGeometry(SGLightBin& lights,
94 const std::vector<SGVec3d>& vertices,
96 const int_list& pts_v)
98 for (unsigned i = 0; i < pts_v.size(); ++i)
99 lights.insert(toVec3f(vertices[pts_v[i]]), color);
103 addPointGeometry(SGDirectionalLightBin& lights,
104 const std::vector<SGVec3d>& vertices,
105 const std::vector<SGVec3f>& normals,
106 const SGVec4f& color,
107 const int_list& pts_v,
108 const int_list& pts_n)
110 // If the normal indices match the vertex indices, use seperate
111 // normal indices. Else reuse the vertex indices for the normals.
112 if (pts_v.size() == pts_n.size()) {
113 for (unsigned i = 0; i < pts_v.size(); ++i)
114 lights.insert(toVec3f(vertices[pts_v[i]]), normals[pts_n[i]], color);
116 for (unsigned i = 0; i < pts_v.size(); ++i)
117 lights.insert(toVec3f(vertices[pts_v[i]]), normals[pts_v[i]], color);
122 insertPtGeometry(const SGBinObject& obj, SGMaterialLib* matlib)
124 if (obj.get_pts_v().size() != obj.get_pts_n().size()) {
125 SG_LOG(SG_TERRAIN, SG_ALERT,
126 "Group list sizes for points do not match!");
130 for (unsigned grp = 0; grp < obj.get_pts_v().size(); ++grp) {
131 std::string materialName = obj.get_pt_materials()[grp];
132 SGMaterial* material = matlib->find(materialName);
133 SGVec4f color = getMaterialLightColor(material);
135 if (3 <= materialName.size() && materialName.substr(0, 3) != "RWY") {
136 // Just plain lights. Not something for the runway.
137 addPointGeometry(tileLights, obj.get_wgs84_nodes(), color,
138 obj.get_pts_v()[grp]);
139 } else if (materialName == "RWY_BLUE_TAXIWAY_LIGHTS"
140 || materialName == "RWY_GREEN_TAXIWAY_LIGHTS") {
141 addPointGeometry(taxiLights, obj.get_wgs84_nodes(), obj.get_normals(),
142 color, obj.get_pts_v()[grp], obj.get_pts_n()[grp]);
143 } else if (materialName == "RWY_VASI_LIGHTS") {
144 vasiLights.push_back(SGDirectionalLightBin());
145 addPointGeometry(vasiLights.back(), obj.get_wgs84_nodes(),
146 obj.get_normals(), color, obj.get_pts_v()[grp],
147 obj.get_pts_n()[grp]);
148 } else if (materialName == "RWY_SEQUENCED_LIGHTS") {
149 rabitLights.push_back(SGDirectionalLightBin());
150 addPointGeometry(rabitLights.back(), obj.get_wgs84_nodes(),
151 obj.get_normals(), color, obj.get_pts_v()[grp],
152 obj.get_pts_n()[grp]);
153 } else if (materialName == "RWY_ODALS_LIGHTS") {
154 odalLights.push_back(SGLightBin());
155 addPointGeometry(odalLights.back(), obj.get_wgs84_nodes(),
156 color, obj.get_pts_v()[grp]);
157 } else if (materialName == "RWY_REIL_LIGHTS") {
158 reilLights.push_back(SGDirectionalLightBin());
159 addPointGeometry(reilLights.back(), obj.get_wgs84_nodes(),
160 obj.get_normals(), color, obj.get_pts_v()[grp],
161 obj.get_pts_n()[grp]);
163 // what is left must be runway lights
164 addPointGeometry(runwayLights, obj.get_wgs84_nodes(),
165 obj.get_normals(), color, obj.get_pts_v()[grp],
166 obj.get_pts_n()[grp]);
175 getTexCoord(const std::vector<SGVec2f>& texCoords, const int_list& tc,
176 const SGVec2f& tcScale, unsigned i)
180 else if (tc.size() == 1)
181 return mult(texCoords[tc[0]], tcScale);
183 return mult(texCoords[tc[i]], tcScale);
187 addTriangleGeometry(SGTexturedTriangleBin& triangles,
188 const std::vector<SGVec3d>& vertices,
189 const std::vector<SGVec3f>& normals,
190 const std::vector<SGVec2f>& texCoords,
191 const int_list& tris_v,
192 const int_list& tris_n,
193 const int_list& tris_tc,
194 const SGVec2f& tcScale)
196 if (tris_v.size() != tris_n.size()) {
197 // If the normal indices do not match, they should be inmplicitly
198 // the same than the vertex indices. So just call ourselves again
199 // with the matching index vector.
200 addTriangleGeometry(triangles, vertices, normals, texCoords,
201 tris_v, tris_v, tris_tc, tcScale);
205 for (unsigned i = 2; i < tris_v.size(); i += 3) {
207 v0.vertex = toVec3f(vertices[tris_v[i-2]]);
208 v0.normal = normals[tris_n[i-2]];
209 v0.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i-2);
211 v1.vertex = toVec3f(vertices[tris_v[i-1]]);
212 v1.normal = normals[tris_n[i-1]];
213 v1.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i-1);
215 v2.vertex = toVec3f(vertices[tris_v[i]]);
216 v2.normal = normals[tris_n[i]];
217 v2.texCoord = getTexCoord(texCoords, tris_tc, tcScale, i);
218 triangles.insert(v0, v1, v2);
223 addStripGeometry(SGTexturedTriangleBin& triangles,
224 const std::vector<SGVec3d>& vertices,
225 const std::vector<SGVec3f>& normals,
226 const std::vector<SGVec2f>& texCoords,
227 const int_list& strips_v,
228 const int_list& strips_n,
229 const int_list& strips_tc,
230 const SGVec2f& tcScale)
232 if (strips_v.size() != strips_n.size()) {
233 // If the normal indices do not match, they should be inmplicitly
234 // the same than the vertex indices. So just call ourselves again
235 // with the matching index vector.
236 addStripGeometry(triangles, vertices, normals, texCoords,
237 strips_v, strips_v, strips_tc, tcScale);
241 for (unsigned i = 2; i < strips_v.size(); ++i) {
243 v0.vertex = toVec3f(vertices[strips_v[i-2]]);
244 v0.normal = normals[strips_n[i-2]];
245 v0.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i-2);
247 v1.vertex = toVec3f(vertices[strips_v[i-1]]);
248 v1.normal = normals[strips_n[i-1]];
249 v1.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i-1);
251 v2.vertex = toVec3f(vertices[strips_v[i]]);
252 v2.normal = normals[strips_n[i]];
253 v2.texCoord = getTexCoord(texCoords, strips_tc, tcScale, i);
255 triangles.insert(v1, v0, v2);
257 triangles.insert(v0, v1, v2);
262 addFanGeometry(SGTexturedTriangleBin& triangles,
263 const std::vector<SGVec3d>& vertices,
264 const std::vector<SGVec3f>& normals,
265 const std::vector<SGVec2f>& texCoords,
266 const int_list& fans_v,
267 const int_list& fans_n,
268 const int_list& fans_tc,
269 const SGVec2f& tcScale)
271 if (fans_v.size() != fans_n.size()) {
272 // If the normal indices do not match, they should be implicitly
273 // the same than the vertex indices. So just call ourselves again
274 // with the matching index vector.
275 addFanGeometry(triangles, vertices, normals, texCoords,
276 fans_v, fans_v, fans_tc, tcScale);
281 v0.vertex = toVec3f(vertices[fans_v[0]]);
282 v0.normal = normals[fans_n[0]];
283 v0.texCoord = getTexCoord(texCoords, fans_tc, tcScale, 0);
285 v1.vertex = toVec3f(vertices[fans_v[1]]);
286 v1.normal = normals[fans_n[1]];
287 v1.texCoord = getTexCoord(texCoords, fans_tc, tcScale, 1);
288 for (unsigned i = 2; i < fans_v.size(); ++i) {
290 v2.vertex = toVec3f(vertices[fans_v[i]]);
291 v2.normal = normals[fans_n[i]];
292 v2.texCoord = getTexCoord(texCoords, fans_tc, tcScale, i);
293 triangles.insert(v0, v1, v2);
298 SGVec2f getTexCoordScale(const std::string& name, SGMaterialLib* matlib)
301 return SGVec2f(1, 1);
302 SGMaterial* material = matlib->find(name);
304 return SGVec2f(1, 1);
306 return material->get_tex_coord_scale();
310 insertSurfaceGeometry(const SGBinObject& obj, SGMaterialLib* matlib)
312 if (obj.get_tris_n().size() < obj.get_tris_v().size() ||
313 obj.get_tris_tc().size() < obj.get_tris_v().size()) {
314 SG_LOG(SG_TERRAIN, SG_ALERT,
315 "Group list sizes for triangles do not match!");
319 for (unsigned grp = 0; grp < obj.get_tris_v().size(); ++grp) {
320 std::string materialName = obj.get_tri_materials()[grp];
321 SGVec2f tcScale = getTexCoordScale(materialName, matlib);
322 addTriangleGeometry(materialTriangleMap[materialName],
323 obj.get_wgs84_nodes(), obj.get_normals(),
324 obj.get_texcoords(), obj.get_tris_v()[grp],
325 obj.get_tris_n()[grp], obj.get_tris_tc()[grp],
329 if (obj.get_strips_n().size() < obj.get_strips_v().size() ||
330 obj.get_strips_tc().size() < obj.get_strips_v().size()) {
331 SG_LOG(SG_TERRAIN, SG_ALERT,
332 "Group list sizes for strips do not match!");
335 for (unsigned grp = 0; grp < obj.get_strips_v().size(); ++grp) {
336 std::string materialName = obj.get_strip_materials()[grp];
337 SGVec2f tcScale = getTexCoordScale(materialName, matlib);
338 addStripGeometry(materialTriangleMap[materialName],
339 obj.get_wgs84_nodes(), obj.get_normals(),
340 obj.get_texcoords(), obj.get_strips_v()[grp],
341 obj.get_strips_n()[grp], obj.get_strips_tc()[grp],
345 if (obj.get_fans_n().size() < obj.get_fans_v().size() ||
346 obj.get_fans_tc().size() < obj.get_fans_v().size()) {
347 SG_LOG(SG_TERRAIN, SG_ALERT,
348 "Group list sizes for fans do not match!");
351 for (unsigned grp = 0; grp < obj.get_fans_v().size(); ++grp) {
352 std::string materialName = obj.get_fan_materials()[grp];
353 SGVec2f tcScale = getTexCoordScale(materialName, matlib);
354 addFanGeometry(materialTriangleMap[materialName],
355 obj.get_wgs84_nodes(), obj.get_normals(),
356 obj.get_texcoords(), obj.get_fans_v()[grp],
357 obj.get_fans_n()[grp], obj.get_fans_tc()[grp],
363 osg::Node* getSurfaceGeometry(SGMaterialLib* matlib) const
365 if (materialTriangleMap.empty())
368 osg::Geode* geode = new osg::Geode;
369 SGMaterialTriangleMap::const_iterator i;
370 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
371 // CHUNCKED (sic) here splits up unconnected triangles parts of
372 // the mesh into different Geometry sets, presumably for better
373 // culling. I (timoore) believe it is more performant to build
374 // the biggest indexed sets possible at the expense of tight
378 SGMaterial *mat = matlib->find(i->first);
380 std::list<SGTexturedTriangleBin::TriangleVector> connectSets;
381 i->second.getConnectedSets(connectSets);
383 std::list<SGTexturedTriangleBin::TriangleVector>::iterator j;
384 for (j = connectSets.begin(); j != connectSets.end(); ++j) {
385 osg::Geometry* geometry = i->second.buildGeometry(*j);
387 geometry->setStateSet(mat->get_state());
388 geode->addDrawable(geometry);
391 osg::Geometry* geometry = i->second.buildGeometry();
392 SGMaterial *mat = matlib->find(i->first);
394 geometry->setStateSet(mat->get_state());
395 geode->addDrawable(geometry);
401 void computeRandomSurfaceLights(SGMaterialLib* matlib)
403 SGMaterialTriangleMap::iterator i;
405 // generate a repeatable random seed
407 mt_init(&seed, unsigned(123));
409 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
410 SGMaterial *mat = matlib->find(i->first);
414 float coverage = mat->get_light_coverage();
417 if (coverage < 10000.0) {
418 SG_LOG(SG_INPUT, SG_ALERT, "Light coverage is "
419 << coverage << ", pushing up to 10000");
423 std::vector<SGVec3f> randomPoints;
424 i->second.addRandomSurfacePoints(coverage, 3, randomPoints);
425 std::vector<SGVec3f>::iterator j;
426 for (j = randomPoints.begin(); j != randomPoints.end(); ++j) {
427 float zombie = mt_rand(&seed);
428 // factor = sg_random() ^ 2, range = 0 .. 1 concentrated towards 0
429 float factor = mt_rand(&seed);
434 if ( zombie > 0.5 ) {
435 // 50% chance of yellowish
436 color = SGVec4f(0.9f, 0.9f, 0.3f, bright - factor * 0.2f);
437 } else if (zombie > 0.15f) {
438 // 35% chance of whitish
439 color = SGVec4f(0.9, 0.9f, 0.8f, bright - factor * 0.2f);
440 } else if (zombie > 0.05f) {
441 // 10% chance of orangish
442 color = SGVec4f(0.9f, 0.6f, 0.2f, bright - factor * 0.2f);
444 // 5% chance of redish
445 color = SGVec4f(0.9f, 0.2f, 0.2f, bright - factor * 0.2f);
447 randomTileLights.insert(*j, color);
452 void computeRandomForest(SGMaterialLib* matlib)
454 SGMaterialTriangleMap::iterator i;
456 // generate a repeatable random seed
458 mt_init(&seed, unsigned(586));
460 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
461 SGMaterial *mat = matlib->find(i->first);
465 float coverage = mat->get_tree_coverage();
469 // Attributes that don't vary by tree
470 randomForest.texture = mat->get_tree_texture();
471 randomForest.range = mat->get_tree_range();
472 randomForest.width = mat->get_tree_width();
473 randomForest.height = mat->get_tree_height();
474 randomForest.texture_varieties = mat->get_tree_varieties();
476 std::vector<SGVec3f> randomPoints;
477 i->second.addRandomSurfacePoints(coverage, 0, randomPoints);
478 std::vector<SGVec3f>::iterator j;
479 for (j = randomPoints.begin(); j != randomPoints.end(); ++j) {
481 // Apply a random scaling factor and texture index.
482 float scale = (mt_rand(&seed) + mt_rand(&seed)) / 2.0f + 0.5f;
483 int v = (int) (mt_rand(&seed) * mat->get_tree_varieties());
484 if (v == mat->get_tree_varieties()) v--;
485 randomForest.insert(*j, v, scale);
490 void computeRandomObjects(SGMaterialLib* matlib)
492 SGMaterialTriangleMap::iterator i;
494 // generate a repeatable random seed
496 mt_init(&seed, unsigned(123));
498 for (i = materialTriangleMap.begin(); i != materialTriangleMap.end(); ++i) {
499 SGMaterial *mat = matlib->find(i->first);
503 int group_count = mat->get_object_group_count();
507 for (int j = 0; j < group_count; j++)
509 SGMatModelGroup *object_group = mat->get_object_group(j);
510 int nObjects = object_group->get_object_count();
514 // For each of the random models in the group, determine an appropriate
515 // number of random placements and insert them.
516 for (int k = 0; k < nObjects; k++) {
517 SGMatModel * object = object_group->get_object(k);
519 std::vector<SGVec3f> randomPoints;
521 i->second.addRandomPoints(object->get_coverage_m2(), randomPoints);
522 std::vector<SGVec3f>::iterator l;
523 for (l = randomPoints.begin(); l != randomPoints.end(); ++l) {
524 randomModels.insert(*l, object, (int)object->get_randomized_range_m(&seed));
533 bool insertBinObj(const SGBinObject& obj, SGMaterialLib* matlib)
535 if (!insertPtGeometry(obj, matlib))
537 if (!insertSurfaceGeometry(obj, matlib))
543 typedef std::pair<osg::Node*, int> ModelLOD;
544 struct MakeQuadLeaf {
545 osg::LOD* operator() () const { return new osg::LOD; }
548 void operator() (osg::LOD* leaf, ModelLOD& mlod) const
550 leaf->addChild(mlod.first, 0, mlod.second);
553 struct GetModelLODCoord {
554 GetModelLODCoord(const osg::Matrix& transform) : _transform(transform) {}
555 GetModelLODCoord(const GetModelLODCoord& rhs) : _transform(rhs._transform)
557 osg::Vec3 operator() (const ModelLOD& mlod) const
559 return mlod.first->getBound().center() * _transform;
561 osg::Matrix _transform;
564 typedef QuadTreeBuilder<osg::LOD*, ModelLOD, MakeQuadLeaf, AddModelLOD,
565 GetModelLODCoord> RandomObjectsQuadtree;
568 SGLoadBTG(const std::string& path, SGMaterialLib *matlib, bool calc_lights, bool use_random_objects, bool use_random_vegetation)
571 if (!tile.read_bin(path))
574 SGTileGeometryBin tileGeometryBin;
575 if (!tileGeometryBin.insertBinObj(tile, matlib))
578 SGVec3d center = tile.get_gbs_center2();
579 SGGeod geodPos = SGGeod::fromCart(center);
580 SGQuatd hlOr = SGQuatd::fromLonLat(geodPos);
581 SGVec3f up = toVec3f(hlOr.backTransform(SGVec3d(0, 0, -1)));
582 GroundLightManager* lightManager = GroundLightManager::instance();
584 osg::ref_ptr<osg::Group> lightGroup = new SGOffsetTransform(0.94);
585 osg::ref_ptr<osg::Group> randomObjects;
586 osg::ref_ptr<osg::Group> randomForest;
587 osg::Group* terrainGroup = new osg::Group;
589 osg::Node* node = tileGeometryBin.getSurfaceGeometry(matlib);
591 terrainGroup->addChild(node);
593 if (use_random_objects || use_random_vegetation) {
595 // Simple matrix for used for flipping models that have been oriented
596 // with the center of the tile but upside down.
597 static const osg::Matrix flip(1, 0, 0, 0,
601 // Determine an rotation matrix for the models to place them
602 // perpendicular to the earth's surface. We use the same matrix,
603 // based on the centre of the tile, as the small angular differences
604 // between different points on the tile aren't worth worrying about
605 // for random objects. We also need to flip the orientation 180 degrees
606 osg::Matrix mAtt = flip * osg::Matrix::rotate(hlOr.osg());
607 // The inverse goes from world coordinates to Z up tile coordinates.
608 osg::Matrix world2Tile(osg::Matrix(hlOr.osg().conj()) * flip);
610 if (use_random_objects) {
611 tileGeometryBin.computeRandomObjects(matlib);
613 if (tileGeometryBin.randomModels.getNumModels() > 0) {
614 // Generate a repeatable random seed
616 mt_init(&seed, unsigned(123));
618 std::vector<ModelLOD> models;
619 for (unsigned int i = 0;
620 i < tileGeometryBin.randomModels.getNumModels(); i++) {
621 SGMatModelBin::MatModel obj
622 = tileGeometryBin.randomModels.getMatModel(i);
623 osg::Node* node = sgGetRandomModel(obj.model);
625 // Create a matrix to place the object in the correct
626 // location, and then apply the rotation matrix created
627 // above, with an additional random heading rotation if appropriate.
628 osg::Matrix transformMat(mAtt);
629 transformMat.postMult(osg::Matrix::translate(obj.position.osg()));
630 if (obj.model->get_heading_type() == SGMatModel::HEADING_RANDOM) {
631 // Rotate the object around the z axis.
632 double hdg = mt_rand(&seed) * M_PI * 2;
633 transformMat.preMult(osg::Matrix::rotate(hdg,
634 osg::Vec3d(0.0, 0.0, 1.0)));
636 osg::MatrixTransform* position =
637 new osg::MatrixTransform(transformMat);
638 position->addChild(node);
639 models.push_back(ModelLOD(position, obj.lod));
641 RandomObjectsQuadtree quadtree((GetModelLODCoord(world2Tile)),
643 quadtree.buildQuadTree(models.begin(), models.end());
644 randomObjects = quadtree.getRoot();
645 randomObjects->setName("random objects");
649 if (use_random_vegetation) {
650 // Now add some random forest.
651 tileGeometryBin.computeRandomForest(matlib);
653 if (tileGeometryBin.randomForest.getNumTrees() > 0) {
654 randomForest = createForest(tileGeometryBin.randomForest, mAtt);
655 randomForest->setName("random trees");
661 // FIXME: ugly, has a side effect
662 tileGeometryBin.computeRandomSurfaceLights(matlib);
664 if (tileGeometryBin.tileLights.getNumLights() > 0
665 || tileGeometryBin.randomTileLights.getNumLights() > 0) {
666 osg::Group* groundLights0 = new osg::Group;
667 groundLights0->setStateSet(lightManager->getGroundLightStateSet());
668 groundLights0->setNodeMask(GROUNDLIGHTS0_BIT);
669 osg::Geode* geode = new osg::Geode;
670 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.tileLights));
671 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 4, -0.3f));
672 groundLights0->addChild(geode);
673 lightGroup->addChild(groundLights0);
675 if (tileGeometryBin.randomTileLights.getNumLights() > 0) {
676 osg::Group* groundLights1 = new osg::Group;
677 groundLights1->setStateSet(lightManager->getGroundLightStateSet());
678 groundLights1->setNodeMask(GROUNDLIGHTS1_BIT);
679 osg::Group* groundLights2 = new osg::Group;
680 groundLights2->setStateSet(lightManager->getGroundLightStateSet());
681 groundLights2->setNodeMask(GROUNDLIGHTS2_BIT);
682 osg::Geode* geode = new osg::Geode;
683 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights, 2, -0.15f));
684 groundLights1->addChild(geode);
685 lightGroup->addChild(groundLights1);
686 geode = new osg::Geode;
687 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.randomTileLights));
688 groundLights2->addChild(geode);
689 lightGroup->addChild(groundLights2);
693 if (!tileGeometryBin.vasiLights.empty()) {
694 osg::Geode* vasiGeode = new osg::Geode;
695 SGVec4f red(1, 0, 0, 1);
696 SGMaterial* mat = matlib->find("RWY_RED_LIGHTS");
698 red = mat->get_light_color();
699 SGVec4f white(1, 1, 1, 1);
700 mat = matlib->find("RWY_WHITE_LIGHTS");
702 white = mat->get_light_color();
704 SGDirectionalLightListBin::const_iterator i;
705 for (i = tileGeometryBin.vasiLights.begin();
706 i != tileGeometryBin.vasiLights.end(); ++i) {
707 vasiGeode->addDrawable(SGLightFactory::getVasi(up, *i, red, white));
709 vasiGeode->setCullCallback(new SGPointSpriteLightCullCallback(osg::Vec3(1, 0.0001, 0.000001), 6));
710 vasiGeode->setStateSet(lightManager->getRunwayLightStateSet());
711 lightGroup->addChild(vasiGeode);
714 if (tileGeometryBin.runwayLights.getNumLights() > 0
715 || !tileGeometryBin.rabitLights.empty()
716 || !tileGeometryBin.reilLights.empty()
717 || !tileGeometryBin.odalLights.empty()) {
718 osg::Group* rwyLights = new osg::Group;
719 rwyLights->setCullCallback(new SGPointSpriteLightCullCallback);
720 rwyLights->setStateSet(lightManager->getRunwayLightStateSet());
721 rwyLights->setNodeMask(RUNWAYLIGHTS_BIT);
722 if (tileGeometryBin.runwayLights.getNumLights() != 0) {
723 osg::Geode* geode = new osg::Geode;
724 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin
726 rwyLights->addChild(geode);
728 SGDirectionalLightListBin::const_iterator i;
729 for (i = tileGeometryBin.rabitLights.begin();
730 i != tileGeometryBin.rabitLights.end(); ++i) {
731 rwyLights->addChild(SGLightFactory::getSequenced(*i));
733 for (i = tileGeometryBin.reilLights.begin();
734 i != tileGeometryBin.reilLights.end(); ++i) {
735 rwyLights->addChild(SGLightFactory::getSequenced(*i));
737 SGLightListBin::const_iterator j;
738 for (j = tileGeometryBin.odalLights.begin();
739 j != tileGeometryBin.odalLights.end(); ++j) {
740 rwyLights->addChild(SGLightFactory::getOdal(*j));
742 lightGroup->addChild(rwyLights);
745 if (tileGeometryBin.taxiLights.getNumLights() > 0) {
746 osg::Group* taxiLights = new osg::Group;
747 taxiLights->setCullCallback(new SGPointSpriteLightCullCallback);
748 taxiLights->setStateSet(lightManager->getTaxiLightStateSet());
749 taxiLights->setNodeMask(RUNWAYLIGHTS_BIT);
750 osg::Geode* geode = new osg::Geode;
751 geode->addDrawable(SGLightFactory::getLights(tileGeometryBin.taxiLights));
752 taxiLights->addChild(geode);
753 lightGroup->addChild(taxiLights);
756 // The toplevel transform for that tile.
757 osg::MatrixTransform* transform = new osg::MatrixTransform;
758 transform->setName(path);
759 transform->setMatrix(osg::Matrix::translate(center.osg()));
760 transform->addChild(terrainGroup);
761 if (lightGroup->getNumChildren() > 0) {
762 osg::LOD* lightLOD = new osg::LOD;
763 lightLOD->addChild(lightGroup.get(), 0, 30000);
764 // VASI is always on, so doesn't use light bits.
765 lightLOD->setNodeMask(LIGHTS_BITS | MODEL_BIT);
766 transform->addChild(lightLOD);
769 if (randomObjects.valid() || randomForest.valid()) {
771 // Add a LoD node, so we don't try to display anything when the tile center
772 // is more than 20km away.
773 osg::LOD* objectLOD = new osg::LOD;
775 if (randomObjects.valid()) objectLOD->addChild(randomObjects.get(), 0, 20000);
776 if (randomForest.valid()) objectLOD->addChild(randomForest.get(), 0, 20000);
778 unsigned nodeMask = SG_NODEMASK_CASTSHADOW_BIT | SG_NODEMASK_RECIEVESHADOW_BIT | SG_NODEMASK_TERRAIN_BIT;
779 objectLOD->setNodeMask(nodeMask);
780 transform->addChild(objectLOD);