SSAO.cc 24.8 KB
Newer Older
Jan Möbius's avatar
   
Jan Möbius committed
1
2
/*===========================================================================*\
*                                                                            *
Jan Möbius's avatar
Jan Möbius committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
*                              OpenFlipper                                   *
*      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen       *
*                           www.openflipper.org                              *
*                                                                            *
*--------------------------------------------------------------------------- *
*  This file is part of OpenFlipper.                                         *
*                                                                            *
*  OpenFlipper is free software: you can redistribute it and/or modify       *
*  it under the terms of the GNU Lesser General Public License as            *
*  published by the Free Software Foundation, either version 3 of            *
*  the License, or (at your option) any later version with the               *
*  following exceptions:                                                     *
*                                                                            *
*  If other files instantiate templates or use macros                        *
*  or inline functions from this file, or you compile this file and          *
*  link it with other files to produce an executable, this file does         *
*  not by itself cause the resulting executable to be covered by the         *
*  GNU Lesser General Public License. This exception does not however        *
*  invalidate any other reasons why the executable file might be             *
*  covered by the GNU Lesser General Public License.                         *
*                                                                            *
*  OpenFlipper is distributed in the hope that it will be useful,            *
*  but WITHOUT ANY WARRANTY; without even the implied warranty of            *
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the             *
*  GNU Lesser General Public License for more details.                       *
*                                                                            *
*  You should have received a copy of the GNU LesserGeneral Public           *
*  License along with OpenFlipper. If not,                                   *
*  see <http://www.gnu.org/licenses/>.                                       *
*                                                                            *
\*===========================================================================*/

/*===========================================================================*\
*                                                                            *
*   $Revision$                                                       *
*   $LastChangedBy$                                                *
*   $Date$                     *
Jan Möbius's avatar
   
Jan Möbius committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
*                                                                            *
\*===========================================================================*/


#include "SSAO.hh"
#include <OpenFlipper/common/GlobalOptions.hh>
#include <OpenFlipper/BasePlugin/PluginFunctions.hh>

// shader debug mode triggers a shader reload after resizing the view window
//#define SSAO_SHADER_DEBUG_MODE

const unsigned int SSAOPlugin::numSamples_ = 32;

SSAOPlugin::ViewerResources::ViewerResources()
{
  memset(this, 0, sizeof(ViewerResources));
}

void SSAOPlugin::initializePlugin()
{
  memset(shaders_, 0, sizeof(shaders_));
  memset(programs_, 0, sizeof(programs_));

  randomVecTex_ = 0;

  generateSamplingKernel();
}

//////////////////////////////////////////////////////////////////////////

void SSAOPlugin::generateSamplingKernel()
{
  for (unsigned int i = 0; i < numSamples_; ++i)
  {
    ACG::Vec3f r; // get 3 random floats in [-0.5, 0.5]
    for (int k = 0; k < 3; ++k)
    {
      unsigned int x = (rand()*rand()*rand()) & RAND_MAX;
      r[k] = float(x) / float(RAND_MAX); // [0, 1]
      r[k] -= 0.5f;
    }
    // sphere to hemisphere
    r[2] = fabsf(r[2]);

    r.normalize();

    // more samples near the fragment
    // compute a sample distance accordingly
    float d = float(i+1) / float(numSamples_);
    d *= d;
    if (d < 0.1f) d = 0.1f;

    r *= d;

    samplingKernel_[i] = r;
  }
}

//////////////////////////////////////////////////////////////////////////

void SSAOPlugin::exit()
{
  destroyResources();
}

//////////////////////////////////////////////////////////////////////////

QString SSAOPlugin::rendererName() {
  return QString("SSAO Renderer");
}

//////////////////////////////////////////////////////////////////////////

void SSAOPlugin::supportedDrawModes(ACG::SceneGraph::DrawModes::DrawMode& _mode) 
{
  _mode =  ACG::SceneGraph::DrawModes::DEFAULT;
}

