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 {
autoUpdateLod(Terrain * terrain,bool synchronous,const Any & data)48     void TerrainAutoUpdateLodByDistance::autoUpdateLod(Terrain *terrain, bool synchronous, const Any &data)
49     {
50         if( terrain )
51             autoUpdateLodByDistance(terrain, synchronous, any_cast<Real>(data));
52     }
53 
autoUpdateLodByDistance(Terrain * terrain,bool synchronous,const Real holdDistance)54     void TerrainAutoUpdateLodByDistance::autoUpdateLodByDistance(Terrain *terrain, bool synchronous, const Real holdDistance)
55     {
56         if (!terrain->isLoaded())
57             return;
58 
59         // calculate error terms
60         const Viewport* vp = terrain->getSceneManager()->getCurrentViewport();
61         if(!vp)
62             return;
63         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         Real A = 1.0f / Math::Tan(cam->getFOVy() * 0.5f);
69         // T = 2 * maxPixelError / vertRes
70         Real maxPixelError = TerrainGlobalOptions::getSingleton().getMaxPixelError() * cam->_getLodBiasInverse();
71         Real T = 2.0f * maxPixelError / (Real)vp->getActualHeight();
72 
73         // CFactor = A / T
74         Real cFactor = A / T;
75 
76         int maxLod = traverseTreeByDistance(terrain->getQuadTree(), cam, cFactor, holdDistance);
77         if (maxLod >= 0)
78             terrain->load(maxLod,synchronous);
79     }
80 
traverseTreeByDistance(TerrainQuadTreeNode * node,const Camera * cam,Real cFactor,const Real holdDistance)81     int TerrainAutoUpdateLodByDistance::traverseTreeByDistance(TerrainQuadTreeNode *node,
82             const Camera *cam, Real cFactor, const Real holdDistance)
83     {
84         if (!node->isLeaf())
85         {
86             int tmp = -1;
87             for (int i = 0; i < 4; ++i)
88             {
89                 int ret = traverseTreeByDistance(node->getChild(i), cam, cFactor, holdDistance);
90                 if (ret != -1)
91                 {
92                     if (tmp == -1 || ret < tmp)
93                         tmp = ret;
94                 }
95             }
96 
97             if (tmp != -1)
98                 return tmp;
99         }
100 
101         Vector3 localPos = cam->getDerivedPosition() - node->getLocalCentre() - node->getTerrain()->getPosition();
102         Real dist;
103         if (TerrainGlobalOptions::getSingleton().getUseRayBoxDistanceCalculation())
104         {
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             Vector3 dir(node->getAABB().getCenter() - localPos);
108             dir.normalise();
109             Ray ray(localPos, dir);
110             std::pair<bool, Real> intersectRes = Math::intersects(ray, node->getAABB());
111 
112             // ray will always intersect, we just want the distance
113             dist = intersectRes.second;
114         }
115         else
116         {
117             // distance to tile centre
118             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             dist -= (node->getBoundingRadius() * 0.5f);
122         }
123 
124         // For each LOD, the distance at which the LOD will transition *downwards*
125         // is given by
126         // distTransition = maxDelta * cFactor;
127         for (uint16 lodLevel = 0; lodLevel < node->getLodCount(); ++lodLevel)
128         {
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             if (lodLevel+1 == node->getLodCount() && !node->getParent())
132                 return lodLevel + node->getBaseLod();
133             else
134             {
135                 // Calculate or reuse transition distance
136                 Real distTransition;
137                 if (Math::RealEqual(cFactor, node->getLodLevel(lodLevel)->lastCFactor))
138                     distTransition = node->getLodLevel(lodLevel)->lastTransitionDist;
139                 else
140                     distTransition = node->getLodLevel(lodLevel)->maxHeightDelta * cFactor;
141 
142                 if ((dist - holdDistance) < distTransition)
143                     return lodLevel + node->getBaseLod();
144             }
145         }
146 
147         return -1;
148     }
149 }
150