Fix depth test and transparent background bug in FBO

This commit is contained in:
Jack Christensen 2024-08-17 19:10:41 -04:00
parent 1c75e7ddd8
commit 0362632222
2 changed files with 16 additions and 12 deletions

View File

@ -42,15 +42,14 @@ void FBO::Init(int width, int height) {
// Generate the Depth renderbuffer // Generate the Depth renderbuffer
glGenRenderbuffers(1, &depth_rbo_id_); glGenRenderbuffers(1, &depth_rbo_id_);
glBindRenderbuffer(GL_RENDERBUFFER, depth_rbo_id_); glBindRenderbuffer(GL_RENDERBUFFER, depth_rbo_id_);
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, width, height);
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_rbo_id_); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_rbo_id_);
glBindFramebuffer(GL_FRAMEBUFFER, 0);
// Check if the framebuffer is complete // Check if the framebuffer is complete
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
std::cerr << "Error: Framebuffer is not complete!" << std::endl; std::cerr << "Error: Framebuffer is not complete!" << std::endl;
} }
glBindFramebuffer(GL_FRAMEBUFFER, 0);
} }
void FBO::Bind() { void FBO::Bind() {
@ -63,6 +62,9 @@ void FBO::Unbind() {
void FBO::Cleanup() { void FBO::Cleanup() {
if (color_texture_id_ != -1) { if (color_texture_id_ != -1) {
glDeleteTextures(1, &position_texture_id_);
}
if (position_texture_id_ != -1) {
glDeleteTextures(1, &color_texture_id_); glDeleteTextures(1, &color_texture_id_);
} }
if (depth_rbo_id_ != -1) { if (depth_rbo_id_ != -1) {
@ -75,4 +77,8 @@ void FBO::Cleanup() {
GLuint FBO::GetColorTexture() const { GLuint FBO::GetColorTexture() const {
return color_texture_id_; return color_texture_id_;
}
GLuint FBO::GetPositionTexture() const {
return position_texture_id_;
} }

View File

@ -56,8 +56,8 @@ Scene::Scene(int width, int height)
angle_(-glm::pi<float>() / 2.0f), angle_(-glm::pi<float>() / 2.0f),
scale_(1.0f), scale_(1.0f),
aspect_(static_cast<float>(width) / static_cast<float>(height)), aspect_(static_cast<float>(width) / static_cast<float>(height)),
near_z_(0.01f), near_z_(0.1f),
far_z_(100.0f), far_z_(10.0f),
fov_(glm::pi<float>() / 6.0f) fov_(glm::pi<float>() / 6.0f)
{} {}
@ -74,7 +74,7 @@ void Scene::Init() {
// GL_DEPTH_TEST causes artifacts in meshes made up of multiple triangles // GL_DEPTH_TEST causes artifacts in meshes made up of multiple triangles
// disabling for now // disabling for now
//glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE); glEnable(GL_CULL_FACE);
fbo_.Init(window_width, window_height); fbo_.Init(window_width, window_height);
@ -128,7 +128,7 @@ void Scene::GenerateGrid(int divisions, std::vector<Vertex>& verts, std::vector<
// Currently creates a test triangle and initializes its buffers // Currently creates a test triangle and initializes its buffers
void Scene::InitBuffers() { void Scene::InitBuffers() {
int divisions = 2; // Number of divisions along one axis of the grid int divisions = 1 + (tessellationFactor / 64); // Number of divisions along one axis of the grid
GenerateGrid(divisions, vertices, indices); GenerateGrid(divisions, vertices, indices);
// Create and bind VAO, VBO, and EBO, and pass the data to OpenGL // Create and bind VAO, VBO, and EBO, and pass the data to OpenGL
@ -203,15 +203,14 @@ void Scene::ReloadShader() {
} }
void Scene::Display(GLFWwindow* window) { void Scene::Display(GLFWwindow* window) {
fbo_.Bind();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
fbo_.Bind();
glUseProgram(shader_program_); glUseProgram(shader_program_);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
view_matrix_ = glm::lookAt(glm::vec3(1.4f, 1.4f, 1.4f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); view_matrix_ = glm::lookAt(glm::vec3(0.0f, 1.4f, -1.4f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
Uniforms::SceneData.PV = projection_matrix_ * view_matrix_; // Projection-View matrix Uniforms::SceneData.PV = projection_matrix_ * view_matrix_; // Projection-View matrix
Uniforms::BufferSceneData(); Uniforms::BufferSceneData();
@ -236,7 +235,7 @@ void Scene::Display(GLFWwindow* window) {
fbo_.Unbind(); fbo_.Unbind();
post_fbo_.Bind(); post_fbo_.Bind();
glDisable(GL_DEPTH_TEST); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(quad_shader_program_); glUseProgram(quad_shader_program_);
@ -255,7 +254,6 @@ void Scene::Display(GLFWwindow* window) {
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
post_fbo_.Unbind(); post_fbo_.Unbind();
glEnable(GL_DEPTH_TEST);
DrawGui(window); DrawGui(window);
glfwSwapBuffers(window); glfwSwapBuffers(window);