From 3acc8525b09c2c3a2e3a03e95ed401dae7954070 Mon Sep 17 00:00:00 2001 From: Nigel Barink Date: Fri, 19 May 2023 16:04:52 +0200 Subject: [PATCH] Adding ImGui , fixing some minor framebuffer error --- CheckErrors.h | 26 ++++++++++++ premake5.lua | 12 +++++- src/Renderer/OutlinePass.cpp | 75 ++++++++++++++++++++++------------- src/Renderer/OutlinePass.h | 7 ++-- src/Renderer/RenderBuffer.cpp | 2 +- src/Renderer/Renderer.cpp | 16 ++++++-- src/Renderer/SkyboxPass.cpp | 8 +++- src/main.cpp | 36 ++++++++++++++++- 8 files changed, 141 insertions(+), 41 deletions(-) create mode 100644 CheckErrors.h diff --git a/CheckErrors.h b/CheckErrors.h new file mode 100644 index 0000000..9577270 --- /dev/null +++ b/CheckErrors.h @@ -0,0 +1,26 @@ +#pragma once +#include "GLFW/glfw3.h" +#include +#include +#include +GLenum glCheckError_(const char* file, int line) { + GLenum errorCode; + while ((errorCode = glGetError()) != GL_NO_ERROR) + { + std::string error; + switch (errorCode) { + case GL_INVALID_ENUM: error = "INVALID_ENUM"; break; + case GL_INVALID_VALUE: error = "INVALID_VALUE"; break; + case GL_INVALID_OPERATION: error = "INVALID_OPERATION"; break; + case GL_STACK_OVERFLOW: error = "STACK_OVERFLOW"; break; + case GL_STACK_UNDERFLOW: error = "STACK_UNDERFLOW"; break; + case GL_OUT_OF_MEMORY: error = "OUT_OF_MEMORY"; break; + case GL_INVALID_FRAMEBUFFER_OPERATION: error = "INVALID_FRAMEBUFFER_OPERATION"; break; + } + std::cout << error << " | " << file << "(" << line << ")" << std::endl; + + } + return errorCode; +} + +#define glCheckError() glCheckError_(__FILE__, __LINE__) \ No newline at end of file diff --git a/premake5.lua b/premake5.lua index b61034e..abc99fe 100644 --- a/premake5.lua +++ b/premake5.lua @@ -8,7 +8,14 @@ project "LearnOpenGL" targetdir "build/" targetname "LearnOpenGL" - files {"vendor/GLAD/src/glad.c","src/**.h", "src/**.cpp"} + files { + "vendor/GLAD/src/glad.c", + "vendor/ImGui/imgui*.cpp", + "vendor/ImGui/backends/imgui_impl_glfw.cpp", + "vendor/ImGui/backends/imgui_impl_opengl3.cpp", + "src/**.h", + "src/**.cpp" + } includedirs { "vendor/GLAD/include", @@ -16,7 +23,8 @@ project "LearnOpenGL" "vendor/assimp/include", "vendor/assimp/build/include", "vendor/glm", - "vendor/stb" + "vendor/stb", + "vendor/ImGui" } libdirs{ diff --git a/src/Renderer/OutlinePass.cpp b/src/Renderer/OutlinePass.cpp index ea29f62..e7ef6dc 100644 --- a/src/Renderer/OutlinePass.cpp +++ b/src/Renderer/OutlinePass.cpp @@ -1,40 +1,59 @@ #include "OutlinePass.h" #include "Renderer.h" +#include "../../CheckErrors.h" OutlinePass::OutlinePass(Model& sceneObject, glm::mat4& model, glm::mat4& projection, Camera& camera) : RenderPass("Renderpass - Outline", "../Shaders/shader.vs", "../Shaders/outlineshader.fs" ) , m_model(sceneObject), camera(camera), projection(projection), model(model) { - // Framebuffer outline effect - std::cout << "Create Framebuffer" << std::endl; - - m_outlineFrameBuffer = FrameBuffer(); - m_outlineFrameBuffer.Bind(); - - // Create a texture attachment (colour attachment) - std::cout << "Create Texture" << std::endl; - m_depthTexture = CreateTexture(800,600); - - // attach texture to the frame buffer as a colour attachment - m_outlineFrameBuffer.Attach(*m_depthTexture); - - // Add depth buffer attachment - std::cout << "Create RenderBuffer" << std::endl; - m_outlineRenderBuffer = RenderBuffer(); - m_outlineRenderBuffer.Bind(); - m_outlineRenderBuffer.UseDepthAndStencil(); - m_outlineRenderBuffer.Unbind(); - - // attach depth buffer to our framebuffer - glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_outlineRenderBuffer.id); - m_outlineFrameBuffer.Unbind(); - - std::cout << "Success!" << std::endl; - } -OutlinePass::~OutlinePass(){ +void OutlinePass::Create() { + // Framebuffer outline effect + std::cout << "Create Framebuffer" << std::endl; - delete m_depthTexture; + m_outlineFrameBuffer = new FrameBuffer(); + glCheckError(); + + m_outlineFrameBuffer->Bind(); + glCheckError(); + + // Create a texture attachment (colour attachment) + std::cout << "Create Texture" << std::endl; + m_colourTexture = CreateTexture(800, 600); + glCheckError(); + + // attach texture to the frame buffer as a colour attachment + m_outlineFrameBuffer->Attach(*m_colourTexture); + glCheckError(); + + // Add depth buffer attachment + std::cout << "Create RenderBuffer" << std::endl; + m_outlineRenderBuffer = new RenderBuffer(); + glCheckError(); + + m_outlineRenderBuffer->Bind(); + glCheckError(); + + m_outlineRenderBuffer->UseDepthAndStencil(); + glCheckError(); + + m_outlineRenderBuffer->Unbind(); + glCheckError(); + + // attach depth buffer to our framebuffer + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_outlineRenderBuffer->id); + glCheckError(); + + m_outlineFrameBuffer->Unbind(); + glCheckError(); + + std::cout << "Success!" << std::endl; +} + + +OutlinePass::~OutlinePass(){ + delete m_colourTexture; + delete m_outlineRenderBuffer; } void OutlinePass::Render() diff --git a/src/Renderer/OutlinePass.h b/src/Renderer/OutlinePass.h index aa85175..4f4c68c 100644 --- a/src/Renderer/OutlinePass.h +++ b/src/Renderer/OutlinePass.h @@ -13,12 +13,13 @@ public: void Render() override; OutlinePass (Model& sceneObject, glm::mat4& model , glm::mat4& projection, Camera& camera) ; + void Create(); ~OutlinePass(); private: - FrameBuffer m_outlineFrameBuffer; - RenderBuffer m_outlineRenderBuffer; - Texture* m_depthTexture; + FrameBuffer* m_outlineFrameBuffer; + RenderBuffer* m_outlineRenderBuffer; + Texture* m_colourTexture; glm::mat4 model; glm::mat4 projection; diff --git a/src/Renderer/RenderBuffer.cpp b/src/Renderer/RenderBuffer.cpp index c5c5c43..4e4320e 100644 --- a/src/Renderer/RenderBuffer.cpp +++ b/src/Renderer/RenderBuffer.cpp @@ -21,5 +21,5 @@ RenderBuffer::RenderBuffer() RenderBuffer::~RenderBuffer() { - glDeleteRenderbuffers(GL_RENDERBUFFER, &id); + // glDeleteRenderbuffers(GL_RENDERBUFFER, &id); } diff --git a/src/Renderer/Renderer.cpp b/src/Renderer/Renderer.cpp index d1db5eb..2c650db 100644 --- a/src/Renderer/Renderer.cpp +++ b/src/Renderer/Renderer.cpp @@ -9,18 +9,23 @@ #include #include #include - void Renderer::Setup() { framebuffer.Bind(); + ColourBuffer = CreateTexture(800,600); + + framebuffer.Attach(*ColourBuffer); renderbufferObject.Bind(); + renderbufferObject.UseDepthAndStencil(); + renderbufferObject.Unbind(); + framebuffer.Attach(renderbufferObject); if(framebuffer.IsComplete() == false ){ @@ -33,6 +38,7 @@ void Renderer::Setup() void Renderer::Render( ) { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Recalculate the Camera's view matrix @@ -44,7 +50,6 @@ void Renderer::Render( ) // 1. Skybox pass skyboxpass.Render(); - // 2. Scene pass scenepass.Render(); @@ -68,8 +73,9 @@ void Renderer::Render( ) m_shader.use(); m_shader.setInt("screenTexture",0); glBindVertexArray(ScreenVAO); - + glBindTexture(GL_TEXTURE_2D, ColourBuffer->id); + glDrawArrays(GL_TRIANGLES, 0, 6); @@ -91,6 +97,10 @@ Renderer::Renderer(Camera& camera, Model& scene) : m_shader("../Shaders/Framebuffers.vs", "../Shaders/Framebuffers.fs") { + + outlinepass.Create(); + + std::vector faces = { "../Textures/skybox/right.jpg", "../Textures/skybox/left.jpg", diff --git a/src/Renderer/SkyboxPass.cpp b/src/Renderer/SkyboxPass.cpp index af604b4..d24757f 100644 --- a/src/Renderer/SkyboxPass.cpp +++ b/src/Renderer/SkyboxPass.cpp @@ -1,5 +1,4 @@ #include "SkyboxPass.h" - SkyboxPass::SkyboxPass(Skybox& skybox, Camera& camera, glm::mat4& projection) : RenderPass("Render Pass - Skybox", "../Shaders/skybox.vs", "../Shaders/Cubemap.fs"), skybox(skybox), camera(camera), @@ -15,14 +14,19 @@ void SkyboxPass::Render() glDepthMask(GL_FALSE); m_shader.use(); - m_shader.setMat4("projection", projection); + m_shader.setMat4("projection", projection); glm::mat4 centeredView = glm::mat4(glm::mat3(camera.GetViewMatrix())); m_shader.setMat4("view", centeredView); + skybox.Bind(); + + glDrawArrays(GL_TRIANGLES, 0, 36); + skybox.Unbind(); + glDepthMask(GL_TRUE); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index af13024..e34651f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,10 +6,17 @@ #include "Primitives/Skybox.h" #include "Primitives/shader.h" #include "Primitives/camera.h" -#include "Application.h" +#include +#include +#include + #define STB_IMAGE_IMPLEMENTATION #include +#include "Application.h" + + + void processInput( GLFWwindow* window); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void framebuffer_size_callback(GLFWwindow* window, int width, int height); @@ -30,6 +37,16 @@ public: // Create a window Window window(800,600, "LearnOpenGL"); + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO& io = ImGui::GetIO();(void)io; + + + ImGui::StyleColorsDark(); + + ImGui_ImplGlfw_InitForOpenGL(window.ptr(), true); + ImGui_ImplOpenGL3_Init("#version 460"); + stbi_set_flip_vertically_on_load(true); Model backpack("../Models/backpack.obj"); @@ -37,6 +54,8 @@ public: Renderer renderer = Renderer(camera, backpack); renderer.Setup(); + + //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); while(!window.shouldClose()) { @@ -46,12 +65,25 @@ public: glfwPollEvents(); processInput(window.ptr()); - renderer.Render(); + + + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + ImGui::ShowDemoWindow(nullptr); + + ImGui::Render(); + + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); window.SwapBuffers(); } + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + } };