Adding ImGui , fixing some minor framebuffer error

This commit is contained in:
Nigel Barink 2023-05-19 16:04:52 +02:00
parent dc89cc6173
commit 3acc8525b0
8 changed files with 141 additions and 41 deletions

26
CheckErrors.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include "GLFW/glfw3.h"
#include <string>
#include <glad/glad.h>
#include <iostream>
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__)

View File

@ -8,7 +8,14 @@ project "LearnOpenGL"
targetdir "build/" targetdir "build/"
targetname "LearnOpenGL" 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 { includedirs {
"vendor/GLAD/include", "vendor/GLAD/include",
@ -16,7 +23,8 @@ project "LearnOpenGL"
"vendor/assimp/include", "vendor/assimp/include",
"vendor/assimp/build/include", "vendor/assimp/build/include",
"vendor/glm", "vendor/glm",
"vendor/stb" "vendor/stb",
"vendor/ImGui"
} }
libdirs{ libdirs{

View File

@ -1,40 +1,59 @@
#include "OutlinePass.h" #include "OutlinePass.h"
#include "Renderer.h" #include "Renderer.h"
#include "../../CheckErrors.h"
OutlinePass::OutlinePass(Model& sceneObject, glm::mat4& model, glm::mat4& projection, Camera& camera) 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) : 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() void OutlinePass::Render()

View File

@ -13,12 +13,13 @@ public:
void Render() override; void Render() override;
OutlinePass (Model& sceneObject, glm::mat4& model , glm::mat4& projection, Camera& camera) ; OutlinePass (Model& sceneObject, glm::mat4& model , glm::mat4& projection, Camera& camera) ;
void Create();
~OutlinePass(); ~OutlinePass();
private: private:
FrameBuffer m_outlineFrameBuffer; FrameBuffer* m_outlineFrameBuffer;
RenderBuffer m_outlineRenderBuffer; RenderBuffer* m_outlineRenderBuffer;
Texture* m_depthTexture; Texture* m_colourTexture;
glm::mat4 model; glm::mat4 model;
glm::mat4 projection; glm::mat4 projection;

View File

@ -21,5 +21,5 @@ RenderBuffer::RenderBuffer()
RenderBuffer::~RenderBuffer() RenderBuffer::~RenderBuffer()
{ {
glDeleteRenderbuffers(GL_RENDERBUFFER, &id); // glDeleteRenderbuffers(GL_RENDERBUFFER, &id);
} }

View File

@ -9,18 +9,23 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
void Renderer::Setup() void Renderer::Setup()
{ {
framebuffer.Bind(); framebuffer.Bind();
ColourBuffer = CreateTexture(800,600); ColourBuffer = CreateTexture(800,600);
framebuffer.Attach(*ColourBuffer); framebuffer.Attach(*ColourBuffer);
renderbufferObject.Bind(); renderbufferObject.Bind();
renderbufferObject.UseDepthAndStencil(); renderbufferObject.UseDepthAndStencil();
renderbufferObject.Unbind(); renderbufferObject.Unbind();
framebuffer.Attach(renderbufferObject); framebuffer.Attach(renderbufferObject);
if(framebuffer.IsComplete() == false ){ if(framebuffer.IsComplete() == false ){
@ -33,6 +38,7 @@ void Renderer::Setup()
void Renderer::Render( ) void Renderer::Render( )
{ {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
// Recalculate the Camera's view matrix // Recalculate the Camera's view matrix
@ -44,7 +50,6 @@ void Renderer::Render( )
// 1. Skybox pass // 1. Skybox pass
skyboxpass.Render(); skyboxpass.Render();
// 2. Scene pass // 2. Scene pass
scenepass.Render(); scenepass.Render();
@ -68,8 +73,9 @@ void Renderer::Render( )
m_shader.use(); m_shader.use();
m_shader.setInt("screenTexture",0); m_shader.setInt("screenTexture",0);
glBindVertexArray(ScreenVAO); glBindVertexArray(ScreenVAO);
glBindTexture(GL_TEXTURE_2D, ColourBuffer->id); glBindTexture(GL_TEXTURE_2D, ColourBuffer->id);
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
@ -91,6 +97,10 @@ Renderer::Renderer(Camera& camera, Model& scene) :
m_shader("../Shaders/Framebuffers.vs", "../Shaders/Framebuffers.fs") m_shader("../Shaders/Framebuffers.vs", "../Shaders/Framebuffers.fs")
{ {
outlinepass.Create();
std::vector<std::string> faces = { std::vector<std::string> faces = {
"../Textures/skybox/right.jpg", "../Textures/skybox/right.jpg",
"../Textures/skybox/left.jpg", "../Textures/skybox/left.jpg",

View File

@ -1,5 +1,4 @@
#include "SkyboxPass.h" #include "SkyboxPass.h"
SkyboxPass::SkyboxPass(Skybox& skybox, Camera& camera, glm::mat4& projection) : RenderPass("Render Pass - Skybox", "../Shaders/skybox.vs", "../Shaders/Cubemap.fs"), SkyboxPass::SkyboxPass(Skybox& skybox, Camera& camera, glm::mat4& projection) : RenderPass("Render Pass - Skybox", "../Shaders/skybox.vs", "../Shaders/Cubemap.fs"),
skybox(skybox), skybox(skybox),
camera(camera), camera(camera),
@ -15,14 +14,19 @@ void SkyboxPass::Render()
glDepthMask(GL_FALSE); glDepthMask(GL_FALSE);
m_shader.use(); m_shader.use();
m_shader.setMat4("projection", projection);
m_shader.setMat4("projection", projection);
glm::mat4 centeredView = glm::mat4(glm::mat3(camera.GetViewMatrix())); glm::mat4 centeredView = glm::mat4(glm::mat3(camera.GetViewMatrix()));
m_shader.setMat4("view", centeredView); m_shader.setMat4("view", centeredView);
skybox.Bind(); skybox.Bind();
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);
skybox.Unbind(); skybox.Unbind();
glDepthMask(GL_TRUE); glDepthMask(GL_TRUE);
} }

View File

@ -6,10 +6,17 @@
#include "Primitives/Skybox.h" #include "Primitives/Skybox.h"
#include "Primitives/shader.h" #include "Primitives/shader.h"
#include "Primitives/camera.h" #include "Primitives/camera.h"
#include "Application.h" #include <imgui.h>
#include <backends/imgui_impl_glfw.h>
#include <backends/imgui_impl_opengl3.h>
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h> #include <stb_image.h>
#include "Application.h"
void processInput( GLFWwindow* window); void processInput( GLFWwindow* window);
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset); void scroll_callback(GLFWwindow* window, double xoffset, double yoffset);
void framebuffer_size_callback(GLFWwindow* window, int width, int height); void framebuffer_size_callback(GLFWwindow* window, int width, int height);
@ -30,6 +37,16 @@ public:
// Create a window // Create a window
Window window(800,600, "LearnOpenGL"); 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); stbi_set_flip_vertically_on_load(true);
Model backpack("../Models/backpack.obj"); Model backpack("../Models/backpack.obj");
@ -37,6 +54,8 @@ public:
Renderer renderer = Renderer(camera, backpack); Renderer renderer = Renderer(camera, backpack);
renderer.Setup(); renderer.Setup();
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
while(!window.shouldClose()) while(!window.shouldClose())
{ {
@ -46,12 +65,25 @@ public:
glfwPollEvents(); glfwPollEvents();
processInput(window.ptr()); processInput(window.ptr());
renderer.Render(); renderer.Render();
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
ImGui::ShowDemoWindow(nullptr);
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
window.SwapBuffers(); window.SwapBuffers();
} }
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
} }
}; };