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

Revision 3027, 4.0 KB checked in by mattausch, 16 years ago (diff)

implemented shader program wrapper

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