Project

General

Profile

SO3Engine
SO3SSAOLogic.cpp
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OpenSpace3D
4For the latest info, see http://www.openspace3d.com
5
6Copyright (c) 2012 I-maginer
7
8This program is free software; you can redistribute it and/or modify it under
9the terms of the GNU Lesser General Public License as published by the Free Software
10Foundation; either version 2 of the License, or (at your option) any later
11version.
12
13This program is distributed in the hope that it will be useful, but WITHOUT
14ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
16
17You should have received a copy of the GNU Lesser General Public License along with
18this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19Place - Suite 330, Boston, MA 02111-1307, USA, or go to
20http://www.gnu.org/copyleft/lesser.txt
21
22-----------------------------------------------------------------------------
23*/
24
26
27#include <Ogre.h>
28
29namespace SO3
30{
31
32SSsaoListener::SSsaoListener(Ogre::CompositorInstance* instance) : mInstance(instance)
33{
34}
35
36void SSsaoListener::notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
37{
38 if (pass_id != 42 || !mat->getSupportedTechnique(0)) // not SSAO, return
39 return;
40
41 // this is the camera you're using
42 Ogre::Camera *cam = mInstance->getChain()->getViewport()->getCamera();
43
44 // calculate the far-top-right corner in view-space
45 Ogre::Vector3 farCorner = cam->getViewMatrix(true) * cam->getWorldSpaceCorners()[4];
46
47 // get the pass
48 Ogre::Pass *pass = mat->getSupportedTechnique(0)->getPass(0);
49
50 // get the vertex shader parameters
51 Ogre::GpuProgramParametersSharedPtr params = pass->getVertexProgramParameters();
52
53 // set the camera's far-top-right corner
54 if (params->_findNamedConstantDefinition("farCorner"))
55 params->setNamedConstant("farCorner", farCorner);
56
57 // get the fragment shader parameters
58 params = pass->getFragmentProgramParameters();
59
60 // set the projection matrix we need
61 static const Ogre::Matrix4 CLIP_SPACE_TO_IMAGE_SPACE(0.5, 0, 0, 0.5,
62 0, -0.5, 0, 0.5,
63 0, 0, 1, 0,
64 0, 0, 0, 1);
65
66 if (params->_findNamedConstantDefinition("ptMat"))
67 params->setNamedConstant("ptMat", CLIP_SPACE_TO_IMAGE_SPACE * cam->getProjectionMatrixWithRSDepth());
68
69 if (params->_findNamedConstantDefinition("far"))
70 params->setNamedConstant("far", cam->getFarClipDistance());
71}
72
73Ogre::CompositorInstance::Listener* SSsaoLogic::createListener(Ogre::CompositorInstance* instance)
74{
75 return new SSsaoListener(instance);
76}
77
78}
SSsaoListener(Ogre::CompositorInstance *instance)
void notifyMaterialRender(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat)
virtual Ogre::CompositorInstance::Listener * createListener(Ogre::CompositorInstance *instance)