Dicewars-Siedler/src/engine/renderer/model/FramebufferObject.h
2026-02-14 20:52:30 +01:00

60 lines
1.9 KiB
C++

//
// Created by sebastian on 14.02.26.
//
#ifndef DICEWARS_SIEDLER_FRAMEBUFFEROBJECT_H
#define DICEWARS_SIEDLER_FRAMEBUFFEROBJECT_H
#include <stdexcept>
#include "glad/glad.h"
class FramebufferObject {
public:
FramebufferObject(int width, int height, bool withDepth = true) : width(width), height(height) {
// Fbo erzeugen
glGenFramebuffers(1, &fboID);
glBindFramebuffer(GL_FRAMEBUFFER, fboID);
// Color Texture
glGenTextures(1, &colorTextureID);
glBindTexture(GL_TEXTURE_2D, colorTextureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTextureID, 0);
if (withDepth) {
glGenRenderbuffers(1, &rboDepthID);
glBindRenderbuffer(GL_RENDERBUFFER, rboDepthID);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, rboDepthID);
}
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
throw std::runtime_error("Framebuffer is incomplete!");
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
~FramebufferObject() {
glDeleteFramebuffers(1, &fboID);
glDeleteTextures(1, &colorTextureID);
if (rboDepthID) glDeleteRenderbuffers(1, &rboDepthID);
}
void bind() const { glBindFramebuffer(GL_FRAMEBUFFER, fboID); }
void unbind() const { glBindFramebuffer(GL_FRAMEBUFFER, 0); }
GLuint getColorTexture() const;
private:
GLuint fboID = 0;
GLuint colorTextureID = 0;
GLuint rboDepthID = 0;
int width, height;
};
#endif //DICEWARS_SIEDLER_FRAMEBUFFEROBJECT_H