Framebuffer.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #ifndef FRAMEBUFFER_H
  2. #define FRAMEBUFFER_H
  3. #include <iostream>
  4. #include "rendering/Texture.h"
  5. #include "utils/ArrayList.h"
  6. #include "utils/Size.h"
  7. template<int N>
  8. class Framebuffer final {
  9. ArrayList<Texture, N> textures;
  10. GL::Framebuffer 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,
  17. "framebuffer size and amount of arguments do not match");
  18. for(int i = 0; i < N; i++) {
  19. textures.add(init[i]);
  20. textures[i].setClampWrap();
  21. if(init[i].linear) {
  22. textures[i].setLinearFilter();
  23. }
  24. }
  25. }
  26. ~Framebuffer() {
  27. GL::deleteFramebuffers(buffer);
  28. }
  29. Framebuffer(const Framebuffer&) = delete;
  30. Framebuffer(Framebuffer&&) = delete;
  31. Framebuffer& operator=(const Framebuffer&) = delete;
  32. Framebuffer& operator=(Framebuffer&&) = delete;
  33. bool init(const Size& size) {
  34. buffer = GL::genFramebuffer();
  35. GL::bindFramebuffer(buffer);
  36. ArrayList<GL::ColorAttachment, N> attachments;
  37. for(Texture& t : textures) {
  38. t.setData(size.width, size.height);
  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::printFramebufferError();
  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 Size& size) {
  57. for(Texture& t : textures) {
  58. t.setData(size.width, size.height);
  59. }
  60. }
  61. };
  62. #endif