Ssao.cpp 5.5 KB

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