]> git.mxchange.org Git - simgear.git/blob - simgear/scene/tgdb/GroundLightManager.cxx
Use node masks and shared state sets to manage ground lights
[simgear.git] / simgear / scene / tgdb / GroundLightManager.cxx
1 #include <osg/Fog>
2
3 #include <simgear/scene/util/RenderConstants.hxx>
4 #include "GroundLightManager.hxx"
5
6
7
8 using namespace osg;
9
10 namespace
11 {
12 StateSet* makeLightSS()
13 {
14     StateSet* ss = new StateSet;
15     Fog* fog = new Fog;
16     fog->setMode(Fog::EXP2);
17     ss->setAttribute(fog);
18     ss->setDataVariance(Object::DYNAMIC);
19     return ss;
20 }
21 }
22
23 namespace simgear
24 {
25 GroundLightManager::GroundLightManager()
26 {
27     osg::Fog* fog;
28     runwayLightSS = makeLightSS();
29     taxiLightSS = makeLightSS();
30     groundLightSS = makeLightSS();
31 }
32
33 GroundLightManager* GroundLightManager::instance()
34 {
35     static ref_ptr<GroundLightManager> manager = new GroundLightManager;
36     return manager.get();
37 }
38
39 void GroundLightManager::update(const SGUpdateVisitor* updateVisitor)
40 {
41     osg::Fog* fog;
42     SGVec4f fogColor = updateVisitor->getFogColor();
43     fog = static_cast<osg::Fog*>(runwayLightSS
44                                  ->getAttribute(StateAttribute::FOG));
45     fog->setColor(fogColor.osg());
46     fog->setDensity(updateVisitor->getRunwayFogExp2Density());
47     fog = static_cast<osg::Fog*>(taxiLightSS
48                                  ->getAttribute(StateAttribute::FOG));
49     fog->setColor(fogColor.osg());
50     fog->setDensity(updateVisitor->getTaxiFogExp2Density());
51     fog = static_cast<osg::Fog*>(groundLightSS
52                                  ->getAttribute(StateAttribute::FOG));
53     fog->setColor(fogColor.osg());
54     fog->setDensity(updateVisitor->getGroundLightsFogExp2Density());
55 }
56
57 unsigned GroundLightManager::getLightNodeMask(const SGUpdateVisitor* updateVisitor)
58 {
59     unsigned mask = 0;
60     // The current sun angle in degree
61     float sun_angle = updateVisitor->getSunAngleDeg();
62     if (sun_angle > 85 || updateVisitor->getVisibility() < 5000)
63         mask |= RUNWAYLIGHTS_BIT;
64     // ground lights
65     if ( sun_angle > 95 )
66         mask |= GROUNDLIGHTS2_BIT;
67     if ( sun_angle > 92 )
68         mask |= GROUNDLIGHTS1_BIT;
69     if ( sun_angle > 89 )
70         mask |= GROUNDLIGHTS0_BIT;
71     return mask;
72 }
73 }