//////////////////////////////////////////////////////////////////////////

Christopher Tenter's avatar
Christopher Tenter committed
120
void SSAOPlugin::reloadResources(int _viewerId, unsigned int _sceneTexWidth, unsigned int _sceneTexHeight) 
Jan Möbius's avatar
   
Jan Möbius committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
{
  ViewerResources* p = &viewerRes_[_viewerId];

  // save window size
  unsigned int vpWidth = p->glWidth_, vpHeight = p->glHeight_;
  if (!p->glWidth_ || !p->glHeight_) return;

  destroyResources(_viewerId);

  p->glWidth_ = vpWidth;
  p->glHeight_ = vpHeight;

  p->rtWidth_ = p->glWidth_;
  p->rtHeight_ = p->glHeight_;

  p->rtDownWidth_ = p->rtWidth_ / 2;
  p->rtDownHeight_ = p->rtHeight_ / 2;
Christopher Tenter's avatar
Christopher Tenter committed
138
139
140

  p->rtSceneWidth_ = _sceneTexWidth;
  p->rtSceneHeight_ = _sceneTexHeight;
Jan Möbius's avatar
   
Jan Möbius committed
141
142
143
144
145
146
147
148
149
  
  // the scene texture contains the color result of a standard scene render pass
  // format: R8G8B8A8
  glGenTextures(1, &p->sceneBufTex_);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, p->sceneBufTex_);
  // texture access: clamped
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  // filter: none (1 to 1 mapping in final pass)
Christopher Tenter's avatar
Christopher Tenter committed
150
151
152
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, p->rtSceneWidth_, p->rtSceneHeight_, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
Jan Möbius's avatar
   
Jan Möbius committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

  // depth buf render texture
  // format: R32F, maybe change to R16F if it works ok
  glGenTextures(1, &p->depthBufTex_);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, p->depthBufTex_);
  // texture access: clamped
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  // filter: linear
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, p->glWidth_, p->glHeight_, 0, GL_RGB, GL_FLOAT, 0);

  // scene normals
  glGenTextures(1, &p->sceneNormalTex_);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, p->sceneNormalTex_);
  // texture access: clamped
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  // filter: linear
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, p->glWidth_, p->glHeight_, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);

  // downsampled render textures
  // format: R32F
  for (int i = 0; i < 2; ++i)
  {
    glGenTextures(1, i ? (&p->downsampledTex_) : (&p->downsampledTmpTex_));
    ACG::GLState::bindTexture(GL_TEXTURE_2D, i ? (p->downsampledTex_) : (p->downsampledTmpTex_));
    // texture access: clamped
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    // filter: linear
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, p->rtDownWidth_, p->rtDownHeight_, 0, GL_RGB, GL_FLOAT, 0);
  }

  glGenTextures(1, &p->occlusionTex_);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, p->occlusionTex_);
  // texture access: clamped
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  // filter: linear
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  glTexImage2D(GL_TEXTURE_2D, 0, GL_R32F, p->glWidth_, p->glHeight_, 0, GL_RGB, GL_FLOAT, 0);

  // end of texture creation
  ACG::GLState::bindTexture(GL_TEXTURE_2D, 0);

  ACG::glCheckErrors();

  // create depth render buffer
Christopher Tenter's avatar
Christopher Tenter committed
208
209
  glGenRenderbuffersEXT(1, &p->depthSSAORenderBuf_);
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, p->depthSSAORenderBuf_);
Jan Möbius's avatar
   
Jan Möbius committed
210
211
  glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, p->glWidth_, p->glHeight_);
  glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
Christopher Tenter's avatar
Christopher Tenter committed
212
  
Jan Möbius's avatar
   
Jan Möbius committed
213
  // initialize the fbo
Christopher Tenter's avatar
Christopher Tenter committed
214
215
  glGenFramebuffersEXT(1, &p->ssaoFbo_);
  ACG::GLState::bindFramebuffer(GL_FRAMEBUFFER_EXT, p->ssaoFbo_);
