#ifndef _DEFERREDRENDERER_H__ #define _DEFERREDRENDERER_H__ #include "common.h" #include "glInterface.h" #include #include namespace CHCDemoEngine { class FrameBufferObject; class Vector3; class Camera; 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, and a scaling factor. The parameter scaleFactor must be reciprocal value of the scale factor used for creating the world space position texture. It is used recover the exact scene size that was scaled in order to improve floating point precision. */ DeferredRenderer(int w, int h, Camera *cam, float scaleFactor); /** 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 */ void Render(FrameBufferObject *fbo, const Matrix4x4 &oldProjViewMatrix, const Matrix4x4 &projViewMatrix, float tempCohFactor, DirectionalLight *light, bool useToneMapping, ShadowMap *shadowMap = NULL ); /** Initialises the deferred shader and loads the required shaders: This function has to be called only once. */ static void Init(CGcontext context); ~DeferredRenderer(); void SetUseTemporalCoherence(bool temporal); enum SAMPLING_METHOD {SAMPLING_POISSON, SAMPLING_QUADRATIC, SAMPLING_DEFAULT}; enum SHADING_METHOD {DEFAULT, SSAO, GI}; void SetSamplingMethod(SAMPLING_METHOD s); void SetShadingMethod(SHADING_METHOD s); static int colorBufferIdx; protected: void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor, const Matrix4x4 &oldProjViewMatrix, const Matrix4x4 &projViewMatrix); void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor, const Matrix4x4 &oldProjViewMatrix); 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, DirectionalLight *light, float imageKey, float whiteLum, float middleGrey); void CombineSsao(FrameBufferObject *fbo); void CombineIllum(FrameBufferObject *fbo); void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light); /** Helper method that computes the view vectors in the corners of the current view frustum. */ void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br); void DownSample(FrameBufferObject *fbo); //////////// int mWidth; int mHeight; /// a scale factor of scene positions in order to get better float precision in the shader float mScaleFactor; Camera *mCamera; bool mUseTemporalCoherence; int mSamplingMethod; int mFboIndex; int mShadingMethod; bool mRegenerateSamples; // the main fbo we are working with FrameBufferObject *mFbo; FrameBufferObject *mDownSampleFbo; }; } // namespace #endif // _SsaoShader_H__