terravisor/source/fbo.cpp

78 lines
2.7 KiB
C++

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include "fbo.h"
FBO::FBO() : fbo_id_(-1), color_texture_id_(-1), position_texture_id_(-1), depth_rbo_id_(-1) {}
FBO::~FBO() {
Cleanup();
}
void FBO::Init(int width, int height) {
// Generate the framebuffer
glGenFramebuffers(1, &fbo_id_);
glBindFramebuffer(GL_FRAMEBUFFER, fbo_id_);
// Generate the color texture
glGenTextures(1, &color_texture_id_);
glBindTexture(GL_TEXTURE_2D, color_texture_id_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
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, color_texture_id_, 0);
// Generate the position texture
glGenTextures(1, &position_texture_id_);
glBindTexture(GL_TEXTURE_2D, position_texture_id_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
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_ATTACHMENT1, GL_TEXTURE_2D, position_texture_id_, 0);
GLenum drawBuffers[2] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1};
glDrawBuffers(2, drawBuffers);
// RBO was causing issues. Not using for now
// Generate the Depth renderbuffer
glGenRenderbuffers(1, &depth_rbo_id_);
glBindRenderbuffer(GL_RENDERBUFFER, depth_rbo_id_);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_rbo_id_);
// Check if the framebuffer is complete
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
std::cerr << "Error: Framebuffer is not complete!" << std::endl;
}
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void FBO::Bind() {
glBindFramebuffer(GL_FRAMEBUFFER, fbo_id_);
}
void FBO::Unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void FBO::Cleanup() {
if (color_texture_id_ != -1) {
glDeleteTextures(1, &color_texture_id_);
}
if (depth_rbo_id_ != -1) {
glDeleteTextures(1, &depth_rbo_id_);
}
if (fbo_id_ != -1) {
glDeleteFramebuffers(1, &fbo_id_);
}
}
GLuint FBO::GetColorTexture() const {
return color_texture_id_;
}