Jan Möbius's avatar
   
Jan Möbius committed
216
217
218

  // color_attachment order:
  // scene color, depth, scene normals, occlusion
Christopher Tenter's avatar
Christopher Tenter committed
219
220
221
222
  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, p->depthBufTex_, 0);
  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, p->sceneNormalTex_, 0);
  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT2_EXT, GL_TEXTURE_2D, p->occlusionTex_, 0);
  glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, p->depthSSAORenderBuf_);
Jan Möbius's avatar
   
Jan Möbius committed
223
224

  GLenum fboStatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
Christopher Tenter's avatar
Christopher Tenter committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
  if(fboStatus != GL_FRAMEBUFFER_COMPLETE_EXT)
    printf("SSAO Plugin: ssaoFbo failed to initialize\n");


  glGenFramebuffersEXT(1, &p->sceneFbo_);
  ACG::GLState::bindFramebuffer(GL_FRAMEBUFFER_EXT, p->sceneFbo_);
  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, p->sceneBufTex_, 0);

  if (p->rtSceneWidth_ > p->rtWidth_ || p->rtSceneHeight_ > p->rtHeight_)
  {
    // use new depth buffer for multisampling
    glGenRenderbuffersEXT(1, &p->depthSceneRenderBuf_);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, p->depthSceneRenderBuf_);
    glRenderbufferStorageEXT(GL_RENDERBUFFER_EXT, GL_DEPTH_COMPONENT, p->rtSceneWidth_, p->rtSceneHeight_);
    glBindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
   
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, p->depthSceneRenderBuf_);
  }
  else
    glFramebufferRenderbufferEXT(GL_FRAMEBUFFER_EXT, GL_DEPTH_ATTACHMENT_EXT, GL_RENDERBUFFER_EXT, p->depthSSAORenderBuf_);

  fboStatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
Jan Möbius's avatar
   
Jan Möbius committed
247
248
249
  if(fboStatus != GL_FRAMEBUFFER_COMPLETE_EXT)
    printf("SSAO Plugin: sceneFbo failed to initialize\n");

Christopher Tenter's avatar
Christopher Tenter committed
250

Jan Möbius's avatar
   
Jan Möbius committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  glGenFramebuffersEXT(1, &p->blurFbo_);
  ACG::GLState::bindFramebuffer(GL_FRAMEBUFFER_EXT, p->blurFbo_);
  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, p->downsampledTex_, 0);
  glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_TEXTURE_2D, p->downsampledTmpTex_, 0);


  fboStatus = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER_EXT);
  if(fboStatus != GL_FRAMEBUFFER_COMPLETE_EXT)
    printf("SSAO Plugin: blurFbo failed to initialize\n");


  ACG::GLState::bindFramebuffer(GL_FRAMEBUFFER_EXT, 0);


  // load shaders

  const char* ShaderFiles[] = {"SSAO/init_vertex.glsl",
    "SSAO/fullscreen_vertex.glsl",
    "SSAO/init_fragment.glsl",
    "SSAO/downsampling_fragment.glsl",
    "SSAO/blur_fragment.glsl",
    "SSAO/ssao_fragment.glsl",
Christopher Tenter's avatar
Christopher Tenter committed
273
274
275
    "SSAO/final_fragment.glsl",
    "SSAO/final_MSAA_vertex.glsl",
    "SSAO/final_MSAA_fragment.glsl"};
Jan Möbius's avatar
   
Jan Möbius committed
276

Christopher Tenter's avatar
Christopher Tenter committed
277
  for (int i = 0; i < 9; ++i)
Jan Möbius's avatar
   
