Framebuffer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef FRAMEBUFFER_H
  2. #define FRAMEBUFFER_H
  3. #include <iostream>
  4. #include "utils/List.h"
  5. #include "utils/Size.h"
  6. #include "wrapper/Texture.h"
  7. template<int N>
  8. class Framebuffer final {
  9. List<Texture, N> textures;
  10. GLuint buffer;
  11. public:
  12. template<typename... Args>
  13. Framebuffer(const TextureFormat& a, Args&&... args) : buffer(0) {
  14. const int size = sizeof...(args) + 1;
  15. TextureFormat init[size] = {a, args...};
  16. static_assert(N == size, "framebuffer size and amount of arguments do not match");
  17. for(int i = 0; i < N; i++) {
  18. textures.add(init[i]);
  19. textures[i].setClampWrap();
  20. }
  21. }
  22. ~Framebuffer() {
  23. glDeleteFramebuffers(1, &buffer);
  24. }
  25. Framebuffer(const Framebuffer&) = delete;
  26. Framebuffer(Framebuffer&&) = delete;
  27. Framebuffer& operator=(const Framebuffer&) = delete;
  28. Framebuffer& operator=(Framebuffer&&) = delete;
  29. bool init(const Size& size) {
  30. glGenFramebuffers(1, &buffer);
  31. glBindFramebuffer(GL_FRAMEBUFFER, buffer);
  32. List<GLenum, N> attachments;
  33. for(Texture& t : textures) {
  34. t.setData(size.width, size.height);
  35. if(t.format.depth) {
  36. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, t.texture, 0);
  37. } else {
  38. GLenum c = GL_COLOR_ATTACHMENT0 + attachments.getLength();
  39. glFramebufferTexture2D(GL_FRAMEBUFFER, c, GL_TEXTURE_2D, t.texture, 0);
  40. attachments.add(c);
  41. }
  42. }
  43. glDrawBuffers(attachments.getLength(), attachments.begin());
  44. return hasError();
  45. }
  46. void bindAndClear() {
  47. glBindFramebuffer(GL_FRAMEBUFFER, buffer);
  48. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  49. }
  50. void bindTextureTo(int index, int textureUnit) const {
  51. textures[index].bindTo(textureUnit);
  52. }
  53. void resize(const Size& size) {
  54. for(Texture& t : textures) {
  55. t.setData(size.width, size.height);
  56. }
  57. }
  58. private:
  59. bool hasError() const {
  60. GLenum error = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  61. if(error == GL_FRAMEBUFFER_COMPLETE) {
  62. return false;
  63. }
  64. switch(error) {
  65. case GL_FRAMEBUFFER_UNDEFINED: std::cout << "undefined framebuffer\n"; return true;
  66. case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: std::cout << "incomplete framebuffer attachment\n"; return true;
  67. case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:
  68. std::cout << "incomplete missing framebuffer attachment\n";
  69. return true;
  70. case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER:
  71. std::cout << "incomplete framebuffer draw buffer\n";
  72. return true;
  73. case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER:
  74. std::cout << "incomplete framebuffer read buffer\n";
  75. return true;
  76. case GL_FRAMEBUFFER_UNSUPPORTED: std::cout << "unsupported framebuffer\n"; return true;
  77. case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE:
  78. std::cout << "incomplete framebuffer multisample\n";
  79. return true;
  80. case GL_FRAMEBUFFER_INCOMPLETE_LAYER_TARGETS:
  81. std::cout << "incomplete framebuffer layer targets\n";
  82. return true;
  83. }
  84. return "unknown error";
  85. }
  86. };
  87. #endif