File: lightutil.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (159 lines) | stat: -rw-r--r-- 6,083 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "lightutil.hpp"

#include <osg/Group>
#include <osg/Light>

#include <osgParticle/ParticleSystem>

#include <components/esm3/loadligh.hpp>
#include <components/fallback/fallback.hpp>
#include <components/sceneutil/lightcommon.hpp>

#include "lightcontroller.hpp"
#include "lightmanager.hpp"
#include "visitor.hpp"

namespace
{
    class CheckEmptyLightVisitor : public osg::NodeVisitor
    {
    public:
        CheckEmptyLightVisitor()
            : osg::NodeVisitor(TRAVERSE_ALL_CHILDREN)
        {
        }

        void apply(osg::Drawable& drawable) override
        {
            if (!mEmpty)
                return;

            if (dynamic_cast<const osgParticle::ParticleSystem*>(&drawable))
                mEmpty = false;
            else
                traverse(drawable);
        }

        void apply(osg::Geometry& geometry) override { mEmpty = false; }

        bool mEmpty = true;
    };
}

namespace SceneUtil
{

    void configureLight(osg::Light* light, float radius, bool isExterior)
    {
        float quadraticAttenuation = 0.f;
        float linearAttenuation = 0.f;
        float constantAttenuation = 0.f;

        static const bool useConstant = Fallback::Map::getBool("LightAttenuation_UseConstant");
        static const bool useLinear = Fallback::Map::getBool("LightAttenuation_UseLinear");
        static const bool useQuadratic = Fallback::Map::getBool("LightAttenuation_UseQuadratic");
        // User file might provide nonsense values
        // Clamp these settings to prevent badness (e.g. illegal OpenGL calls)
        static const float constantValue = std::max(Fallback::Map::getFloat("LightAttenuation_ConstantValue"), 0.f);
        static const float linearValue = std::max(Fallback::Map::getFloat("LightAttenuation_LinearValue"), 0.f);
        static const float quadraticValue = std::max(Fallback::Map::getFloat("LightAttenuation_QuadraticValue"), 0.f);
        static const float linearRadiusMult
            = std::max(Fallback::Map::getFloat("LightAttenuation_LinearRadiusMult"), 0.f);
        static const float quadraticRadiusMult
            = std::max(Fallback::Map::getFloat("LightAttenuation_QuadraticRadiusMult"), 0.f);
        static const int linearMethod = Fallback::Map::getInt("LightAttenuation_LinearMethod");
        static const int quadraticMethod = Fallback::Map::getInt("LightAttenuation_QuadraticMethod");
        static const bool outQuadInLin = Fallback::Map::getBool("LightAttenuation_OutQuadInLin");

        if (useConstant)
            constantAttenuation = constantValue;

        if (useLinear)
        {
            linearAttenuation = linearMethod == 0 ? linearValue : 0.01f;
            float r = radius * linearRadiusMult;
            if (r > 0.f && (linearMethod == 1 || linearMethod == 2))
                linearAttenuation = linearValue / std::pow(r, linearMethod);
        }

        if (useQuadratic && (!outQuadInLin || isExterior))
        {
            quadraticAttenuation = quadraticMethod == 0 ? quadraticValue : 0.01f;
            float r = radius * quadraticRadiusMult;
            if (r > 0.f && (quadraticMethod == 1 || quadraticMethod == 2))
                quadraticAttenuation = quadraticValue / std::pow(r, quadraticMethod);
        }

        // If the values are still nonsense, try to at least prevent UB and disable attenuation
        if (constantAttenuation == 0.f && linearAttenuation == 0.f && quadraticAttenuation == 0.f)
            constantAttenuation = 1.f;

        light->setConstantAttenuation(constantAttenuation);
        light->setLinearAttenuation(linearAttenuation);
        light->setQuadraticAttenuation(quadraticAttenuation);
    }

    osg::ref_ptr<LightSource> addLight(
        osg::Group* node, const SceneUtil::LightCommon& esmLight, unsigned int lightMask, bool isExterior)
    {
        SceneUtil::FindByNameVisitor visitor("AttachLight");
        node->accept(visitor);

        osg::Group* attachTo = visitor.mFoundNode ? visitor.mFoundNode : node;
        osg::ref_ptr<LightSource> lightSource
            = createLightSource(esmLight, lightMask, isExterior, osg::Vec4f(0, 0, 0, 1));
        attachTo->addChild(lightSource);

        CheckEmptyLightVisitor emptyVisitor;
        node->accept(emptyVisitor);

        lightSource->setEmpty(emptyVisitor.mEmpty);

        return lightSource;
    }

    osg::ref_ptr<LightSource> createLightSource(
        const SceneUtil::LightCommon& esmLight, unsigned int lightMask, bool isExterior, const osg::Vec4f& ambient)
    {
        osg::ref_ptr<SceneUtil::LightSource> lightSource(new SceneUtil::LightSource);
        osg::ref_ptr<osg::Light> light(new osg::Light);
        lightSource->setNodeMask(lightMask);

        // The minimum scene light radius is 16 in Morrowind
        const float radius = std::max(esmLight.mRadius, 16.f);
        lightSource->setRadius(radius);

        configureLight(light, radius, isExterior);

        osg::Vec4f diffuse = esmLight.mColor;
        osg::Vec4f specular = esmLight.mColor; // ESM format doesn't provide specular
        if (esmLight.mNegative)
        {
            diffuse *= -1;
            diffuse.a() = 1;
            // Using specular lighting for negative lights is unreasonable
            specular = osg::Vec4f();
        }
        light->setDiffuse(diffuse);
        light->setAmbient(ambient);
        light->setSpecular(specular);

        lightSource->setLight(light);

        osg::ref_ptr<SceneUtil::LightController> ctrl(new SceneUtil::LightController);
        ctrl->setDiffuse(light->getDiffuse());
        ctrl->setSpecular(light->getSpecular());
        if (esmLight.mFlicker)
            ctrl->setType(SceneUtil::LightController::LT_Flicker);
        if (esmLight.mFlickerSlow)
            ctrl->setType(SceneUtil::LightController::LT_FlickerSlow);
        if (esmLight.mPulse)
            ctrl->setType(SceneUtil::LightController::LT_Pulse);
        if (esmLight.mPulseSlow)
            ctrl->setType(SceneUtil::LightController::LT_PulseSlow);

        lightSource->addUpdateCallback(ctrl);

        return lightSource;
    }
}