Jan Möbius committed
278
279
280
281
282
283
284
285
286
  {
    QString shaderFile = OpenFlipper::Options::shaderDirStr() + QDir::separator() + QString(ShaderFiles[i]);

#ifdef SSAO_SHADER_DEBUG_MODE
    delete shaders_[i];
#else
    if (shaders_[i]) continue;
#endif

Christopher Tenter's avatar
Christopher Tenter committed
287
    if (i < 2 || i == 7) // first two are vertex shaders
Jan Möbius's avatar
   
Jan Möbius committed
288
289
290
291
      shaders_[i] = GLSL::loadVertexShader(shaderFile.toUtf8());
    else
      shaders_[i] = GLSL::loadFragmentShader(shaderFile.toUtf8());

Christopher Tenter's avatar
Christopher Tenter committed
292
293
    if (!shaders_[i])
    {
Jan Möbius's avatar
   
Jan Möbius committed
294
295
296
297
298
299
      log(LOGERR, QString(ShaderFiles[i]) + QString(" could not be loaded and compiled"));
      return;
    }
  }

  // all needed glprograms
Christopher Tenter's avatar
Christopher Tenter committed
300
  for (int i = 0; i < 6; ++i)
Jan Möbius's avatar
   
Jan Möbius committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
  {
#ifndef SSAO_SHADER_DEBUG_MODE
    if (!programs_[i])
#endif
    {
      delete programs_[i];
      programs_[i] = new GLSL::Program();
      GLSL::Program* pr = programs_[i];

      switch (i)
      {
      case PROG_INIT:
        pr->attach(shaders_[0]);
        pr->attach(shaders_[2]); break;

      case PROG_DOWNSAMPLING:
        pr->attach(shaders_[1]);
        pr->attach(shaders_[3]); break;

      case PROG_BLUR:
        pr->attach(shaders_[1]);
        pr->attach(shaders_[4]); break;

      case PROG_SSAO:
        pr->attach(shaders_[1]);
        pr->attach(shaders_[5]); break;

      case PROG_FINAL:
        pr->attach(shaders_[1]);
        pr->attach(shaders_[6]); break;
Christopher Tenter's avatar
Christopher Tenter committed
331
332
333
334

      case PROG_FINAL_MSAA:
        pr->attach(shaders_[7]);
        pr->attach(shaders_[8]); break;
Jan Möbius's avatar
   
Jan Möbius committed
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
      }

      pr->link();
    }
  }

  if (!randomVecTex_)
  {
    // random vector texture
    glGenTextures(1, &randomVecTex_);
    ACG::GLState::bindTexture(GL_TEXTURE_2D, randomVecTex_);
    // texture access: wrapped
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    // filter: none
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

    ACG::Vec4f randVecs[16];
    for (int i = 0; i < 16; ++i)
    {
      ACG::Vec3f x;
      for (int k = 0; k < 3; ++k)
        x[k] = float(rand()) / float(RAND_MAX);

      float theta = x[0] * 6.2831853f; // 2pi
      float phi = x[1] * 6.2831853f;
      randVecs[i][0] = sinf(phi);
      randVecs[i][1] = cosf(phi);
      randVecs[i][2] = sinf(theta);
      randVecs[i][3] = cosf(theta);
    }
Christopher Tenter's avatar
Christopher Tenter committed
367
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F_ARB, 4, 4, 0, GL_RGBA, GL_FLOAT, randVecs);
Jan Möbius's avatar
   
Jan Möbius committed
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
  }
  
  ACG::GLState::bindTexture(GL_TEXTURE_2D, 0);

  ACG::glCheckErrors();
}

//////////////////////////////////////////////////////////////////////////

void SSAOPlugin::destroyResources() {

  for (unsigned int i = 0; i < sizeof(programs_) / sizeof(programs_[0]); ++i)
  {
    delete programs_[i]; programs_[i] = 0;
  }

  for (unsigned int i = 0; i < sizeof(shaders_) / sizeof(shaders_[0]); ++i)
  {
    delete shaders_[i];
    shaders_[i] = 0;
  }

  if (randomVecTex_) glDeleteTextures(1, &randomVecTex_);
  randomVecTex_ = 0;

  // free all viewer specific resources
  std::map<int, ViewerResources>::iterator resIt = viewerRes_.begin();
  for (; resIt != viewerRes_.end(); ++resIt)
    destroyResources(resIt->first);
}

