Refactor fbo to make for functional, less OOP

This commit is contained in:
Jack Christensen 2024-08-20 00:47:41 -04:00
parent 25147837fc
commit d144434f59
5 changed files with 106 additions and 121 deletions

View File

@ -4,8 +4,8 @@ Size=400,400
Collapsed=0
[Window][Terrain Controls]
Pos=856,19
Size=424,701
Pos=868,19
Size=412,701
Collapsed=0
DockId=0x00000002,0
@ -24,12 +24,12 @@ Collapsed=0
[Window][Scene Window]
Pos=0,19
Size=854,701
Size=866,701
Collapsed=0
DockId=0x00000001,0
[Docking][Data]
DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=1280,701 Split=X Selected=0x9F2D9299
DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=854,701 CentralNode=1 Selected=0x9F2D9299
DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=424,701 Selected=0xF69494A7
DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=866,701 CentralNode=1 Selected=0x9F2D9299
DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=412,701 Selected=0xF69494A7

View File

@ -3,24 +3,21 @@
#include <GLFW/glfw3.h>
class FBO {
public:
FBO();
~FBO();
GLuint fbo_id_;
GLuint albedo_gbuffer_id_;
GLuint position_gbuffer_id_;
GLuint normal_gbuffer_id_;
GLuint depth_rbo_id_;
void Init(int width, int height);
void Bind();
void Unbind();
void Cleanup();
GLuint GetAlbedoGbuffer() const;
GLuint GetPositionGbuffer() const;
GLuint GetNormalGbuffer() const;
struct Framebuffer {
GLuint id = 0;
GLuint albedoTexture = 0;
GLuint positionTexture = 0;
GLuint normalTexture = 0;
GLuint depthTexture = 0;
};
namespace FBO {
Framebuffer GenerateFramebuffer(int width, int height);
GLuint CreateFramebuffer();
GLuint CreateTexture(int width, int height, GLenum format);
GLuint CreateDepthTexture(int width, int height);
void AttachTextureToFramebuffer(GLuint framebufferId, GLuint textureId, GLenum attachment);
void Cleanup(const Framebuffer& fbo);
}
#endif // FBO_H_

View File

@ -34,8 +34,8 @@ class Scene {
void InitQuadBuffers();
void InitShaders();
FBO fbo_;
FBO post_fbo_;
Framebuffer geo_fbo_;
Framebuffer lht_fbo_;
GLuint shader_program_;
GLuint quad_shader_program_;
GLuint vao_;

View File

@ -4,98 +4,86 @@
#include "fbo.h"
FBO::FBO() : fbo_id_(-1), albedo_gbuffer_id_(-1), position_gbuffer_id_(-1), normal_gbuffer_id_(-1), depth_rbo_id_(-1) {}
namespace FBO {
Framebuffer GenerateFramebuffer(int width, int height) {
Framebuffer fbo;
fbo.id = CreateFramebuffer();
glBindFramebuffer(GL_FRAMEBUFFER, fbo.id);
FBO::~FBO() {
Cleanup();
}
fbo.albedoTexture = CreateTexture(width, height, GL_RGBA);
fbo.positionTexture = CreateTexture(width, height, GL_RGBA);
fbo.normalTexture = CreateTexture(width, height, GL_RGBA);
fbo.depthTexture = CreateDepthTexture(width, height);
void FBO::Init(int width, int height) {
// Generate the framebuffer
glGenFramebuffers(1, &fbo_id_);
glBindFramebuffer(GL_FRAMEBUFFER, fbo_id_);
AttachTextureToFramebuffer(fbo.id, fbo.albedoTexture, GL_COLOR_ATTACHMENT0);
AttachTextureToFramebuffer(fbo.id, fbo.positionTexture, GL_COLOR_ATTACHMENT1);
AttachTextureToFramebuffer(fbo.id, fbo.normalTexture, GL_COLOR_ATTACHMENT2);
// Generate the albedo gbuffer
glGenTextures(1, &albedo_gbuffer_id_);
glBindTexture(GL_TEXTURE_2D, albedo_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_ATTACHMENT0, GL_TEXTURE_2D, albedo_gbuffer_id_, 0);
GLenum drawBuffers[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
glDrawBuffers(3, drawBuffers);
// Generate the position gbuffer
glGenTextures(1, &position_gbuffer_id_);
glBindTexture(GL_TEXTURE_2D, position_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_ATTACHMENT1, GL_TEXTURE_2D, position_gbuffer_id_, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
std::cerr << "Error: Framebuffer is not complete!" << std::endl;
}
// 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);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
GLenum drawBuffers[3] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2};
glDrawBuffers(3, 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_COMPONENT32, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_rbo_id_);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Check if the framebuffer is complete
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
std::cerr << "Error: Framebuffer is not complete!" << std::endl;
return fbo;
}
}
void FBO::Bind() {
glBindFramebuffer(GL_FRAMEBUFFER, fbo_id_);
}
void FBO::Unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void FBO::Cleanup() {
if (albedo_gbuffer_id_ != -1) {
glDeleteTextures(1, &albedo_gbuffer_id_);
GLuint CreateFramebuffer() {
GLuint fboId = 0;
glGenFramebuffers(1, &fboId);
return fboId;
}
if (position_gbuffer_id_ != -1) {
glDeleteTextures(1, &position_gbuffer_id_);
}
if (normal_gbuffer_id_ != -1) {
glDeleteTextures(1, &normal_gbuffer_id_);
}
if (depth_rbo_id_ != -1) {
glDeleteTextures(1, &depth_rbo_id_);
}
if (fbo_id_ != -1) {
glDeleteFramebuffers(1, &fbo_id_);
}
}
GLuint FBO::GetAlbedoGbuffer() const {
return albedo_gbuffer_id_;
}
GLuint CreateTexture(int width, int height, GLenum format) {
GLuint textureId = 0;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, format, 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);
glBindTexture(GL_TEXTURE_2D, 0);
if(textureId == 0) {
std::cerr << "Texture was not created!" << std::endl;
}
return textureId;
}
GLuint FBO::GetPositionGbuffer() const {
return position_gbuffer_id_;
}
GLuint CreateDepthTexture(int width, int height) {
GLuint depthId;
glGenRenderbuffers(1, &depthId);
glBindRenderbuffer(GL_RENDERBUFFER, depthId);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthId);
if(depthId == 0) {
std::cerr << "Depth Texture was not created!" << std::endl;
}
return depthId;
}
GLuint FBO::GetNormalGbuffer() const {
return normal_gbuffer_id_;
}
void AttachTextureToFramebuffer(GLuint framebufferId, GLuint textureId, GLenum attachment) {
glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, textureId, 0);
}
void Cleanup(const Framebuffer& fbo) {
if (fbo.albedoTexture != 0) {
glDeleteTextures(1, &fbo.albedoTexture);
}
if (fbo.positionTexture != 0) {
glDeleteTextures(1, &fbo.positionTexture);
}
if (fbo.normalTexture != 0) {
glDeleteTextures(1, &fbo.normalTexture);
}
if (fbo.depthTexture != 0) {
glDeleteTextures(1, &fbo.depthTexture);
}
if (fbo.id != 0) {
glDeleteFramebuffers(1, &fbo.id);
}
}
}; // namespace

