1 | #include "SsaoShader.h"
|
---|
2 | #include "FrameBufferObject.h"
|
---|
3 | #include "RenderState.h"
|
---|
4 | #include "SampleGenerator.h"
|
---|
5 | #include "Vector3.h"
|
---|
6 | #include "Camera.h"
|
---|
7 | #include "shaderenv.h"
|
---|
8 | #include "RndGauss.h"
|
---|
9 | #include "Halton.h"
|
---|
10 |
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | static GLenum mymrt[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_COLOR_ATTACHMENT3_EXT};
|
---|
15 |
|
---|
16 |
|
---|
17 | namespace CHCDemoEngine
|
---|
18 | {
|
---|
19 |
|
---|
20 | static CGprogram sCgSsaoProgram = NULL;
|
---|
21 | static CGprogram sCgGiProgram = NULL;
|
---|
22 |
|
---|
23 | static CGprogram sCgDeferredProgram = NULL;
|
---|
24 | static CGprogram sCgAntiAliasingProgram = NULL;
|
---|
25 |
|
---|
26 | static CGparameter sColorsTexCombineParam;
|
---|
27 | static CGparameter sSsaoTexCombineParam;
|
---|
28 |
|
---|
29 | static CGparameter sColorsTexDeferredParam;
|
---|
30 | static CGparameter sPositionsTexDeferredParam;
|
---|
31 | static CGparameter sNormalsTexDeferredParam;
|
---|
32 |
|
---|
33 |
|
---|
34 | static CGprogram sCgCombinedSsaoProgram = NULL;
|
---|
35 | static CGprogram sCgCombinedIllumProgram = NULL;
|
---|
36 |
|
---|
37 | ///////////////////////////////////////7
|
---|
38 |
|
---|
39 |
|
---|
40 | static CGparameter sColorsTexParam;
|
---|
41 | static CGparameter sPositionsTexParam;
|
---|
42 | static CGparameter sNormalsTexParam;
|
---|
43 |
|
---|
44 | static CGparameter sOldModelViewProjMatrixParam;
|
---|
45 | static CGparameter sMaxDepthParam;
|
---|
46 | static CGparameter sSamplesParam;
|
---|
47 | static CGparameter sOldTexParam;
|
---|
48 | static CGparameter sNoiseTexParam;
|
---|
49 | static CGparameter sNoiseMultiplierParam;
|
---|
50 | static CGparameter sExpFactorParam;
|
---|
51 |
|
---|
52 |
|
---|
53 |
|
---|
54 | ///////////////////////////////////////
|
---|
55 |
|
---|
56 |
|
---|
57 | static CGparameter sColorsTexGiParam;
|
---|
58 | static CGparameter sPositionsTexGiParam;
|
---|
59 | static CGparameter sNormalsTexGiParam;
|
---|
60 |
|
---|
61 |
|
---|
62 | static CGparameter sOldModelViewProjMatrixGiParam;
|
---|
63 | static CGparameter sMaxDepthGiParam;
|
---|
64 | static CGparameter sSamplesGiParam;
|
---|
65 | static CGparameter sOldSsaoTexGiParam;
|
---|
66 | static CGparameter sOldIllumTexGiParam;
|
---|
67 | static CGparameter sNoiseTexGiParam;
|
---|
68 | static CGparameter sNoiseMultiplierGiParam;
|
---|
69 | static CGparameter sExpFactorGiParam;
|
---|
70 |
|
---|
71 |
|
---|
72 | static CGparameter sColorsTexCombinedIllumParam;
|
---|
73 | static CGparameter sSsaoTexCombinedIllumParam;
|
---|
74 | static CGparameter sIllumTexCombinedIllumParam;
|
---|
75 |
|
---|
76 | static CGparameter sColorsTexCombinedSsaoParam;
|
---|
77 | static CGparameter sSsaoTexCombinedSsaoParam;
|
---|
78 |
|
---|
79 |
|
---|
80 | ////////////
|
---|
81 |
|
---|
82 | static CGparameter sColorsTexAntiAliasingParam;
|
---|
83 | static CGparameter sNormalsTexAntiAliasingParam;
|
---|
84 |
|
---|
85 | static GLuint noiseTex = 0;
|
---|
86 |
|
---|
87 | // ssao random spherical samples
|
---|
88 | static Sample2 samples[NUM_SAMPLES];
|
---|
89 |
|
---|
90 |
|
---|
91 | static void PrintGLerror(char *msg)
|
---|
92 | {
|
---|
93 | GLenum errCode;
|
---|
94 | const GLubyte *errStr;
|
---|
95 |
|
---|
96 | if ((errCode = glGetError()) != GL_NO_ERROR)
|
---|
97 | {
|
---|
98 | errStr = gluErrorString(errCode);
|
---|
99 | fprintf(stderr,"OpenGL ERROR: %s: %s\n", errStr, msg);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /** Generate poisson disc distributed sample points on the unit disc
|
---|
105 | */
|
---|
106 | static void GenerateSamples(int sampling)
|
---|
107 | {
|
---|
108 | switch (sampling)
|
---|
109 | {
|
---|
110 | case SsaoShader::POISSON:
|
---|
111 | {
|
---|
112 | static PoissonDiscSampleGenerator poisson(NUM_SAMPLES, 1.0f);
|
---|
113 | poisson.Generate((Sample2 *)samples);
|
---|
114 | }
|
---|
115 | break;
|
---|
116 | case SsaoShader::GAUSS:
|
---|
117 | {
|
---|
118 | const float radius = 1.0f;
|
---|
119 | const float sigma = 0.25f;//ComputeSigmaFromRadius(1.0f);
|
---|
120 |
|
---|
121 | static HaltonSequence halton;
|
---|
122 |
|
---|
123 | for (int i = 0; i < NUM_SAMPLES; ++ i)
|
---|
124 | {
|
---|
125 | Sample2 s;
|
---|
126 | Vector3 input;
|
---|
127 |
|
---|
128 | halton.GetNext(3, (float *)&input.x);
|
---|
129 |
|
---|
130 | //GaussianOn2D(cinput, // input (RND in interval [0-1)x[0-1))
|
---|
131 | // sigma, // standard deviation of gaussian distribution
|
---|
132 | // outputVec) // resulting vector according to gaussian distr.
|
---|
133 |
|
---|
134 | PolarGaussianOnDisk(input, // input (RND in interval [0-1)x[0-1))
|
---|
135 | sigma, // standard deviation of gaussian distribution
|
---|
136 | radius, // standard deviation of gaussian distribution
|
---|
137 | s); // result
|
---|
138 | samples[i] = s;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | break;
|
---|
142 | default:
|
---|
143 | break;
|
---|
144 | }
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | static void CreateNoiseTex2D(int w, int h)
|
---|
149 | {
|
---|
150 | //GLubyte *randomNormals = new GLubyte[mWidth * mHeight * 3];
|
---|
151 | float *randomNormals = new float[w * h * 3];
|
---|
152 |
|
---|
153 | for (int i = 0; i < w * h * 3; i += 3)
|
---|
154 | {
|
---|
155 | // create random samples on a circle
|
---|
156 | const float rx = RandomValue(0, 1);
|
---|
157 | const float theta = 2.0f * acos(sqrt(1.0f - rx));
|
---|
158 |
|
---|
159 | //randomNormals[i + 0] = (GLubyte)((cos(theta) * 0.5f + 0.5f) * 255.0f);
|
---|
160 | //randomNormals[i + 1] = (GLubyte)((sin(theta) * 0.5f + 0.5f) * 255.0f);
|
---|
161 | randomNormals[i + 0] = cos(theta);
|
---|
162 | randomNormals[i + 1] = sin(theta);
|
---|
163 | randomNormals[i + 2] = 0;
|
---|
164 | }
|
---|
165 |
|
---|
166 | glEnable(GL_TEXTURE_2D);
|
---|
167 | glGenTextures(1, &noiseTex);
|
---|
168 | glBindTexture(GL_TEXTURE_2D, noiseTex);
|
---|
169 |
|
---|
170 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
---|
171 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
---|
172 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
---|
173 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
---|
174 |
|
---|
175 | //glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, randomNormals);
|
---|
176 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F_ARB, w, h, 0, GL_RGB, GL_FLOAT, randomNormals);
|
---|
177 |
|
---|
178 | glBindTexture(GL_TEXTURE_2D, 0);
|
---|
179 | glDisable(GL_TEXTURE_2D);
|
---|
180 |
|
---|
181 | delete [] randomNormals;
|
---|
182 |
|
---|
183 | cout << "created noise texture" << endl;
|
---|
184 |
|
---|
185 | PrintGLerror("noisetexture");
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | SsaoShader::SsaoShader(int w, int h, Camera *cam, float scaleFactor):
|
---|
190 | mWidth(w), mHeight(h),
|
---|
191 | mCamera(cam),
|
---|
192 | mScaleFactor(scaleFactor),
|
---|
193 | mUseTemporalCoherence(true),
|
---|
194 | mUseGlobIllum(false),
|
---|
195 | mSampling(POISSON),
|
---|
196 | mFboIndex(0)
|
---|
197 | {
|
---|
198 | // create noise texture for ssao
|
---|
199 | CreateNoiseTex2D(w, h);
|
---|
200 |
|
---|
201 | ///////////
|
---|
202 | //-- the flip-flop fbos
|
---|
203 |
|
---|
204 | /*mNewFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
|
---|
205 |
|
---|
206 | mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
207 | mNewFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
208 |
|
---|
209 |
|
---|
210 | ///////////////////////
|
---|
211 |
|
---|
212 | mOldFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
|
---|
213 |
|
---|
214 | mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
215 | mOldFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);*/
|
---|
216 |
|
---|
217 | mFbo = new FrameBufferObject(w, h, FrameBufferObject::DEPTH_NONE);
|
---|
218 |
|
---|
219 | mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
220 | mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
221 | mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
222 | mFbo->AddColorBuffer(ColorBufferObject::BUFFER_FLOAT_32, ColorBufferObject::WRAP_CLAMP_TO_EDGE, ColorBufferObject::FILTER_LINEAR, false);
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | SsaoShader::~SsaoShader()
|
---|
227 | {
|
---|
228 | if (sCgSsaoProgram) cgDestroyProgram(sCgSsaoProgram);
|
---|
229 | if (sCgDeferredProgram) cgDestroyProgram(sCgDeferredProgram);
|
---|
230 | if (sCgSsaoProgram) cgDestroyProgram(sCgSsaoProgram);
|
---|
231 | if (sCgGiProgram) cgDestroyProgram(sCgGiProgram);
|
---|
232 | if (sCgAntiAliasingProgram) cgDestroyProgram(sCgAntiAliasingProgram);
|
---|
233 |
|
---|
234 | //DEL_PTR(mNewFbo);
|
---|
235 | //DEL_PTR(mOldFbo);
|
---|
236 | DEL_PTR(mFbo);
|
---|
237 |
|
---|
238 | glDeleteTextures(1, &noiseTex);
|
---|
239 | }
|
---|
240 |
|
---|
241 |
|
---|
242 | void SsaoShader::SetUseGlobIllum(bool useGlobIllum)
|
---|
243 | {
|
---|
244 | mUseGlobIllum = useGlobIllum;
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | void SsaoShader::SetUseTemporalCoherence(bool temporal)
|
---|
249 | {
|
---|
250 | mUseTemporalCoherence = temporal;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | void SsaoShader::Init(CGcontext context)
|
---|
255 | {
|
---|
256 | sCgDeferredProgram =
|
---|
257 | cgCreateProgramFromFile(context,
|
---|
258 | CG_SOURCE,
|
---|
259 | "src/shaders/deferred.cg",
|
---|
260 | RenderState::sCgFragmentProfile,
|
---|
261 | "main",
|
---|
262 | NULL);
|
---|
263 |
|
---|
264 | if (sCgDeferredProgram != NULL)
|
---|
265 | {
|
---|
266 | cgGLLoadProgram(sCgDeferredProgram);
|
---|
267 |
|
---|
268 | // we need size of texture for scaling
|
---|
269 | sPositionsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "positions");
|
---|
270 | sColorsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "colors");
|
---|
271 | sNormalsTexDeferredParam = cgGetNamedParameter(sCgDeferredProgram, "normals");
|
---|
272 | }
|
---|
273 | else
|
---|
274 | cerr << "deferred program failed to load" << endl;
|
---|
275 |
|
---|
276 |
|
---|
277 | ///////////////
|
---|
278 |
|
---|
279 | sCgSsaoProgram =
|
---|
280 | cgCreateProgramFromFile(context,
|
---|
281 | CG_SOURCE,
|
---|
282 | "src/shaders/ssao.cg",
|
---|
283 | RenderState::sCgFragmentProfile,
|
---|
284 | "main",
|
---|
285 | NULL);
|
---|
286 |
|
---|
287 | if (sCgSsaoProgram != NULL)
|
---|
288 | {
|
---|
289 | cgGLLoadProgram(sCgSsaoProgram);
|
---|
290 |
|
---|
291 | // we need size of texture for scaling
|
---|
292 | sPositionsTexParam = cgGetNamedParameter(sCgSsaoProgram, "positions");
|
---|
293 | sColorsTexParam = cgGetNamedParameter(sCgSsaoProgram, "colors");
|
---|
294 | sNormalsTexParam = cgGetNamedParameter(sCgSsaoProgram, "normals");
|
---|
295 | sNoiseTexParam = cgGetNamedParameter(sCgSsaoProgram, "noiseTexture");
|
---|
296 | sNoiseMultiplierParam = cgGetNamedParameter(sCgSsaoProgram, "noiseMultiplier");
|
---|
297 |
|
---|
298 | sOldModelViewProjMatrixParam = cgGetNamedParameter(sCgSsaoProgram, "oldModelViewProj");
|
---|
299 | sMaxDepthParam = cgGetNamedParameter(sCgSsaoProgram, "maxDepth");
|
---|
300 | sExpFactorParam = cgGetNamedParameter(sCgSsaoProgram, "expFactor");
|
---|
301 |
|
---|
302 | sOldTexParam = cgGetNamedParameter(sCgSsaoProgram, "oldTex");
|
---|
303 |
|
---|
304 | cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
|
---|
305 |
|
---|
306 | // generate samples for ssao kernel
|
---|
307 | //GenerateSamples(mSampling);
|
---|
308 |
|
---|
309 | sSamplesParam = cgGetNamedParameter(sCgSsaoProgram, "samples");
|
---|
310 | //cgSetArraySize(sSamplesParam, NUM_SAMPLES);
|
---|
311 | //cgCompileProgram(sCgSsaoProgram);
|
---|
312 |
|
---|
313 | //cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
|
---|
314 | }
|
---|
315 | else
|
---|
316 | cerr << "ssao program failed to load" << endl;
|
---|
317 |
|
---|
318 | sCgGiProgram =
|
---|
319 | cgCreateProgramFromFile(context,
|
---|
320 | CG_SOURCE,
|
---|
321 | "src/shaders/globillum.cg",
|
---|
322 | RenderState::sCgFragmentProfile,
|
---|
323 | "main",
|
---|
324 | NULL);
|
---|
325 |
|
---|
326 | if (sCgGiProgram != NULL)
|
---|
327 | {
|
---|
328 | cgGLLoadProgram(sCgGiProgram);
|
---|
329 |
|
---|
330 | // we need size of texture for scaling
|
---|
331 | sPositionsTexGiParam = cgGetNamedParameter(sCgGiProgram, "positions");
|
---|
332 | sColorsTexGiParam = cgGetNamedParameter(sCgGiProgram, "colors");
|
---|
333 | sNormalsTexGiParam = cgGetNamedParameter(sCgGiProgram, "normals");
|
---|
334 | sNoiseTexGiParam = cgGetNamedParameter(sCgGiProgram, "noiseTexture");
|
---|
335 | sNoiseMultiplierGiParam = cgGetNamedParameter(sCgGiProgram, "noiseMultiplier");
|
---|
336 |
|
---|
337 | sOldModelViewProjMatrixGiParam = cgGetNamedParameter(sCgGiProgram, "oldModelViewProj");
|
---|
338 | sMaxDepthGiParam = cgGetNamedParameter(sCgGiProgram, "maxDepth");
|
---|
339 | sExpFactorGiParam = cgGetNamedParameter(sCgGiProgram, "expFactor");
|
---|
340 |
|
---|
341 | sSamplesGiParam = cgGetNamedParameter(sCgGiProgram, "samples");
|
---|
342 |
|
---|
343 | sOldSsaoTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldSsaoTex");
|
---|
344 | sOldIllumTexGiParam = cgGetNamedParameter(sCgGiProgram, "oldIllumTex");
|
---|
345 |
|
---|
346 | // generate samples for ssao kernel
|
---|
347 | //GenerateSamples();
|
---|
348 | //cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
|
---|
349 |
|
---|
350 | cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
|
---|
351 | }
|
---|
352 | else
|
---|
353 | cerr << "globillum program failed to load" << endl;
|
---|
354 |
|
---|
355 | sCgCombinedIllumProgram =
|
---|
356 | cgCreateProgramFromFile(context,
|
---|
357 | CG_SOURCE,
|
---|
358 | "src/shaders/globillum.cg",
|
---|
359 | RenderState::sCgFragmentProfile,
|
---|
360 | "combine",
|
---|
361 | NULL);
|
---|
362 |
|
---|
363 | if (sCgCombinedIllumProgram != NULL)
|
---|
364 | {
|
---|
365 | cgGLLoadProgram(sCgCombinedIllumProgram);
|
---|
366 |
|
---|
367 | sColorsTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "colors");
|
---|
368 | sSsaoTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "ssaoTex");
|
---|
369 | sIllumTexCombinedIllumParam = cgGetNamedParameter(sCgCombinedIllumProgram, "illumTex");
|
---|
370 | }
|
---|
371 | else
|
---|
372 | cerr << "combined illum program failed to load" << endl;
|
---|
373 |
|
---|
374 |
|
---|
375 | sCgCombinedSsaoProgram =
|
---|
376 | cgCreateProgramFromFile(context,
|
---|
377 | CG_SOURCE,
|
---|
378 | "src/shaders/ssao.cg",
|
---|
379 | RenderState::sCgFragmentProfile,
|
---|
380 | "combine",
|
---|
381 | NULL);
|
---|
382 |
|
---|
383 | if (sCgCombinedSsaoProgram != NULL)
|
---|
384 | {
|
---|
385 | cgGLLoadProgram(sCgCombinedSsaoProgram);
|
---|
386 |
|
---|
387 | sColorsTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "colors");
|
---|
388 | sSsaoTexCombinedSsaoParam = cgGetNamedParameter(sCgCombinedSsaoProgram, "ssaoTex");
|
---|
389 | }
|
---|
390 | else
|
---|
391 | cerr << "combied illum program failed to load" << endl;
|
---|
392 |
|
---|
393 |
|
---|
394 | sCgAntiAliasingProgram =
|
---|
395 | cgCreateProgramFromFile(context,
|
---|
396 | CG_SOURCE,
|
---|
397 | "src/shaders/antialiasing.cg",
|
---|
398 | RenderState::sCgFragmentProfile,
|
---|
399 | "main",
|
---|
400 | NULL);
|
---|
401 |
|
---|
402 | if (sCgAntiAliasingProgram != NULL)
|
---|
403 | {
|
---|
404 | cgGLLoadProgram(sCgAntiAliasingProgram);
|
---|
405 |
|
---|
406 | sColorsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "colors");
|
---|
407 | sNormalsTexAntiAliasingParam = cgGetNamedParameter(sCgAntiAliasingProgram, "normals");
|
---|
408 | }
|
---|
409 | else
|
---|
410 | cerr << "antialiasing program failed to load" << endl;
|
---|
411 |
|
---|
412 | PrintGLerror("init");
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | void SsaoShader::Render(FrameBufferObject *fbo,
|
---|
417 | const Matrix4x4 &oldProjViewMatrix,
|
---|
418 | float expFactor)
|
---|
419 | {
|
---|
420 |
|
---|
421 | // switch roles of old and new fbo
|
---|
422 | // the algorihm uses two input fbos, where the one
|
---|
423 | // contais the color buffer from the last frame,
|
---|
424 | // the other one will be written
|
---|
425 | //swap(mNewFbo, mOldFbo);
|
---|
426 | mFboIndex = 2 - mFboIndex;
|
---|
427 | FrameBufferObject::Release();
|
---|
428 |
|
---|
429 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
430 |
|
---|
431 | glDisable(GL_ALPHA_TEST);
|
---|
432 | glDisable(GL_TEXTURE_2D);
|
---|
433 | glDisable(GL_LIGHTING);
|
---|
434 |
|
---|
435 | glPushAttrib(GL_VIEWPORT_BIT);
|
---|
436 | glViewport(0, 0, mWidth, mHeight);
|
---|
437 |
|
---|
438 | glMatrixMode(GL_PROJECTION);
|
---|
439 | glPushMatrix();
|
---|
440 | glLoadIdentity();
|
---|
441 |
|
---|
442 | glMatrixMode(GL_MODELVIEW);
|
---|
443 | glPushMatrix();
|
---|
444 | glLoadIdentity();
|
---|
445 |
|
---|
446 | const float offs = 0.5f;
|
---|
447 | glOrtho(-offs, offs, -offs, offs, 0, 1);
|
---|
448 |
|
---|
449 | FirstPass(fbo);
|
---|
450 |
|
---|
451 | if (!mUseGlobIllum)
|
---|
452 | {
|
---|
453 | ComputeSsao(fbo, expFactor, oldProjViewMatrix);
|
---|
454 | CombineSsao(fbo);
|
---|
455 | }
|
---|
456 | else
|
---|
457 | {
|
---|
458 | ComputeGlobIllum(fbo, expFactor, oldProjViewMatrix);
|
---|
459 | CombineIllum(fbo);
|
---|
460 | }
|
---|
461 |
|
---|
462 | AntiAliasing(fbo);
|
---|
463 |
|
---|
464 | glEnable(GL_LIGHTING);
|
---|
465 | glDisable(GL_TEXTURE_2D);
|
---|
466 |
|
---|
467 | glMatrixMode(GL_PROJECTION);
|
---|
468 | glPopMatrix();
|
---|
469 |
|
---|
470 | glMatrixMode(GL_MODELVIEW);
|
---|
471 | glPopMatrix();
|
---|
472 |
|
---|
473 | glPopAttrib();
|
---|
474 |
|
---|
475 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
476 | }
|
---|
477 |
|
---|
478 |
|
---|
479 | void SsaoShader::ComputeSsao(FrameBufferObject *fbo,
|
---|
480 | float expFactor,
|
---|
481 | const Matrix4x4 &oldProjViewMatrix
|
---|
482 | )
|
---|
483 | {
|
---|
484 | cgGLSetMatrixParameterfc(sOldModelViewProjMatrixParam, (const float *)oldProjViewMatrix.x);
|
---|
485 |
|
---|
486 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
487 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
488 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
489 |
|
---|
490 | if (1)
|
---|
491 | {
|
---|
492 | // generate mip map levels for position texture
|
---|
493 | glBindTexture(GL_TEXTURE_2D, positionsTex);
|
---|
494 | glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
495 | }
|
---|
496 |
|
---|
497 |
|
---|
498 | // read the second buffer, write to the first buffer
|
---|
499 | //mNewFbo->Bind();
|
---|
500 | mFbo->Bind();
|
---|
501 | glDrawBuffers(1, mymrt + mFboIndex);
|
---|
502 |
|
---|
503 |
|
---|
504 | //GLuint oldTex = mOldFbo->GetColorBuffer(0)->GetTexture();
|
---|
505 | GLuint oldTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
|
---|
506 |
|
---|
507 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
508 |
|
---|
509 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
510 | cgGLBindProgram(sCgSsaoProgram);
|
---|
511 |
|
---|
512 | cgGLSetTextureParameter(sPositionsTexParam, positionsTex);
|
---|
513 | cgGLEnableTextureParameter(sPositionsTexParam);
|
---|
514 |
|
---|
515 | cgGLSetTextureParameter(sColorsTexParam, colorsTex);
|
---|
516 | cgGLEnableTextureParameter(sColorsTexParam);
|
---|
517 |
|
---|
518 | cgGLSetTextureParameter(sNormalsTexParam, normalsTex);
|
---|
519 | cgGLEnableTextureParameter(sNormalsTexParam);
|
---|
520 |
|
---|
521 | cgGLSetTextureParameter(sNoiseTexParam, noiseTex);
|
---|
522 | cgGLEnableTextureParameter(sNoiseTexParam);
|
---|
523 |
|
---|
524 | cgGLSetTextureParameter(sOldTexParam, oldTex);
|
---|
525 | cgGLEnableTextureParameter(sOldTexParam);
|
---|
526 |
|
---|
527 | cgGLSetParameter1f(sMaxDepthParam, mScaleFactor);
|
---|
528 |
|
---|
529 |
|
---|
530 | if (mUseTemporalCoherence)
|
---|
531 | {
|
---|
532 | cgGLSetParameter1f(sNoiseMultiplierParam, RandomValue(3.0f, 17.0f));
|
---|
533 | cgGLSetParameter1f(sExpFactorParam, expFactor);
|
---|
534 |
|
---|
535 |
|
---|
536 | // q: should we generate new samples or only rotate the old ones?
|
---|
537 | // in the first case, the sample patterns look nicer, but the kernel
|
---|
538 | // needs longer to converge
|
---|
539 | GenerateSamples(mSampling);
|
---|
540 | cgGLSetParameterArray2f(sSamplesParam, 0, NUM_SAMPLES, (const float *)samples);
|
---|
541 | }
|
---|
542 | else
|
---|
543 | {
|
---|
544 | cgGLSetParameter1f(sExpFactorParam, 1.0f);
|
---|
545 | }
|
---|
546 |
|
---|
547 | Vector3 tl, tr, bl, br;
|
---|
548 | ComputeViewVectors(tl, tr, bl, br);
|
---|
549 |
|
---|
550 | glBegin(GL_QUADS);
|
---|
551 |
|
---|
552 | // note: slightly larger texture hides ambient occlusion error on border but costs resolution
|
---|
553 | //const float new_offs = 0.55f;
|
---|
554 | const float new_offs = 0.5f;
|
---|
555 |
|
---|
556 | glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
|
---|
557 | glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
|
---|
558 | glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs, new_offs, -0.5f);
|
---|
559 | glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs, new_offs, -0.5f);
|
---|
560 |
|
---|
561 | glEnd();
|
---|
562 |
|
---|
563 | cgGLDisableTextureParameter(sColorsTexParam);
|
---|
564 | cgGLDisableTextureParameter(sPositionsTexParam);
|
---|
565 | cgGLDisableTextureParameter(sNormalsTexParam);
|
---|
566 | cgGLDisableTextureParameter(sNoiseTexParam);
|
---|
567 | cgGLDisableTextureParameter(sOldTexParam);
|
---|
568 |
|
---|
569 |
|
---|
570 | FrameBufferObject::Release();
|
---|
571 |
|
---|
572 | PrintGLerror("ssao first pass");
|
---|
573 | }
|
---|
574 |
|
---|
575 |
|
---|
576 | void SsaoShader::ComputeViewVectors(Vector3 &tl, Vector3 &tr, Vector3 &bl, Vector3 &br)
|
---|
577 | {
|
---|
578 | Vector3 ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr;
|
---|
579 |
|
---|
580 | mCamera->ComputePoints(ftl, ftr, fbl, fbr, ntl, ntr, nbl, nbr);
|
---|
581 |
|
---|
582 | bl = Normalize(nbl - fbl);
|
---|
583 | br = Normalize(nbr - fbr);
|
---|
584 | tl = Normalize(ntl - ftl);
|
---|
585 | tr = Normalize(ntr - ftr);
|
---|
586 | }
|
---|
587 |
|
---|
588 |
|
---|
589 |
|
---|
590 | static void SetVertex(float x, float y, float x_offs, float y_offs)
|
---|
591 | {
|
---|
592 | glMultiTexCoord2fARB(GL_TEXTURE0_ARB, x, y); // center
|
---|
593 | glMultiTexCoord2fARB(GL_TEXTURE1_ARB, x - x_offs, y + y_offs); // left top
|
---|
594 | glMultiTexCoord2fARB(GL_TEXTURE2_ARB, x + x_offs, y - y_offs); // right bottom
|
---|
595 | glMultiTexCoord2fARB(GL_TEXTURE3_ARB, x + x_offs, y + y_offs); // right top
|
---|
596 | glMultiTexCoord2fARB(GL_TEXTURE4_ARB, x - x_offs, y - y_offs); // left bottom
|
---|
597 |
|
---|
598 | glMultiTexCoord4fARB(GL_TEXTURE5_ARB, x - x_offs, y, x + x_offs, y); // left right
|
---|
599 | glMultiTexCoord4fARB(GL_TEXTURE6_ARB, x, y + y_offs, x, y - y_offs); // top bottom
|
---|
600 |
|
---|
601 | glVertex3f(x - 0.5f, y - 0.5f, -0.5f);
|
---|
602 | }
|
---|
603 |
|
---|
604 |
|
---|
605 | void SsaoShader::AntiAliasing(FrameBufferObject *fbo)
|
---|
606 | {
|
---|
607 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
608 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
609 |
|
---|
610 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
611 |
|
---|
612 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
613 | cgGLBindProgram(sCgAntiAliasingProgram);
|
---|
614 |
|
---|
615 | cgGLSetTextureParameter(sColorsTexAntiAliasingParam, colorsTex);
|
---|
616 | cgGLEnableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
617 |
|
---|
618 | cgGLSetTextureParameter(sNormalsTexAntiAliasingParam, normalsTex);
|
---|
619 | cgGLEnableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
620 |
|
---|
621 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
622 |
|
---|
623 | float offs2 = 0.5f;
|
---|
624 |
|
---|
625 | glBegin(GL_QUADS);
|
---|
626 |
|
---|
627 | // the neighbouring texels
|
---|
628 | float x_offs = 1.0f / mWidth;
|
---|
629 | float y_offs = 1.0f / mHeight;
|
---|
630 |
|
---|
631 | SetVertex(0, 0, x_offs, y_offs);
|
---|
632 | SetVertex(1, 0, x_offs, y_offs);
|
---|
633 | SetVertex(1, 1, x_offs, y_offs);
|
---|
634 | SetVertex(0, 1, x_offs, y_offs);
|
---|
635 |
|
---|
636 | glEnd();
|
---|
637 |
|
---|
638 | cgGLDisableTextureParameter(sColorsTexAntiAliasingParam);
|
---|
639 | cgGLDisableTextureParameter(sNormalsTexAntiAliasingParam);
|
---|
640 |
|
---|
641 | PrintGLerror("antialiasing");
|
---|
642 | }
|
---|
643 |
|
---|
644 |
|
---|
645 | void SsaoShader::FirstPass(FrameBufferObject *fbo)
|
---|
646 | {
|
---|
647 | GLuint colorsTex = fbo->GetColorBuffer(0)->GetTexture();
|
---|
648 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
649 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
650 |
|
---|
651 | fbo->Bind();
|
---|
652 |
|
---|
653 | glDrawBuffers(1, mymrt + 3);
|
---|
654 |
|
---|
655 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
656 |
|
---|
657 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
658 |
|
---|
659 | cgGLBindProgram(sCgDeferredProgram);
|
---|
660 |
|
---|
661 | cgGLSetTextureParameter(sColorsTexDeferredParam, colorsTex);
|
---|
662 | cgGLEnableTextureParameter(sColorsTexDeferredParam);
|
---|
663 |
|
---|
664 | cgGLSetTextureParameter(sPositionsTexDeferredParam, positionsTex);
|
---|
665 | cgGLEnableTextureParameter(sPositionsTexDeferredParam);
|
---|
666 |
|
---|
667 | cgGLSetTextureParameter(sNormalsTexDeferredParam, normalsTex);
|
---|
668 | cgGLEnableTextureParameter(sNormalsTexDeferredParam);
|
---|
669 |
|
---|
670 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
671 |
|
---|
672 | const float offs = 0.5f;
|
---|
673 |
|
---|
674 | glBegin(GL_QUADS);
|
---|
675 |
|
---|
676 | glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
|
---|
677 | glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
|
---|
678 | glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f);
|
---|
679 | glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f);
|
---|
680 |
|
---|
681 | glEnd();
|
---|
682 |
|
---|
683 | cgGLDisableTextureParameter(sColorsTexDeferredParam);
|
---|
684 | cgGLDisableTextureParameter(sPositionsTexDeferredParam);
|
---|
685 | cgGLDisableTextureParameter(sNormalsTexDeferredParam);
|
---|
686 |
|
---|
687 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
688 |
|
---|
689 | FrameBufferObject::Release();
|
---|
690 |
|
---|
691 | PrintGLerror("deferred shading");
|
---|
692 | }
|
---|
693 |
|
---|
694 |
|
---|
695 | void SsaoShader::ComputeGlobIllum(FrameBufferObject *fbo,
|
---|
696 | float expFactor,
|
---|
697 | const Matrix4x4 &oldProjViewMatrix
|
---|
698 | )
|
---|
699 | {
|
---|
700 | cgGLSetMatrixParameterfc(sOldModelViewProjMatrixGiParam, (const float *)oldProjViewMatrix.x);
|
---|
701 |
|
---|
702 | //GLuint colorsTex = mFbo->GetColorBuffer(0)->GetTexture();
|
---|
703 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
704 | GLuint positionsTex = fbo->GetColorBuffer(1)->GetTexture();
|
---|
705 | GLuint normalsTex = fbo->GetColorBuffer(2)->GetTexture();
|
---|
706 |
|
---|
707 | if (1)
|
---|
708 | {
|
---|
709 | // generate mip map levels for position texture
|
---|
710 | glBindTexture(GL_TEXTURE_2D, positionsTex);
|
---|
711 | glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
712 |
|
---|
713 | // generate mip map levels for position texture
|
---|
714 | glBindTexture(GL_TEXTURE_2D, colorsTex);
|
---|
715 | glGenerateMipmapEXT(GL_TEXTURE_2D);
|
---|
716 | }
|
---|
717 |
|
---|
718 |
|
---|
719 | // read the second buffer, write to the first buffer
|
---|
720 | //mNewFbo->Bind();
|
---|
721 | mFbo->Bind();
|
---|
722 |
|
---|
723 | glDrawBuffers(2, mymrt + mFboIndex);
|
---|
724 |
|
---|
725 | //GLuint oldSsaoTex = mOldFbo->GetColorBuffer(0)->GetTexture();
|
---|
726 | //GLuint oldIllumTex = mOldFbo->GetColorBuffer(1)->GetTexture();
|
---|
727 |
|
---|
728 | GLuint oldSsaoTex = mFbo->GetColorBuffer(2 - mFboIndex)->GetTexture();
|
---|
729 | GLuint oldIllumTex = mFbo->GetColorBuffer(2 - mFboIndex + 1)->GetTexture();
|
---|
730 |
|
---|
731 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
732 |
|
---|
733 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
734 | cgGLBindProgram(sCgGiProgram);
|
---|
735 |
|
---|
736 | cgGLSetTextureParameter(sPositionsTexGiParam, positionsTex);
|
---|
737 | cgGLEnableTextureParameter(sPositionsTexGiParam);
|
---|
738 |
|
---|
739 | cgGLSetTextureParameter(sColorsTexGiParam, colorsTex);
|
---|
740 | cgGLEnableTextureParameter(sColorsTexGiParam);
|
---|
741 |
|
---|
742 | cgGLSetTextureParameter(sNormalsTexGiParam, normalsTex);
|
---|
743 | cgGLEnableTextureParameter(sNormalsTexGiParam);
|
---|
744 |
|
---|
745 | cgGLSetTextureParameter(sNoiseTexGiParam, noiseTex);
|
---|
746 | cgGLEnableTextureParameter(sNoiseTexGiParam);
|
---|
747 |
|
---|
748 | cgGLSetTextureParameter(sOldSsaoTexGiParam, oldSsaoTex);
|
---|
749 | cgGLEnableTextureParameter(sOldSsaoTexGiParam);
|
---|
750 |
|
---|
751 | cgGLSetTextureParameter(sOldIllumTexGiParam, oldIllumTex);
|
---|
752 | cgGLEnableTextureParameter(sOldIllumTexGiParam);
|
---|
753 |
|
---|
754 | cgGLSetParameter1f(sMaxDepthGiParam, mScaleFactor);
|
---|
755 |
|
---|
756 |
|
---|
757 | if (mUseTemporalCoherence)
|
---|
758 | {
|
---|
759 | cgGLSetParameter1f(sNoiseMultiplierGiParam, RandomValue(3.0f, 17.0f));
|
---|
760 | cgGLSetParameter1f(sExpFactorGiParam, expFactor);
|
---|
761 |
|
---|
762 | // q: should we generate new samples or only rotate the old ones?
|
---|
763 | // in the first case, the sample patterns look nicer, but the kernel
|
---|
764 | // needs longer to converge
|
---|
765 | GenerateSamples(mSampling);
|
---|
766 | cgGLSetParameterArray2f(sSamplesGiParam, 0, NUM_SAMPLES, (const float *)samples);
|
---|
767 | }
|
---|
768 | else
|
---|
769 | {
|
---|
770 | cgGLSetParameter1f(sExpFactorGiParam, 1.0f);
|
---|
771 | }
|
---|
772 |
|
---|
773 | Vector3 tl, tr, bl, br;
|
---|
774 | ComputeViewVectors(tl, tr, bl, br);
|
---|
775 |
|
---|
776 | glBegin(GL_QUADS);
|
---|
777 |
|
---|
778 | // note: slightly larger texture hides ambient occlusion error on border but costs resolution
|
---|
779 | //const float new_offs = 0.55f;
|
---|
780 | const float new_offs = 0.5f;
|
---|
781 |
|
---|
782 | glTexCoord2f(0, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, bl.x, bl.y, bl.z); glVertex3f(-new_offs, -new_offs, -0.5f);
|
---|
783 | glTexCoord2f(1, 0); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, br.x, br.y, br.z); glVertex3f( new_offs, -new_offs, -0.5f);
|
---|
784 | glTexCoord2f(1, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tr.x, tr.y, tr.z); glVertex3f( new_offs, new_offs, -0.5f);
|
---|
785 | glTexCoord2f(0, 1); glMultiTexCoord3fARB(GL_TEXTURE1_ARB, tl.x, tl.y, tl.z); glVertex3f(-new_offs, new_offs, -0.5f);
|
---|
786 |
|
---|
787 | glEnd();
|
---|
788 |
|
---|
789 | cgGLDisableTextureParameter(sColorsTexGiParam);
|
---|
790 | cgGLDisableTextureParameter(sPositionsTexGiParam);
|
---|
791 | cgGLDisableTextureParameter(sNormalsTexGiParam);
|
---|
792 | cgGLDisableTextureParameter(sNoiseTexGiParam);
|
---|
793 | cgGLDisableTextureParameter(sOldSsaoTexGiParam);
|
---|
794 | cgGLDisableTextureParameter(sOldIllumTexGiParam);
|
---|
795 |
|
---|
796 | FrameBufferObject::Release();
|
---|
797 |
|
---|
798 | PrintGLerror("globillum first pass");
|
---|
799 | }
|
---|
800 |
|
---|
801 |
|
---|
802 | void SsaoShader::CombineIllum(FrameBufferObject *fbo)
|
---|
803 | {
|
---|
804 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
805 |
|
---|
806 | //GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
|
---|
807 | //GLuint illumTex = mNewFbo->GetColorBuffer(1)->GetTexture();
|
---|
808 | GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
|
---|
809 | GLuint illumTex = mFbo->GetColorBuffer(mFboIndex + 1)->GetTexture();
|
---|
810 |
|
---|
811 |
|
---|
812 | fbo->Bind();
|
---|
813 |
|
---|
814 | // overwrite old color texture
|
---|
815 | glDrawBuffers(1, mymrt);
|
---|
816 |
|
---|
817 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
818 |
|
---|
819 | cgGLBindProgram(sCgCombinedIllumProgram);
|
---|
820 |
|
---|
821 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
822 |
|
---|
823 |
|
---|
824 | cgGLSetTextureParameter(sColorsTexCombinedIllumParam, colorsTex);
|
---|
825 | cgGLEnableTextureParameter(sColorsTexCombinedIllumParam);
|
---|
826 |
|
---|
827 | cgGLSetTextureParameter(sSsaoTexCombinedIllumParam, ssaoTex);
|
---|
828 | cgGLEnableTextureParameter(sSsaoTexCombinedIllumParam);
|
---|
829 |
|
---|
830 | cgGLSetTextureParameter(sIllumTexCombinedIllumParam, illumTex);
|
---|
831 | cgGLEnableTextureParameter(sIllumTexCombinedIllumParam);
|
---|
832 |
|
---|
833 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
834 |
|
---|
835 | const float offs = 0.5f;
|
---|
836 |
|
---|
837 | glBegin(GL_QUADS);
|
---|
838 |
|
---|
839 | glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
|
---|
840 | glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
|
---|
841 | glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f);
|
---|
842 | glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f);
|
---|
843 |
|
---|
844 | glEnd();
|
---|
845 |
|
---|
846 | cgGLDisableTextureParameter(sColorsTexCombinedIllumParam);
|
---|
847 | cgGLDisableTextureParameter(sSsaoTexCombinedIllumParam);
|
---|
848 | cgGLDisableTextureParameter(sIllumTexCombinedIllumParam);
|
---|
849 |
|
---|
850 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
851 |
|
---|
852 | FrameBufferObject::Release();
|
---|
853 |
|
---|
854 | PrintGLerror("combine");
|
---|
855 | }
|
---|
856 |
|
---|
857 |
|
---|
858 | void SsaoShader::CombineSsao(FrameBufferObject *fbo)
|
---|
859 | {
|
---|
860 | GLuint colorsTex = fbo->GetColorBuffer(3)->GetTexture();
|
---|
861 | //GLuint ssaoTex = mNewFbo->GetColorBuffer(0)->GetTexture();
|
---|
862 | GLuint ssaoTex = mFbo->GetColorBuffer(mFboIndex)->GetTexture();
|
---|
863 |
|
---|
864 | fbo->Bind();
|
---|
865 |
|
---|
866 | // write into old color texture (not needed anymore)
|
---|
867 | glDrawBuffers(1, mymrt);
|
---|
868 |
|
---|
869 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
---|
870 |
|
---|
871 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
872 |
|
---|
873 | cgGLBindProgram(sCgCombinedSsaoProgram);
|
---|
874 |
|
---|
875 | cgGLSetTextureParameter(sColorsTexCombinedSsaoParam, colorsTex);
|
---|
876 | cgGLEnableTextureParameter(sColorsTexCombinedSsaoParam);
|
---|
877 |
|
---|
878 | cgGLSetTextureParameter(sSsaoTexCombinedSsaoParam, ssaoTex);
|
---|
879 | cgGLEnableTextureParameter(sSsaoTexCombinedSsaoParam);
|
---|
880 |
|
---|
881 |
|
---|
882 | glColor3f(1.0f, 1.0f, 1.0f);
|
---|
883 |
|
---|
884 | const float offs = 0.5f;
|
---|
885 |
|
---|
886 | glBegin(GL_QUADS);
|
---|
887 |
|
---|
888 | glTexCoord2f(0, 0); glVertex3f(-offs, -offs, -0.5f);
|
---|
889 | glTexCoord2f(1, 0); glVertex3f( offs, -offs, -0.5f);
|
---|
890 | glTexCoord2f(1, 1); glVertex3f( offs, offs, -0.5f);
|
---|
891 | glTexCoord2f(0, 1); glVertex3f(-offs, offs, -0.5f);
|
---|
892 |
|
---|
893 | glEnd();
|
---|
894 |
|
---|
895 | cgGLDisableTextureParameter(sColorsTexCombinedSsaoParam);
|
---|
896 | cgGLDisableTextureParameter(sSsaoTexCombinedSsaoParam);
|
---|
897 |
|
---|
898 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
899 |
|
---|
900 | FrameBufferObject::Release();
|
---|
901 |
|
---|
902 | PrintGLerror("combine ssao");
|
---|
903 | }
|
---|
904 |
|
---|
905 |
|
---|
906 |
|
---|
907 | } // namespace
|
---|