//////////////////////////////////////////////////////////////////////////

void SSAOPlugin::destroyResources(int _viewerId)
{
  ViewerResources* p = &viewerRes_[_viewerId];

  if (p->sceneFbo_) glDeleteFramebuffersEXT(1, &p->sceneFbo_);
Christopher Tenter's avatar
Christopher Tenter committed
406
  if (p->ssaoFbo_) glDeleteFramebuffersEXT(1, &p->ssaoFbo_);
Jan Möbius's avatar
   
Jan Möbius committed
407
408
  if (p->blurFbo_) glDeleteFramebuffersEXT(1, &p->blurFbo_);

Christopher Tenter's avatar
Christopher Tenter committed
409
410
411
  if (p->depthSSAORenderBuf_) glDeleteRenderbuffersEXT(1, &p->depthSSAORenderBuf_);
  
  if (p->depthSceneRenderBuf_) glDeleteRenderbuffersEXT(1, &p->depthSceneRenderBuf_);
Jan Möbius's avatar
   
Jan Möbius committed
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495

  if (p->sceneBufTex_) glDeleteTextures(1, &p->sceneBufTex_);
  if (p->depthBufTex_) glDeleteTextures(1, &p->depthBufTex_);
  if (p->downsampledTex_) glDeleteTextures(1, &p->downsampledTex_);
  if (p->downsampledTmpTex_) glDeleteTextures(1, &p->downsampledTmpTex_);
  if (p->occlusionTex_) glDeleteTextures(1, &p->occlusionTex_);
  if (p->sceneNormalTex_) glDeleteTextures(1, &p->sceneNormalTex_);

  // zero out
  memset(p, 0, sizeof(ViewerResources));
}

//////////////////////////////////////////////////////////////////////////

void SSAOPlugin::drawQuadProj(float x0, float y0, float w, float h)
{
  // quad in projection space
  // here only position are passed to GL
  // tex-coords can be generated in a vertex-shader as follows
  //  uv = pos * (.5, -.5) + (.5, .5)

  glBegin(GL_QUADS);
  {
    glVertex2f(x0,  y0);
    glVertex2f(x0, y0-h);
    glVertex2f(x0+w, y0-h);
    glVertex2f(x0+w, y0);
  }
  glEnd();
}

//////////////////////////////////////////////////////////////////////////

void SSAOPlugin::drawScenePass(ACG::GLState* _glState, Viewer::ViewerProperties& _properties, BaseNode* _sceneGraphRoot)
{
  ACG::SceneGraph::DrawAction action(_properties.drawMode(), *_glState, false);
  ACG::SceneGraph::traverse_multipass(_sceneGraphRoot, action, *_glState, _properties.drawMode());
}

//////////////////////////////////////////////////////////////////////////

void SSAOPlugin::gaussianBlurPass(const ViewerResources* _pViewer, const float* _texelSize, 
                                  GLenum _targetAttachement, GLuint _srcTexture)
{
  // standard deviation for gaussian blur filter
  const float gaussStDev = 1.0f;

  ACG::GLState::drawBuffer(_targetAttachement);

  float gaussKernel[5];
  float sum = 0.0f; // sum of kernel tabs
  for (int i = 0; i < 5; ++i)
  {
    // 1 / (4 pi s^2) e^(-x^2 / s^2 ),  constant factor useless here
    gaussKernel[i] = powf(2.71828f, -float(i*i)*(_texelSize[0]*_texelSize[0] + _texelSize[1]*_texelSize[1]) /
      (gaussStDev*gaussStDev));
    sum += gaussKernel[i];
  }
  // normalize kernel
  for (int i = 0; i < 5; ++i)
    gaussKernel[i] /= sum;

  ACG::GLState::activeTexture(GL_TEXTURE0);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, _srcTexture);

  programs_[PROG_BLUR]->setUniform("Tex", 0);
  programs_[PROG_BLUR]->setUniform("TexelSize", ACG::Vec2f(_texelSize));
  programs_[PROG_BLUR]->setUniform("Kernel", gaussKernel, 5);

  drawQuadProj();
}

