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

Revision 2892, 11.1 KB checked in by mattausch, 16 years ago (diff)
Line 
1#include "DeferredShader.h"
2#include "FrameBufferObject.h"
3#include "RenderState.h"
4#include "ShadowMapping.h"
5
6
7using namespace std;
8
9
10namespace CHCDemoEngine
11{
12
13
14static void PrintGLerror(char *msg)
15{
16        GLenum errCode;
17        const GLubyte *errStr;
18       
19        if ((errCode = glGetError()) != GL_NO_ERROR)
20        {
21                errStr = gluErrorString(errCode);
22                fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
23        }
24}
25
26
27static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT,  GL_COLOR_ATTACHMENT3_EXT};
28
29
30static CGprogram sCgDeferredProgram;
31static CGprogram sCgDeferredShadowProgram;
32static CGprogram sCgAntiAliasingProgram;
33
34
35static CGparameter sColorsTexParam;
36static CGparameter sPositionsTexParam;
37static CGparameter sNormalsTexParam;
38
39static CGparameter sColorsTexAntiAliasingParam;
40static CGparameter sNormalsTexAntiAliasingParam;
41
42static CGparameter sShadowMapParam;
43static CGparameter sPositionsTexShadowParam; 
44static CGparameter sColorsTexShadowParam; 
45static CGparameter sNormalsTexShadowParam;
46
47static CGparameter sShadowMatrixParam;
48
49
50
51
52DeferredShader::DeferredShader(int w, int h):
53mWidth(w), mHeight(h)
54{
55        //mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
56        //mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
57}
58
59
60DeferredShader::~DeferredShader()
61{
62        if (sCgDeferredProgram)
63                cgDestroyProgram(sCgDeferredProgram);
64
65        //DEL_PTR(mFbo);
66}
67
68
69void DeferredShader::Init(CGcontext context)
70{
71        sCgDeferredProgram =
72                cgCreateProgramFromFile(context,
73                                                                CG_SOURCE,
74                                                                "src/shaders/deferred.cg",
75                                                                RenderState::sCgFragmentProfile,
76                                                                "main",
77                                                                NULL);
78
79        if (sCgDeferredProgram != NULL)
80        {
81                cgGLLoadProgram(sCgDeferredProgram);
82
83                // we need size of texture for scaling
84                sPositionsTexParam = cgGetNamedParameter(sCgDeferredProgram, "positions"); 
85                sColorsTexParam = cgGetNamedParameter(sCgDeferredProgram, "colors"); 
86                sNormalsTexParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
87        }
88        else
89                cerr << "deferred program failed to load" << endl;
90
91        sCgAntiAliasingProgram =
92                cgCreateProgramFromFile(context,
93                                                                CG_SOURCE,
94                                                                "src/shaders/antialiasing.cg",
95                                                                RenderState::sCgFragmentProfile,
96                                                                "main",
97                                                                NULL);
98
99        if (sCgAntiAliasingProgram != NULL)
100        {
101                cgGLLoadProgram(sCgAntiAliasingProgram);
102
103                sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors"); 
104                sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
105        }
106        else
107                cerr << "antialiasing program failed to load" << endl;
108
109        sCgDeferredShadowProgram =
110                cgCreateProgramFromFile(context,
111                                                                CG_SOURCE,
112                                                                "src/shaders/deferred.cg",
113                                                                RenderState::sCgFragmentProfile,
114                                                                "main_shadow",
115                                                                NULL);
116
117        if (sCgDeferredShadowProgram != NULL)
118        {
119                cgGLLoadProgram(sCgDeferredShadowProgram);
120
121                // we need size of texture for scaling
122                sPositionsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "positions"); 
123                sColorsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "colors"); 
124                sNormalsTexShadowParam = cgGetNamedParameter(sCgDeferredShadowProgram, "normals");
125                sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap"); 
126
127                sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
128        }
129        else
130                cerr << "deferred program failed to load" << endl;
131
132        PrintGLerror("init");
133}
134
135
136void DeferredShader::Render(FrameBufferObject *fbo)
137{
138        cgGLEnableProfile(RenderState::sCgFragmentProfile);
139
140        glDisable(GL_ALPHA_TEST);
141        glDisable(GL_TEXTURE_2D);
142        glDisable(GL_LIGHTING);
143
144        glPushAttrib(GL_VIEWPORT_BIT);
145        glViewport(0, 0, mWidth, mHeight);
146
147        glMatrixMode(GL_PROJECTION);
148        glPushMatrix();
149        glLoadIdentity();
150
151        glMatrixMode(GL_MODELVIEW);
152        glPushMatrix();
153        glLoadIdentity();
154
155        const float offs = 0.5f;
156        glOrtho(-offs, offs, -offs, offs, 0, 1);
157
158
159        FirstPass(fbo);
160        AntiAliasing(fbo);
161
162        glEnable(GL_LIGHTING);
163        glDisable(GL_TEXTURE_2D);
164       
165        glMatrixMode(GL_PROJECTION);
166        glPopMatrix();
167
168        glMatrixMode(GL_MODELVIEW);
169        glPopMatrix();
170
171        glPopAttrib();
172
173        cgGLDisableProfile(RenderState::sCgFragmentProfile);
174}
175
176
177void DeferredShader::Render(FrameBufferObject *fbo,
178                                                        const Matrix4x4 &matViewing,
179                                                        ShadowMapping *shadowMapping)
180{
181        cgGLEnableProfile(RenderState::sCgFragmentProfile);
182
183        glDisable(GL_ALPHA_TEST);
184        glDisable(GL_TEXTURE_2D);
185        glDisable(GL_LIGHTING);
186
187        glPushAttrib(GL_VIEWPORT_BIT);
188        glViewport(0, 0, mWidth, mHeight);
189
190        glMatrixMode(GL_PROJECTION);
191        glPushMatrix();
192        glLoadIdentity();
193
194        glMatrixMode(GL_MODELVIEW);
195        glPushMatrix();
196        glLoadIdentity();
197
198        const float offs = 0.5f;
199        glOrtho(-offs, offs, -offs, offs, 0, 1);
200
201        FirstPassShadow(fbo, matViewing, shadowMapping);
202        AntiAliasing(fbo);
203
204        glEnable(GL_LIGHTING);
205        glDisable(GL_TEXTURE_2D);
206       
207        glMatrixMode(GL_PROJECTION);
208        glPopMatrix();
209
210        glMatrixMode(GL_MODELVIEW);
211        glPopMatrix();
212
213        glPopAttrib();
214
215        cgGLDisableProfile(RenderState::sCgFragmentProfile);
216}
217
218
219void DeferredShader::FirstPass(FrameBufferObject *fbo)
220{
221        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
222        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
223        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
224
225        fbo->Bind();
226        glDrawBuffers(1, mymrt + 3);
227
228        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
229
230        cgGLEnableProfile(RenderState::sCgFragmentProfile);
231        cgGLBindProgram(sCgDeferredProgram);
232
233        cgGLSetTextureParameter(sColorsTexParam, colorsTex);
234        cgGLEnableTextureParameter(sColorsTexParam);
235
236        cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
237        cgGLEnableTextureParameter(sPositionsTexParam);
238
239        cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
240        cgGLEnableTextureParameter(sNormalsTexParam);
241       
242        glColor3f(1.0f, 1.0f, 1.0f);
243       
244        glBegin(GL_QUADS);
245
246        float offs2 = 0.5f;
247
248        glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
249        glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
250        glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
251        glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
252
253        glEnd();
254
255        cgGLDisableTextureParameter(sColorsTexParam);
256        cgGLDisableTextureParameter(sPositionsTexParam);
257        cgGLDisableTextureParameter(sNormalsTexParam);
258
259        cgGLDisableProfile(RenderState::sCgFragmentProfile);
260       
261        FrameBufferObject::Release();
262
263        PrintGLerror("deferred shading");
264}
265
266
267static void SetVertex(float x, float y, float x_offs, float y_offs)
268{
269        glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
270        glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
271        glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
272        glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
273        glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
274
275        glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
276        glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
277
278        glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
279}
280
281
282void DeferredShader::AntiAliasing(FrameBufferObject *fbo)
283{
284        GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
285        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
286
287       
288        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
289
290        cgGLEnableProfile(RenderState::sCgFragmentProfile);
291
292        cgGLBindProgram(sCgAntiAliasingProgram);
293
294        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
295        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
296
297        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
298        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
299       
300        glColor3f(1.0f, 1.0f, 1.0f);
301
302        float offs2 = 0.5f;
303
304        glBegin(GL_QUADS);
305
306        // the neighbouring texels
307        float x_offs = 1.0f / mWidth;
308        float y_offs = 1.0f / mHeight;
309
310        SetVertex(0, 0, x_offs, y_offs);
311        SetVertex(1, 0, x_offs, y_offs);
312        SetVertex(1, 1, x_offs, y_offs);
313        SetVertex(0, 1, x_offs, y_offs);
314
315        glEnd();
316
317        cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
318        cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
319
320        cgGLDisableProfile(RenderState::sCgFragmentProfile);
321
322        PrintGLerror("antialiasing");
323}
324
325
326void DeferredShader::FirstPassShadow(FrameBufferObject *fbo,
327                                                                         const Matrix4x4 &matViewing,
328                                                                         ShadowMapping *shadowMapping)
329{
330        GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
331        GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
332        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
333        GLuint shadowMap = shadowMapping->mFbo->GetDepthTex();
334
335        static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f,
336                                                                0.0f, 0.5f, 0.0f, 0.0f,
337                                                                0.0f, 0.0f, 0.5f, 0.0f,
338                                                                0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1]
339
340        Matrix4x4 inverseView = matViewing;
341        inverseView.Invert();
342
343        Matrix4x4 lightProjView = shadowMapping->mLightViewMatrix * shadowMapping->mLightProjectionMatrix;
344
345        //cout << "matview:\n" << matViewing << endl;
346        //cout << "proj:\n" <<  shadowMapping->mLightProjectionMatrix << endl;
347        //cout << "view:\n" <<  shadowMapping->mLightViewMatrix << endl;
348
349        // compute combined matrix that transforms pixels into light texture space
350        /*Matrix4x4 shadowMatrix = biasMatrix *
351                                                         shadowMapping->mLightProjectionMatrix *
352                                                         shadowMapping->mLightViewMatrix *
353                                                         inverseView;
354*/
355        Matrix4x4 shadowMatrix = //inverseView *
356                                                         lightProjView *
357                                                         biasMatrix;
358/*
359Matrix4x4 shadowMatrix = biasMatrix *
360                                                         lightProjView;
361                                                         //inverseView;
362*/
363                                                         cout << "combined:\n" <<  shadowMatrix << endl;
364
365        fbo->Bind();
366        glDrawBuffers(1, mymrt + 3);
367
368       
369        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
370
371        cgGLEnableProfile(RenderState::sCgFragmentProfile);
372
373        cgGLBindProgram(sCgDeferredShadowProgram);
374
375        cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex);
376        cgGLEnableTextureParameter(sColorsTexShadowParam);
377
378        cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex);
379        cgGLEnableTextureParameter(sPositionsTexShadowParam);
380
381        cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex);
382        cgGLEnableTextureParameter(sNormalsTexShadowParam);
383       
384        cgGLSetTextureParameter(sShadowMapParam, shadowMap);
385        cgGLEnableTextureParameter(sShadowMapParam);
386
387        cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
388
389        glColor3f(1.0f, 1.0f, 1.0f);
390       
391        glBegin(GL_QUADS);
392
393        float offs2 = 0.5f;
394
395        glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
396        glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
397        glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
398        glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
399
400        glEnd();
401
402        cgGLDisableTextureParameter(sColorsTexShadowParam);
403        cgGLDisableTextureParameter(sPositionsTexShadowParam);
404        cgGLDisableTextureParameter(sNormalsTexShadowParam);
405        cgGLDisableTextureParameter(sShadowMapParam);
406
407        cgGLDisableProfile(RenderState::sCgFragmentProfile);
408       
409        FrameBufferObject::Release();
410
411        PrintGLerror("deferred shading");
412}
413
414
415
416} // namespace
Note: See TracBrowser for help on using the repository browser.