Coverage Report

Created: 2026-02-26 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ogre/Components/Terrain/src/OgreTerrainAutoUpdateLod.cpp
Line
Count
Source
1
/*
2
-----------------------------------------------------------------------------
3
This source file is part of OGRE
4
(Object-oriented Graphics Rendering Engine)
5
For the latest info, see http://www.ogre3d.org/
6
7
Copyright (c) 2000-2014 Torus Knot Software Ltd
8
9
Permission is hereby granted, free of charge, to any person obtaining a copy
10
of this software and associated documentation files (the "Software"), to deal
11
in the Software without restriction, including without limitation the rights
12
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
copies of the Software, and to permit persons to whom the Software is
14
furnished to do so, subject to the following conditions:
15
16
The above copyright notice and this permission notice shall be included in
17
all copies or substantial portions of the Software.
18
19
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25
THE SOFTWARE.
26
-----------------------------------------------------------------------------
27
*/
28
#include "OgreTerrain.h"
29
#include "OgreTerrainQuadTreeNode.h"
30
#include "OgreCamera.h"
31
#include "OgreSceneManager.h"
32
#include "OgreViewport.h"
33
#include "OgreLogManager.h"
34
#include "OgreRoot.h"
35
#include "OgreRenderSystem.h"
36
#include "OgreRay.h"
37
#include "OgreTerrainAutoUpdateLod.h"
38
39
/*
40
#if OGRE_COMPILER == OGRE_COMPILER_MSVC
41
// we do lots of conversions here, casting them all is tedious & cluttered, we know what we're doing
42
#   pragma warning (disable : 4244)
43
#endif
44
*/
45
46
namespace Ogre
47
{
48
    void TerrainAutoUpdateLodByDistance::autoUpdateLod(Terrain *terrain, bool synchronous, const Any &data)
49
0
    {
50
0
        if( terrain )
51
0
            autoUpdateLodByDistance(terrain, synchronous, any_cast<Real>(data));
52
0
    }
53
54
    void TerrainAutoUpdateLodByDistance::autoUpdateLodByDistance(Terrain *terrain, bool synchronous, const Real holdDistance)
55
0
    {
56
0
        if (!terrain->isLoaded())
57
0
            return;
58
59
        // calculate error terms
60
0
        const Viewport* vp = terrain->getSceneManager()->getCurrentViewport();
61
0
        if(!vp)
62
0
            return;
63
0
        const Camera* cam = vp->getCamera()->getLodCamera();
64
65
        // W. de Boer 2000 calculation
66
        // A = vp_near / abs(vp_top)
67
        // A = 1 / tan(fovy*0.5)    (== 1 for fovy=45*2)
68
0
        Real A = 1.0f / Math::Tan(cam->getFOVy() * 0.5f);
69
        // T = 2 * maxPixelError / vertRes
70
0
        Real maxPixelError = TerrainGlobalOptions::getSingleton().getMaxPixelError() * cam->_getLodBiasInverse();
71
0
        Real T = 2.0f * maxPixelError / (Real)vp->getActualHeight();
72
73
        // CFactor = A / T
74
0
        Real cFactor = A / T;
75
76
0
        int maxLod = traverseTreeByDistance(terrain->getQuadTree(), cam, cFactor, holdDistance);
77
0
        if (maxLod >= 0)
78
0
            terrain->load(maxLod,synchronous);
79
0
    }
80
81
    int TerrainAutoUpdateLodByDistance::traverseTreeByDistance(TerrainQuadTreeNode *node,
82
            const Camera *cam, Real cFactor, const Real holdDistance)
83
0
    {
84
0
        if (!node->isLeaf())
85
0
        {
86
0
            int tmp = -1;
87
0
            for (int i = 0; i < 4; ++i)
88
0
            {
89
0
                int ret = traverseTreeByDistance(node->getChild(i), cam, cFactor, holdDistance);
90
0
                if (ret != -1)
91
0
                {
92
0
                    if (tmp == -1 || ret < tmp)
93
0
                        tmp = ret;
94
0
                }
95
0
            }
96
97
0
            if (tmp != -1)
98
0
                return tmp;
99
0
        }
100
101
0
        Vector3 localPos = cam->getDerivedPosition() - node->getLocalCentre() - node->getTerrain()->getPosition();
102
0
        Real dist;
103
0
        if (TerrainGlobalOptions::getSingleton().getUseRayBoxDistanceCalculation())
104
0
        {
105
            // Get distance to this terrain node (to closest point of the box)
106
            // head towards centre of the box (note, box may not cover mLocalCentre because of height)
107
0
            Vector3 dir(node->getBoundingBox().getCenter() - localPos);
108
0
            dir.normalise();
109
0
            Ray ray(localPos, dir);
110
0
            std::pair<bool, Real> intersectRes = Math::intersects(ray, node->getBoundingBox());
111
112
            // ray will always intersect, we just want the distance
113
0
            dist = intersectRes.second;
114
0
        }
115
0
        else
116
0
        {
117
            // distance to tile centre
118
0
            dist = localPos.length();
119
            // deduct half the radius of the box, assume that on average the
120
            // worst case is best approximated by this
121
0
            dist -= (node->getBoundingRadius() * 0.5f);
122
0
        }
123
124
        // For each LOD, the distance at which the LOD will transition *downwards*
125
        // is given by
126
        // distTransition = maxDelta * cFactor;
127
0
        for (uint16 lodLevel = 0; lodLevel < node->getLodCount(); ++lodLevel)
128
0
        {
129
            // If we have no parent, and this is the lowest LOD, we always render
130
            // this is the 'last resort' so to speak, we always enoucnter this last
131
0
            if (lodLevel+1 == node->getLodCount() && !node->getParent())
132
0
                return lodLevel + node->getBaseLod();
133
0
            else
134
0
            {
135
                // Calculate or reuse transition distance
136
0
                Real distTransition;
137
0
                if (Math::RealEqual(cFactor, node->getLodLevel(lodLevel)->lastCFactor))
138
0
                    distTransition = node->getLodLevel(lodLevel)->lastTransitionDist;
139
0
                else
140
0
                    distTransition = node->getLodLevel(lodLevel)->maxHeightDelta * cFactor;
141
142
0
                if ((dist - holdDistance) < distTransition)
143
0
                    return lodLevel + node->getBaseLod();
144
0
            }
145
0
        }
146
147
0
        return -1;
148
0
    }
149
}