Framebuffer.h 1.9 KB

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