SSAOBlurShader.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include "client/engine/shader/SSAOBlurShader.h"
  2. #include "client/engine/Wrapper.h"
  3. SSAOBlurShader::SSAOBlurShader()
  4. {
  5. }
  6. SSAOBlurShader::~SSAOBlurShader()
  7. {
  8. glDeleteFramebuffers(1, &framebuffer);
  9. glDeleteTextures(1, &texture);
  10. }
  11. bool SSAOBlurShader::init()
  12. {
  13. program.compile("resources/shader/ssaoBlurVertex.vs", "resources/shader/ssaoBlurFragment.fs");
  14. if(!program.isValid())
  15. {
  16. return false;
  17. }
  18. // generate framebuffer
  19. glGenFramebuffers(1, &framebuffer);
  20. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  21. // color texture
  22. glGenTextures(1, &texture);
  23. glBindTexture(GL_TEXTURE_2D, texture);
  24. glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, Engine::getWidth(), Engine::getHeight(), 0, GL_RGB, GL_FLOAT, NULL);
  25. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  26. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  27. // attache color texture to framebuffer
  28. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
  29. // check if framebuffer is okay
  30. if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  31. {
  32. cout << "ssao blur frame buffer is not complete!" << endl;
  33. return false;
  34. }
  35. // unbind framebuffer
  36. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  37. return true;
  38. }
  39. void SSAOBlurShader::resize()
  40. {
  41. glBindTexture(GL_TEXTURE_2D, texture);
  42. glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, Engine::getWidth(), Engine::getHeight(), 0, GL_RGB, GL_FLOAT, NULL);
  43. }
  44. void SSAOBlurShader::preRender()
  45. {
  46. // bind ssao blur shader program
  47. glUseProgram(program.getProgram());
  48. // bind ssao blur framebuffer
  49. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  50. //glBindFramebuffer(GL_FRAMEBUFFER, 0);
  51. // clear color buffer
  52. glClear(GL_COLOR_BUFFER_BIT);
  53. // depth testing is not needed
  54. glDisable(GL_DEPTH_TEST);
  55. }
  56. void SSAOBlurShader::bindTexture(unsigned int textureUnit)
  57. {
  58. glActiveTexture(GL_TEXTURE0 + textureUnit);
  59. glBindTexture(GL_TEXTURE_2D, texture);
  60. }