1 | #include "DeferredShader.h"
|
---|
2 | #include "FrameBufferObject.h"
|
---|
3 | #include "RenderState.h"
|
---|
4 | #include "ShadowMapping.h"
|
---|
5 |
|
---|
6 |
|
---|
7 | using namespace std;
|
---|
8 |
|
---|
9 |
|
---|
10 | namespace CHCDemoEngine
|
---|
11 | {
|
---|
12 |
|
---|
13 |
|
---|
14 | static 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 |
|
---|
27 | static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
|
---|
28 |
|
---|
29 |
|
---|
30 | static CGprogram sCgDeferredProgram;
|
---|
31 | static CGprogram sCgDeferredShadowProgram;
|
---|
32 | static CGprogram sCgAntiAliasingProgram;
|
---|
33 |
|
---|
34 |
|
---|
35 | static CGparameter sColorsTexParam;
|
---|
36 | static CGparameter sPositionsTexParam;
|
---|
37 | static CGparameter sNormalsTexParam;
|
---|
38 |
|
---|
39 | static CGparameter sColorsTexAntiAliasingParam;
|
---|
40 | static CGparameter sNormalsTexAntiAliasingParam;
|
---|
41 |
|
---|
42 | static CGparameter sShadowMapParam;
|
---|
43 | static CGparameter sPositionsTexShadowParam;
|
---|
44 | static CGparameter sColorsTexShadowParam;
|
---|
45 | static CGparameter sNormalsTexShadowParam;
|
---|
46 |
|
---|
47 | static CGparameter sShadowMatrixParam;
|
---|
48 | static CGparameter sMaxDepthParam;
|
---|
49 |
|
---|
50 |
|
---|
51 |
|
---|
52 | DeferredShader::DeferredShader(int w, int h, float scaleFactor):
|
---|
53 | mWidth(w), mHeight(h), mScaleFactor(scaleFactor)
|
---|
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 |
|
---|
60 | DeferredShader::~DeferredShader()
|
---|
61 | {
|
---|
62 | if (sCgDeferredProgram)
|
---|
63 | cgDestroyProgram(sCgDeferredProgram);
|
---|
64 |
|
---|
65 | //DEL_PTR(mFbo);
|
---|
66 | }
|
---|
67 |
|
---|
68 |
|
---|
69 | void 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 | sMaxDepthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth");
|
---|
127 | sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
|
---|
128 | }
|
---|
129 | else
|
---|
130 | cerr << "deferred program failed to load" << endl;
|
---|
131 |
|
---|
132 | PrintGLerror("init");
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
136 | void 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 |
|
---|
177 | void DeferredShader::Render(FrameBufferObject *fbo,
|
---|
178 | ShadowMap *shadowMap)
|
---|
179 | {
|
---|
180 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
181 |
|
---|
182 | glDisable(GL_ALPHA_TEST);
|
---|
183 | glDisable(GL_TEXTURE_2D);
|
---|
184 | glDisable(GL_LIGHTING);
|
---|
185 | glDepthMask(GL_FALSE);
|
---|
186 |
|
---|
187 |
|
---|
188 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
189 | glViewport(0, 0, mWidth, mHeight);
|
---|
190 |
|
---|
191 | glMatrixMode(GL_PROJECTION);
|
---|
192 | glPushMatrix();
|
---|
193 | glLoadIdentity();
|
---|
194 |
|
---|
195 | glMatrixMode(GL_MODELVIEW);
|
---|
196 | glPushMatrix();
|
---|
197 | glLoadIdentity();
|
---|
198 |
|
---|
199 | const float offs = 0.5f;
|
---|
200 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
201 |
|
---|
202 | FirstPassShadow(fbo, shadowMap);
|
---|
203 | AntiAliasing(fbo);
|
---|
204 |
|
---|
205 | glEnable(GL_LIGHTING);
|
---|
206 | glEnable(GL_TEXTURE_2D);
|
---|
207 | glDepthMask(GL_TRUE);
|
---|
208 |
|
---|
209 | glMatrixMode(GL_PROJECTION);
|
---|
210 | glPopMatrix();
|
---|
211 |
|
---|
212 | glMatrixMode(GL_MODELVIEW);
|
---|
213 | glPopMatrix();
|
---|
214 |
|
---|
215 | glPopAttrib();
|
---|
216 |
|
---|
217 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
218 | }
|
---|
219 |
|
---|
220 |
|
---|
221 | void DeferredShader::FirstPass(FrameBufferObject *fbo)
|
---|
222 | {
|
---|
223 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
224 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
225 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
226 |
|
---|
227 | fbo->Bind();
|
---|
228 | glDrawBuffers(1, mymrt + 3);
|
---|
229 |
|
---|
230 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
231 |
|
---|
232 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
233 | cgGLBindProgram(sCgDeferredProgram);
|
---|
234 |
|
---|
235 | cgGLSetTextureParameter(sColorsTexParam, colorsTex);
|
---|
236 | cgGLEnableTextureParameter(sColorsTexParam);
|
---|
237 |
|
---|
238 | cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
|
---|
239 | cgGLEnableTextureParameter(sPositionsTexParam);
|
---|
240 |
|
---|
241 | cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
|
---|
242 | cgGLEnableTextureParameter(sNormalsTexParam);
|
---|
243 |
|
---|
244 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
245 |
|
---|
246 | glBegin(GL_QUADS);
|
---|
247 |
|
---|
248 | float offs2 = 0.5f;
|
---|
249 |
|
---|
250 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
251 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
252 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
253 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
254 |
|
---|
255 | glEnd();
|
---|
256 |
|
---|
257 | cgGLDisableTextureParameter(sColorsTexParam);
|
---|
258 | cgGLDisableTextureParameter(sPositionsTexParam);
|
---|
259 | cgGLDisableTextureParameter(sNormalsTexParam);
|
---|
260 |
|
---|
261 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
262 |
|
---|
263 | FrameBufferObject::Release();
|
---|
264 |
|
---|
265 | PrintGLerror("deferred shading");
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | static void SetVertex(float x, float y, float x_offs, float y_offs)
|
---|
270 | {
|
---|
271 | glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
|
---|
272 | glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
|
---|
273 | glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
|
---|
274 | glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
|
---|
275 | glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
|
---|
276 |
|
---|
277 | glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
|
---|
278 | glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
|
---|
279 |
|
---|
280 | glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
|
---|
281 | }
|
---|
282 |
|
---|
283 |
|
---|
284 | void DeferredShader::AntiAliasing(FrameBufferObject *fbo)
|
---|
285 | {
|
---|
286 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
287 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
288 |
|
---|
289 |
|
---|
290 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
291 |
|
---|
292 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
293 |
|
---|
294 | cgGLBindProgram(sCgAntiAliasingProgram);
|
---|
295 |
|
---|
296 | cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
|
---|
297 | cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
298 |
|
---|
299 | cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
|
---|
300 | cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
301 |
|
---|
302 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
303 |
|
---|
304 | float offs2 = 0.5f;
|
---|
305 |
|
---|
306 | glBegin(GL_QUADS);
|
---|
307 |
|
---|
308 | // the neighbouring texels
|
---|
309 | float x_offs = 1.0f / mWidth;
|
---|
310 | float y_offs = 1.0f / mHeight;
|
---|
311 |
|
---|
312 | SetVertex(0, 0, x_offs, y_offs);
|
---|
313 | SetVertex(1, 0, x_offs, y_offs);
|
---|
314 | SetVertex(1, 1, x_offs, y_offs);
|
---|
315 | SetVertex(0, 1, x_offs, y_offs);
|
---|
316 |
|
---|
317 | glEnd();
|
---|
318 |
|
---|
319 | cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
320 | cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
321 |
|
---|
322 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
323 |
|
---|
324 | PrintGLerror("antialiasing");
|
---|
325 | }
|
---|
326 |
|
---|
327 |
|
---|
328 | void DeferredShader::FirstPassShadow(FrameBufferObject *fbo,
|
---|
329 | ShadowMap *shadowMap)
|
---|
330 | {
|
---|
331 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
332 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
333 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
334 | GLuint shadowTex = shadowMap->GetShadowTexture();
|
---|
335 |
|
---|
336 | Matrix4x4 shadowMatrix;
|
---|
337 | shadowMap->GetTextureMatrix(shadowMatrix);
|
---|
338 |
|
---|
339 | fbo->Bind();
|
---|
340 | glDrawBuffers(1, mymrt + 3);
|
---|
341 |
|
---|
342 |
|
---|
343 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
344 |
|
---|
345 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
346 |
|
---|
347 | cgGLBindProgram(sCgDeferredShadowProgram);
|
---|
348 |
|
---|
349 | cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex);
|
---|
350 | cgGLEnableTextureParameter(sColorsTexShadowParam);
|
---|
351 |
|
---|
352 | cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex);
|
---|
353 | cgGLEnableTextureParameter(sPositionsTexShadowParam);
|
---|
354 |
|
---|
355 | cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex);
|
---|
356 | cgGLEnableTextureParameter(sNormalsTexShadowParam);
|
---|
357 |
|
---|
358 | cgGLSetTextureParameter(sShadowMapParam, shadowTex);
|
---|
359 | cgGLEnableTextureParameter(sShadowMapParam);
|
---|
360 |
|
---|
361 | cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
|
---|
362 |
|
---|
363 | cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
|
---|
364 |
|
---|
365 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
366 |
|
---|
367 | glBegin(GL_QUADS);
|
---|
368 |
|
---|
369 | float offs2 = 0.5f;
|
---|
370 |
|
---|
371 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
372 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
373 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
374 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
375 |
|
---|
376 | glEnd();
|
---|
377 |
|
---|
378 | cgGLDisableTextureParameter(sColorsTexShadowParam);
|
---|
379 | cgGLDisableTextureParameter(sPositionsTexShadowParam);
|
---|
380 | cgGLDisableTextureParameter(sNormalsTexShadowParam);
|
---|
381 | cgGLDisableTextureParameter(sShadowMapParam);
|
---|
382 |
|
---|
383 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
384 |
|
---|
385 | FrameBufferObject::Release();
|
---|
386 |
|
---|
387 | PrintGLerror("deferred shading");
|
---|
388 | }
|
---|
389 |
|
---|
390 |
|
---|
391 |
|
---|
392 | } // namespace
|
---|