Framebuffer.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. template<typename... Args>
  12. Framebuffer(const TextureFormat& a, Args&&... args) : buffer(0) {
  13. const int size = sizeof...(args) + 1;
  14. TextureFormat init[size] = {a, args...};
  15. static_assert(N == size,
  16. "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. if(init[i].linear) {
  21. textures[i].setLinearFilter();
  22. }
  23. }
  24. }
  25. ~Framebuffer() {
  26. GL::deleteFramebuffers(buffer);
  27. }
  28. Framebuffer(const Framebuffer&) = delete;
  29. Framebuffer(Framebuffer&&) = delete;
  30. Framebuffer& operator=(const Framebuffer&) = delete;
  31. Framebuffer& operator=(Framebuffer&&) = delete;
  32. bool init(const Size& size) {
  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