r_pps_ssao.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. The file contains functions and vars used for the deferred shading/post-processing stage/SSAO pass.
  3. */
  4. #include "renderer.h"
  5. #include "resource.h"
  6. #include "texture.h"
  7. #include "scene.h"
  8. #include "r_private.h"
  9. #include "fbo.h"
  10. namespace r {
  11. namespace pps {
  12. namespace ssao {
  13. /*
  14. =======================================================================================================================================
  15. VARS =
  16. =======================================================================================================================================
  17. */
  18. static fbo_t fbo; // yet another FBO
  19. float rendering_quality = 0.25; // the rendering_quality of the SSAO fai. Chose low so it can blend
  20. bool enabled = true;
  21. static uint wwidth, wheight; // window width and height
  22. texture_t fai; // SSAO factor
  23. static shader_prog_t* shdr_ppp_ssao;
  24. static texture_t* noise_map;
  25. /*
  26. =======================================================================================================================================
  27. Init =
  28. =======================================================================================================================================
  29. */
  30. void Init()
  31. {
  32. if( rendering_quality<0.0 || rendering_quality>1.0 ) ERROR("Incorect r::pps:ssao::rendering_quality");
  33. wwidth = r::rendering_quality * r::pps::ssao::rendering_quality * r::w;
  34. wheight = r::rendering_quality * r::pps::ssao::rendering_quality * r::h;
  35. // create FBO
  36. fbo.Create();
  37. fbo.Bind();
  38. // inform in what buffers we draw
  39. fbo.SetNumOfColorAttachements(1);
  40. // create the texes
  41. fai.CreateEmpty( wwidth, wheight, GL_ALPHA8, GL_ALPHA );
  42. fai.TexParameter( GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  43. fai.TexParameter( GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  44. // attach
  45. glFramebufferTexture2DEXT( GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT, GL_TEXTURE_2D, fai.GetGLID(), 0 );
  46. // test if success
  47. if( !fbo.CheckStatus() )
  48. FATAL( "Cannot create deferred shading post-processing stage SSAO pass FBO" );
  49. // unbind
  50. fbo.Unbind();
  51. // init shaders
  52. shdr_ppp_ssao = rsrc::shaders.Load( "shaders/pps_ssao.glsl" );
  53. // load noise map and disable temporaly the texture compression and enable mipmaping
  54. bool tex_compr = r::texture_compression;
  55. bool mipmaping = r::mipmaping;
  56. r::texture_compression = false;
  57. r::mipmaping = true;
  58. noise_map = rsrc::textures.Load( "gfx/noise3.tga" );
  59. noise_map->TexParameter( GL_TEXTURE_WRAP_S, GL_REPEAT );
  60. noise_map->TexParameter( GL_TEXTURE_WRAP_T, GL_REPEAT );
  61. //noise_map->TexParameter( GL_TEXTURE_MAG_FILTER, GL_NEAREST );
  62. //noise_map->TexParameter( GL_TEXTURE_MIN_FILTER, GL_NEAREST );
  63. r::texture_compression = tex_compr;
  64. r::mipmaping = mipmaping;
  65. }
  66. /*
  67. =======================================================================================================================================
  68. RunPass =
  69. =======================================================================================================================================
  70. */
  71. void RunPass( const camera_t& cam )
  72. {
  73. fbo.Bind();
  74. r::SetViewport( 0, 0, wwidth, wheight );
  75. glDisable( GL_BLEND );
  76. glDisable( GL_DEPTH_TEST );
  77. // set the shader
  78. shdr_ppp_ssao->Bind();
  79. glUniform2fv( shdr_ppp_ssao->GetUniformLocation(0), 1, &(vec2_t(cam.GetZNear(), cam.GetZFar()))[0] );
  80. shdr_ppp_ssao->LocTexUnit( shdr_ppp_ssao->GetUniformLocation(1), ms::depth_fai, 0 );
  81. shdr_ppp_ssao->LocTexUnit( shdr_ppp_ssao->GetUniformLocation(2), *noise_map, 1 );
  82. shdr_ppp_ssao->LocTexUnit( "ms_normal_fai", ms::normal_fai, 2 );
  83. // Draw quad
  84. glEnableClientState( GL_VERTEX_ARRAY );
  85. glEnableClientState( GL_TEXTURE_COORD_ARRAY );
  86. glVertexPointer( 2, GL_FLOAT, 0, quad_vert_cords );
  87. glDrawArrays( GL_QUADS, 0, 4 );
  88. glDisableClientState( GL_VERTEX_ARRAY );
  89. // end
  90. fbo.Unbind();
  91. }
  92. }}} // end namespaces