Add anonymous namespace and add bind and unbind functions to fbo file

This commit is contained in:
Jack Christensen 2024-08-20 01:35:19 -04:00
parent ca26a4f068
commit e3a67c6d00
3 changed files with 55 additions and 43 deletions

View File

@ -13,10 +13,8 @@ struct Framebuffer {
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 Bind(const Framebuffer& fbo);
void Unbind();
void Cleanup(const Framebuffer& fbo);
}

View File

@ -4,6 +4,52 @@
#include "fbo.h"
namespace {
GLuint CreateFramebuffer() {
GLuint fboId = 0;
glGenFramebuffers(1, &fboId);
return fboId;
}
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 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);
glBindRenderbuffer(GL_RENDERBUFFER, 0);
if(depthId == 0) {
std::cerr << "Depth Texture was not created!" << std::endl;
}
return depthId;
}
void AttachTextureToFramebuffer(GLuint framebufferId, GLuint textureId, GLenum attachment) {
glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, textureId, 0);
}
}; // namespace
namespace FBO {
Framebuffer GenerateFramebuffer(int width, int height) {
Framebuffer fbo;
@ -31,42 +77,12 @@ namespace FBO {
return fbo;
}
GLuint CreateFramebuffer() {
GLuint fboId = 0;
glGenFramebuffers(1, &fboId);
return fboId;
void Bind(const Framebuffer& fbo) {
glBindFramebuffer(GL_FRAMEBUFFER, fbo.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 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;
}
void AttachTextureToFramebuffer(GLuint framebufferId, GLuint textureId, GLenum attachment) {
glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, textureId, 0);
void Unbind() {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
void Cleanup(const Framebuffer& fbo) {

View File

@ -204,7 +204,7 @@ void Scene::ReloadShader() {
void Scene::Display(GLFWwindow* window) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindFramebuffer(GL_FRAMEBUFFER, geo_fbo_.id);
FBO::Bind(geo_fbo_);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader_program_);
@ -233,9 +233,7 @@ void Scene::Display(GLFWwindow* window) {
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_FRAMEBUFFER, lht_fbo_.id);
FBO::Bind(lht_fbo_);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(quad_shader_program_);
@ -258,7 +256,7 @@ void Scene::Display(GLFWwindow* window) {
glBindVertexArray(quad_vao_);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
FBO::Unbind();
DrawGui(window);
glfwSwapBuffers(window);