#ifndef _DEFERREDRENDERER_H__ #define _DEFERREDRENDERER_H__ #include "common.h" #include "Matrix4x4.h" #include "Vector3.h" namespace CHCDemoEngine { class FrameBufferObject; class Vector3; class PerspectiveCamera; class Matrix4x4; class ShadowMap; class DirectionalLight; /** This class implements a deferred shading algorithm that takes a frame buffer object as input and outputs an image in the given size */ class DeferredRenderer { public: /** Constructor for a deferred shader taking the requested output image size, the current camera; */ DeferredRenderer(int w, int h, PerspectiveCamera *cam); /** The algorithm renders the scene given an fbo consists of 1 color buffer, 1 position buffer, and 1 normal buffer. We also need the projection view matrix of the last frame for reprojection, and a smoothing factor for temporal coherence */ ~DeferredRenderer(); void Render(FrameBufferObject *fbo, float tempCohFactor, DirectionalLight *light, bool useToneMapping, ShadowMap *shadowMap = NULL ); void SetUseTemporalCoherence(bool temporal); enum SAMPLING_METHOD {SAMPLING_POISSON, SAMPLING_QUADRATIC, SAMPLING_DEFAULT}; enum SHADING_METHOD {DEFAULT, SSAO, GI}; /** Set the samplig method for the indirect illumination */ void SetSamplingMethod(SAMPLING_METHOD s); /** Set the shading method (SSAO, SSAO + color bleeding */ void SetShadingMethod(SHADING_METHOD s); // hack: store the color buffer idx for the currently used flip flop-MRT here // TODO matt: make this less hacky static int colorBufferIdx; protected: void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor); void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor); void FirstPass(FrameBufferObject *fbo, DirectionalLight *light); void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap); void ComputeToneParameters(FrameBufferObject *fbo, DirectionalLight *light, float &imageKey, float &whiteLum, float &middleGrey); void ToneMap(FrameBufferObject *fbo, float imageKey, float whiteLum, float middleGrey); void CombineSsao(FrameBufferObject *fbo); void CombineIllum(FrameBufferObject *fbo); void SmoothSsao(FrameBufferObject *fbo); void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light); /** Downsample buffer of fbo to buffer of downSampleFbo. The downSampleFbo must have half the resolution of fbo. */ void DownSample(FrameBufferObject *fbo, int bufferIdx, FrameBufferObject *downSampleFbo, int downSampleBufferIdx, ShaderProgram *program); void DrawQuad(ShaderProgram *p); void Output(FrameBufferObject *fbo); /** Initialises the deferred shader and loads the required shaders: This function has to be called only once. */ void InitCg(); /** Is called once per render call and sets the most important parameters. */ void InitFrame(); void FlipFbos(FrameBufferObject *fbo); //////////// int mWidth; int mHeight; PerspectiveCamera *mCamera; bool mUseTemporalCoherence; int mSamplingMethod; int mShadingMethod; bool mRegenerateSamples; int mIllumFboIndex; // the fbo for indirect illumination (ssao + color bleeding) FrameBufferObject *mIllumFbo; FrameBufferObject *mDownSampleFbo; FBOContainer mFBOs; static ShaderContainer sShaders; Matrix4x4 mProjViewMatrix; Matrix4x4 mOldProjViewMatrix; Vector3 mCornersView[4]; Vector3 mOldCornersView[4]; Vector3 mEyePos; Vector3 mOldEyePos; }; } // namespace #endif // _SsaoShader_H__