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:40:42 -04:00
|
|
|
FBO::FBO() : fbo_id_(-1), albedo_gbuffer_id_(-1), position_gbuffer_id_(-1), normal_gbuffer_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_);
|
|
|
|
|
2024-08-17 19:40:42 -04:00
|
|
|
// Generate the albedo gbuffer
|
|
|
|
glGenTextures(1, &albedo_gbuffer_id_);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, albedo_gbuffer_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);
|
2024-08-17 19:40:42 -04:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, albedo_gbuffer_id_, 0);
|
2024-08-13 17:47:16 -04:00
|
|
|
|
2024-08-17 19:40:42 -04:00
|
|
|
// Generate the position gbuffer
|
|
|
|
glGenTextures(1, &position_gbuffer_id_);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, position_gbuffer_id_);
|
2024-08-17 19:08:11 -04:00
|
|
|
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);
|
2024-08-17 19:40:42 -04:00
|
|
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, position_gbuffer_id_, 0);
|
2024-08-17 19:08:11 -04:00
|
|
|
|
2024-08-17 19:40:42 -04:00
|
|
|
// Generate the normal gbuffer
|
|
|
|
glGenTextures(1, &normal_gbuffer_id_);
|
|
|
|
glBindTexture(GL_TEXTURE_2D, normal_gbuffer_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_ATTACHMENT2, GL_TEXTURE_2D, normal_gbuffer_id_, 0);
|
|
|
|
|
|
|
|
GLenum drawBuffers[3] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2};
|
|
|
|
glDrawBuffers(3, drawBuffers);
|
2024-08-17 19:08:11 -04:00
|
|
|
|
|
|
|
// 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_);
|
2024-08-17 19:10:41 -04:00
|
|
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, width, height);
|
2024-08-13 17:47:16 -04:00
|
|
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_rbo_id_);
|
|
|
|
|
2024-08-17 19:10:41 -04:00
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
2024-08-13 17:47:16 -04:00
|
|
|
// Check if the framebuffer is complete
|
|
|
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
|
|
|
std::cerr << "Error: Framebuffer is not complete!" << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FBO::Bind() {
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo_id_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FBO::Unbind() {
|
|
|
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FBO::Cleanup() {
|
2024-08-17 19:40:42 -04:00
|
|
|
if (albedo_gbuffer_id_ != -1) {
|
|
|
|
glDeleteTextures(1, &albedo_gbuffer_id_);
|
|
|
|
}
|
|
|
|
if (position_gbuffer_id_ != -1) {
|
|
|
|
glDeleteTextures(1, &position_gbuffer_id_);
|
2024-08-17 19:10:41 -04:00
|
|
|
}
|
2024-08-17 19:40:42 -04:00
|
|
|
if (normal_gbuffer_id_ != -1) {
|
|
|
|
glDeleteTextures(1, &normal_gbuffer_id_);
|
2024-08-13 17:47:16 -04:00
|
|
|
}
|
|
|
|
if (depth_rbo_id_ != -1) {
|
|
|
|
glDeleteTextures(1, &depth_rbo_id_);
|
|
|
|
}
|
|
|
|
if (fbo_id_ != -1) {
|
|
|
|
glDeleteFramebuffers(1, &fbo_id_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-17 19:40:42 -04:00
|
|
|
GLuint FBO::GetAlbedoGbuffer() const {
|
|
|
|
return albedo_gbuffer_id_;
|
|
|
|
}
|
|
|
|
|
|
|
|
GLuint FBO::GetPositionGbuffer() const {
|
|
|
|
return position_gbuffer_id_;
|
2024-08-17 19:10:41 -04:00
|
|
|
}
|
|
|
|
|
2024-08-17 19:40:42 -04:00
|
|
|
GLuint FBO::GetNormalGbuffer() const {
|
|
|
|
return normal_gbuffer_id_;
|
2024-08-13 17:47:16 -04:00
|
|
|
}
|