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/
7 Copyright (c) 2000-2006 Torus Knot Software Ltd
8 Also see acknowledgements in Readme.html
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License along with
20 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22 http://www.gnu.org/copyleft/lesser.txt.
24 You may alternatively use this source under the terms of a specific version of
25 the OGRE Unrestricted License provided you have obtained such a license from
26 Torus Knot Software Ltd.
27 -----------------------------------------------------------------------------
30 #include "OgreStableHeaders.h"
31 #include "OgrePixelCountLodStrategy.h"
33 #include "OgreViewport.h"
38 //-----------------------------------------------------------------------
39 template<> PixelCountLodStrategy
* Singleton
<PixelCountLodStrategy
>::ms_Singleton
= 0;
40 PixelCountLodStrategy
* PixelCountLodStrategy::getSingletonPtr(void)
44 PixelCountLodStrategy
& PixelCountLodStrategy::getSingleton(void)
46 assert( ms_Singleton
); return ( *ms_Singleton
);
48 //-----------------------------------------------------------------------
49 PixelCountLodStrategy::PixelCountLodStrategy()
50 : LodStrategy("PixelCount")
52 //-----------------------------------------------------------------------
53 Real
PixelCountLodStrategy::getValueImpl(const MovableObject
*movableObject
, const Ogre::Camera
*camera
) const
56 const Viewport
*viewport
= camera
->getViewport();
59 Real viewportArea
= viewport
->getActualWidth() * viewport
->getActualHeight();
61 // Get area of unprojected circle with object bounding radius
62 Real boundingArea
= Math::PI
* Math::Sqr(movableObject
->getBoundingRadius());
64 // Base computation on projection type
65 switch (camera
->getProjectionType())
69 // Get camera distance
70 Real distanceSquared
= movableObject
->getParentNode()->getSquaredViewDepth(camera
);
72 // Check for 0 distance
73 if (distanceSquared
<= std::numeric_limits
<Real
>::epsilon())
74 return getBaseValue();
76 // Get projection matrix (this is done to avoid computation of tan(fov / 2))
77 const Matrix4
& projectionMatrix
= camera
->getProjectionMatrix();
79 // Estimate pixel count
80 return (boundingArea
* viewportArea
* projectionMatrix
[0][0] * projectionMatrix
[1][1]) / distanceSquared
;
84 // Compute orthographic area
85 Real orthoArea
= camera
->getOrthoWindowHeight() * camera
->getOrthoWindowWidth();
87 // Check for 0 orthographic area
88 if (orthoArea
<= std::numeric_limits
<Real
>::epsilon())
89 return getBaseValue();
91 // Estimate pixel count
92 return (boundingArea
* viewportArea
) / orthoArea
;
96 // This case is not covered for obvious reasons
101 //---------------------------------------------------------------------
102 Real
PixelCountLodStrategy::getBaseValue() const
104 // Use the maximum possible value as base
105 return std::numeric_limits
<Real
>::max();
107 //---------------------------------------------------------------------
108 Real
PixelCountLodStrategy::transformBias(Real factor
) const
110 // No transformation required for pixel count strategy
113 //---------------------------------------------------------------------
114 ushort
PixelCountLodStrategy::getIndex(Real value
, const Mesh::MeshLodUsageList
& meshLodUsageList
) const
116 // Values are descending
117 return getIndexDescending(value
, meshLodUsageList
);
119 //---------------------------------------------------------------------
120 ushort
PixelCountLodStrategy::getIndex(Real value
, const Material::LodValueList
& materialLodValueList
) const
122 // Values are descending
123 return getIndexDescending(value
, materialLodValueList
);
125 //---------------------------------------------------------------------
126 void PixelCountLodStrategy::sort(Mesh::MeshLodUsageList
& meshLodUsageList
) const
129 sortDescending(meshLodUsageList
);
131 //---------------------------------------------------------------------
132 bool PixelCountLodStrategy::isSorted(const Mesh::LodValueList
& values
) const
134 // Check if values are sorted descending
135 return isSortedDescending(values
);