Ssao.cpp 5.1 KB

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