[2952] | 1 | #ifndef _DEFERREDRENDERER_H__
|
---|
| 2 | #define _DEFERREDRENDERER_H__
|
---|
[2858] | 3 |
|
---|
| 4 | #include "common.h"
|
---|
[3085] | 5 | #include "Matrix4x4.h"
|
---|
| 6 | #include "Vector3.h"
|
---|
[2896] | 7 |
|
---|
[2858] | 8 | namespace CHCDemoEngine
|
---|
| 9 | {
|
---|
| 10 |
|
---|
| 11 | class FrameBufferObject;
|
---|
[2860] | 12 | class Vector3;
|
---|
[3062] | 13 | class PerspectiveCamera;
|
---|
[2861] | 14 | class Matrix4x4;
|
---|
[2896] | 15 | class ShadowMap;
|
---|
[2952] | 16 | class DirectionalLight;
|
---|
[2858] | 17 |
|
---|
[2896] | 18 |
|
---|
[3019] | 19 |
|
---|
| 20 |
|
---|
[2859] | 21 | /** This class implements a deferred shading algorithm that takes
|
---|
| 22 | a frame buffer object as input and outputs an image in the given size
|
---|
[2858] | 23 | */
|
---|
[2896] | 24 | class DeferredRenderer
|
---|
[2858] | 25 | {
|
---|
| 26 | public:
|
---|
[2874] | 27 | /** Constructor for a deferred shader taking the requested output image size,
|
---|
[3216] | 28 | the current camera, and if the ssao should be full or half resolution
|
---|
[2858] | 29 | */
|
---|
[3216] | 30 | DeferredRenderer(int w, int h, PerspectiveCamera *cam, bool ssaoUsefulResolution);
|
---|
| 31 | /** Destructor
|
---|
[2858] | 32 | */
|
---|
[3021] | 33 | ~DeferredRenderer();
|
---|
[3216] | 34 | /** The main render function
|
---|
| 35 | Currently our fbo consists of one combined color + depth buffer, a normal buffer,
|
---|
| 36 | and a buffer holding the difference of the pixel positions from the last frame.
|
---|
[3021] | 37 |
|
---|
[3216] | 38 | Set useToneMapping to true if tone mapping should be applied
|
---|
| 39 | Set useAntiAliasing true if some basic edge antialiasing should be performed
|
---|
| 40 | If a shadowMap is specified that is not NULL, the shadow mapped shading algorithm is applied.
|
---|
| 41 |
|
---|
| 42 | The temporal coherence factor is the maximal number of ssao samples that are accumulated
|
---|
| 43 | without losing any prior sample information.
|
---|
| 44 | Set this number too low and flickering can be seen, too
|
---|
| 45 | high and the adaption to some changes in the ssao might be too slow. Usually from about
|
---|
| 46 | 1000 samples a flickering cannot be seen.
|
---|
| 47 | */
|
---|
[2900] | 48 | void Render(FrameBufferObject *fbo,
|
---|
[2901] | 49 | float tempCohFactor,
|
---|
[2952] | 50 | DirectionalLight *light,
|
---|
[2991] | 51 | bool useToneMapping,
|
---|
[3175] | 52 | bool useAntiAliasing,
|
---|
[2991] | 53 | ShadowMap *shadowMap = NULL
|
---|
| 54 | );
|
---|
[2859] | 55 |
|
---|
[3021] | 56 |
|
---|
[2930] | 57 | enum SAMPLING_METHOD {SAMPLING_POISSON, SAMPLING_QUADRATIC, SAMPLING_DEFAULT};
|
---|
[3216] | 58 | /** Use ssao or ssao + color bleeding
|
---|
| 59 | */
|
---|
[2896] | 60 | enum SHADING_METHOD {DEFAULT, SSAO, GI};
|
---|
[3068] | 61 | /** Set the samplig method for the indirect illumination
|
---|
| 62 | */
|
---|
[2896] | 63 | void SetSamplingMethod(SAMPLING_METHOD s);
|
---|
[3068] | 64 | /** Set the shading method (SSAO, SSAO + color bleeding
|
---|
| 65 | */
|
---|
[2897] | 66 | void SetShadingMethod(SHADING_METHOD s);
|
---|
[3216] | 67 | /** Sort the samples so texture access is faster
|
---|
| 68 | */
|
---|
[3215] | 69 | void SetSortSamples(bool sortSamples);
|
---|
[3216] | 70 | /** Sets ssao sample intensity.
|
---|
| 71 | */
|
---|
[3212] | 72 | void SetSampleIntensity(float sampleIntensity);
|
---|
[3216] | 73 | /** Sets ssao kernel radius.
|
---|
| 74 | */
|
---|
[3212] | 75 | void SetKernelRadius(float kernelRadius);
|
---|
[3216] | 76 | /** Sets ssao filter radius.
|
---|
| 77 | */
|
---|
| 78 | void SetSsaoFilterRadius(float radius);
|
---|
[3215] | 79 | /** Sets the number of visible pixels of the sun
|
---|
| 80 | */
|
---|
| 81 | void SetSunVisiblePixels(int visiblePixels);
|
---|
[3216] | 82 | /** If true tem poral coherence is used for ssao
|
---|
| 83 | */
|
---|
| 84 | void SetUseTemporalCoherence(bool temporal);
|
---|
[3219] | 85 | /** if set to something other than -1 the current frame is stored on disc
|
---|
| 86 | using the specified frame number
|
---|
| 87 | */
|
---|
[3220] | 88 | void SetSaveFrame(const std::string &suffix, int frameNumber);
|
---|
[3212] | 89 |
|
---|
| 90 |
|
---|
[3068] | 91 | // hack: store the color buffer idx for the currently used flip flop-MRT here
|
---|
[3038] | 92 | // TODO matt: make this less hacky
|
---|
[2976] | 93 | static int colorBufferIdx;
|
---|
[2887] | 94 |
|
---|
[3038] | 95 |
|
---|
[2859] | 96 | protected:
|
---|
| 97 |
|
---|
[3085] | 98 | void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor);
|
---|
[2859] | 99 |
|
---|
[3085] | 100 | void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor);
|
---|
[2873] | 101 |
|
---|
[2952] | 102 | void FirstPass(FrameBufferObject *fbo, DirectionalLight *light);
|
---|
[2868] | 103 |
|
---|
[2952] | 104 | void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap);
|
---|
[2896] | 105 |
|
---|
[3038] | 106 | void ComputeToneParameters(FrameBufferObject *fbo,
|
---|
| 107 | DirectionalLight *light,
|
---|
| 108 | float &imageKey,
|
---|
| 109 | float &whiteLum,
|
---|
| 110 | float &middleGrey);
|
---|
[2972] | 111 |
|
---|
[3007] | 112 | void ToneMap(FrameBufferObject *fbo, float imageKey, float whiteLum, float middleGrey);
|
---|
[2972] | 113 |
|
---|
[2973] | 114 |
|
---|
[2880] | 115 | void CombineSsao(FrameBufferObject *fbo);
|
---|
| 116 | void CombineIllum(FrameBufferObject *fbo);
|
---|
[3219] | 117 | /** Does some basic antialiasing (searches for edges using a edge detector,
|
---|
| 118 | smoothes these edges.
|
---|
| 119 | This function is usually the last function in the pipeline,
|
---|
| 120 | so one can specify if the frame should be put out directly or stored to
|
---|
| 121 | another texture.
|
---|
| 122 | */
|
---|
| 123 | void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light, bool displayFrame = true);
|
---|
[3006] | 124 | /** Downsample buffer of fbo to buffer of downSampleFbo. The downSampleFbo must have half the
|
---|
| 125 | resolution of fbo.
|
---|
| 126 | */
|
---|
[3137] | 127 | void DownSample(FrameBufferObject *fbo,
|
---|
| 128 | int bufferIdx,
|
---|
| 129 | FrameBufferObject *downSampleFbo,
|
---|
| 130 | int downSampleBufferIdx,
|
---|
| 131 | ShaderProgram *program);
|
---|
[2972] | 132 |
|
---|
[3026] | 133 | void DrawQuad(ShaderProgram *p);
|
---|
[2972] | 134 |
|
---|
[3132] | 135 | void Output(FrameBufferObject *fbo);
|
---|
| 136 |
|
---|
[3038] | 137 | /** Initialises the deferred shader and loads the required shaders:
|
---|
| 138 | This function has to be called only once.
|
---|
| 139 | */
|
---|
| 140 | void InitCg();
|
---|
[3085] | 141 | /** Is called once per render call and sets the most important parameters.
|
---|
| 142 | */
|
---|
| 143 | void InitFrame();
|
---|
[3026] | 144 |
|
---|
[3132] | 145 | void FlipFbos(FrameBufferObject *fbo);
|
---|
[3026] | 146 |
|
---|
[3155] | 147 | void PrepareSsao(FrameBufferObject *fbo);
|
---|
[3132] | 148 |
|
---|
[3167] | 149 | void SortSamples();
|
---|
[3155] | 150 |
|
---|
[3213] | 151 | void LenseFlare(FrameBufferObject *fbo, DirectionalLight *light);
|
---|
| 152 |
|
---|
[3216] | 153 | void PrepareSsaoFilter();
|
---|
[3155] | 154 |
|
---|
[3219] | 155 | void SaveFrame(FrameBufferObject *fbo);
|
---|
[3216] | 156 |
|
---|
[3219] | 157 |
|
---|
[2859] | 158 | ////////////
|
---|
| 159 |
|
---|
[3215] | 160 | /// deferred shading output image width
|
---|
[2859] | 161 | int mWidth;
|
---|
[3215] | 162 | /// deferred shading output image height
|
---|
[2859] | 163 | int mHeight;
|
---|
| 164 |
|
---|
[3216] | 165 | //////////////////
|
---|
| 166 |
|
---|
| 167 |
|
---|
[3062] | 168 | PerspectiveCamera *mCamera;
|
---|
[2860] | 169 |
|
---|
[2875] | 170 | bool mUseTemporalCoherence;
|
---|
[2887] | 171 |
|
---|
[2896] | 172 | int mSamplingMethod;
|
---|
| 173 |
|
---|
| 174 | int mShadingMethod;
|
---|
| 175 |
|
---|
| 176 | bool mRegenerateSamples;
|
---|
[2976] | 177 |
|
---|
[3019] | 178 | int mIllumFboIndex;
|
---|
[3215] | 179 | /// the fbo for indirect illumination (ssao + color bleeding)
|
---|
[3019] | 180 | FrameBufferObject *mIllumFbo;
|
---|
[2994] | 181 |
|
---|
| 182 | FrameBufferObject *mDownSampleFbo;
|
---|
[3019] | 183 |
|
---|
| 184 | FBOContainer mFBOs;
|
---|
| 185 |
|
---|
[3085] | 186 | Matrix4x4 mProjViewMatrix;
|
---|
| 187 | Matrix4x4 mOldProjViewMatrix;
|
---|
| 188 |
|
---|
| 189 | Vector3 mCornersView[4];
|
---|
| 190 | Vector3 mOldCornersView[4];
|
---|
| 191 |
|
---|
| 192 | Vector3 mEyePos;
|
---|
| 193 | Vector3 mOldEyePos;
|
---|
[3189] | 194 |
|
---|
| 195 | bool mSortSamples;
|
---|
[3212] | 196 |
|
---|
| 197 | float mKernelRadius;
|
---|
[3216] | 198 | float mSsaoFilterRadius;
|
---|
[3212] | 199 | float mSampleIntensity;
|
---|
[3215] | 200 |
|
---|
| 201 | int mSunVisiblePixels;
|
---|
[3219] | 202 |
|
---|
| 203 | int mSavedFrameNumber;
|
---|
[3220] | 204 | std::string mSavedFrameSuffix;
|
---|
[2858] | 205 | };
|
---|
| 206 |
|
---|
[3038] | 207 |
|
---|
[2858] | 208 | } // namespace
|
---|
[2886] | 209 |
|
---|
[3219] | 210 | #endif // _SSAOSHADER_H__ |
---|