Refactor fbo to make for functional, less OOP
This commit is contained in:
parent
25147837fc
commit
d144434f59
10
imgui.ini
10
imgui.ini
|
@ -4,8 +4,8 @@ Size=400,400
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
[Window][Terrain Controls]
|
[Window][Terrain Controls]
|
||||||
Pos=856,19
|
Pos=868,19
|
||||||
Size=424,701
|
Size=412,701
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000002,0
|
DockId=0x00000002,0
|
||||||
|
|
||||||
|
@ -24,12 +24,12 @@ Collapsed=0
|
||||||
|
|
||||||
[Window][Scene Window]
|
[Window][Scene Window]
|
||||||
Pos=0,19
|
Pos=0,19
|
||||||
Size=854,701
|
Size=866,701
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000001,0
|
DockId=0x00000001,0
|
||||||
|
|
||||||
[Docking][Data]
|
[Docking][Data]
|
||||||
DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=1280,701 Split=X Selected=0x9F2D9299
|
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=0x00000001 Parent=0x6F42A598 SizeRef=866,701 CentralNode=1 Selected=0x9F2D9299
|
||||||
DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=424,701 Selected=0xF69494A7
|
DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=412,701 Selected=0xF69494A7
|
||||||
|
|
||||||
|
|
|
@ -3,24 +3,21 @@
|
||||||
|
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
class FBO {
|
struct Framebuffer {
|
||||||
public:
|
GLuint id = 0;
|
||||||
FBO();
|
GLuint albedoTexture = 0;
|
||||||
~FBO();
|
GLuint positionTexture = 0;
|
||||||
|
GLuint normalTexture = 0;
|
||||||
GLuint fbo_id_;
|
GLuint depthTexture = 0;
|
||||||
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;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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_
|
#endif // FBO_H_
|
|
@ -34,8 +34,8 @@ class Scene {
|
||||||
void InitQuadBuffers();
|
void InitQuadBuffers();
|
||||||
void InitShaders();
|
void InitShaders();
|
||||||
|
|
||||||
FBO fbo_;
|
Framebuffer geo_fbo_;
|
||||||
FBO post_fbo_;
|
Framebuffer lht_fbo_;
|
||||||
GLuint shader_program_;
|
GLuint shader_program_;
|
||||||
GLuint quad_shader_program_;
|
GLuint quad_shader_program_;
|
||||||
GLuint vao_;
|
GLuint vao_;
|
||||||
|
|
154
source/fbo.cpp
154
source/fbo.cpp
|
@ -4,98 +4,86 @@
|
||||||
|
|
||||||
#include "fbo.h"
|
#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() {
|
fbo.albedoTexture = CreateTexture(width, height, GL_RGBA);
|
||||||
Cleanup();
|
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) {
|
AttachTextureToFramebuffer(fbo.id, fbo.albedoTexture, GL_COLOR_ATTACHMENT0);
|
||||||
// Generate the framebuffer
|
AttachTextureToFramebuffer(fbo.id, fbo.positionTexture, GL_COLOR_ATTACHMENT1);
|
||||||
glGenFramebuffers(1, &fbo_id_);
|
AttachTextureToFramebuffer(fbo.id, fbo.normalTexture, GL_COLOR_ATTACHMENT2);
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo_id_);
|
|
||||||
|
|
||||||
// Generate the albedo gbuffer
|
GLenum drawBuffers[3] = { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2 };
|
||||||
glGenTextures(1, &albedo_gbuffer_id_);
|
glDrawBuffers(3, drawBuffers);
|
||||||
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);
|
|
||||||
|
|
||||||
// Generate the position gbuffer
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||||
glGenTextures(1, &position_gbuffer_id_);
|
std::cerr << "Error: Framebuffer is not complete!" << std::endl;
|
||||||
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);
|
|
||||||
|
|
||||||
// Generate the normal gbuffer
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
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};
|
return fbo;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void FBO::Bind() {
|
GLuint CreateFramebuffer() {
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo_id_);
|
GLuint fboId = 0;
|
||||||
}
|
glGenFramebuffers(1, &fboId);
|
||||||
|
return fboId;
|
||||||
void FBO::Unbind() {
|
|
||||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void FBO::Cleanup() {
|
|
||||||
if (albedo_gbuffer_id_ != -1) {
|
|
||||||
glDeleteTextures(1, &albedo_gbuffer_id_);
|
|
||||||
}
|
}
|
||||||
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 {
|
GLuint CreateTexture(int width, int height, GLenum format) {
|
||||||
return albedo_gbuffer_id_;
|
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 {
|
GLuint CreateDepthTexture(int width, int height) {
|
||||||
return position_gbuffer_id_;
|
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 {
|
void AttachTextureToFramebuffer(GLuint framebufferId, GLuint textureId, GLenum attachment) {
|
||||||
return normal_gbuffer_id_;
|
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
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#include "Uniforms.h"
|
#include "Uniforms.h"
|
||||||
#include "InitShader.h" //Functions for loading shaders from text files
|
#include "InitShader.h" //Functions for loading shaders from text files
|
||||||
#include "DebugCallback.h"
|
#include "DebugCallback.h"
|
||||||
|
#include "load_texture.h"
|
||||||
|
|
||||||
#include <FreeImage.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 kQuadFragmentPath = "shaders/quad_fragment.glsl";
|
||||||
|
|
||||||
const std::string kHGTPath = "hgt/N02E016.hgt";
|
const std::string kHGTPath = "hgt/N02E016.hgt";
|
||||||
|
|
||||||
GLuint tex_id = -1;
|
GLuint tex_id = -1;
|
||||||
|
|
||||||
float tessellationFactor = 32.0f;
|
float tessellationFactor = 32.0f;
|
||||||
|
@ -68,8 +68,8 @@ Scene::~Scene() {
|
||||||
glDeleteProgram(shader_program_);
|
glDeleteProgram(shader_program_);
|
||||||
glDeleteVertexArrays(1, &vao_);
|
glDeleteVertexArrays(1, &vao_);
|
||||||
|
|
||||||
fbo_.~FBO();
|
FBO::Cleanup(geo_fbo_);
|
||||||
post_fbo_.~FBO();
|
FBO::Cleanup(lht_fbo_);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Scene::Init() {
|
void Scene::Init() {
|
||||||
|
@ -78,8 +78,8 @@ void Scene::Init() {
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
|
|
||||||
fbo_.Init(window_width, window_height);
|
geo_fbo_ = FBO::GenerateFramebuffer(window_width, window_height);
|
||||||
post_fbo_.Init(window_width, window_height);
|
lht_fbo_ = FBO::GenerateFramebuffer(window_width, window_height);
|
||||||
|
|
||||||
std::vector<int16_t> heightData = LoadHGT(kHGTPath, 3601, 3601);
|
std::vector<int16_t> heightData = LoadHGT(kHGTPath, 3601, 3601);
|
||||||
tex_id = CreateHeightmapTexture(heightData, 3601, 3601);
|
tex_id = CreateHeightmapTexture(heightData, 3601, 3601);
|
||||||
|
@ -207,7 +207,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);
|
||||||
fbo_.Bind();
|
glBindFramebuffer(GL_FRAMEBUFFER, geo_fbo_.id);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
glUseProgram(shader_program_);
|
glUseProgram(shader_program_);
|
||||||
|
@ -236,19 +236,19 @@ void Scene::Display(GLFWwindow* window) {
|
||||||
|
|
||||||
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
//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);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
glUseProgram(quad_shader_program_);
|
glUseProgram(quad_shader_program_);
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D, fbo_.GetAlbedoGbuffer());
|
glBindTexture(GL_TEXTURE_2D, geo_fbo_.albedoTexture);
|
||||||
glActiveTexture(GL_TEXTURE1);
|
glActiveTexture(GL_TEXTURE1);
|
||||||
glBindTexture(GL_TEXTURE_2D, fbo_.GetPositionGbuffer());
|
glBindTexture(GL_TEXTURE_2D, geo_fbo_.positionTexture);
|
||||||
glActiveTexture(GL_TEXTURE2);
|
glActiveTexture(GL_TEXTURE2);
|
||||||
glBindTexture(GL_TEXTURE_2D, fbo_.GetNormalGbuffer());
|
glBindTexture(GL_TEXTURE_2D, geo_fbo_.normalTexture);
|
||||||
|
|
||||||
GLint colorTextureLoc = glGetUniformLocation(quad_shader_program_, "albedoGbuffer");
|
GLint colorTextureLoc = glGetUniformLocation(quad_shader_program_, "albedoGbuffer");
|
||||||
glUniform1i(colorTextureLoc, 0);
|
glUniform1i(colorTextureLoc, 0);
|
||||||
|
@ -261,7 +261,7 @@ void Scene::Display(GLFWwindow* window) {
|
||||||
glBindVertexArray(quad_vao_);
|
glBindVertexArray(quad_vao_);
|
||||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||||
|
|
||||||
post_fbo_.Unbind();
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
|
||||||
DrawGui(window);
|
DrawGui(window);
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
|
@ -330,7 +330,7 @@ void Scene::DrawGui(GLFWwindow* window) {
|
||||||
// Draw FBO to ImGui window
|
// Draw FBO to ImGui window
|
||||||
ImGui::Begin("Scene Window");
|
ImGui::Begin("Scene Window");
|
||||||
ImVec2 windowSize = ImGui::GetContentRegionAvail();
|
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::End();
|
||||||
|
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
|
|
Loading…
Reference in New Issue