[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 | If a shadowMap is specified that is not NULL, the shadow mapped shading algorithm is applied.
|
---|
| 39 | */
|
---|
[2900] | 40 | void Render(FrameBufferObject *fbo,
|
---|
[2952] | 41 | DirectionalLight *light,
|
---|
[2991] | 42 | ShadowMap *shadowMap = NULL
|
---|
| 43 | );
|
---|
[2859] | 44 |
|
---|
[3021] | 45 |
|
---|
[2930] | 46 | enum SAMPLING_METHOD {SAMPLING_POISSON, SAMPLING_QUADRATIC, SAMPLING_DEFAULT};
|
---|
[3216] | 47 | /** Use ssao or ssao + color bleeding
|
---|
| 48 | */
|
---|
[2896] | 49 | enum SHADING_METHOD {DEFAULT, SSAO, GI};
|
---|
[3068] | 50 | /** Set the samplig method for the indirect illumination
|
---|
| 51 | */
|
---|
[2896] | 52 | void SetSamplingMethod(SAMPLING_METHOD s);
|
---|
[3068] | 53 | /** Set the shading method (SSAO, SSAO + color bleeding
|
---|
| 54 | */
|
---|
[2897] | 55 | void SetShadingMethod(SHADING_METHOD s);
|
---|
[3242] | 56 | /** Sort the samples in order to provide faster texture accesses.
|
---|
[3216] | 57 | */
|
---|
[3242] | 58 | //void SetSortSamples(bool sortSamples);
|
---|
[3216] | 59 | /** Sets ssao sample intensity.
|
---|
| 60 | */
|
---|
[3212] | 61 | void SetSampleIntensity(float sampleIntensity);
|
---|
[3216] | 62 | /** Sets ssao kernel radius.
|
---|
| 63 | */
|
---|
[3212] | 64 | void SetKernelRadius(float kernelRadius);
|
---|
[3216] | 65 | /** Sets ssao filter radius.
|
---|
| 66 | */
|
---|
| 67 | void SetSsaoFilterRadius(float radius);
|
---|
[3242] | 68 | /** Passes the number of pixels that are visible from the sun.
|
---|
[3215] | 69 | */
|
---|
| 70 | void SetSunVisiblePixels(int visiblePixels);
|
---|
[3242] | 71 | /** If true temporal coherence is used for ssao
|
---|
[3216] | 72 | */
|
---|
| 73 | void SetUseTemporalCoherence(bool temporal);
|
---|
[3219] | 74 | /** if set to something other than -1 the current frame is stored on disc
|
---|
| 75 | using the specified frame number
|
---|
| 76 | */
|
---|
[3220] | 77 | void SetSaveFrame(const std::string &suffix, int frameNumber);
|
---|
[3242] | 78 | /** Sets the maximal visible distance.
|
---|
| 79 | */
|
---|
[3235] | 80 | void SetMaxDistance(float maxDist);
|
---|
[3242] | 81 | /** Enables / disables toneMapping
|
---|
| 82 | */
|
---|
| 83 | void SetUseToneMapping(bool toneMapping);
|
---|
| 84 |
|
---|
| 85 | /** Enable antiAliasing if some basic edge antialiasing should be performed
|
---|
| 86 | */
|
---|
| 87 | void SetUseAntiAliasing(bool antiAliasing);
|
---|
| 88 | /** Enables / disables depth of field.
|
---|
| 89 | */
|
---|
| 90 | void SetUseDepthOfField(bool dof);
|
---|
| 91 | /**
|
---|
| 92 | The temporal coherence factor is the maximal number of ssao samples that are accumulated
|
---|
| 93 | without losing any prior sample information.
|
---|
| 94 | Set this number too low and flickering can be seen, too
|
---|
| 95 | high and the adaption to some changes in the ssao might be too slow. Usually from about
|
---|
| 96 | 1000 samples a flickering cannot be seen.
|
---|
| 97 | */
|
---|
| 98 | void SetTemporalCoherenceFactorForSsao(float factor);
|
---|
[3212] | 99 |
|
---|
[3235] | 100 |
|
---|
[3068] | 101 | // hack: store the color buffer idx for the currently used flip flop-MRT here
|
---|
[3038] | 102 | // TODO matt: make this less hacky
|
---|
[2976] | 103 | static int colorBufferIdx;
|
---|
[2887] | 104 |
|
---|
[3038] | 105 |
|
---|
[2859] | 106 | protected:
|
---|
| 107 |
|
---|
[3085] | 108 | void ComputeSsao(FrameBufferObject *fbo, float tempCohFactor);
|
---|
[2859] | 109 |
|
---|
[3085] | 110 | void ComputeGlobIllum(FrameBufferObject *fbo, float tempCohFactor);
|
---|
[2873] | 111 |
|
---|
[2952] | 112 | void FirstPass(FrameBufferObject *fbo, DirectionalLight *light);
|
---|
[2868] | 113 |
|
---|
[2952] | 114 | void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap);
|
---|
[2896] | 115 |
|
---|
[3038] | 116 | void ComputeToneParameters(FrameBufferObject *fbo,
|
---|
| 117 | DirectionalLight *light,
|
---|
| 118 | float &imageKey,
|
---|
| 119 | float &whiteLum,
|
---|
| 120 | float &middleGrey);
|
---|
[2972] | 121 |
|
---|
[3007] | 122 | void ToneMap(FrameBufferObject *fbo, float imageKey, float whiteLum, float middleGrey);
|
---|
[2972] | 123 |
|
---|
[2973] | 124 |
|
---|
[2880] | 125 | void CombineSsao(FrameBufferObject *fbo);
|
---|
[3242] | 126 |
|
---|
[2880] | 127 | void CombineIllum(FrameBufferObject *fbo);
|
---|
[3219] | 128 | /** Does some basic antialiasing (searches for edges using a edge detector,
|
---|
| 129 | smoothes these edges.
|
---|
| 130 | This function is usually the last function in the pipeline,
|
---|
| 131 | so one can specify if the frame should be put out directly or stored to
|
---|
| 132 | another texture.
|
---|
| 133 | */
|
---|
| 134 | void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light, bool displayFrame = true);
|
---|
[3006] | 135 | /** Downsample buffer of fbo to buffer of downSampleFbo. The downSampleFbo must have half the
|
---|
| 136 | resolution of fbo.
|
---|
| 137 | */
|
---|
[3137] | 138 | void DownSample(FrameBufferObject *fbo,
|
---|
| 139 | int bufferIdx,
|
---|
| 140 | FrameBufferObject *downSampleFbo,
|
---|
| 141 | int downSampleBufferIdx,
|
---|
| 142 | ShaderProgram *program);
|
---|
[2972] | 143 |
|
---|
[3026] | 144 | void DrawQuad(ShaderProgram *p);
|
---|
[2972] | 145 |
|
---|
[3132] | 146 | void Output(FrameBufferObject *fbo);
|
---|
| 147 |
|
---|
[3038] | 148 | /** Initialises the deferred shader and loads the required shaders:
|
---|
| 149 | This function has to be called only once.
|
---|
| 150 | */
|
---|
| 151 | void InitCg();
|
---|
[3085] | 152 | /** Is called once per render call and sets the most important parameters.
|
---|
| 153 | */
|
---|
| 154 | void InitFrame();
|
---|
[3026] | 155 |
|
---|
[3132] | 156 | void FlipFbos(FrameBufferObject *fbo);
|
---|
[3026] | 157 |
|
---|
[3155] | 158 | void PrepareSsao(FrameBufferObject *fbo);
|
---|
[3132] | 159 |
|
---|
[3167] | 160 | void SortSamples();
|
---|
[3155] | 161 |
|
---|
[3213] | 162 | void LenseFlare(FrameBufferObject *fbo, DirectionalLight *light);
|
---|
| 163 |
|
---|
[3216] | 164 | void PrepareSsaoFilter();
|
---|
[3155] | 165 |
|
---|
[3219] | 166 | void SaveFrame(FrameBufferObject *fbo);
|
---|
[3216] | 167 |
|
---|
[3232] | 168 | void DepthOfField(FrameBufferObject *fbo);
|
---|
[3219] | 169 |
|
---|
[3232] | 170 |
|
---|
[3235] | 171 |
|
---|
[2859] | 172 | ////////////
|
---|
| 173 |
|
---|
[3215] | 174 | /// deferred shading output image width
|
---|
[2859] | 175 | int mWidth;
|
---|
[3215] | 176 | /// deferred shading output image height
|
---|
[2859] | 177 | int mHeight;
|
---|
| 178 |
|
---|
[3216] | 179 | //////////////////
|
---|
| 180 |
|
---|
| 181 |
|
---|
[3062] | 182 | PerspectiveCamera *mCamera;
|
---|
[2860] | 183 |
|
---|
[2875] | 184 | bool mUseTemporalCoherence;
|
---|
[2887] | 185 |
|
---|
[2896] | 186 | int mSamplingMethod;
|
---|
| 187 |
|
---|
| 188 | int mShadingMethod;
|
---|
| 189 |
|
---|
| 190 | bool mRegenerateSamples;
|
---|
[2976] | 191 |
|
---|
[3019] | 192 | int mIllumFboIndex;
|
---|
[3215] | 193 | /// the fbo for indirect illumination (ssao + color bleeding)
|
---|
[3019] | 194 | FrameBufferObject *mIllumFbo;
|
---|
[2994] | 195 |
|
---|
| 196 | FrameBufferObject *mDownSampleFbo;
|
---|
[3019] | 197 |
|
---|
| 198 | FBOContainer mFBOs;
|
---|
| 199 |
|
---|
[3085] | 200 | Matrix4x4 mProjViewMatrix;
|
---|
| 201 | Matrix4x4 mOldProjViewMatrix;
|
---|
| 202 |
|
---|
| 203 | Vector3 mCornersView[4];
|
---|
| 204 | Vector3 mOldCornersView[4];
|
---|
| 205 |
|
---|
| 206 | Vector3 mEyePos;
|
---|
| 207 | Vector3 mOldEyePos;
|
---|
[3189] | 208 |
|
---|
| 209 | bool mSortSamples;
|
---|
[3212] | 210 |
|
---|
| 211 | float mKernelRadius;
|
---|
[3216] | 212 | float mSsaoFilterRadius;
|
---|
[3212] | 213 | float mSampleIntensity;
|
---|
[3215] | 214 |
|
---|
| 215 | int mSunVisiblePixels;
|
---|
[3219] | 216 |
|
---|
| 217 | int mSavedFrameNumber;
|
---|
[3220] | 218 | std::string mSavedFrameSuffix;
|
---|
[3235] | 219 |
|
---|
| 220 | float mMaxDistance;
|
---|
[3242] | 221 |
|
---|
| 222 | float mTempCohFactor;
|
---|
| 223 | bool mUseToneMapping;
|
---|
| 224 | bool mUseAntiAliasing;
|
---|
| 225 | bool mUseDepthOfField;
|
---|
[2858] | 226 | };
|
---|
| 227 |
|
---|
[3038] | 228 |
|
---|
[2858] | 229 | } // namespace
|
---|
[2886] | 230 |
|
---|
[3219] | 231 | #endif // _SSAOSHADER_H__ |
---|