1 | #ifndef _DEFERREDRENDERER_H__
|
---|
2 | #define _DEFERREDRENDERER_H__
|
---|
3 |
|
---|
4 | #include "common.h"
|
---|
5 | #include "glInterface.h"
|
---|
6 | #include "ShaderProgram.h"
|
---|
7 |
|
---|
8 | #include <Cg/cg.h>
|
---|
9 | #include <Cg/cgGL.h>
|
---|
10 |
|
---|
11 |
|
---|
12 | namespace CHCDemoEngine
|
---|
13 | {
|
---|
14 |
|
---|
15 | class FrameBufferObject;
|
---|
16 | class Vector3;
|
---|
17 | class PerspectiveCamera;
|
---|
18 | class Matrix4x4;
|
---|
19 | class ShadowMap;
|
---|
20 | class DirectionalLight;
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 |
|
---|
25 | /** This class implements a deferred shading algorithm that takes
|
---|
26 | a frame buffer object as input and outputs an image in the given size
|
---|
27 | */
|
---|
28 | class DeferredRenderer
|
---|
29 | {
|
---|
30 | public:
|
---|
31 | /** Constructor for a deferred shader taking the requested output image size,
|
---|
32 | the current camera, and a scaling factor.
|
---|
33 |
|
---|
34 | The parameter scaleFactor must be reciprocal value of the
|
---|
35 | scale factor used for creating the world space position texture. It is used recover the
|
---|
36 | exact scene size that was scaled in order to improve floating point precision.
|
---|
37 | */
|
---|
38 | DeferredRenderer(int w, int h, PerspectiveCamera *cam, float scaleFactor);
|
---|
39 | /** The algorithm renders the scene given an fbo consists of 1 color buffer,
|
---|
40 | 1 position buffer, and 1 normal buffer.
|
---|
41 | We also need the projection view matrix of the last frame for reprojection, and
|
---|
42 | a smoothing factor for temporal coherence
|
---|
43 | */
|
---|
44 |
|
---|
45 | ~DeferredRenderer();
|
---|
46 |
|
---|
47 | void Render(FrameBufferObject *fbo,
|
---|
48 | const Matrix4x4 &oldProjViewMatrix,
|
---|
49 | const Matrix4x4 &projViewMatrix,
|
---|
50 | float tempCohFactor,
|
---|
51 | DirectionalLight *light,
|
---|
52 | bool useToneMapping,
|
---|
53 | ShadowMap *shadowMap = NULL
|
---|
54 | );
|
---|
55 |
|
---|
56 |
|
---|
57 | void SetUseTemporalCoherence(bool temporal);
|
---|
58 |
|
---|
59 | enum SAMPLING_METHOD {SAMPLING_POISSON, SAMPLING_QUADRATIC, SAMPLING_DEFAULT};
|
---|
60 | enum SHADING_METHOD {DEFAULT, SSAO, GI};
|
---|
61 |
|
---|
62 | void SetSamplingMethod(SAMPLING_METHOD s);
|
---|
63 |
|
---|
64 | void SetShadingMethod(SHADING_METHOD s);
|
---|
65 |
|
---|
66 | // hack: store the color buffer idx for the first flipflip-mrt here
|
---|
67 | // TODO matt: make this less hacky
|
---|
68 | static int colorBufferIdx;
|
---|
69 |
|
---|
70 |
|
---|
71 | protected:
|
---|
72 |
|
---|
73 | void ComputeSsao(FrameBufferObject *fbo,
|
---|
74 | float tempCohFactor,
|
---|
75 | const Matrix4x4 &projViewMatrix,
|
---|
76 | const Matrix4x4 &oldProjViewMatrix
|
---|
77 | );
|
---|
78 |
|
---|
79 | void ComputeGlobIllum(FrameBufferObject *fbo,
|
---|
80 | float tempCohFactor,
|
---|
81 | const Matrix4x4 &projViewMatrix,
|
---|
82 | const Matrix4x4 &oldProjViewMatrix);
|
---|
83 |
|
---|
84 | void FirstPass(FrameBufferObject *fbo, DirectionalLight *light);
|
---|
85 |
|
---|
86 | void FirstPassShadow(FrameBufferObject *fbo, DirectionalLight *light, ShadowMap *shadowMap);
|
---|
87 |
|
---|
88 | void ComputeToneParameters(FrameBufferObject *fbo,
|
---|
89 | DirectionalLight *light,
|
---|
90 | float &imageKey,
|
---|
91 | float &whiteLum,
|
---|
92 | float &middleGrey);
|
---|
93 |
|
---|
94 | void ToneMap(FrameBufferObject *fbo, float imageKey, float whiteLum, float middleGrey);
|
---|
95 |
|
---|
96 |
|
---|
97 | void CombineSsao(FrameBufferObject *fbo);
|
---|
98 | void CombineIllum(FrameBufferObject *fbo);
|
---|
99 |
|
---|
100 | void AntiAliasing(FrameBufferObject *fbo, DirectionalLight *light);
|
---|
101 | /** Helper method that computes the view vectors in the corners of the current view frustum.
|
---|
102 | */
|
---|
103 | void ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br);
|
---|
104 |
|
---|
105 | /** Downsample buffer of fbo to buffer of downSampleFbo. The downSampleFbo must have half the
|
---|
106 | resolution of fbo.
|
---|
107 | */
|
---|
108 | void DownSample(FrameBufferObject *fbo, int bufferIdx,
|
---|
109 | FrameBufferObject *downSampleFbo, int downSampleBufferIdx);
|
---|
110 |
|
---|
111 | void DrawQuad(ShaderProgram *p);
|
---|
112 |
|
---|
113 | /** Initialises the deferred shader and loads the required shaders:
|
---|
114 | This function has to be called only once.
|
---|
115 | */
|
---|
116 | void InitCg();
|
---|
117 |
|
---|
118 |
|
---|
119 | ////////////
|
---|
120 |
|
---|
121 | int mWidth;
|
---|
122 | int mHeight;
|
---|
123 |
|
---|
124 | PerspectiveCamera *mCamera;
|
---|
125 |
|
---|
126 | bool mUseTemporalCoherence;
|
---|
127 |
|
---|
128 | int mSamplingMethod;
|
---|
129 |
|
---|
130 | int mShadingMethod;
|
---|
131 |
|
---|
132 | bool mRegenerateSamples;
|
---|
133 |
|
---|
134 | int mIllumFboIndex;
|
---|
135 | // the fbo for indirect illumination (ssao + color bleeding)
|
---|
136 | FrameBufferObject *mIllumFbo;
|
---|
137 |
|
---|
138 | FrameBufferObject *mDownSampleFbo;
|
---|
139 |
|
---|
140 | FBOContainer mFBOs;
|
---|
141 |
|
---|
142 | static ShaderContainer sShaders;
|
---|
143 | };
|
---|
144 |
|
---|
145 |
|
---|
146 | } // namespace
|
---|
147 |
|
---|
148 | #endif // _SsaoShader_H__ |
---|