Ssao.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /**
  2. * @file
  3. *
  4. * Post-processing stage screen space ambient occlusion pass
  5. */
  6. #include <boost/lexical_cast.hpp>
  7. #include "Renderer.h"
  8. #include "Camera.h"
  9. //======================================================================================================================
  10. // initBlurFbos =
  11. //======================================================================================================================
  12. void Renderer::Pps::Ssao::initBlurFbo(Fbo& fbo, Texture& fai)
  13. {
  14. // create FBO
  15. fbo.create();
  16. fbo.bind();
  17. // inform in what buffers we draw
  18. fbo.setNumOfColorAttachements(1);
  19. // create the texes
  20. fai.createEmpty2D(bwidth, bheight, GL_ALPHA8, GL_ALPHA, GL_FLOAT, false);
  21. fai.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  22. fai.setTexParameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  23. // attach
  24. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fai.getGlId(), 0);
  25. // test if success
  26. if(!fbo.isGood())
  27. FATAL("Cannot create deferred shading post-processing stage SSAO blur FBO");
  28. // unbind
  29. fbo.unbind();
  30. }
  31. //======================================================================================================================
  32. // init =
  33. //======================================================================================================================
  34. void Renderer::Pps::Ssao::init()
  35. {
  36. width = renderingQuality * r.width;
  37. height = renderingQuality * r.height;
  38. bwidth = height * bluringQuality;
  39. bheight = height * bluringQuality;
  40. //
  41. // init FBOs
  42. //
  43. // create FBO
  44. pass0Fbo.create();
  45. pass0Fbo.bind();
  46. // inform in what buffers we draw
  47. pass0Fbo.setNumOfColorAttachements(1);
  48. // create the FAI
  49. pass0Fai.createEmpty2D(width, height, GL_ALPHA8, GL_ALPHA, GL_FLOAT, false);
  50. pass0Fai.setTexParameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  51. pass0Fai.setTexParameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  52. // attach
  53. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, pass0Fai.getGlId(), 0);
  54. // test if success
  55. if(!pass0Fbo.isGood())
  56. FATAL("Cannot create deferred shading post-processing stage SSAO pass FBO");
  57. // unbind
  58. pass0Fbo.unbind();
  59. initBlurFbo(pass1Fbo, pass1Fai);
  60. initBlurFbo(pass2Fbo, fai);
  61. //
  62. // Shaders
  63. //
  64. ssaoSProg.loadRsrc("shaders/PpsSsao.glsl");
  65. string pps = "#define _PPS_SSAO_PASS_0_\n#define PASS0_FAI_WIDTH " + lexical_cast<string>(static_cast<float>(width)) +
  66. "\n";
  67. string prefix = "Pass0Width" + lexical_cast<string>(width);
  68. blurSProg.loadRsrc(ShaderProg::createSrcCodeToCache("shaders/PpsSsaoBlur.glsl", pps.c_str(), prefix.c_str()).c_str());
  69. pps = "#define _PPS_SSAO_PASS_1_\n#define PASS1_FAI_HEIGHT " + lexical_cast<string>(static_cast<float>(bheight)) +
  70. "\n";
  71. prefix = "Pass1Height" + lexical_cast<string>(bheight);
  72. blurSProg2.loadRsrc(ShaderProg::createSrcCodeToCache("shaders/PpsSsaoBlur.glsl", pps.c_str(),
  73. prefix.c_str()).c_str());
  74. camerarangeUniVar = ssaoSProg->findUniVar("camerarange");
  75. msDepthFaiUniVar = ssaoSProg->findUniVar("msDepthFai");
  76. noiseMapUniVar = ssaoSProg->findUniVar("noiseMap");
  77. msNormalFaiUniVar = ssaoSProg->findUniVar("msNormalFai");
  78. blurSProgFaiUniVar = blurSProg->findUniVar("tex"); /// @todo rename the tex in the shader
  79. blurSProg2FaiUniVar = blurSProg2->findUniVar("tex"); /// @todo rename the tex in the shader
  80. //
  81. // noise map
  82. //
  83. /// @todo fix this crap
  84. // load noise map and disable temporally the texture compression and enable mipmapping
  85. bool texCompr = Texture::compressionEnabled;
  86. bool mipmaping = Texture::mipmappingEnabled;
  87. Texture::compressionEnabled = false;
  88. Texture::mipmappingEnabled = true;
  89. noiseMap.loadRsrc("gfx/noise3.tga");
  90. noiseMap->setTexParameter(GL_TEXTURE_WRAP_S, GL_REPEAT);
  91. noiseMap->setTexParameter(GL_TEXTURE_WRAP_T, GL_REPEAT);
  92. //noise_map->setTexParameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  93. //noise_map->setTexParameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  94. Texture::compressionEnabled = texCompr;
  95. Texture::mipmappingEnabled = mipmaping;
  96. }
  97. //======================================================================================================================
  98. // run =
  99. //======================================================================================================================
  100. void Renderer::Pps::Ssao::run()
  101. {
  102. const Camera& cam = *r.cam;
  103. glDisable(GL_BLEND);
  104. glDisable(GL_DEPTH_TEST);
  105. // 1st pass
  106. Renderer::setViewport(0, 0, width, height);
  107. pass0Fbo.bind();
  108. ssaoSProg->bind();
  109. Vec2 camRange(cam.getZNear(), cam.getZFar());
  110. camerarangeUniVar->setVec2(&camRange);
  111. msDepthFaiUniVar->setTexture(r.ms.depthFai, 0);
  112. noiseMapUniVar->setTexture(*noiseMap, 1);
  113. msNormalFaiUniVar->setTexture(r.ms.normalFai, 2);
  114. Renderer::drawQuad(0);
  115. // for 2nd and 3rd passes
  116. Renderer::setViewport(0, 0, bwidth, bheight);
  117. // 2nd pass
  118. pass1Fbo.bind();
  119. blurSProg->bind();
  120. blurSProgFaiUniVar->setTexture(pass0Fai, 0);
  121. Renderer::drawQuad(0);
  122. // 3rd pass
  123. pass2Fbo.bind();
  124. blurSProg2->bind();
  125. blurSProg2FaiUniVar->setTexture(pass1Fai, 0);
  126. Renderer::drawQuad(0);
  127. // end
  128. Fbo::unbind();
  129. }