OverlayShader.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "client/engine/shader/OverlayShader.h"
  2. #include "client/engine/Wrapper.h"
  3. OverlayShader::OverlayShader()
  4. {
  5. }
  6. OverlayShader::~OverlayShader()
  7. {
  8. }
  9. bool OverlayShader::init()
  10. {
  11. program.compile("resources/shader/overlayVertex.vs", "resources/shader/overlayFragment.fs");
  12. if(!program.isValid())
  13. {
  14. return false;
  15. }
  16. // get uniform locations
  17. unifUseTexture = glGetUniformLocation(program.getProgram(), "useTexture");
  18. unifUseColor = glGetUniformLocation(program.getProgram(), "useColor");
  19. unifViewMatrix = glGetUniformLocation(program.getProgram(), "viewMatrix");
  20. unifModelMatrix = glGetUniformLocation(program.getProgram(), "modelMatrix");
  21. return true;
  22. }
  23. void OverlayShader::preRender()
  24. {
  25. // bind overlay shader program
  26. glUseProgram(program.getProgram());
  27. // reset view and model matrix
  28. view.setToIdentity();
  29. glUniformMatrix4fv(unifViewMatrix, 1, 0, view.getValues());
  30. glUniformMatrix4fv(unifModelMatrix, 1, 0, view.getValues());
  31. // set defaults
  32. setUseTexture(true);
  33. setUseColor(false);
  34. // reset to default framebuffer
  35. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  36. // clear color buffer
  37. glClear(GL_COLOR_BUFFER_BIT);
  38. // depth testing is not needed
  39. glDisable(GL_DEPTH_TEST);
  40. }
  41. void OverlayShader::setViewMatrix()
  42. {
  43. view.set(0, 0, (2.0f * Engine::getScale()) / Engine::getWidth());
  44. view.set(0, 1, 0.0f);
  45. view.set(0, 2, 0.0f);
  46. view.set(0, 3, -1.0f);
  47. view.set(1, 0, 0.0f);
  48. view.set(1, 1, (-2.0f * Engine::getScale()) / Engine::getHeight());
  49. view.set(1, 2, 0.0f);
  50. view.set(1, 3, 1.0f);
  51. view.set(2, 0, 0.0f);
  52. view.set(2, 1, 0.0f);
  53. view.set(2, 2, (-1.0f * Engine::getScale()) / Engine::getHeight());
  54. view.set(2, 3, 0.5f);
  55. view.set(3, 0, 0.0f);
  56. view.set(3, 1, 0.0f);
  57. view.set(3, 0, 0.0f);
  58. view.set(3, 3, 1.0f);
  59. glUniformMatrix4fv(unifViewMatrix, 1, 0, view.getValues());
  60. }
  61. void OverlayShader::setModelMatrix(const float* data)
  62. {
  63. glUniformMatrix4fv(unifModelMatrix, 1, 0, data);
  64. }
  65. void OverlayShader::setUseTexture(bool use)
  66. {
  67. glUniform1i(unifUseTexture, use);
  68. }
  69. void OverlayShader::setUseColor(bool use)
  70. {
  71. glUniform1i(unifUseColor, use);
  72. }