WorldPostShader.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include "client/engine/shader/WorldPostShader.h"
  2. #include "client/engine/Wrapper.h"
  3. WorldPostShader::WorldPostShader()
  4. {
  5. }
  6. WorldPostShader::~WorldPostShader()
  7. {
  8. glDeleteFramebuffers(1, &framebuffer);
  9. glDeleteTextures(1, &texture);
  10. }
  11. bool WorldPostShader::init()
  12. {
  13. program.compile("resources/shader/worldPostVertex.vs", "resources/shader/worldPostFragment.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_RGBA, Engine::getWidth(), Engine::getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, 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 << "world post frame buffer is not complete!" << endl;
  33. return false;
  34. }
  35. // unbind framebuffer
  36. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  37. return true;
  38. }
  39. void WorldPostShader::resize()
  40. {
  41. glBindTexture(GL_TEXTURE_2D, texture);
  42. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Engine::getWidth(), Engine::getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
  43. }
  44. void WorldPostShader::preRender()
  45. {
  46. // bind ssao shader program
  47. glUseProgram(program.getProgram());
  48. // bind ssao framebuffer
  49. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  50. // clear color buffer
  51. glClear(GL_COLOR_BUFFER_BIT);
  52. // depth testing is not needed
  53. glDisable(GL_DEPTH_TEST);
  54. }
  55. void WorldPostShader::bindTexture(unsigned int textureUnit)
  56. {
  57. glActiveTexture(GL_TEXTURE0 + textureUnit);
  58. glBindTexture(GL_TEXTURE_2D, texture);
  59. }