2024-08-13 17:47:16 -04:00
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "fbo.h"
|
|
|
|
|
2024-08-17 19:08:11 -04:00
|
|
|
FBO::FBO() : fbo_id_(-1), color_texture_id_(-1), position_texture_id_(-1), depth_rbo_id_(-1) {}
|
2024-08-13 17:47:16 -04:00
|
|
|
|
|
|
|
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_);
|
2024-08-14 16:40:51 -04:00
|
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, nullptr);
|
2024-08-14 02:16:53 -04:00
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
2024-08-13 17:47:16 -04:00
|
|
|
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);
|
|
|
|
|
2024-08-17 19:08:11 -04:00
|
|
|
// 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
|
2024-08-13 17:47:16 -04:00
|
|
|
// 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_;
|
|
|
|
}
|