File: terraindrawable.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 (165 lines) | stat: -rw-r--r-- 5,378 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
160
161
162
163
164
165
#include "terraindrawable.hpp"

#include <osg/ClusterCullingCallback>
#include <osgUtil/CullVisitor>

#include <components/sceneutil/lightmanager.hpp>

#include "compositemaprenderer.hpp"

namespace Terrain
{

    TerrainDrawable::TerrainDrawable() {}

    TerrainDrawable::~TerrainDrawable() {}

    TerrainDrawable::TerrainDrawable(const TerrainDrawable& copy, const osg::CopyOp& copyop)
        : osg::Geometry(copy, copyop)
        , mPasses(copy.mPasses)
        , mLightListCallback(copy.mLightListCallback)
    {
    }

    void TerrainDrawable::accept(osg::NodeVisitor& nv)
    {
        if (nv.getVisitorType() != osg::NodeVisitor::CULL_VISITOR)
        {
            osg::Geometry::accept(nv);
        }
        else if (nv.validNodeMask(*this))
        {
            nv.pushOntoNodePath(this);
            cull(static_cast<osgUtil::CullVisitor*>(&nv));
            nv.popFromNodePath();
        }
    }

    inline float distance(const osg::Vec3& coord, const osg::Matrix& matrix)
    {
        return -((float)coord[0] * (float)matrix(0, 2) + (float)coord[1] * (float)matrix(1, 2)
            + (float)coord[2] * (float)matrix(2, 2) + matrix(3, 2));
    }

    // canot use ClusterCullingCallback::cull: viewpoint != eyepoint
    //  !osgfixpotential!
    bool clusterCull(osg::ClusterCullingCallback* cb, const osg::Vec3f& eyePoint, bool shadowcam)
    {
        float _deviation = cb->getDeviation();
        const osg::Vec3& _controlPoint = cb->getControlPoint();
        osg::Vec3 _normal = cb->getNormal();
        if (shadowcam)
            _normal = _normal * -1; // inverting for shadowcam frontfaceculing
        float _radius = cb->getRadius();
        if (_deviation <= -1.0f)
            return false;
        osg::Vec3 eye_cp = eyePoint - _controlPoint;
        float radius = eye_cp.length();
        if (radius < _radius)
            return false;
        float deviation = (eye_cp * _normal) / radius;
        return deviation < _deviation;
    }

    void TerrainDrawable::cull(osgUtil::CullVisitor* cv)
    {
        const osg::BoundingBox& bb = getBoundingBox();

        if (_cullingActive && cv->isCulled(getBoundingBox()))
            return;

        bool shadowcam = cv->getCurrentCamera()->getName() == "ShadowCamera";

        if (cv->getCullingMode() & osg::CullStack::CLUSTER_CULLING
            && clusterCull(mClusterCullingCallback, cv->getEyePoint(), shadowcam))
            return;

        osg::RefMatrix& matrix = *cv->getModelViewMatrix();

        if (cv->getComputeNearFarMode() != osg::CullSettings::DO_NOT_COMPUTE_NEAR_FAR && bb.valid())
        {
            if (!cv->updateCalculatedNearFar(matrix, *this, false))
                return;
        }

        float depth = bb.valid() ? distance(bb.center(), matrix) : 0.0f;
        if (osg::isNaN(depth))
            return;

        if (shadowcam)
        {
            cv->addDrawableAndDepth(this, &matrix, depth);
            return;
        }

        if (mCompositeMap && mCompositeMapRenderer)
        {
            mCompositeMapRenderer->setImmediate(mCompositeMap);
            mCompositeMapRenderer = nullptr;
        }

        bool pushedLight = mLightListCallback && mLightListCallback->pushLightState(this, cv);

        osg::StateSet* stateset = getStateSet();
        if (stateset)
            cv->pushStateSet(stateset);

        for (PassVector::const_iterator it = mPasses.begin(); it != mPasses.end(); ++it)
        {
            cv->pushStateSet(*it);
            cv->addDrawableAndDepth(this, &matrix, depth);
            cv->popStateSet();
        }

        if (stateset)
            cv->popStateSet();
        if (pushedLight)
            cv->popStateSet();
    }

    void TerrainDrawable::createClusterCullingCallback()
    {
        mClusterCullingCallback = new osg::ClusterCullingCallback(this);
    }

    void TerrainDrawable::setPasses(const TerrainDrawable::PassVector& passes)
    {
        mPasses = passes;
    }

    void TerrainDrawable::setLightListCallback(SceneUtil::LightListCallback* lightListCallback)
    {
        mLightListCallback = lightListCallback;
    }

    void TerrainDrawable::setupWaterBoundingBox(float waterheight, float margin)
    {
        osg::Vec3Array* vertices = static_cast<osg::Vec3Array*>(getVertexArray());
        for (unsigned int i = 0; i < vertices->size(); ++i)
        {
            const osg::Vec3f& vertex = (*vertices)[i];
            if (vertex.z() <= waterheight)
                mWaterBoundingBox.expandBy(vertex);
        }
        if (mWaterBoundingBox.valid())
        {
            const osg::BoundingBox& bb = getBoundingBox();
            mWaterBoundingBox.xMin() = std::max(bb.xMin(), mWaterBoundingBox.xMin() - margin);
            mWaterBoundingBox.yMin() = std::max(bb.yMin(), mWaterBoundingBox.yMin() - margin);
            mWaterBoundingBox.xMax() = std::min(bb.xMax(), mWaterBoundingBox.xMax() + margin);
            mWaterBoundingBox.xMax() = std::min(bb.xMax(), mWaterBoundingBox.xMax() + margin);
        }
    }

    void TerrainDrawable::compileGLObjects(osg::RenderInfo& renderInfo) const
    {
        for (PassVector::const_iterator it = mPasses.begin(); it != mPasses.end(); ++it)
        {
            osg::StateSet* stateset = *it;
            stateset->compileGLObjects(*renderInfo.getState());
        }

        osg::Geometry::compileGLObjects(renderInfo);
    }

}