123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- #ifndef FRAMEBUFFER_H
- #define FRAMEBUFFER_H
- #include <iostream>
- #include "rendering/Texture.h"
- #include "utils/ArrayList.h"
- #include "utils/Size.h"
- template<int N>
- class Framebuffer final {
- ArrayList<Texture, N> textures;
- GL::Framebuffer buffer;
- public:
- template<typename... Args>
- Framebuffer(const TextureFormat& a, Args&&... args) : buffer(0) {
- const int size = sizeof...(args) + 1;
- TextureFormat init[size] = {a, args...};
- static_assert(N == size,
- "framebuffer size and amount of arguments do not match");
- for(int i = 0; i < N; i++) {
- textures.add(init[i]);
- textures[i].setClampWrap();
- if(init[i].linear) {
- textures[i].setLinearFilter();
- }
- }
- }
- ~Framebuffer() {
- GL::deleteFramebuffers(buffer);
- }
- Framebuffer(const Framebuffer&) = delete;
- Framebuffer(Framebuffer&&) = delete;
- Framebuffer& operator=(const Framebuffer&) = delete;
- Framebuffer& operator=(Framebuffer&&) = delete;
- bool init(const Size& size) {
- buffer = GL::genFramebuffer();
- GL::bindFramebuffer(buffer);
- ArrayList<GL::ColorAttachment, N> attachments;
- for(Texture& t : textures) {
- t.setData(size.width, size.height);
- if(t.format.depth) {
- GL::framebufferDepthTexture2D(t.texture);
- } else {
- attachments.add(GL::framebufferColorTexture2D(
- t.texture, attachments.getLength()));
- }
- }
- GL::drawBuffers(attachments.getLength(), attachments.begin());
- return GL::printFramebufferError();
- }
- void bindAndClear() {
- GL::bindFramebuffer(buffer);
- GL::clear();
- }
- void bindTextureTo(int index, int textureUnit) const {
- textures[index].bindTo(textureUnit);
- }
- void resize(const Size& size) {
- for(Texture& t : textures) {
- t.setData(size.width, size.height);
- }
- }
- };
- #endif
|