[1] | 1 | /** |
---|
| 2 | Implementation of a Deferred Shading engine in OGRE, using Multiple Render Targets and |
---|
| 3 | HLSL/GLSL high level language shaders. |
---|
| 4 | // W.J. :wumpus: van der Laan 2005 // |
---|
| 5 | |
---|
| 6 | Deferred shading renders the scene to a 'fat' texture format, using a shader that outputs colour, |
---|
| 7 | normal, depth, and possible other attributes per fragment. Multi Render Target is required as we |
---|
| 8 | are dealing with many outputs which get written into multiple render textures in the same pass. |
---|
| 9 | |
---|
| 10 | After rendering the scene in this format, the shading (lighting) can be done as a post process. |
---|
| 11 | This means that lighting is done in screen space. Adding them requires nothing more than rendering |
---|
| 12 | a screenful quad; thus the method allows for an enormous amount of lights without noticable |
---|
| 13 | performance loss. |
---|
| 14 | |
---|
| 15 | Little lights affecting small area ("Minilights") can be even further optimised by rendering |
---|
| 16 | their convex bounding geometry. This is also shown in this demo by 6 swarming lights. |
---|
| 17 | |
---|
| 18 | The paper for GDC2004 on Deferred Shading can be found here: |
---|
| 19 | http://www.talula.demon.co.uk/DeferredShading.pdf |
---|
| 20 | ******************************************************************************* |
---|
| 21 | Copyright (c) W.J. van der Laan |
---|
| 22 | |
---|
| 23 | Permission is hereby granted, free of charge, to any person obtaining a copy of |
---|
| 24 | this software and associated documentation files (the "Software"), to deal in |
---|
| 25 | the Software without restriction, including without limitation the rights to use, |
---|
| 26 | copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the |
---|
| 27 | Software, and to permit persons to whom the Software is furnished to do so, subject |
---|
| 28 | to the following conditions: |
---|
| 29 | |
---|
| 30 | The above copyright notice and this permission notice shall be included in all copies |
---|
| 31 | or substantial portions of the Software. |
---|
| 32 | |
---|
| 33 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
---|
| 34 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
---|
| 35 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
---|
| 36 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
---|
| 37 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE |
---|
| 38 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
| 39 | ******************************************************************************* |
---|
| 40 | */ |
---|
| 41 | #ifndef H_WJ_DeferredShadingSystem |
---|
| 42 | #define H_WJ_DeferredShadingSystem |
---|
| 43 | |
---|
| 44 | #include "OgreCompositorInstance.h" |
---|
| 45 | #include "OgreSceneManager.h" |
---|
| 46 | #include "OgreSceneNode.h" |
---|
| 47 | #include "OgreMaterial.h" |
---|
| 48 | class MLight; |
---|
| 49 | class MaterialGenerator; |
---|
| 50 | |
---|
| 51 | /** System to manage Deferred Shading for a camera/render target. |
---|
| 52 | */ |
---|
| 53 | class DeferredShadingSystem: public Ogre::CompositorInstance::Listener |
---|
| 54 | { |
---|
| 55 | public: |
---|
| 56 | DeferredShadingSystem(Ogre::Viewport *vp, Ogre::SceneManager *sm, Ogre::Camera *cam); |
---|
| 57 | ~DeferredShadingSystem(); |
---|
| 58 | |
---|
| 59 | enum DSMode |
---|
| 60 | { |
---|
| 61 | DSM_SINGLEPASS = 0, // Single pass + two lights |
---|
| 62 | DSM_MULTIPASS = 1, // Multi pass |
---|
| 63 | DSM_SHOWCOLOUR = 2, // Show diffuse (for debugging) |
---|
| 64 | DSM_SHOWNORMALS = 3, // Show normals (for debugging) |
---|
| 65 | DSM_SHOWDSP = 4, // Show depth and specular channel (for debugging) |
---|
| 66 | DSM_COUNT = 5 |
---|
| 67 | }; |
---|
| 68 | |
---|
| 69 | /** Set rendering mode (one of DSMode) |
---|
| 70 | */ |
---|
| 71 | void setMode(DSMode mode); |
---|
| 72 | |
---|
| 73 | /** Activate or deactivate system |
---|
| 74 | */ |
---|
| 75 | void setActive(bool active); |
---|
| 76 | |
---|
| 77 | /** Create a new MiniLight |
---|
| 78 | */ |
---|
| 79 | MLight *createMLight(); |
---|
| 80 | |
---|
| 81 | /** Destroy a MiniLight |
---|
| 82 | */ |
---|
| 83 | void destroyMLight(MLight *m); |
---|
| 84 | |
---|
| 85 | /** Update fat (origin) render target |
---|
| 86 | */ |
---|
| 87 | void update(); |
---|
| 88 | |
---|
| 89 | /// Visibility mask for scene |
---|
| 90 | static const Ogre::uint32 SceneVisibilityMask = 0x00000001; |
---|
| 91 | /// Visibility mask for post-processing geometry (lights, unlit particles) |
---|
| 92 | static const Ogre::uint32 PostVisibilityMask = 0x00000002; |
---|
| 93 | |
---|
| 94 | /** @copydoc CompositorInstance::Listener::notifyMaterialSetup |
---|
| 95 | */ |
---|
| 96 | virtual void notifyMaterialSetup(Ogre::uint32 pass_id, Ogre::MaterialPtr &mat); |
---|
| 97 | protected: |
---|
| 98 | Ogre::Viewport *mViewport; |
---|
| 99 | Ogre::SceneManager *mSceneMgr; |
---|
| 100 | Ogre::Camera *mCamera; |
---|
| 101 | |
---|
| 102 | // Fat render target |
---|
| 103 | Ogre::MultiRenderTarget *rttTex; |
---|
| 104 | // Filters |
---|
| 105 | Ogre::CompositorInstance *mInstance[DSM_COUNT]; |
---|
| 106 | // Active/inactive |
---|
| 107 | bool mActive; |
---|
| 108 | DSMode mCurrentMode; |
---|
| 109 | |
---|
| 110 | std::set<MLight*> mLights; |
---|
| 111 | Ogre::TexturePtr mTexture0, mTexture1; |
---|
| 112 | |
---|
| 113 | MaterialGenerator *mLightMaterialGenerator; |
---|
| 114 | |
---|
| 115 | void createResources(); |
---|
| 116 | void initialiseLightGeometry(); |
---|
| 117 | void setupMaterial(const Ogre::MaterialPtr &mat); |
---|
| 118 | }; |
---|
| 119 | |
---|
| 120 | #endif |
---|