WorldShadowShader.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "WorldShadowShader.h"
  2. #include "../Wrapper.h"
  3. WorldShadowShader::WorldShadowShader()
  4. {
  5. }
  6. WorldShadowShader::~WorldShadowShader()
  7. {
  8. glDeleteFramebuffers(1, &framebuffer);
  9. glDeleteTextures(1, &depthTexture);
  10. }
  11. bool WorldShadowShader::init()
  12. {
  13. program.compile("shader/worldShadowVertex.vs", "shader/worldShadowFragment.fs");
  14. if(!program.isValid())
  15. {
  16. return false;
  17. }
  18. // generate framebuffer
  19. glGenFramebuffers(1, &framebuffer);
  20. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  21. glDrawBuffer(GL_NONE);
  22. glReadBuffer(GL_NONE);
  23. // depth texture
  24. glGenTextures(1, &depthTexture);
  25. glBindTexture(GL_TEXTURE_2D, depthTexture);
  26. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, Engine::getWidth(), Engine::getHeight(), 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
  27. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  28. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  29. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
  30. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
  31. // attache depth texture to framebuffer
  32. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0);
  33. // check if framebuffer is okay
  34. if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
  35. {
  36. cout << "world shadow frame buffer is not complete!" << endl;
  37. return false;
  38. }
  39. // unbind framebuffer
  40. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  41. // get uniform locations
  42. unifProjMatrix = glGetUniformLocation(program.getProgram(), "projMatrix");
  43. unifViewMatrix = glGetUniformLocation(program.getProgram(), "viewMatrix");
  44. unifModelMatrix = glGetUniformLocation(program.getProgram(), "modelMatrix");
  45. return true;
  46. }
  47. void WorldShadowShader::resize()
  48. {
  49. glBindTexture(GL_TEXTURE_2D, depthTexture);
  50. glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, Engine::getWidth(), Engine::getHeight(), 0, GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);
  51. }
  52. void WorldShadowShader::preRender(const float* projMatrix)
  53. {
  54. // bind world shadow shader program
  55. glUseProgram(program.getProgram());
  56. // set projection matrix uniform
  57. glUniformMatrix4fv(unifProjMatrix, 1, 0, projMatrix);
  58. // bind world framebuffer
  59. glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
  60. // clear color, depth and stencil buffer
  61. glClear(GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  62. // no color, depth testing is needed
  63. glEnable(GL_DEPTH_TEST);
  64. }
  65. void WorldShadowShader::bindDepthTexture(unsigned int textureUnit)
  66. {
  67. glActiveTexture(GL_TEXTURE0 + textureUnit);
  68. glBindTexture(GL_TEXTURE_2D, depthTexture);
  69. }
  70. void WorldShadowShader::setViewMatrix(const float* data)
  71. {
  72. glUniformMatrix4fv(unifViewMatrix, 1, 0, data);
  73. }
  74. void WorldShadowShader::setModelMatrix(const float* data)
  75. {
  76. glUniformMatrix4fv(unifModelMatrix, 1, 0, data);
  77. }