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

Revision 2866, 6.7 KB checked in by mattausch, 16 years ago (diff)

bug: downsampling of positions does not work

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