source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredRenderer.h @ 3019

Revision 3019, 3.9 KB checked in by mattausch, 16 years ago (diff)

detected memory leaks mainly in shadowmapping!!
strange problems with deferred rendering, seems to be uninitialized sometimes (solved?)

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