source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/DeferredShader.cpp @ 2868

Revision 2868, 6.6 KB checked in by mattausch, 16 years ago (diff)

changed ssao to multipass algorithm

Line 
1#include "DeferredShader.h"
2#include "FrameBufferObject.h"
3#include "RenderState.h"
4
5
6using namespace std;
7
8
9namespace CHCDemoEngine
10{
11
12
13static void PrintGLerror(char *msg)
14{
15        GLenum errCode;
16        const GLubyte *errStr;
17       
18        if ((errCode = glGetError()) != GL_NO_ERROR)
19        {
20                errStr = gluErrorString(errCode);
21                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
22        }
23}
24
25
26
27static CGprogram sCgDeferredProgram;
28static CGprogram sCgAntiAliasingProgram;
29
30
31static CGparameter sColorsTexParam;
32static CGparameter sPositionsTexParam;
33static CGparameter sNormalsTexParam;
34
35static CGparameter sColorsTexAntiAliasingParam;
36static CGparameter sNormalsTexAntiAliasingParam;
37
38
39DeferredShader::DeferredShader(int w, int h):
40mWidth(w), mHeight(h)
41{
42        mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
43        // the diffuse color buffer
44        mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
45}
46
47
48DeferredShader::~DeferredShader()
49{
50        if (sCgDeferredProgram)
51                cgDestroyProgram(sCgDeferredProgram);
52}
53
54
55void DeferredShader::Init(CGcontext context)
56{
57        sCgDeferredProgram =
58                cgCreateProgramFromFile(context,
59                                                                CG_SOURCE,
60                                                                "src/shaders/deferred.cg",
61                                                                RenderState::sCgFragmentProfile,
62                                                                "main",
63                                                                NULL);
64
65        if (sCgDeferredProgram != NULL)
66        {
67                cgGLLoadProgram(sCgDeferredProgram);
68
69                // we need size of texture for scaling
70                sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions"); 
71                sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors"); 
72                sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
73        }
74        else
75                cerr << "deferred program failed to load" << endl;
76
77        sCgAntiAliasingProgram =
78                cgCreateProgramFromFile(context,
79                                                                CG_SOURCE,
80                                                                "src/shaders/antialiasing.cg",
81                                                                RenderState::sCgFragmentProfile,
82                                                                "main",
83                                                                NULL);
84
85        if (sCgAntiAliasingProgram != NULL)
86        {
87                cgGLLoadProgram(sCgAntiAliasingProgram);
88
89                sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors"); 
90                sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
91        }
92        else
93                cerr << "antialiasing program failed to load" << endl;
94
95
96        PrintGLerror("init");
97}
98
99
100void DeferredShader::Render(FrameBufferObject *fbo)
101{
102        FirstPass(fbo);
103        AntiAliasing(fbo);
104}
105
106
107void DeferredShader::FirstPass(FrameBufferObject *fbo)
108{
109        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
110        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
111        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
112
113        mFbo->Bind();
114
115        glPushAttrib(GL_VIEWPORT_BIT);
116        glViewport(0, 0, mWidth, mHeight);
117
118        glDisable(GL_ALPHA_TEST);
119        glDisable(GL_TEXTURE_2D);
120        glDisable(GL_LIGHTING);
121       
122        glMatrixMode(GL_PROJECTION);
123        glPushMatrix();
124        glLoadIdentity();
125
126        glMatrixMode(GL_MODELVIEW);
127        glPushMatrix();
128        glLoadIdentity();
129
130        const float offs = 0.5f;
131       
132        glOrtho(-offs, offs, -offs, offs, 0, 1);
133        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
134
135        cgGLEnableProfile(RenderState::sCgFragmentProfile);
136
137        cgGLBindProgram(sCgDeferredProgram);
138
139        cgGLSetTextureParameter(sColorsTexParam, colorsTex);
140        cgGLEnableTextureParameter(sColorsTexParam);
141
142        cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
143        cgGLEnableTextureParameter(sPositionsTexParam);
144
145        cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
146        cgGLEnableTextureParameter(sNormalsTexParam);
147       
148        glColor3f(1.0f, 1.0f, 1.0f);
149       
150        glBegin(GL_QUADS);
151
152        float offs2 = 0.5f;
153
154        glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
155        glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
156        glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
157        glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
158
159        glEnd();
160
161        cgGLDisableTextureParameter(sColorsTexParam);
162        cgGLDisableTextureParameter(sPositionsTexParam);
163        cgGLDisableTextureParameter(sNormalsTexParam);
164
165        cgGLDisableProfile(RenderState::sCgFragmentProfile);
166       
167        glEnable(GL_LIGHTING);
168        glDisable(GL_TEXTURE_2D);
169       
170        glMatrixMode(GL_PROJECTION);
171        glPopMatrix();
172
173        glMatrixMode(GL_MODELVIEW);
174        glPopMatrix();
175
176        glPopAttrib();
177
178        FrameBufferObject::Release();
179
180        PrintGLerror("deferred shading");
181}
182
183
184static void SetVertex(float x, float y, float x_offs, float y_offs)
185{
186        glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
187        glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
188        glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
189        glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
190        glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
191
192        glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
193        glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
194
195        glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
196}
197
198
199void DeferredShader::AntiAliasing(FrameBufferObject *fbo)
200{
201        GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture();
202        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
203
204        glPushAttrib(GL_VIEWPORT_BIT);
205        glViewport(0, 0, mWidth, mHeight);
206
207        glDisable(GL_ALPHA_TEST);
208        glDisable(GL_TEXTURE_2D);
209        glDisable(GL_LIGHTING);
210       
211        glMatrixMode(GL_PROJECTION);
212        glPushMatrix();
213        glLoadIdentity();
214
215        glMatrixMode(GL_MODELVIEW);
216        glPushMatrix();
217        glLoadIdentity();
218
219        const float offs = 0.5f;
220       
221        glOrtho(-offs, offs, -offs, offs, 0, 1);
222        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
223
224        cgGLEnableProfile(RenderState::sCgFragmentProfile);
225
226        cgGLBindProgram(sCgAntiAliasingProgram);
227
228        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
229        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
230
231        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
232        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
233       
234        glColor3f(1.0f, 1.0f, 1.0f);
235
236        float offs2 = 0.5f;
237
238        glBegin(GL_QUADS);
239
240        // the neighbouring texels
241        float x_offs = 1.0f / mWidth;
242        float y_offs = 1.0f / mHeight;
243
244        SetVertex(0, 0, x_offs, y_offs);
245        SetVertex(1, 0, x_offs, y_offs);
246        SetVertex(1, 1, x_offs, y_offs);
247        SetVertex(0, 1, x_offs, y_offs);
248
249        glEnd();
250
251        cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
252        cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
253
254        cgGLDisableProfile(RenderState::sCgFragmentProfile);
255       
256        glEnable(GL_LIGHTING);
257        glDisable(GL_TEXTURE_2D);
258       
259        glMatrixMode(GL_PROJECTION);
260        glPopMatrix();
261
262        glMatrixMode(GL_MODELVIEW);
263        glPopMatrix();
264
265        glPopAttrib();
266
267        PrintGLerror("deferred shading");
268}
269
270
271} // namespace
Note: See TracBrowser for help on using the repository browser.