//////////////////////////////////////////////////////////////////////////

void SSAOPlugin::render(ACG::GLState* _glState, Viewer::ViewerProperties& _properties)
{
  glPushAttrib(GL_ALL_ATTRIB_BITS);

  int viewerId = _properties.viewerId();

  ViewerResources* pViewer = &viewerRes_[viewerId];
  pViewer->glWidth_ = _glState->viewport_width();
  pViewer->glHeight_ = _glState->viewport_height();

Christopher Tenter's avatar
Christopher Tenter committed
496
497
498
499
500
501
502
  if (_properties.multisampling())
  {
    if ((pViewer->glWidth_ * 2 != pViewer->rtSceneWidth_) || (pViewer->glHeight_ * 2 != pViewer->rtSceneHeight_))
      reloadResources(viewerId, pViewer->glWidth_ * 2, pViewer->glHeight_ * 2);
  }
  else if ((pViewer->glWidth_ != pViewer->rtSceneWidth_) || (pViewer->glHeight_ != pViewer->rtSceneHeight_))
    reloadResources(viewerId, pViewer->glWidth_, pViewer->glHeight_);
Jan Möbius's avatar
   
Jan Möbius committed
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522

  BaseNode* sceneGraphRoot = PluginFunctions::getSceneGraphRootNode();  

  GLenum drawBuffers[] = {GL_COLOR_ATTACHMENT0_EXT,
    GL_COLOR_ATTACHMENT1_EXT,
    GL_COLOR_ATTACHMENT2_EXT,
    GL_COLOR_ATTACHMENT3_EXT,
    GL_COLOR_ATTACHMENT4_EXT,
    GL_COLOR_ATTACHMENT5_EXT,
    GL_COLOR_ATTACHMENT6_EXT};

  // the farthest depth value possible in the depth buffer
  const float maxDepth = 1000.0f;

  GLint oldViewport[4];
  glGetIntegerv(GL_VIEWPORT, oldViewport);

  for (int i = 0; i < 6; ++i)
  {
    ACG::GLState::activeTexture(GL_TEXTURE0 + i);
Christopher Tenter's avatar
Christopher Tenter committed
523
    ACG::GLState::bindTexture(GL_TEXTURE_2D, 0);
Jan Möbius's avatar
   
Jan Möbius committed
524
525
526
527
528
529
  }

  float texelSize[4] = {1.0f / float(pViewer->rtWidth_), 1.0f / float(pViewer->rtHeight_), 0.0f, 0.0f};


  // ---------------------------------------------
Christopher Tenter's avatar
Christopher Tenter committed
530
531
532
533
  // 1. render scene with standard materials:
  glViewport(0, 0, pViewer->rtSceneWidth_, pViewer->rtSceneHeight_);

  ACG::GLState::bindFramebuffer(GL_FRAMEBUFFER_EXT, pViewer->sceneFbo_);
Jan Möbius's avatar
   
Jan Möbius committed
534
535
536
537
538
539
  ACG::GLState::drawBuffer(drawBuffers[0]); // scene buffer in render target 0
  glClearColor(_glState->clear_color()[0], _glState->clear_color()[1], _glState->clear_color()[2], 0);

  // NOTE: for some reason the early z pass optimization does not work here
  //  using the depth buffer from previous pass gives z fighting
  // early z cull optimization settings:
Christopher Tenter's avatar
Christopher Tenter committed
540
541
542
543
544
  //   ACG::GLState::enable(GL_DEPTH_TEST);
  //   ACG::GLState::depthFunc(GL_LEQUAL);
  //   ACG::GLState::lockDepthFunc();
  //   glDepthMask(GL_FALSE); // disable z writing
  //   glClear(GL_COLOR_BUFFER_BIT);
Jan Möbius's avatar
   
Jan Möbius committed
545
546
547
548
549
550
551
552
553
554

  // without early z cull:
  ACG::GLState::enable(GL_DEPTH_TEST);
  ACG::GLState::depthFunc(GL_LESS);
  glDepthMask(GL_TRUE);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  drawScenePass(_glState, _properties, sceneGraphRoot);

  ACG::GLState::unlockDepthFunc(); // unlock less-equal depth function
Christopher Tenter's avatar
Christopher Tenter committed
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582


  if (pViewer->rtSceneWidth_ != pViewer->glWidth_ || pViewer->rtSceneHeight_ != pViewer->glHeight_)
    glViewport(0, 0, pViewer->glWidth_, pViewer->glHeight_);

  // ---------------------------------------------
  // 2. init depth and normal targets
  ACG::GLState::bindFramebuffer(GL_FRAMEBUFFER_EXT, pViewer->ssaoFbo_);
  ACG::GLState::enable(GL_DEPTH_TEST);
  ACG::GLState::depthFunc(GL_LESS);
  glDepthMask(GL_TRUE);

  // color attachment 0 and 1 stores the scene depth and normals
  // clear first
  ACG::GLState::drawBuffer(drawBuffers[0]);
  glClearColor(maxDepth, 0.0f, 0.0f, 0.0f);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  ACG::GLState::drawBuffer(drawBuffers[1]);
  glClearColor(0.5f, 0.5f, 0.0f, 0.0f);
  glClear(GL_COLOR_BUFFER_BIT);

  ACG::GLState::drawBuffers(2, drawBuffers);

  programs_[PROG_INIT]->use();
  drawScenePass(_glState, _properties, sceneGraphRoot);
  programs_[PROG_INIT]->disable();

Jan Möbius's avatar
   
Jan Möbius committed
583
584
  // ---------------------------------------------
  // 3. compute occlusion
Christopher Tenter's avatar
Christopher Tenter committed
585
  ACG::GLState::drawBuffer(drawBuffers[2]); // occlusion buffer in render target 2
Jan Möbius's avatar
   
Jan Möbius committed
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
  ACG::GLState::disable(GL_DEPTH_TEST);

  texelSize[0] = 1.0f / float(pViewer->rtWidth_);
  texelSize[1] = 1.0f / float(pViewer->rtHeight_);

  programs_[PROG_SSAO]->use();
  programs_[PROG_SSAO]->setUniform("TexelSize", ACG::Vec2f(texelSize[0], texelSize[1]));
  programs_[PROG_SSAO]->setUniform("ScreenSize", ACG::Vec2f(pViewer->rtWidth_, pViewer->rtHeight_));
  {
    GLint location = programs_[PROG_SSAO]->getUniformLocation("Kernel");
    glUniform3fv(location, 32, (GLfloat*)samplingKernel_);
  }

  programs_[PROG_SSAO]->setUniform("RandTex", 3);
  programs_[PROG_SSAO]->setUniform("NormalTex", 2);
  programs_[PROG_SSAO]->setUniform("SceneTex", 1);
  programs_[PROG_SSAO]->setUniform("DepthTex", 0);

  ACG::GLState::activeTexture(GL_TEXTURE3);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, randomVecTex_);

  ACG::GLState::activeTexture(GL_TEXTURE2);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, pViewer->sceneNormalTex_);

  ACG::GLState::activeTexture(GL_TEXTURE1);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, pViewer->sceneBufTex_);

  ACG::GLState::activeTexture(GL_TEXTURE0);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, pViewer->depthBufTex_);

  drawQuadProj();


  ACG::GLState::activeTexture(GL_TEXTURE2);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, 0);

  // ---------------------------------------------
  // 4. downsample the occlusion texture to 1/4 its size
  glViewport(0, 0, pViewer->rtDownWidth_, pViewer->rtDownHeight_);

  ACG::GLState::bindFramebuffer(GL_FRAMEBUFFER_EXT, pViewer->blurFbo_);
  ACG::GLState::drawBuffer(drawBuffers[0]);
  // disable depth testing and writing from now on
  ACG::GLState::disable(GL_DEPTH_TEST);

  programs_[PROG_DOWNSAMPLING]->use();
  programs_[PROG_DOWNSAMPLING]->setUniform("TexelSize", ACG::Vec2f(texelSize));

  // bind depth rt
  ACG::GLState::activeTexture(GL_TEXTURE0);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, pViewer->occlusionTex_);
  programs_[PROG_DOWNSAMPLING]->setUniform("Tex", 0);

  drawQuadProj();

  //-----------------------------------------
  // 5. gaussian blur filter

  programs_[PROG_BLUR]->use();
  programs_[PROG_BLUR]->setUniform("DepthTex", 1);
