From 1b4ce73811d074e82eb9b596cf64ee85d3f4b6b4 Mon Sep 17 00:00:00 2001 From: Huseyn Ismayilov Date: Fri, 2 Aug 2024 03:32:51 +0400 Subject: [PATCH] feat: Create buffer, vertex array and window classes --- editor/include/Editor.h | 17 ++----- editor/include/UI.h | 34 -------------- editor/resources/imgui.ini | 34 +++++++------- editor/src/Editor.cpp | 48 +++----------------- editor/src/main.cpp | 6 +-- engine/include/Engine.h | 14 ++++-- engine/include/FrameBuffer.h | 29 ++++++++++++ engine/include/Graphics.h | 28 ------------ engine/include/Renderer.h | 39 ++++++++++++++++ engine/include/Shader.h | 18 +++++--- engine/include/UI.h | 31 +++++++++++++ engine/include/VertexArray.h | 22 ++++++++++ engine/include/VertexBuffer.h | 23 ++++++++++ engine/include/Window.h | 43 ++++++++++++++++++ engine/src/Engine.cpp | 36 +++++++++------ engine/src/FrameBuffer.cpp | 76 ++++++++++++++++++++++++++++++++ engine/src/Graphics.cpp | 74 ------------------------------- engine/src/Renderer.cpp | 74 +++++++++++++++++++++++++++++++ engine/src/Shader.cpp | 53 ++++++++++++---------- {editor => engine}/src/UI.cpp | 40 ++++++++++++----- engine/src/VertexArray.cpp | 31 +++++++++++++ engine/src/VertexBuffer.cpp | 36 +++++++++++++++ engine/src/Window.cpp | 83 +++++++++++++++++++++++++++++++++++ 23 files changed, 620 insertions(+), 269 deletions(-) delete mode 100644 editor/include/UI.h create mode 100644 engine/include/FrameBuffer.h delete mode 100644 engine/include/Graphics.h create mode 100644 engine/include/Renderer.h create mode 100644 engine/include/UI.h create mode 100644 engine/include/VertexArray.h create mode 100644 engine/include/VertexBuffer.h create mode 100644 engine/include/Window.h create mode 100644 engine/src/FrameBuffer.cpp delete mode 100644 engine/src/Graphics.cpp create mode 100644 engine/src/Renderer.cpp rename {editor => engine}/src/UI.cpp (72%) create mode 100644 engine/src/VertexArray.cpp create mode 100644 engine/src/VertexBuffer.cpp create mode 100644 engine/src/Window.cpp diff --git a/editor/include/Editor.h b/editor/include/Editor.h index 97ed526..cf1f329 100644 --- a/editor/include/Editor.h +++ b/editor/include/Editor.h @@ -1,6 +1,7 @@ #ifndef EDITOR_H #define EDITOR_H +#define GLFW_INCLUDE_NONE #include "Engine.h" #include "UI.h" @@ -10,21 +11,11 @@ public: Editor(); ~Editor(); - void Initialize(); - void Run(); - void Shutdown(); - - GLFWwindow* GetWindow() const; + static void Init(); + static void Run(); + static void Shutdown(); private: - GLFWwindow* window; - Engine engine; - UI ui; - - int screenWidth = 800; - int screenHeight = 600; - - void InitializeWindow(); void InitializeImGui(); }; diff --git a/editor/include/UI.h b/editor/include/UI.h deleted file mode 100644 index b6caf09..0000000 --- a/editor/include/UI.h +++ /dev/null @@ -1,34 +0,0 @@ -#ifndef UI_H -#define UI_H - -#include -#include -#include - -class UI -{ -public: - UI(); - ~UI(); - - void Initialize(GLFWwindow* window); - void Run(); - void Render(); - void Shutdown(); - - void Print(const std::string& message); - - void ShowMenu(); - void ShowHierarchy(); - void ShowProject(); - void ShowConsole(); - void ShowScene(); - void ShowInspector(); - -private: - ImVec4 clearColor = ImVec4(0.0f, 0.0f, 0.0f, 1.00f); - ImVec4* styleColors; - std::string log; -}; - -#endif diff --git a/editor/resources/imgui.ini b/editor/resources/imgui.ini index 00c4073..94e3446 100644 --- a/editor/resources/imgui.ini +++ b/editor/resources/imgui.ini @@ -1,6 +1,6 @@ [Window][DockSpaceViewport_11111111] Pos=0,19 -Size=800,581 +Size=900,581 Collapsed=0 [Window][Debug##Default] @@ -9,41 +9,41 @@ Size=400,400 Collapsed=0 [Window][Hierarchy] -Pos=602,19 -Size=198,581 +Pos=710,19 +Size=190,581 Collapsed=0 DockId=0x00000006,0 [Window][Scene] -Pos=192,19 -Size=408,428 +Pos=199,19 +Size=509,420 Collapsed=0 DockId=0x00000003,0 [Window][Project] -Pos=192,449 -Size=408,151 +Pos=199,441 +Size=509,159 Collapsed=0 DockId=0x00000004,0 [Window][Console] -Pos=192,449 -Size=408,151 +Pos=199,441 +Size=509,159 Collapsed=0 DockId=0x00000004,1 [Window][Inspector] Pos=0,19 -Size=190,581 +Size=197,581 Collapsed=0 DockId=0x00000001,0 [Docking][Data] -DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=0,19 Size=800,581 Split=X - DockNode ID=0x00000005 Parent=0x8B93E3BD SizeRef=600,581 Split=X - DockNode ID=0x00000001 Parent=0x00000005 SizeRef=190,581 Selected=0xE7039252 - DockNode ID=0x00000002 Parent=0x00000005 SizeRef=408,581 Split=Y - DockNode ID=0x00000003 Parent=0x00000002 SizeRef=608,428 CentralNode=1 Selected=0xE192E354 - DockNode ID=0x00000004 Parent=0x00000002 SizeRef=608,151 Selected=0xD04A4B96 - DockNode ID=0x00000006 Parent=0x8B93E3BD SizeRef=198,581 Selected=0x29EABFBD +DockSpace ID=0x8B93E3BD Window=0xA787BDB4 Pos=0,19 Size=900,581 Split=X + DockNode ID=0x00000005 Parent=0x8B93E3BD SizeRef=708,581 Split=X + DockNode ID=0x00000001 Parent=0x00000005 SizeRef=197,581 Selected=0xE7039252 + DockNode ID=0x00000002 Parent=0x00000005 SizeRef=509,581 Split=Y + DockNode ID=0x00000003 Parent=0x00000002 SizeRef=608,420 CentralNode=1 Selected=0xE192E354 + DockNode ID=0x00000004 Parent=0x00000002 SizeRef=608,159 Selected=0xD04A4B96 + DockNode ID=0x00000006 Parent=0x8B93E3BD SizeRef=190,581 Selected=0x29EABFBD diff --git a/editor/src/Editor.cpp b/editor/src/Editor.cpp index 4c34563..bdc6d3e 100644 --- a/editor/src/Editor.cpp +++ b/editor/src/Editor.cpp @@ -1,60 +1,22 @@ -#include #include "Editor.h" -#include "UI.h" -Editor::Editor() : window(nullptr){} +Editor::Editor()= default; Editor::~Editor() { Shutdown(); } -void Editor::Initialize() +void Editor::Init() { - InitializeWindow(); - engine.Initialize(window); - ui.Initialize(window); - Run(); + auto engine = Engine(); + + engine.Run(); } void Editor::Run() { - while (!glfwWindowShouldClose(window)) { - glfwPollEvents(); - ui.Run(); - engine.Render(); - ui.Render(); - glfwSwapBuffers(window); - } } void Editor::Shutdown() { - engine.Shutdown(); - ui.Shutdown(); - glfwDestroyWindow(window); - glfwTerminate(); -} -GLFWwindow* Editor::GetWindow() const { - return window; -} - -void Editor::InitializeWindow() { - if (!glfwInit()) { - std::cerr << "Failed to initialize GLFW" << std::endl; - return; - } - - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - - window = glfwCreateWindow(screenWidth, screenHeight, "Editor", nullptr, nullptr); - if (!window) { - std::cerr << "Failed to create GLFW window" << std::endl; - glfwTerminate(); - return; - } - - glfwMakeContextCurrent(window); - glfwSetFramebufferSizeCallback(window, Engine::FramebufferSizeCallback); } \ No newline at end of file diff --git a/editor/src/main.cpp b/editor/src/main.cpp index ad83cbe..c176fc7 100644 --- a/editor/src/main.cpp +++ b/editor/src/main.cpp @@ -1,8 +1,8 @@ +#include #include "Editor.h" int main(int argc, char** argv) { - Editor editor; - - editor.Initialize(); + Editor::Init(); + std::cout << argv << std::endl; } \ No newline at end of file diff --git a/engine/include/Engine.h b/engine/include/Engine.h index 87befb1..7a542a0 100644 --- a/engine/include/Engine.h +++ b/engine/include/Engine.h @@ -1,8 +1,11 @@ #ifndef ENGINE_H #define ENGINE_H +#define GLFW_INCLUDE_NONE #include -#include "Graphics.h" +#include "Renderer.h" +#include "Window.h" +#include "UI.h" class Engine { @@ -10,14 +13,17 @@ public: Engine(); ~Engine(); - static void FramebufferSizeCallback(GLFWwindow* window, int width, int height); + Window& GetWindow() const { return *m_Window; } - void Initialize(GLFWwindow* window); + static Engine& Get() { return *s_Instance; } + + void Run(); void Render(); void Shutdown(); private: - Graphics graphics; + Window* m_Window; + static Engine* s_Instance; }; #endif diff --git a/engine/include/FrameBuffer.h b/engine/include/FrameBuffer.h new file mode 100644 index 0000000..b6edf32 --- /dev/null +++ b/engine/include/FrameBuffer.h @@ -0,0 +1,29 @@ +#ifndef FRAMEBUFFER_H +#define FRAMEBUFFER_H + +#include +#include +#include + +class FrameBuffer +{ +public: + FrameBuffer(); + FrameBuffer(int width, int height); + ~FrameBuffer(); + + unsigned int GetFrameTexture() const; + + static FrameBuffer Create(int width, int height); + + void RescaleFrameBuffer(int width, int height) const; + void Bind() const; + static void Unbind(); + void Shutdown() const; + +private: + unsigned int m_FBO{}, m_RBO{}; + unsigned int m_Texture{}; +}; + +#endif \ No newline at end of file diff --git a/engine/include/Graphics.h b/engine/include/Graphics.h deleted file mode 100644 index 8a8e2f8..0000000 --- a/engine/include/Graphics.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef GRAPHICS_H -#define GRAPHICS_H - -#include -#include "Shader.h" - -class Graphics -{ -public: - Graphics(); - ~Graphics(); - - static void FramebufferSizeCallback(GLFWwindow* window, int width, int height); - - void Initialize(GLFWwindow* window); - void Render(); - void Shutdown(); - -private: - GLFWwindow* window; - Shader shader; - unsigned int VAO, VBO; - - void LoadShaders(); - void SetupBuffers(); -}; - -#endif \ No newline at end of file diff --git a/engine/include/Renderer.h b/engine/include/Renderer.h new file mode 100644 index 0000000..cd45169 --- /dev/null +++ b/engine/include/Renderer.h @@ -0,0 +1,39 @@ +#ifndef RENDERER_H +#define RENDERER_H + +#define GLFW_INCLUDE_NONE +#include +#include +#include "VertexArray.h" +#include "VertexBuffer.h" +#include "FrameBuffer.h" +#include "Window.h" +#include "Shader.h" +#include "UI.h" + +struct RendererData +{ + VertexArray* m_VAO; + VertexBuffer* m_VBO; + FrameBuffer* m_FBO; + Shader* m_Shader; +}; + +class Renderer +{ +public: + Renderer(); + ~Renderer(); + + static void Init(); + static void Render(); + static void Shutdown(); + + static RendererData s_Data; + +private: + static void LoadShaders(); + static void SetupBuffers(); +}; + +#endif \ No newline at end of file diff --git a/engine/include/Shader.h b/engine/include/Shader.h index d7a5cd8..cf4670e 100644 --- a/engine/include/Shader.h +++ b/engine/include/Shader.h @@ -1,28 +1,36 @@ #ifndef SHADER_H #define SHADER_H +#define GLFW_INCLUDE_NONE +#include #include #include #include +#include +#include +#include +#include class Shader { public: - Shader(); - Shader(const char* vPath, const char* fPath); + Shader() = default; + Shader(const std::string& vPath, const std::string& fPath); ~Shader(); - void Use(); + static Shader Create(const std::string& vPath, const std::string& fPath); - unsigned int GetID(); + unsigned int GetID() const; + void Use() const; + void Shutdown() const; void SetBool(const char* name, bool value) const; void SetInt(const char* name, int value) const; void SetFloat(const char* name, float value) const; void SetMat4(const char* name, glm::mat4 value) const; private: - unsigned int ID; + unsigned int m_ID; }; #endif diff --git a/engine/include/UI.h b/engine/include/UI.h new file mode 100644 index 0000000..5201209 --- /dev/null +++ b/engine/include/UI.h @@ -0,0 +1,31 @@ +#ifndef UI_H +#define UI_H + +#define GLFW_INCLUDE_NONE +#include "FrameBuffer.h" +#include +#include +#include + +class UI +{ +public: + UI(); + ~UI(); + + static void Init(GLFWwindow* window); + static void Run(); + static void Render(const FrameBuffer& sceneBuffer); + static void Shutdown(); + + static void Print(const std::string& message); + + static void ShowMenu(); + static void ShowHierarchy(); + static void ShowProject(); + static void ShowConsole(); + static void ShowScene(const FrameBuffer& sceneBuffer); + static void ShowInspector(); +}; + +#endif diff --git a/engine/include/VertexArray.h b/engine/include/VertexArray.h new file mode 100644 index 0000000..2399604 --- /dev/null +++ b/engine/include/VertexArray.h @@ -0,0 +1,22 @@ +#ifndef VERTEXARRAY_H +#define VERTEXARRAY_H + +#include + +class VertexArray +{ +public: + VertexArray(); + ~VertexArray(); + + static VertexArray Create(); + + void Bind() const; + static void Unbind(); + void Shutdown() const; + +private: + unsigned int m_VAO{}; +}; + +#endif \ No newline at end of file diff --git a/engine/include/VertexBuffer.h b/engine/include/VertexBuffer.h new file mode 100644 index 0000000..af49706 --- /dev/null +++ b/engine/include/VertexBuffer.h @@ -0,0 +1,23 @@ +#ifndef VERTEXBUFFER_H +#define VERTEXBUFFER_H + +#include + +class VertexBuffer +{ +public: + VertexBuffer(); + ~VertexBuffer(); + + static VertexBuffer Create(); + + void Bind() const; + static void Unbind(); + void SetData(int size, const void* data) const; + void Shutdown() const; + +private: + unsigned int m_VBO{}; +}; + +#endif \ No newline at end of file diff --git a/engine/include/Window.h b/engine/include/Window.h new file mode 100644 index 0000000..54ce00f --- /dev/null +++ b/engine/include/Window.h @@ -0,0 +1,43 @@ +#ifndef WINDOW_H +#define WINDOW_H + +#include +#include +#include +#include + +struct WindowData +{ + std::string Title; + int Width, Height; + + WindowData(): Title("Ferx Engine"), Width(900), Height(600){} + WindowData(const std::string& title, int width, int height): Title(title), Width(width), Height(height){}; +}; + +class Window +{ +public: + Window(); + Window(const std::string& title, int width, int height); + ~Window(); + + static Window Create(); + static Window Create(const std::string& title, int width, int height); + static void FramebufferSizeCallback(GLFWwindow* window, int width, int height); + + void Init(); + + GLFWwindow* GetWindow() const; + const std::string& GetTitle() const; + int GetWidth() const; + int GetHeight() const; + + void Shutdown(); + +private: + GLFWwindow* m_Window{}; + WindowData m_Data; +}; + +#endif \ No newline at end of file diff --git a/engine/src/Engine.cpp b/engine/src/Engine.cpp index 3f6534b..14a6cfa 100644 --- a/engine/src/Engine.cpp +++ b/engine/src/Engine.cpp @@ -1,30 +1,38 @@ #include "Engine.h" -Engine::Engine(){} +Engine* Engine::s_Instance = nullptr; + +Engine::Engine() +{ + s_Instance = this; + + m_Window = new Window(); + + Renderer::Init(); + UI::Init(m_Window->GetWindow()); +} Engine::~Engine() { Shutdown(); } -void Engine::FramebufferSizeCallback(GLFWwindow* window, int width, int height) +void Engine::Run() { - Graphics::FramebufferSizeCallback(window, width, height); -} - -void Engine::Initialize(GLFWwindow* window) -{ - graphics.Initialize(window); -} - -void Engine::Render() -{ - graphics.Render(); + while (!glfwWindowShouldClose(m_Window->GetWindow())) { + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + UI::Run(); + Renderer::Render(); + UI::Render(*Renderer::s_Data.m_FBO); + glfwSwapBuffers(m_Window->GetWindow()); + glfwPollEvents(); + } } void Engine::Shutdown() { - graphics.Shutdown(); + Renderer::Shutdown(); } diff --git a/engine/src/FrameBuffer.cpp b/engine/src/FrameBuffer.cpp new file mode 100644 index 0000000..158f83d --- /dev/null +++ b/engine/src/FrameBuffer.cpp @@ -0,0 +1,76 @@ +#include "FrameBuffer.h" + +FrameBuffer::FrameBuffer() = default; + +FrameBuffer::FrameBuffer(int width, int height) +{ + glGenFramebuffers(1, &m_FBO); + glBindFramebuffer(GL_FRAMEBUFFER, m_FBO); + + glGenTextures(1, &m_Texture); + glBindTexture(GL_TEXTURE_2D, m_Texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); + 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, m_Texture, 0); + + glGenRenderbuffers(1, &m_RBO); + glBindRenderbuffer(GL_RENDERBUFFER, m_RBO); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_RBO); + + if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) + std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!" << std::endl; + + glBindFramebuffer(GL_FRAMEBUFFER, 0); + glBindTexture(GL_TEXTURE_2D, 0); + glBindRenderbuffer(GL_RENDERBUFFER, 0); +} + +FrameBuffer::~FrameBuffer() +{ + glDeleteFramebuffers(1, &m_FBO); + glDeleteTextures(1, &m_Texture); + glDeleteRenderbuffers(1, &m_RBO); +} + +unsigned int FrameBuffer::GetFrameTexture() const +{ + return m_Texture; +} + +FrameBuffer FrameBuffer::Create(int width, int height) +{ + return FrameBuffer{width, height}; +} + +void FrameBuffer::Shutdown() const +{ + glDeleteBuffers(1, &m_FBO); + glDeleteBuffers(1, &m_RBO); + glDeleteTextures(1, &m_Texture); +} + + +void FrameBuffer::RescaleFrameBuffer(int width, int height) const +{ + glBindTexture(GL_TEXTURE_2D, m_Texture); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr); + 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, m_Texture, 0); + + glBindRenderbuffer(GL_RENDERBUFFER, m_RBO); + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, width, height); + glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, m_RBO); +} + +void FrameBuffer::Bind() const +{ + glBindFramebuffer(GL_FRAMEBUFFER, m_FBO); +} + +void FrameBuffer::Unbind() +{ + glBindFramebuffer(GL_FRAMEBUFFER, 0); +} \ No newline at end of file diff --git a/engine/src/Graphics.cpp b/engine/src/Graphics.cpp deleted file mode 100644 index 3f62301..0000000 --- a/engine/src/Graphics.cpp +++ /dev/null @@ -1,74 +0,0 @@ -#include -#include -#include "Graphics.h" -#include "Shader.h" - -Graphics::Graphics() : window(nullptr), VAO(0), VBO(0) {} - -Graphics::~Graphics() -{ - Shutdown(); -} - -void Graphics::Initialize(GLFWwindow* window) -{ - this->window = window; - - if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { - std::cerr << "Failed to initialize GLAD" << std::endl; - return; - } - - LoadShaders(); - SetupBuffers(); -} - -void Graphics::LoadShaders() -{ - shader = Shader(RESOURCES_PATH"shaders/shader.vs", RESOURCES_PATH"shaders/shader.fs"); -} - -void Graphics::SetupBuffers() -{ - float vertices[] = { - 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, - -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f - }; - - glGenVertexArrays(1, &VAO); - glGenBuffers(1, &VBO); - - glBindVertexArray(VAO); - - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); - glEnableVertexAttribArray(0); - - glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); - glEnableVertexAttribArray(1); - - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(0); -} - -void Graphics::FramebufferSizeCallback(GLFWwindow* window, int width, int height) -{ - glViewport(0, 0, width, height); -} - -void Graphics::Render() { - glClear(GL_COLOR_BUFFER_BIT); - - shader.Use(); - glBindVertexArray(VAO); - glDrawArrays(GL_TRIANGLES, 0, 3); -} - -void Graphics::Shutdown() -{ - glDeleteVertexArrays(1, &VAO); - glDeleteBuffers(1, &VBO); -} diff --git a/engine/src/Renderer.cpp b/engine/src/Renderer.cpp new file mode 100644 index 0000000..9fd1165 --- /dev/null +++ b/engine/src/Renderer.cpp @@ -0,0 +1,74 @@ +#include +#include "Renderer.h" +#include "Engine.h" + +RendererData Renderer::s_Data; + +Renderer::Renderer() = default; + +Renderer::~Renderer() +{ + Shutdown(); +} + +void Renderer::Init() +{ + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + std::cerr << "Failed to initialize GLAD" << std::endl; + return; + } + + LoadShaders(); + SetupBuffers(); +} + +void Renderer::LoadShaders() +{ + s_Data.m_Shader = new Shader(RESOURCES_PATH"shaders/shader.vs", RESOURCES_PATH"shaders/shader.fs"); +} + +void Renderer::SetupBuffers() +{ + float vertices[] = { + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, + -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, + 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f + }; + + s_Data.m_FBO = new FrameBuffer(FrameBuffer::Create(Engine::Get().GetWindow().GetWidth(), Engine::Get().GetWindow().GetHeight())); + + s_Data.m_VAO = new VertexArray(); + s_Data.m_VBO = new VertexBuffer(); + + s_Data.m_VAO->Bind(); + + s_Data.m_VBO->SetData(sizeof(vertices), vertices); + + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0); + glEnableVertexAttribArray(0); + + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float))); + glEnableVertexAttribArray(1); + + VertexArray::Unbind(); + VertexBuffer::Unbind(); +} + +void Renderer::Render() { + glClearColor(0.0f, 0.1f, 0.2f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + s_Data.m_FBO->Bind(); + + s_Data.m_Shader->Use(); + s_Data.m_VAO->Bind(); + glDrawArrays(GL_TRIANGLES, 0, 3); + FrameBuffer::Unbind(); +} + +void Renderer::Shutdown() +{ + s_Data.m_VAO->Shutdown(); + s_Data.m_VBO->Shutdown(); + s_Data.m_FBO->Shutdown(); + s_Data.m_Shader->Shutdown(); +} diff --git a/engine/src/Shader.cpp b/engine/src/Shader.cpp index 82a870e..f114571 100644 --- a/engine/src/Shader.cpp +++ b/engine/src/Shader.cpp @@ -1,12 +1,6 @@ -#include -#include -#include -#include #include "Shader.h" -Shader::Shader(): ID(0){} - -Shader::Shader(const char* vPath, const char* fPath) +Shader::Shader(const std::string& vPath, const std::string& fPath) { std::string vCode; std::string fCode; @@ -64,15 +58,15 @@ Shader::Shader(const char* vPath, const char* fPath) std::cerr << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl; }; - ID = glCreateProgram(); - glAttachShader(ID, vertex); - glAttachShader(ID, fragment); - glLinkProgram(ID); + m_ID = glCreateProgram(); + glAttachShader(m_ID, vertex); + glAttachShader(m_ID, fragment); + glLinkProgram(m_ID); - glGetProgramiv(ID, GL_LINK_STATUS, &success); + glGetProgramiv(m_ID, GL_LINK_STATUS, &success); if(!success) { - glGetProgramInfoLog(ID, 512, nullptr, infoLog); + glGetProgramInfoLog(m_ID, 512, nullptr, infoLog); std::cerr << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl; } @@ -80,31 +74,44 @@ Shader::Shader(const char* vPath, const char* fPath) glDeleteShader(fragment); } -Shader::~Shader(){}; - -void Shader::Use() +Shader::~Shader() { - glUseProgram(ID); + glDeleteProgram(m_ID); } -unsigned int Shader::GetID() +Shader Shader::Create(const std::string& vPath, const std::string& fPath) { - return ID; + return Shader{vPath, fPath}; +} + +void Shader::Use() const +{ + glUseProgram(m_ID); +} + +void Shader::Shutdown() const +{ + glDeleteProgram(m_ID); +} + +unsigned int Shader::GetID() const +{ + return m_ID; } void Shader::SetBool(const char* name, bool value) const { - glUniform1i(glGetUniformLocation(ID, name), (int)value); + glUniform1i(glGetUniformLocation(m_ID, name), (int)value); } void Shader::SetInt(const char* name, int value) const { - glUniform1i(glGetUniformLocation(ID, name), value); + glUniform1i(glGetUniformLocation(m_ID, name), value); } void Shader::SetFloat(const char* name, float value) const { - glUniform1f(glGetUniformLocation(ID, name), value); + glUniform1f(glGetUniformLocation(m_ID, name), value); } void Shader::SetMat4(const char* name, glm::mat4 value) const { - glUniformMatrix4fv(glGetUniformLocation(ID, name), 1, GL_FALSE, glm::value_ptr(value)); + glUniformMatrix4fv(glGetUniformLocation(m_ID, name), 1, GL_FALSE, glm::value_ptr(value)); } diff --git a/editor/src/UI.cpp b/engine/src/UI.cpp similarity index 72% rename from editor/src/UI.cpp rename to engine/src/UI.cpp index 9bdd73c..026f0de 100644 --- a/editor/src/UI.cpp +++ b/engine/src/UI.cpp @@ -2,20 +2,28 @@ #include #include #include "UI.h" +#include "FrameBuffer.h" +#include "Window.h" -UI::UI(): styleColors(nullptr){} +static ImVec4 m_ClearColor; +static ImVec4* m_StyleColors; +static std::string m_Log; -UI::~UI(){} +UI::UI()= default; -void UI::Initialize(GLFWwindow* window) +UI::~UI()= default; + +void UI::Init(GLFWwindow* window) { + m_ClearColor = ImVec4(0.0f, 0.0f, 0.0f, 1.00f); + IMGUI_CHECKVERSION(); ImGui::CreateContext(); ImGuiIO& io = ImGui::GetIO(); (void)io; io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; ImGuiStyle& style = ImGui::GetStyle(); - styleColors = style.Colors; + m_StyleColors = style.Colors; ImGui::StyleColorsDark(); // Setup Platform/Renderer backends @@ -29,17 +37,17 @@ void UI::Run() ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); ImGui::DockSpaceOverViewport(ImGui::GetMainViewport()); - styleColors[ImGuiCol_DockingEmptyBg] = clearColor; + m_StyleColors[ImGuiCol_DockingEmptyBg] = m_ClearColor; } -void UI::Render() +void UI::Render(const FrameBuffer& sceneBuffer) { ShowConsole(); ShowHierarchy(); ShowInspector(); ShowMenu(); ShowProject(); - ShowScene(); + ShowScene(sceneBuffer); ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); @@ -54,7 +62,7 @@ void UI::Shutdown() void UI::Print(const std::string& message) { - log = message; + m_Log = message; } void UI::ShowConsole(){ @@ -72,7 +80,7 @@ void UI::ShowConsole(){ ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); - ImGui::Text("%s", log.c_str()); + ImGui::Text("%s", m_Log.c_str()); ImGui::End(); } @@ -94,7 +102,7 @@ void UI::ShowInspector() ImGui::Text("This is some useful text."); ImGui::SliderFloat("float", &f, 0.0f, 1.0f); - ImGui::ColorEdit3("clear color", (float*)&clearColor); + ImGui::ColorEdit3("clear color", (float*)&m_ClearColor); if (ImGui::Button("Button")) counter++; @@ -124,10 +132,20 @@ void UI::ShowProject() ImGui::End(); } -void UI::ShowScene() +void UI::ShowScene(const FrameBuffer& sceneBuffer) { ImGui::Begin("Scene"); + { + ImGui::BeginChild("GameRender"); + ImGui::Image( + (ImTextureID)sceneBuffer.GetFrameTexture(), + ImGui::GetContentRegionAvail(), + ImVec2(0, 1), + ImVec2(1, 0) + ); + } + ImGui::EndChild(); ImGui::End(); } diff --git a/engine/src/VertexArray.cpp b/engine/src/VertexArray.cpp new file mode 100644 index 0000000..66c4a80 --- /dev/null +++ b/engine/src/VertexArray.cpp @@ -0,0 +1,31 @@ +#include "VertexArray.h" + +VertexArray::VertexArray() +{ + glGenVertexArrays(1, &m_VAO); +} + +VertexArray::~VertexArray() +{ + Shutdown(); +} + +VertexArray VertexArray::Create() +{ + return VertexArray{}; +} + +void VertexArray::Bind() const +{ + glBindVertexArray(m_VAO); +} + +void VertexArray::Unbind() +{ + glBindVertexArray(0); +} + +void VertexArray::Shutdown() const +{ + glDeleteVertexArrays(1, &m_VAO); +} diff --git a/engine/src/VertexBuffer.cpp b/engine/src/VertexBuffer.cpp new file mode 100644 index 0000000..5cf8713 --- /dev/null +++ b/engine/src/VertexBuffer.cpp @@ -0,0 +1,36 @@ +#include "VertexBuffer.h" + +VertexBuffer::VertexBuffer() +{ + glGenBuffers(1, &m_VBO); +} + +VertexBuffer::~VertexBuffer() +{ + Shutdown(); +} + +VertexBuffer VertexBuffer::Create() +{ + return VertexBuffer{}; +} + +void VertexBuffer::Bind() const +{ + glBindBuffer(GL_ARRAY_BUFFER, m_VBO); +} +void VertexBuffer::Unbind() +{ + glBindBuffer(GL_ARRAY_BUFFER, 0); +} + +void VertexBuffer::SetData(int size, const void* data) const +{ + Bind(); + glBufferData(GL_ARRAY_BUFFER, size, data, GL_STATIC_DRAW); +} + +void VertexBuffer::Shutdown() const +{ + glDeleteBuffers(1, &m_VBO); +} diff --git a/engine/src/Window.cpp b/engine/src/Window.cpp new file mode 100644 index 0000000..3c7a9a0 --- /dev/null +++ b/engine/src/Window.cpp @@ -0,0 +1,83 @@ +#include "Window.h" + +Window::Window() +{ + Init(); +} + +Window::Window(const std::string& title, int width, int height) +{ + m_Data = WindowData{title, width, height}; + Init(); +} + +Window::~Window() +{ + Shutdown(); +} + +void Window::Init() +{ + if (!glfwInit()) { + std::cerr << "Failed to initialize GLFW" << std::endl; + return; + } + + std::cout << "GLFW Initialized" << std::endl; + + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + m_Window = glfwCreateWindow(m_Data.Width, m_Data.Height, m_Data.Title.c_str(), nullptr, nullptr); + if (!m_Window) { + std::cerr << "Failed to create GLFW window" << std::endl; + glfwTerminate(); + return; + } + + glfwMakeContextCurrent(m_Window); + + glfwSetFramebufferSizeCallback(m_Window, FramebufferSizeCallback); +} + +Window Window::Create() +{ + return Window{}; +} + +Window Window::Create(const std::string& title, int width, int height) +{ + return Window{title, width, height}; +} + +void Window::FramebufferSizeCallback(GLFWwindow* window, int width, int height) +{ + glViewport(0, 0, width, height); +} + +GLFWwindow* Window::GetWindow() const +{ + return m_Window; +} + +const std::string& Window::GetTitle() const +{ + return m_Data.Title; +} + +int Window::GetWidth() const +{ + return m_Data.Width; +} + +int Window::GetHeight() const +{ + return m_Data.Height; +} + + +void Window::Shutdown() +{ + glfwDestroyWindow(m_Window); +} \ No newline at end of file