View File

@ -25,6 +25,7 @@
#include "Uniforms.h"
#include "InitShader.h" //Functions for loading shaders from text files
#include "DebugCallback.h"
#include "load_texture.h"
#include <FreeImage.h>
@ -39,7 +40,6 @@ const std::string kQuadVertexPath = "shaders/quad_vertex.glsl";
const std::string kQuadFragmentPath = "shaders/quad_fragment.glsl";
const std::string kHGTPath = "hgt/N02E016.hgt";
GLuint tex_id = -1;
float tessellationFactor = 32.0f;
@ -68,8 +68,8 @@ Scene::~Scene() {
glDeleteProgram(shader_program_);
glDeleteVertexArrays(1, &vao_);
fbo_.~FBO();
post_fbo_.~FBO();
FBO::Cleanup(geo_fbo_);
FBO::Cleanup(lht_fbo_);
}
void Scene::Init() {
@ -78,8 +78,8 @@ void Scene::Init() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
fbo_.Init(window_width, window_height);
post_fbo_.Init(window_width, window_height);
geo_fbo_ = FBO::GenerateFramebuffer(window_width, window_height);
lht_fbo_ = FBO::GenerateFramebuffer(window_width, window_height);
std::vector<int16_t> heightData = LoadHGT(kHGTPath, 3601, 3601);
tex_id = CreateHeightmapTexture(heightData, 3601, 3601);
@ -207,7 +207,7 @@ void Scene::ReloadShader() {
void Scene::Display(GLFWwindow* window) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
fbo_.Bind();
glBindFramebuffer(GL_FRAMEBUFFER, geo_fbo_.id);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader_program_);
@ -236,19 +236,19 @@ void Scene::Display(GLFWwindow* window) {
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
fbo_.Unbind();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
post_fbo_.Bind();
glBindFramebuffer(GL_FRAMEBUFFER, lht_fbo_.id);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(quad_shader_program_);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, fbo_.GetAlbedoGbuffer());
glBindTexture(GL_TEXTURE_2D, geo_fbo_.albedoTexture);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, fbo_.GetPositionGbuffer());
glBindTexture(GL_TEXTURE_2D, geo_fbo_.positionTexture);
glActiveTexture(GL_TEXTURE2);
glBindTexture(GL_TEXTURE_2D, fbo_.GetNormalGbuffer());
glBindTexture(GL_TEXTURE_2D, geo_fbo_.normalTexture);
GLint colorTextureLoc = glGetUniformLocation(quad_shader_program_, "albedoGbuffer");
glUniform1i(colorTextureLoc, 0);
@ -261,7 +261,7 @@ void Scene::Display(GLFWwindow* window) {
glBindVertexArray(quad_vao_);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
post_fbo_.Unbind();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
DrawGui(window);
glfwSwapBuffers(window);
@ -330,7 +330,7 @@ void Scene::DrawGui(GLFWwindow* window) {
// Draw FBO to ImGui window
ImGui::Begin("Scene Window");
ImVec2 windowSize = ImGui::GetContentRegionAvail();
ImGui::Image((void*)(intptr_t)post_fbo_.GetAlbedoGbuffer(), windowSize, ImVec2(0, 1), ImVec2(1, 0));
ImGui::Image((void*)(intptr_t)lht_fbo_.albedoTexture, windowSize, ImVec2(0, 1), ImVec2(1, 0));
ImGui::End();
ImGui::Render();