Christopher Tenter's avatar
Christopher Tenter committed
646
  programs_[PROG_BLUR]->setUniform("EdgeBlur", _properties.multisampling() ? 0.3f : 0.0f);
Jan Möbius's avatar
   
Jan Möbius committed
647
648
649
650
651
652
653
  ACG::GLState::activeTexture(GL_TEXTURE1);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, pViewer->depthBufTex_);

  // horizontal
  texelSize[0] = 1.0f / float(pViewer->rtDownWidth_);
  texelSize[1] = 0.0f;

Christopher Tenter's avatar
Christopher Tenter committed
654
  gaussianBlurPass(pViewer, texelSize, drawBuffers[1], pViewer->downsampledTex_);
Jan Möbius's avatar
   
Jan Möbius committed
655
656
657
658
659

  // vertical
  texelSize[0] = 0.0f;
  texelSize[1] = 1.0f / float(pViewer->rtDownHeight_);

Christopher Tenter's avatar
Christopher Tenter committed
660
  gaussianBlurPass(pViewer, texelSize, drawBuffers[0], pViewer->downsampledTmpTex_);
Jan Möbius's avatar
   
Jan Möbius committed
661
662
663
664
665
666
667
668
669
670
671
672
673
  // blurred result in pViewer->downsampledTex_

  //-----------------------------------------
  // 6. final pass, present result
  glViewport(oldViewport[0], oldViewport[1], oldViewport[2], oldViewport[3]);
  ACG::GLState::bindFramebuffer(GL_FRAMEBUFFER_EXT, 0);

  ACG::GLState::activeTexture(GL_TEXTURE1);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, pViewer->downsampledTex_);

  ACG::GLState::activeTexture(GL_TEXTURE0);
  ACG::GLState::bindTexture(GL_TEXTURE_2D, pViewer->sceneBufTex_);

Christopher Tenter's avatar
Christopher Tenter committed
674
675
676
677
678
679
680
  GLSL::Program* finalProg = programs_[_properties.multisampling() ? PROG_FINAL_MSAA : PROG_FINAL];

  finalProg->use();
  finalProg->setUniform("OcclusionTex", 1);
  finalProg->setUniform("SceneTex", 0);
  if (_properties.multisampling())
    finalProg->setUniform("SceneTexelSize", ACG::Vec2f(1.0f / float(pViewer->rtSceneWidth_), 1.0f / float(pViewer->rtSceneHeight_)));
Jan Möbius's avatar
   
Jan Möbius committed
681
682
683

  drawQuadProj();

Christopher Tenter's avatar
Christopher Tenter committed
684
  finalProg->disable();
Jan Möbius's avatar
   
Jan Möbius committed
685
686
687
688
689
690
691
692
  //-----------------------------------------


  glPopAttrib();
}

Q_EXPORT_PLUGIN2( ssaoplugin , SSAOPlugin );