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 | static CGparameter sSampleWidthParam;
|
---|
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 |
|
---|
126 | sShadowMapParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMap");
|
---|
127 | sMaxDepthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "maxDepth");
|
---|
128 | sSampleWidthParam = cgGetNamedParameter(sCgDeferredShadowProgram, "sampleWidth");
|
---|
129 | sShadowMatrixParam = cgGetNamedParameter(sCgDeferredShadowProgram, "shadowMatrix");
|
---|
130 | }
|
---|
131 | else
|
---|
132 | cerr << "deferred program failed to load" << endl;
|
---|
133 |
|
---|
134 | PrintGLerror("init");
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | void DeferredShader::Render(FrameBufferObject *fbo)
|
---|
139 | {
|
---|
140 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
141 |
|
---|
142 | glDisable(GL_ALPHA_TEST);
|
---|
143 | glDisable(GL_TEXTURE_2D);
|
---|
144 | glDisable(GL_LIGHTING);
|
---|
145 |
|
---|
146 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
147 | glViewport(0, 0, mWidth, mHeight);
|
---|
148 |
|
---|
149 | glMatrixMode(GL_PROJECTION);
|
---|
150 | glPushMatrix();
|
---|
151 | glLoadIdentity();
|
---|
152 |
|
---|
153 | glMatrixMode(GL_MODELVIEW);
|
---|
154 | glPushMatrix();
|
---|
155 | glLoadIdentity();
|
---|
156 |
|
---|
157 | const float offs = 0.5f;
|
---|
158 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
159 |
|
---|
160 | FirstPass(fbo);
|
---|
161 | AntiAliasing(fbo);
|
---|
162 |
|
---|
163 | glEnable(GL_LIGHTING);
|
---|
164 |
|
---|
165 | glMatrixMode(GL_PROJECTION);
|
---|
166 | glPopMatrix();
|
---|
167 |
|
---|
168 | glMatrixMode(GL_MODELVIEW);
|
---|
169 | glPopMatrix();
|
---|
170 |
|
---|
171 | glPopAttrib();
|
---|
172 |
|
---|
173 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
174 | FrameBufferObject::Release();
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | void DeferredShader::Render(FrameBufferObject *fbo,
|
---|
179 | ShadowMap *shadowMap)
|
---|
180 | {
|
---|
181 | FrameBufferObject::Release();
|
---|
182 |
|
---|
183 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
184 |
|
---|
185 | glDisable(GL_ALPHA_TEST);
|
---|
186 | glDisable(GL_TEXTURE_2D);
|
---|
187 | glDisable(GL_LIGHTING);
|
---|
188 | //glDepthMask(GL_FALSE);
|
---|
189 |
|
---|
190 |
|
---|
191 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
192 | glViewport(0, 0, mWidth, mHeight);
|
---|
193 |
|
---|
194 | glMatrixMode(GL_PROJECTION);
|
---|
195 | glPushMatrix();
|
---|
196 | glLoadIdentity();
|
---|
197 |
|
---|
198 | glMatrixMode(GL_MODELVIEW);
|
---|
199 | glPushMatrix();
|
---|
200 | glLoadIdentity();
|
---|
201 |
|
---|
202 | const float offs = 0.5f;
|
---|
203 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
204 |
|
---|
205 | FirstPassShadow(fbo, shadowMap);
|
---|
206 | AntiAliasing(fbo);
|
---|
207 |
|
---|
208 | glEnable(GL_LIGHTING);
|
---|
209 | //glEnable(GL_TEXTURE_2D);
|
---|
210 | //glDepthMask(GL_TRUE);
|
---|
211 |
|
---|
212 | glMatrixMode(GL_PROJECTION);
|
---|
213 | glPopMatrix();
|
---|
214 |
|
---|
215 | glMatrixMode(GL_MODELVIEW);
|
---|
216 | glPopMatrix();
|
---|
217 |
|
---|
218 | glPopAttrib();
|
---|
219 |
|
---|
220 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
221 | FrameBufferObject::Release();
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | void DeferredShader::FirstPass(FrameBufferObject *fbo)
|
---|
226 | {
|
---|
227 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
228 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
229 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
230 |
|
---|
231 | fbo->Bind();
|
---|
232 | glDrawBuffers(1, mymrt + 3);
|
---|
233 |
|
---|
234 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
235 |
|
---|
236 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
237 | cgGLBindProgram(sCgDeferredProgram);
|
---|
238 |
|
---|
239 | cgGLSetTextureParameter(sColorsTexParam, colorsTex);
|
---|
240 | cgGLEnableTextureParameter(sColorsTexParam);
|
---|
241 |
|
---|
242 | cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
|
---|
243 | cgGLEnableTextureParameter(sPositionsTexParam);
|
---|
244 |
|
---|
245 | cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
|
---|
246 | cgGLEnableTextureParameter(sNormalsTexParam);
|
---|
247 |
|
---|
248 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
249 |
|
---|
250 | glBegin(GL_QUADS);
|
---|
251 |
|
---|
252 | float offs2 = 0.5f;
|
---|
253 |
|
---|
254 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
255 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
256 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
257 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
258 |
|
---|
259 | glEnd();
|
---|
260 |
|
---|
261 | cgGLDisableTextureParameter(sColorsTexParam);
|
---|
262 | cgGLDisableTextureParameter(sPositionsTexParam);
|
---|
263 | cgGLDisableTextureParameter(sNormalsTexParam);
|
---|
264 |
|
---|
265 | FrameBufferObject::Release();
|
---|
266 |
|
---|
267 | PrintGLerror("deferred shading");
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 | static void SetVertex(float x, float y, float x_offs, float y_offs)
|
---|
272 | {
|
---|
273 | glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
|
---|
274 | glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
|
---|
275 | glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
|
---|
276 | glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
|
---|
277 | glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
|
---|
278 |
|
---|
279 | glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
|
---|
280 | glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
|
---|
281 |
|
---|
282 | glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | void DeferredShader::AntiAliasing(FrameBufferObject *fbo)
|
---|
287 | {
|
---|
288 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
289 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
290 |
|
---|
291 |
|
---|
292 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
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 | 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 | PrintGLerror("antialiasing");
|
---|
321 | }
|
---|
322 |
|
---|
323 |
|
---|
324 | void DeferredShader::FirstPassShadow(FrameBufferObject *fbo,
|
---|
325 | ShadowMap *shadowMap)
|
---|
326 | {
|
---|
327 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
328 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
329 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
330 | GLuint shadowTex = shadowMap->GetShadowTexture();
|
---|
331 |
|
---|
332 | Matrix4x4 shadowMatrix;
|
---|
333 | shadowMap->GetTextureMatrix(shadowMatrix);
|
---|
334 |
|
---|
335 | fbo->Bind();
|
---|
336 | glDrawBuffers(1, mymrt + 3);
|
---|
337 |
|
---|
338 |
|
---|
339 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
340 |
|
---|
341 | cgGLBindProgram(sCgDeferredShadowProgram);
|
---|
342 |
|
---|
343 | cgGLSetTextureParameter(sColorsTexShadowParam, colorsTex);
|
---|
344 | cgGLEnableTextureParameter(sColorsTexShadowParam);
|
---|
345 |
|
---|
346 | cgGLSetTextureParameter(sPositionsTexShadowParam, positionsTex);
|
---|
347 | cgGLEnableTextureParameter(sPositionsTexShadowParam);
|
---|
348 |
|
---|
349 | cgGLSetTextureParameter(sNormalsTexShadowParam, normalsTex);
|
---|
350 | cgGLEnableTextureParameter(sNormalsTexShadowParam);
|
---|
351 |
|
---|
352 | cgGLSetTextureParameter(sShadowMapParam, shadowTex);
|
---|
353 | cgGLEnableTextureParameter(sShadowMapParam);
|
---|
354 |
|
---|
355 | cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
|
---|
356 | cgGLSetParameter1f(sSampleWidthParam, 1.0f / shadowMap->GetSize());
|
---|
357 |
|
---|
358 | cgGLSetMatrixParameterfc(sShadowMatrixParam, (const float *)shadowMatrix.x);
|
---|
359 |
|
---|
360 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
361 |
|
---|
362 | glBegin(GL_QUADS);
|
---|
363 |
|
---|
364 | float offs2 = 0.5f;
|
---|
365 |
|
---|
366 | glTexCoord2f(0, 0); glVertex3f(-offs2, -offs2, -0.5f);
|
---|
367 | glTexCoord2f(1, 0); glVertex3f( offs2, -offs2, -0.5f);
|
---|
368 | glTexCoord2f(1, 1); glVertex3f( offs2, offs2, -0.5f);
|
---|
369 | glTexCoord2f(0, 1); glVertex3f(-offs2, offs2, -0.5f);
|
---|
370 |
|
---|
371 | glEnd();
|
---|
372 |
|
---|
373 | cgGLDisableTextureParameter(sColorsTexShadowParam);
|
---|
374 | cgGLDisableTextureParameter(sPositionsTexShadowParam);
|
---|
375 | cgGLDisableTextureParameter(sNormalsTexShadowParam);
|
---|
376 | cgGLDisableTextureParameter(sShadowMapParam);
|
---|
377 |
|
---|
378 | FrameBufferObject::Release();
|
---|
379 |
|
---|
380 | PrintGLerror("deferred shading");
|
---|
381 | }
|
---|
382 |
|
---|
383 |
|
---|
384 |
|
---|
385 | } // namespace
|
---|