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 { namespace FBO {
Framebuffer GenerateFramebuffer(int width, int height); Framebuffer GenerateFramebuffer(int width, int height);
GLuint CreateFramebuffer(); void Bind(const Framebuffer& fbo);
GLuint CreateTexture(int width, int height, GLenum format); void Unbind();
GLuint CreateDepthTexture(int width, int height);
void AttachTextureToFramebuffer(GLuint framebufferId, GLuint textureId, GLenum attachment);
void Cleanup(const Framebuffer& fbo); void Cleanup(const Framebuffer& fbo);
} }

View File

@ -4,6 +4,52 @@
#include "fbo.h" #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 { namespace FBO {
Framebuffer GenerateFramebuffer(int width, int height) { Framebuffer GenerateFramebuffer(int width, int height) {
Framebuffer fbo; Framebuffer fbo;
@ -31,42 +77,12 @@ namespace FBO {
return fbo; return fbo;
} }
GLuint CreateFramebuffer() { void Bind(const Framebuffer& fbo) {
GLuint fboId = 0; glBindFramebuffer(GL_FRAMEBUFFER, fbo.id);
glGenFramebuffers(1, &fboId);
return fboId;
} }
GLuint CreateTexture(int width, int height, GLenum format) { void Unbind() {
GLuint textureId = 0; glBindFramebuffer(GL_FRAMEBUFFER, 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 Cleanup(const Framebuffer& fbo) { void Cleanup(const Framebuffer& fbo) {

View File

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