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 | 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 | glDepthMask(GL_FALSE);
|
---|
187 |
|
---|
188 |
|
---|
189 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
190 | glViewport(0, 0, mWidth, mHeight);
|
---|
191 |
|
---|
192 | glMatrixMode(GL_PROJECTION);
|
---|
193 | glPushMatrix();
|
---|
194 | glLoadIdentity();
|
---|
195 |
|
---|
196 | glMatrixMode(GL_MODELVIEW);
|
---|
197 | glPushMatrix();
|
---|
198 | glLoadIdentity();
|
---|
199 |
|
---|
200 | const float offs = 0.5f;
|
---|
201 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
202 |
|
---|
203 | FirstPassShadow(fbo, matViewing, shadowMapping);
|
---|
204 | AntiAliasing(fbo);
|
---|
205 |
|
---|
206 | glEnable(GL_LIGHTING);
|
---|
207 | glEnable(GL_TEXTURE_2D);
|
---|
208 | glDepthMask(GL_TRUE);
|
---|
209 |
|
---|
210 | glMatrixMode(GL_PROJECTION);
|
---|
211 | glPopMatrix();
|
---|
212 |
|
---|
213 | glMatrixMode(GL_MODELVIEW);
|
---|
214 | glPopMatrix();
|
---|
215 |
|
---|
216 | glPopAttrib();
|
---|
217 |
|
---|
218 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
219 | }
|
---|
220 |
|
---|
221 |
|
---|
222 | void DeferredShader::FirstPass(FrameBufferObject *fbo)
|
---|
223 | {
|
---|
224 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
225 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
226 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
227 |
|
---|
228 | fbo->Bind();
|
---|
229 | glDrawBuffers(1, mymrt + 3);
|
---|
230 |
|
---|
231 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
232 |
|
---|
233 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
234 | cgGLBindProgram(sCgDeferredProgram);
|
---|
235 |
|
---|
236 | cgGLSetTextureParameter(sColorsTexParam, colorsTex);
|
---|
237 | cgGLEnableTextureParameter(sColorsTexParam);
|
---|
238 |
|
---|
239 | cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
|
---|
240 | cgGLEnableTextureParameter(sPositionsTexParam);
|
---|
241 |
|
---|
242 | cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
|
---|
243 | cgGLEnableTextureParameter(sNormalsTexParam);
|
---|
244 |
|
---|
245 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
246 |
|
---|
247 | glBegin(GL_QUADS);
|
---|
248 |
|
---|
249 | float offs2 = 0.5f;
|
---|
250 |
|
---|
251 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
252 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
253 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
254 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
255 |
|
---|
256 | glEnd();
|
---|
257 |
|
---|
258 | cgGLDisableTextureParameter(sColorsTexParam);
|
---|
259 | cgGLDisableTextureParameter(sPositionsTexParam);
|
---|
260 | cgGLDisableTextureParameter(sNormalsTexParam);
|
---|
261 |
|
---|
262 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
263 |
|
---|
264 | FrameBufferObject::Release();
|
---|
265 |
|
---|
266 | PrintGLerror("deferred shading");
|
---|
267 | }
|
---|
268 |
|
---|
269 |
|
---|
270 | static void SetVertex(float x, float y, float x_offs, float y_offs)
|
---|
271 | {
|
---|
272 | glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
|
---|
273 | glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
|
---|
274 | glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
|
---|
275 | glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
|
---|
276 | glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
|
---|
277 |
|
---|
278 | glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
|
---|
279 | glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
|
---|
280 |
|
---|
281 | glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | void DeferredShader::AntiAliasing(FrameBufferObject *fbo)
|
---|
286 | {
|
---|
287 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
288 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
289 |
|
---|
290 |
|
---|
291 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
292 |
|
---|
293 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
294 |
|
---|
295 | cgGLBindProgram(sCgAntiAliasingProgram);
|
---|
296 |
|
---|
297 | cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
|
---|
298 | cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
299 |
|
---|
300 | cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
|
---|
301 | cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
302 |
|
---|
303 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
304 |
|
---|
305 | float offs2 = 0.5f;
|
---|
306 |
|
---|
307 | glBegin(GL_QUADS);
|
---|
308 |
|
---|
309 | // the neighbouring texels
|
---|
310 | float x_offs = 1.0f / mWidth;
|
---|
311 | float y_offs = 1.0f / mHeight;
|
---|
312 |
|
---|
313 | SetVertex(0, 0, x_offs, y_offs);
|
---|
314 | SetVertex(1, 0, x_offs, y_offs);
|
---|
315 | SetVertex(1, 1, x_offs, y_offs);
|
---|
316 | SetVertex(0, 1, x_offs, y_offs);
|
---|
317 |
|
---|
318 | glEnd();
|
---|
319 |
|
---|
320 | cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
321 | cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
322 |
|
---|
323 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
324 |
|
---|
325 | PrintGLerror("antialiasing");
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | void DeferredShader::FirstPassShadow(FrameBufferObject *fbo,
|
---|
330 | const Matrix4x4 &matViewing,
|
---|
331 | ShadowMapping *shadowMapping)
|
---|
332 | {
|
---|
333 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
334 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
335 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
336 | GLuint shadowMap = shadowMapping->mFbo->GetDepthTex();
|
---|
337 | /*
|
---|
338 | static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f,
|
---|
339 | 0.0f, 0.5f, 0.0f, 0.0f,
|
---|
340 | 0.0f, 0.0f, 0.5f, 0.0f,
|
---|
341 | 0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1]
|
---|
342 | */
|
---|
343 |
|
---|
344 | static Matrix4x4 biasMatrix(0.5f, 0.0f, 0.0f, 0.5f,
|
---|
345 | 0.0f, 0.5f, 0.0f, 0.5f,
|
---|
346 | 0.0f, 0.0f, 0.5f, 0.5f,
|
---|
347 | 0.0f, 0.0f, 0.0f, 1.0f); //bias from [-1, 1] to [0, 1]
|
---|
348 |
|
---|
349 | Matrix4x4 inverseView = matViewing;
|
---|
350 | inverseView.Invert();
|
---|
351 |
|
---|
352 | Matrix4x4 lightProjView = shadowMapping->mLightViewMatrix * shadowMapping->mLightProjectionMatrix;
|
---|
353 |
|
---|
354 | //cout << "matview:\n" << matViewing << endl;
|
---|
355 | //cout << "proj:\n" << shadowMapping->mLightProjectionMatrix << endl;
|
---|
356 | cout << "view:\n" << shadowMapping->mLightViewMatrix << endl;
|
---|
357 |
|
---|
358 | // compute combined matrix that transforms pixels into light texture space
|
---|
359 | /*Matrix4x4 shadowMatrix = biasMatrix *
|
---|
360 | shadowMapping->mLightProjectionMatrix *
|
---|
361 | shadowMapping->mLightViewMatrix *
|
---|
362 | inverseView;
|
---|
363 | */
|
---|
364 | //Matrix4x4 shadowMatrix = //inverseView * lightProjView * biasMatrix;*
|
---|
365 | Matrix4x4 shadowMatrix = lightProjView*biasMatrix;
|
---|
366 | //inverseView;
|
---|
367 | cout << "combined:\n" << shadowMatrix << endl;
|
---|
368 |
|
---|
369 | fbo->Bind();
|
---|
370 | glDrawBuffers(1, mymrt + 3);
|
---|
371 |
|
---|
372 |
|
---|
373 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
374 |
|
---|
375 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
376 |
|
---|
377 | cgGLBindProgram(sCgDeferredShadowProgram);
|
---|
378 |
|
---|
379 | cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex);
|
---|
380 | cgGLEnableTextureParameter(sColorsTexShadowParam);
|
---|
381 |
|
---|
382 | cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex);
|
---|
383 | cgGLEnableTextureParameter(sPositionsTexShadowParam);
|
---|
384 |
|
---|
385 | cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex);
|
---|
386 | cgGLEnableTextureParameter(sNormalsTexShadowParam);
|
---|
387 |
|
---|
388 | cgGLSetTextureParameter(sShadowMapParam, shadowMap);
|
---|
389 | cgGLEnableTextureParameter(sShadowMapParam);
|
---|
390 |
|
---|
391 | cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
|
---|
392 |
|
---|
393 | cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
|
---|
394 |
|
---|
395 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
396 |
|
---|
397 | glBegin(GL_QUADS);
|
---|
398 |
|
---|
399 | float offs2 = 0.5f;
|
---|
400 |
|
---|
401 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
402 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
403 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
404 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
405 |
|
---|
406 | glEnd();
|
---|
407 |
|
---|
408 | cgGLDisableTextureParameter(sColorsTexShadowParam);
|
---|
409 | cgGLDisableTextureParameter(sPositionsTexShadowParam);
|
---|
410 | cgGLDisableTextureParameter(sNormalsTexShadowParam);
|
---|
411 | cgGLDisableTextureParameter(sShadowMapParam);
|
---|
412 |
|
---|
413 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
414 |
|
---|
415 | FrameBufferObject::Release();
|
---|
416 |
|
---|
417 | PrintGLerror("deferred shading");
|
---|
418 | }
|
---|
419 |
|
---|
420 |
|
---|
421 |
|
---|
422 | } // namespace
|
---|