Framebuffer.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef FRAMEBUFFER_H
  2. #define FRAMEBUFFER_H
  3. #include "data/Array.h"
  4. #include "data/ArrayList.h"
  5. #include "math/Vector.h"
  6. #include "rendering/Texture.h"
  7. template<int N>
  8. class Framebuffer final {
  9. Array<Texture, N> textures;
  10. GL::Framebuffer buffer;
  11. public:
  12. Framebuffer() : buffer(0) {
  13. }
  14. ~Framebuffer() {
  15. GL::deleteFramebuffers(buffer);
  16. }
  17. Framebuffer(const Framebuffer&) = delete;
  18. Framebuffer(Framebuffer&&) = delete;
  19. Framebuffer& operator=(const Framebuffer&) = delete;
  20. Framebuffer& operator=(Framebuffer&&) = delete;
  21. template<typename... Args>
  22. Error init(const IntVector2& size, Args&&... args) {
  23. const int n = sizeof...(args);
  24. Texture::Format init[static_cast<unsigned int>(n)] = {args...};
  25. static_assert(N == n,
  26. "framebuffer size and amount of arguments do not match");
  27. for(int i = 0; i < N; i++) {
  28. textures[i].init(init[i], 0);
  29. textures[i].setClampWrap();
  30. if(init[i].linear) {
  31. textures[i].setLinearFilter();
  32. }
  33. }
  34. buffer = GL::genFramebuffer();
  35. GL::bindFramebuffer(buffer);
  36. ArrayList<GL::ColorAttachment, N> attachments;
  37. for(Texture& t : textures) {
  38. t.setData(size[0], size[1]);
  39. if(t.format.depth) {
  40. GL::framebufferDepthTexture2D(t.texture);
  41. } else {
  42. attachments.add(GL::framebufferColorTexture2D(
  43. t.texture, attachments.getLength()));
  44. }
  45. }
  46. GL::drawBuffers(attachments.getLength(), attachments.begin());
  47. return GL::getFramebufferError();
  48. }
  49. void bindAndClear() {
  50. GL::bindFramebuffer(buffer);
  51. GL::clear();
  52. }
  53. void bindTextureTo(int index, int textureUnit) const {
  54. textures[index].bindTo(textureUnit);
  55. }
  56. void resize(const IntVector2& size) {
  57. for(Texture& t : textures) {
  58. t.setData(size[0], size[1]);
  59. }
  60. }
  61. };
  62. #endif