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

Revision 2867, 6.8 KB checked in by mattausch, 16 years ago (diff)
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       
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        if (1)
115        {
116                glEnable(GL_TEXTURE_2D);
117                // generate mip map levels for position texture
118                glBindTexture(GL_TEXTURE_2D, colorsTex);
119                glGenerateMipmapEXT(GL_TEXTURE_2D);
120        }
121
122        // read the second buffer, write to the first buffer
123        mFbo->Bind();
124
125        glPushAttrib(GL_VIEWPORT_BIT);
126        glViewport(0, 0, mWidth, mHeight);
127
128        glDisable(GL_ALPHA_TEST);
129        glDisable(GL_TEXTURE_2D);
130        glDisable(GL_LIGHTING);
131       
132        glMatrixMode(GL_PROJECTION);
133        glPushMatrix();
134        glLoadIdentity();
135
136        glMatrixMode(GL_MODELVIEW);
137        glPushMatrix();
138        glLoadIdentity();
139
140        const float offs = 0.5f;
141       
142        glOrtho(-offs, offs, -offs, offs, 0, 1);
143        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
144
145        cgGLEnableProfile(RenderState::sCgFragmentProfile);
146
147        cgGLBindProgram(sCgDeferredProgram);
148
149        cgGLSetTextureParameter(sColorsTexParam, colorsTex);
150        cgGLEnableTextureParameter(sColorsTexParam);
151
152        cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
153        cgGLEnableTextureParameter(sPositionsTexParam);
154
155        cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
156        cgGLEnableTextureParameter(sNormalsTexParam);
157       
158        glColor3f(1.0f, 1.0f, 1.0f);
159       
160        glBegin(GL_QUADS);
161
162        float offs2 = 0.5f;
163
164        glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
165        glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
166        glTexCoord2f(1, 1); glVertex3f( offs2,  offs2, -0.5f);
167        glTexCoord2f(0, 1); glVertex3f(-offs2,  offs2, -0.5f);
168
169        glEnd();
170
171        cgGLDisableTextureParameter(sColorsTexParam);
172        cgGLDisableTextureParameter(sPositionsTexParam);
173        cgGLDisableTextureParameter(sNormalsTexParam);
174
175        cgGLDisableProfile(RenderState::sCgFragmentProfile);
176       
177        glEnable(GL_LIGHTING);
178        glDisable(GL_TEXTURE_2D);
179       
180        glMatrixMode(GL_PROJECTION);
181        glPopMatrix();
182
183        glMatrixMode(GL_MODELVIEW);
184        glPopMatrix();
185
186        glPopAttrib();
187
188        FrameBufferObject::Release();
189
190        PrintGLerror("deferred shading");
191}
192
193
194static void SetVertex(float x, float y, float x_offs, float y_offs)
195{
196        glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
197        glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
198        glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
199        glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
200        glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
201
202        glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
203        glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
204
205        glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
206}
207
208
209void DeferredShader::AntiAliasing(FrameBufferObject *fbo)
210{
211        GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture();
212        GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
213
214        glPushAttrib(GL_VIEWPORT_BIT);
215        glViewport(0, 0, mWidth, mHeight);
216
217        glDisable(GL_ALPHA_TEST);
218        glDisable(GL_TEXTURE_2D);
219        glDisable(GL_LIGHTING);
220       
221        glMatrixMode(GL_PROJECTION);
222        glPushMatrix();
223        glLoadIdentity();
224
225        glMatrixMode(GL_MODELVIEW);
226        glPushMatrix();
227        glLoadIdentity();
228
229        const float offs = 0.5f;
230       
231        glOrtho(-offs, offs, -offs, offs, 0, 1);
232        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
233
234        cgGLEnableProfile(RenderState::sCgFragmentProfile);
235
236        cgGLBindProgram(sCgAntiAliasingProgram);
237
238        cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
239        cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
240
241        cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
242        cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
243       
244        glColor3f(1.0f, 1.0f, 1.0f);
245
246        float offs2 = 0.5f;
247
248        glBegin(GL_QUADS);
249
250        // the neighbouring texels
251        float x_offs = 1.0f / mWidth;
252        float y_offs = 1.0f / mHeight;
253
254        SetVertex(0, 0, x_offs, y_offs);
255        SetVertex(1, 0, x_offs, y_offs);
256        SetVertex(1, 1, x_offs, y_offs);
257        SetVertex(0, 1, x_offs, y_offs);
258
259        glEnd();
260
261        cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
262        cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
263
264        cgGLDisableProfile(RenderState::sCgFragmentProfile);
265       
266        glEnable(GL_LIGHTING);
267        glDisable(GL_TEXTURE_2D);
268       
269        glMatrixMode(GL_PROJECTION);
270        glPopMatrix();
271
272        glMatrixMode(GL_MODELVIEW);
273        glPopMatrix();
274
275        glPopAttrib();
276
277        PrintGLerror("deferred shading");
278}
279
280
281} // namespace
Note: See TracBrowser for help on using the repository browser.