Advertisement
Dev-san

SimpleSSAO

Jan 25th, 2012
1,753
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.70 KB | None | 0 0
  1. #ifndef _PFXSSAO_H_
  2. #define _PFXSSAO_H_
  3.  
  4. /*
  5. Copyright (C) 2012 Ilija Boshkov
  6.  
  7. Permission is hereby granted, free of charge, to any person obtaining a copy of
  8. this software and associated documentation files (the "Software"), to deal in
  9. the Software without restriction, including without limitation the rights to
  10. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  11. of the Software, and to permit persons to whom the Software is furnished to do
  12. so, subject to the following conditions:
  13.  
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. SOFTWARE.
  24. */
  25.  
  26. /*  
  27.     Based on the SSAO Demo by nullsquared.
  28.     Original Thread: http://www.ogre3d.org/forums/viewtopic.php?t=42350
  29. */
  30.  
  31. #include <Ogre.h>
  32.  
  33. class PFXSSAO : public Ogre::CompositorInstance::Listener, public Ogre::SceneManager::Listener
  34. {
  35. public:
  36.     Ogre::SceneManager *mSceneMgr;
  37.     Ogre::Camera *mCamera;
  38.     Ogre::Viewport *mViewport;
  39.     Ogre::RenderWindow *mWindow;
  40.     Ogre::CompositorInstance *mCompositor;
  41.     PFXSSAO(Ogre::RenderWindow* wnd, Ogre::Camera* cam)
  42.         : mSceneMgr(0)
  43.         , mCamera(0)
  44.         , mViewport(0)
  45.         , mWindow(0)
  46.         , mCompositor(0)
  47.     {
  48.         mWindow = wnd;
  49.         mCamera = cam;
  50.         Ogre::MaterialManager::getSingleton().setDefaultTextureFiltering(Ogre::TFO_ANISOTROPIC);
  51.         mSceneMgr = mCamera->getSceneManager();
  52.         mViewport = mCamera->getViewport();
  53.         initShadows();
  54.         initSSAO();
  55.     }
  56.  
  57.     ~PFXSSAO()
  58.     {
  59.         mCompositor->removeListener(this);
  60.         Ogre::CompositorManager::getSingleton().removeCompositor(mViewport, "ssao");
  61.         mSceneMgr->removeListener(this);
  62.     }
  63.  
  64.     void initSSAO()
  65.     {
  66.         mCompositor = Ogre::CompositorManager::getSingleton().addCompositor(mViewport, "ssao");
  67.         mCompositor->setEnabled(true);
  68.         mCompositor->addListener(this);
  69.     }
  70.  
  71.     void setEnabled(bool state)
  72.     {
  73.         mCompositor->setEnabled(state);
  74.     }
  75.  
  76.     void toggle()
  77.     {
  78.         mCompositor->setEnabled(!mCompositor->getEnabled());
  79.     }
  80.  
  81.     void initShadows()
  82.     {
  83.         mSceneMgr->setShadowTextureSelfShadow(true);
  84.         mSceneMgr->setShadowTextureCasterMaterial("shadow_caster");
  85.         mSceneMgr->setShadowTextureCount(4);
  86.  
  87.         mSceneMgr->setShadowTextureSize(256);
  88.         mSceneMgr->setShadowTexturePixelFormat(Ogre::PF_FLOAT16_RGB);
  89.         mSceneMgr->setShadowCasterRenderBackFaces(false);
  90.  
  91.         const unsigned numShadowRTTs = mSceneMgr->getShadowTextureCount();
  92.         for (unsigned i = 0; i < numShadowRTTs; ++i)
  93.         {
  94.             Ogre::TexturePtr tex = mSceneMgr->getShadowTexture(i);
  95.             Ogre::Viewport *vp = tex->getBuffer()->getRenderTarget()->getViewport(0);
  96.             vp->setBackgroundColour(Ogre::ColourValue(1, 1, 1, 1));
  97.             vp->setClearEveryFrame(true);
  98.         }
  99.         mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_ADDITIVE_INTEGRATED);
  100.         mSceneMgr->addListener(this);
  101.     }
  102.  
  103.     void notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
  104.     {
  105.         if (pass_id != 42)
  106.             return;
  107.  
  108.         Ogre::Vector3 farCorner = mCamera->getViewMatrix(true) * mCamera->getWorldSpaceCorners()[4];
  109.         Ogre::Pass *pass = mat->getBestTechnique()->getPass(0);
  110.         Ogre::GpuProgramParametersSharedPtr params = pass->getVertexProgramParameters();
  111.         if (params->_findNamedConstantDefinition("farCorner"))
  112.             params->setNamedConstant("farCorner", farCorner);
  113.  
  114.         params = pass->getFragmentProgramParameters();
  115.         static const Ogre::Matrix4 CLIP_SPACE_TO_IMAGE_SPACE(
  116.             0.5,    0,    0,  0.5,
  117.             0,   -0.5,    0,  0.5,
  118.             0,      0,    1,    0,
  119.             0,      0,    0,    1);
  120.         if (params->_findNamedConstantDefinition("ptMat"))
  121.             params->setNamedConstant("ptMat", CLIP_SPACE_TO_IMAGE_SPACE * mCamera->getProjectionMatrixWithRSDepth());
  122.         if (params->_findNamedConstantDefinition("far"))
  123.             params->setNamedConstant("far", mCamera->getFarClipDistance());
  124.     }
  125.  
  126.     void shadowTextureCasterPreViewProj(Ogre::Light *light, Ogre::Camera *cam, size_t)
  127.     {
  128.         float range = light->getAttenuationRange();
  129.         cam->setNearClipDistance(0.01);
  130.         cam->setFarClipDistance(range);
  131.     }
  132.     void shadowTexturesUpdated(size_t) {}
  133.     void shadowTextureReceiverPreViewProj(Ogre::Light*, Ogre::Frustum*) {}
  134.     void preFindVisibleObjects(Ogre::SceneManager*, Ogre::SceneManager::IlluminationRenderStage, Ogre::Viewport*) {}
  135.     void postFindVisibleObjects(Ogre::SceneManager*, Ogre::SceneManager::IlluminationRenderStage, Ogre::Viewport*) {}
  136. };
  137. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement