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-2013 Torus Knot Software Ltd 8 Copyright (c) 2006 Matthias Fink, netAllied GmbH <matthias.fink@web.de> 9 10 Permission is hereby granted, free of charge, to any person obtaining a copy 11 of this software and associated documentation files (the "Software"), to deal 12 in the Software without restriction, including without limitation the rights 13 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14 copies of the Software, and to permit persons to whom the Software is 15 furnished to do so, subject to the following conditions: 16 17 The above copyright notice and this permission notice shall be included in 18 all copies or substantial portions of the Software. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 26 THE SOFTWARE. 27 ----------------------------------------------------------------------------- 28 */ 29 30 #include "OgreStableHeaders.h" 31 #include "OgreShadowCameraSetupPSSM.h" 32 #include "OgreSceneManager.h" 33 #include "OgreCamera.h" 34 35 namespace Ogre 36 { 37 //--------------------------------------------------------------------- PSSMShadowCameraSetup()38 PSSMShadowCameraSetup::PSSMShadowCameraSetup() 39 : mSplitPadding(1.0f), mCurrentIteration(0) 40 { 41 calculateSplitPoints(3, 100, 100000); 42 setOptimalAdjustFactor(0, 5); 43 setOptimalAdjustFactor(1, 1); 44 setOptimalAdjustFactor(2, 0); 45 46 47 } 48 //--------------------------------------------------------------------- ~PSSMShadowCameraSetup()49 PSSMShadowCameraSetup::~PSSMShadowCameraSetup() 50 { 51 } 52 //--------------------------------------------------------------------- calculateSplitPoints(uint splitCount,Real nearDist,Real farDist,Real lambda)53 void PSSMShadowCameraSetup::calculateSplitPoints(uint splitCount, Real nearDist, Real farDist, Real lambda) 54 { 55 if (splitCount < 2) 56 OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot specify less than 2 splits", 57 "PSSMShadowCameraSetup::calculateSplitPoints"); 58 59 mSplitPoints.resize(splitCount + 1); 60 mOptimalAdjustFactors.resize(splitCount); 61 mSplitCount = splitCount; 62 63 mSplitPoints[0] = nearDist; 64 for (size_t i = 1; i < mSplitCount; i++) 65 { 66 Real fraction = (Real)i / (Real)mSplitCount; 67 Real splitPoint = lambda * nearDist * Math::Pow(farDist / nearDist, fraction) + 68 (1.0f - lambda) * (nearDist + fraction * (farDist - nearDist)); 69 70 mSplitPoints[i] = splitPoint; 71 } 72 mSplitPoints[splitCount] = farDist; 73 74 } 75 //--------------------------------------------------------------------- setSplitPoints(const SplitPointList & newSplitPoints)76 void PSSMShadowCameraSetup::setSplitPoints(const SplitPointList& newSplitPoints) 77 { 78 if (newSplitPoints.size() < 3) // 3, not 2 since splits + 1 points 79 OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Cannot specify less than 2 splits", 80 "PSSMShadowCameraSetup::setSplitPoints"); 81 mSplitCount = static_cast<uint>(newSplitPoints.size() - 1); 82 mSplitPoints = newSplitPoints; 83 mOptimalAdjustFactors.resize(mSplitCount); 84 } 85 //--------------------------------------------------------------------- setOptimalAdjustFactor(size_t splitIndex,Real factor)86 void PSSMShadowCameraSetup::setOptimalAdjustFactor(size_t splitIndex, Real factor) 87 { 88 if (splitIndex >= mOptimalAdjustFactors.size()) 89 OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "Split index out of range", 90 "PSSMShadowCameraSetup::setOptimalAdjustFactor"); 91 mOptimalAdjustFactors[splitIndex] = factor; 92 93 } 94 //--------------------------------------------------------------------- getOptimalAdjustFactor() const95 Real PSSMShadowCameraSetup::getOptimalAdjustFactor() const 96 { 97 // simplifies the overriding of the LiSPSM opt adjust factor use 98 return mOptimalAdjustFactors[mCurrentIteration]; 99 } 100 //--------------------------------------------------------------------- getShadowCamera(const Ogre::SceneManager * sm,const Ogre::Camera * cam,const Ogre::Viewport * vp,const Ogre::Light * light,Ogre::Camera * texCam,size_t iteration) const101 void PSSMShadowCameraSetup::getShadowCamera(const Ogre::SceneManager *sm, const Ogre::Camera *cam, 102 const Ogre::Viewport *vp, const Ogre::Light *light, Ogre::Camera *texCam, size_t iteration) const 103 { 104 // apply the right clip distance. 105 Real nearDist = mSplitPoints[iteration]; 106 Real farDist = mSplitPoints[iteration + 1]; 107 108 // Add a padding factor to internal distances so that the connecting split point will not have bad artifacts. 109 if (iteration > 0) 110 { 111 nearDist -= mSplitPadding; 112 } 113 if (iteration < mSplitCount - 1) 114 { 115 farDist += mSplitPadding; 116 } 117 118 mCurrentIteration = iteration; 119 120 // Ouch, I know this is hacky, but it's the easiest way to re-use LiSPSM / Focused 121 // functionality right now without major changes 122 Camera* _cam = const_cast<Camera*>(cam); 123 Real oldNear = _cam->getNearClipDistance(); 124 Real oldFar = _cam->getFarClipDistance(); 125 _cam->setNearClipDistance(nearDist); 126 _cam->setFarClipDistance(farDist); 127 128 LiSPSMShadowCameraSetup::getShadowCamera(sm, cam, vp, light, texCam, iteration); 129 130 // restore near/far 131 _cam->setNearClipDistance(oldNear); 132 _cam->setFarClipDistance(oldFar); 133 134 135 } 136 } 137