diff --git a/CMakeLists.txt b/CMakeLists.txt index 4a8ac4f..91f0595 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,4 @@ +# Set minimum CMake version cmake_minimum_required(VERSION 3.22) # Declare project @@ -10,7 +11,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) # Define variables of directory paths set(THIRDPARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty) -# Add thirdparty libraries +# Add third party libraries add_subdirectory(${THIRDPARTY_DIR}/glad) add_subdirectory(${THIRDPARTY_DIR}/glfw) add_subdirectory(${THIRDPARTY_DIR}/glm) diff --git a/editor/include/Editor.h b/editor/include/Editor.h index 8d99e8f..91c680e 100644 --- a/editor/include/Editor.h +++ b/editor/include/Editor.h @@ -2,7 +2,7 @@ #define GLFW_INCLUDE_NONE #include "Engine.h" -#include "UI.h" +#include "GUI.h" class Editor { @@ -14,6 +14,10 @@ public: static void Run(); static void Shutdown(); + static Editor* GetInstance(); + static Engine* GetEngine(); + private: - void InitializeImGui(); + static Editor* s_Instance; + static Engine* s_Engine; }; \ No newline at end of file diff --git a/engine/include/UI.h b/editor/include/GUI.h similarity index 62% rename from engine/include/UI.h rename to editor/include/GUI.h index c38c2ba..34598cf 100644 --- a/engine/include/UI.h +++ b/editor/include/GUI.h @@ -7,20 +7,11 @@ #include #include -struct InspectorData -{ - glm::vec3 m_Position; - glm::vec3 m_Rotation; - glm::vec3 m_Scale = {1.0f, 1.0f, 1.0f}; - glm::vec3 m_ShaderColor = {1.0f, 1.0f, 1.0f}; - glm::vec3 m_BgColor = {0.0, 0.1f, 0.2f}; -}; - -class UI +class GUI { public: - UI(); - ~UI(); + GUI(); + ~GUI(); static void Init(GLFWwindow* window); static void LoadConfigs(); @@ -29,7 +20,6 @@ public: static void Shutdown(); static void Print(const std::string& message); - static InspectorData GetData(); static void ShowMenu(); static void ShowEntities(); @@ -39,8 +29,6 @@ public: static void ShowProperties(); private: - static InspectorData s_Data; - - static std::string m_Log; - static ImVec4* m_StyleColors; + static std::string s_Log; + static ImVec4* s_StyleColors; }; \ No newline at end of file diff --git a/engine/include/IconsFontAwesome6.h b/editor/include/IconsFontAwesome6.h similarity index 100% rename from engine/include/IconsFontAwesome6.h rename to editor/include/IconsFontAwesome6.h diff --git a/editor/src/Editor.cpp b/editor/src/Editor.cpp index bdc6d3e..684e769 100644 --- a/editor/src/Editor.cpp +++ b/editor/src/Editor.cpp @@ -1,6 +1,12 @@ #include "Editor.h" -Editor::Editor()= default; +Editor* Editor::s_Instance = nullptr; +Engine* Editor::s_Engine = nullptr; + +Editor::Editor() +{ + s_Instance = this; +} Editor::~Editor() { @@ -9,14 +15,22 @@ Editor::~Editor() void Editor::Init() { - auto engine = Engine(); + s_Engine = new Engine(); - engine.Run(); + GUI::Init(s_Engine->GetWindow()->GetNativeWindow()); + + Run(); } -void Editor::Run() { +void Editor::Run() +{ + while (!glfwWindowShouldClose(s_Engine->GetWindow()->GetNativeWindow())) { + s_Engine->Run(); + GUI::Run(); + GUI::Render(*Renderer::GetData().m_FBO); + } } -void Editor::Shutdown() { +void Editor::Shutdown(){ } \ No newline at end of file diff --git a/engine/src/UI.cpp b/editor/src/GUI.cpp similarity index 56% rename from engine/src/UI.cpp rename to editor/src/GUI.cpp index 931c315..4094ea8 100644 --- a/engine/src/UI.cpp +++ b/editor/src/GUI.cpp @@ -1,19 +1,19 @@ #include #include #include -#include "UI.h" +#include "GUI.h" + #include "FrameBuffer.h" #include "Window.h" -InspectorData UI::s_Data; -std::string UI::m_Log; -ImVec4* UI::m_StyleColors; +std::string GUI::s_Log; +ImVec4* GUI::s_StyleColors; -UI::UI()= default; +GUI::GUI()= default; -UI::~UI()= default; +GUI::~GUI()= default; -void UI::Init(GLFWwindow* window) +void GUI::Init(GLFWwindow* window) { IMGUI_CHECKVERSION(); ImGui::CreateContext(); @@ -25,7 +25,7 @@ void UI::Init(GLFWwindow* window) ImGui_ImplOpenGL3_Init(); } -void UI::LoadConfigs() +void GUI::LoadConfigs() { ImGuiIO& io = ImGui::GetIO(); (void)io; io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; @@ -46,7 +46,7 @@ void UI::LoadConfigs() ImGui::StyleColorsDark(); ImGuiStyle* style = &ImGui::GetStyle(); - m_StyleColors = style->Colors; + s_StyleColors = style->Colors; style->WindowMenuButtonPosition = ImGuiDir_None; style->WindowBorderSize = 0.0f; @@ -60,47 +60,44 @@ void UI::LoadConfigs() style->SeparatorTextPadding = ImVec2(5.0f, 5.0f); style->TabBarBorderSize = 2.0f; - m_StyleColors[ImGuiCol_WindowBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.0f); - m_StyleColors[ImGuiCol_Border] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); - m_StyleColors[ImGuiCol_PopupBg] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); - m_StyleColors[ImGuiCol_FrameBg] = ImVec4(0.09f, 0.09f, 0.09f, 1.0f); - m_StyleColors[ImGuiCol_FrameBgHovered] = ImVec4(0.03f, 0.03f, 0.03f, 0.8f); - m_StyleColors[ImGuiCol_FrameBgActive] = ImVec4(0.03f, 0.03f, 0.03f, 1.0f); - m_StyleColors[ImGuiCol_TitleBg] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); - m_StyleColors[ImGuiCol_TitleBgActive] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); - m_StyleColors[ImGuiCol_MenuBarBg] = ImVec4(0.11f, 0.11f, 0.11f, 1.0f); - m_StyleColors[ImGuiCol_Header] = ImVec4(0.08f, 0.08f, 0.08f, 1.0f); - m_StyleColors[ImGuiCol_HeaderHovered] = ImVec4(0.08f, 0.42f, 0.14f, 0.8f); - m_StyleColors[ImGuiCol_HeaderActive] = ImVec4(0.08f, 0.42f, 0.14f, 1.0f); - m_StyleColors[ImGuiCol_Tab] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); - m_StyleColors[ImGuiCol_TabHovered] = ImVec4(0.2f, 0.2f, 0.2f, 0.5f); - m_StyleColors[ImGuiCol_TabActive] = ImVec4(0.2f, 0.2f, 0.2f, 1.0f); - m_StyleColors[ImGuiCol_TabUnfocused] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); - m_StyleColors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.2f, 0.2f, 0.2f, 1.0f); - m_StyleColors[ImGuiCol_CheckMark] = ImVec4(0.08f, 0.42f, 0.14f, 1.0f); - m_StyleColors[ImGuiCol_SliderGrab] = ImVec4(0.08f, 0.42f, 0.14f, 0.8f); - m_StyleColors[ImGuiCol_SliderGrabActive] = ImVec4(0.08f, 0.42f, 0.14f, 1.0f); - m_StyleColors[ImGuiCol_Button] = ImVec4(0.2f, 0.2f, 0.2f, 1.0f); - m_StyleColors[ImGuiCol_ButtonHovered] = ImVec4(0.08f, 0.42f, 0.14f, 0.5f); - m_StyleColors[ImGuiCol_ButtonActive] = ImVec4(0.08f, 0.42f, 0.14f, 1.0f); - m_StyleColors[ImGuiCol_TextSelectedBg] = ImVec4(0.08f, 0.42f, 0.14f, 0.35f); + s_StyleColors[ImGuiCol_WindowBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.0f); + s_StyleColors[ImGuiCol_Border] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); + s_StyleColors[ImGuiCol_PopupBg] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); + s_StyleColors[ImGuiCol_FrameBg] = ImVec4(0.09f, 0.09f, 0.09f, 1.0f); + s_StyleColors[ImGuiCol_FrameBgHovered] = ImVec4(0.03f, 0.03f, 0.03f, 0.8f); + s_StyleColors[ImGuiCol_FrameBgActive] = ImVec4(0.03f, 0.03f, 0.03f, 1.0f); + s_StyleColors[ImGuiCol_TitleBg] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); + s_StyleColors[ImGuiCol_TitleBgActive] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); + s_StyleColors[ImGuiCol_MenuBarBg] = ImVec4(0.11f, 0.11f, 0.11f, 1.0f); + s_StyleColors[ImGuiCol_Header] = ImVec4(0.08f, 0.08f, 0.08f, 1.0f); + s_StyleColors[ImGuiCol_HeaderHovered] = ImVec4(0.08f, 0.42f, 0.14f, 0.8f); + s_StyleColors[ImGuiCol_HeaderActive] = ImVec4(0.08f, 0.42f, 0.14f, 1.0f); + s_StyleColors[ImGuiCol_Tab] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); + s_StyleColors[ImGuiCol_TabHovered] = ImVec4(0.2f, 0.2f, 0.2f, 0.5f); + s_StyleColors[ImGuiCol_TabActive] = ImVec4(0.2f, 0.2f, 0.2f, 1.0f); + s_StyleColors[ImGuiCol_TabUnfocused] = ImVec4(0.06f, 0.06f, 0.06f, 1.0f); + s_StyleColors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.2f, 0.2f, 0.2f, 1.0f); + s_StyleColors[ImGuiCol_CheckMark] = ImVec4(0.08f, 0.42f, 0.14f, 1.0f); + s_StyleColors[ImGuiCol_SliderGrab] = ImVec4(0.08f, 0.42f, 0.14f, 0.8f); + s_StyleColors[ImGuiCol_SliderGrabActive] = ImVec4(0.08f, 0.42f, 0.14f, 1.0f); + s_StyleColors[ImGuiCol_Button] = ImVec4(0.2f, 0.2f, 0.2f, 1.0f); + s_StyleColors[ImGuiCol_ButtonHovered] = ImVec4(0.08f, 0.42f, 0.14f, 0.5f); + s_StyleColors[ImGuiCol_ButtonActive] = ImVec4(0.08f, 0.42f, 0.14f, 1.0f); + s_StyleColors[ImGuiCol_TextSelectedBg] = ImVec4(0.08f, 0.42f, 0.14f, 0.35f); } -InspectorData UI::GetData() -{ - return s_Data; -} - -void UI::Run() +void GUI::Run() { ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); ImGui::DockSpaceOverViewport(ImGui::GetMainViewport()); - m_StyleColors[ImGuiCol_DockingEmptyBg] = ImVec4(s_Data.m_BgColor.x, s_Data.m_BgColor.y, s_Data.m_BgColor.z, 1.0f); + + glm::vec3* clearColor = Renderer::GetData().m_ClearColor; + s_StyleColors[ImGuiCol_DockingEmptyBg] = ImVec4(clearColor->x, clearColor->y, clearColor->z, 1.0f); } -void UI::Render(const FrameBuffer& sceneBuffer) +void GUI::Render(const FrameBuffer& sceneBuffer) { ShowConsole(); ShowEntities(); @@ -113,23 +110,23 @@ void UI::Render(const FrameBuffer& sceneBuffer) ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); } -void UI::Shutdown() +void GUI::Shutdown() { ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplGlfw_Shutdown(); ImGui::DestroyContext(); } -void UI::Print(const std::string& message) +void GUI::Print(const std::string& message) { - m_Log += message + '\n'; + s_Log += message + '\n'; } -void UI::ShowConsole(){ +void GUI::ShowConsole(){ ImGui::Begin(ICON_FA_TERMINAL" Console"); if(ImGui::Button("Clear")){ - m_Log.clear(); + s_Log.clear(); } ImGui::SameLine(); @@ -140,12 +137,12 @@ void UI::ShowConsole(){ ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); - ImGui::Text("%s", m_Log.c_str()); + ImGui::Text("%s", s_Log.c_str()); ImGui::End(); } -void UI::ShowEntities() +void GUI::ShowEntities() { ImGui::Begin(ICON_FA_CUBE" Entities"); @@ -154,16 +151,19 @@ void UI::ShowEntities() ImGui::End(); } -void UI::ShowProperties() +void GUI::ShowProperties() { ImGui::Begin(ICON_FA_BARS_STAGGERED" Properties"); if(ImGui::CollapsingHeader("Transform")) { ImGui::BeginGroup(); - ImGui::DragFloat3("Position", glm::value_ptr(s_Data.m_Position), 0.2f); - ImGui::DragFloat3("Rotation", glm::value_ptr(s_Data.m_Rotation), 0.4f); - ImGui::DragFloat3("Scale", glm::value_ptr(s_Data.m_Scale), 0.1f); + glm::vec3* position = Renderer::GetData().m_Cube->GetPosition(); + glm::vec3* rotation = Renderer::GetData().m_Cube->GetRotation(); + glm::vec3* scale = Renderer::GetData().m_Cube->GetScale(); + ImGui::DragFloat3("Position", glm::value_ptr(*position), 0.2f); + ImGui::DragFloat3("Rotation", glm::value_ptr(*rotation), 0.4f); + ImGui::DragFloat3("Scale", glm::value_ptr(*scale), 0.1f); ImGui::EndGroup(); } @@ -171,14 +171,15 @@ void UI::ShowProperties() ImGui::BeginGroup(); ImGui::Text("Colors"); - ImGui::ColorEdit3("Shader Color", glm::value_ptr(s_Data.m_ShaderColor)); - ImGui::ColorEdit3("Background Color", glm::value_ptr(s_Data.m_BgColor)); + glm::vec3* shaderColor = Renderer::GetData().m_Cube->GetShaderColor(); + ImGui::ColorEdit3("Shader Color", glm::value_ptr(*shaderColor)); + ImGui::ColorEdit3("Background Color", glm::value_ptr(*Renderer::GetData().m_ClearColor)); ImGui::EndGroup(); ImGui::End(); } -void UI::ShowMenu() +void GUI::ShowMenu() { if(ImGui::BeginMainMenuBar()){ if(ImGui::BeginMenu("File")){ @@ -191,14 +192,14 @@ void UI::ShowMenu() } } -void UI::ShowFiles() +void GUI::ShowFiles() { ImGui::Begin(ICON_FA_FOLDER" Files"); ImGui::End(); } -void UI::ShowScene(const FrameBuffer& sceneBuffer) +void GUI::ShowScene(const FrameBuffer& sceneBuffer) { ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{0, 0}); ImGui::Begin(ICON_FA_CLAPPERBOARD" Scene"); diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index ef50fdc..389d57a 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -1,19 +1,45 @@ +# Set minimum CMake version +cmake_minimum_required(VERSION 3.22) + +# Declare project project(engine) -set(ENGINE_SOURCE_DIR src) -set(ENGINE_INCLUDE_DIR include) +# Define variables of source files and include directories +set(CORE_INCLUDES core) +set(INPUT_INCLUDES core/input) +file(GLOB_RECURSE CORE_SOURCES "${CORE_INCLUDES}/*.cpp") +file(GLOB_RECURSE INPUT_SOURCES "${INPUT_INCLUDES}/*.cpp") -file(GLOB_RECURSE ENGINE_SOURCES ${ENGINE_SOURCE_DIR}/*.cpp) +set(RENDERING_INCLUDES rendering) +file(GLOB_RECURSE RENDERING_SOURCES "${RENDERING_INCLUDES}/*.cpp") +set(SCENE_INCLUDES scene) +set(COMPONENTS_INCLUDES scene/components) +file(GLOB SCENE_SOURCES "${SCENE_INCLUDES}/*.cpp") +file(GLOB COMPONENTS_SOURCES "${COMPONENTS_INCLUDES}/*.cpp") + +set(UI_INCLUDES ui) +file(GLOB UI_SOURCES "${UI_INCLUDES}/*.cpp") + +# Build engine as library add_library(${PROJECT_NAME}) +# Add resources folder if(CMAKE_BUILD_TYPE STREQUAL "Release") target_compile_definitions(${PROJECT_NAME} PUBLIC RESOURCES_PATH="./resources/") else() target_compile_definitions(${PROJECT_NAME} PUBLIC RESOURCES_PATH="${CMAKE_SOURCE_DIR}/editor/resources/") endif() -target_sources(${PROJECT_NAME} PRIVATE ${ENGINE_SOURCES}) -target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR}) - +# Link sources, include directories, and third party libraries +target_include_directories(${PROJECT_NAME} PUBLIC + ${CORE_INCLUDES} ${INPUT_INCLUDES} + ${RENDERING_INCLUDES} + ${SCENE_INCLUDES} ${COMPONENTS_INCLUDES} + ${UI_INCLUDES}) +target_sources(${PROJECT_NAME} PRIVATE + ${CORE_SOURCES} ${INPUT_SOURCES} + ${RENDERING_SOURCES} + ${SCENE_SOURCES} ${COMPONENTS_SOURCES} + ${UI_SOURCES}) target_link_libraries(${PROJECT_NAME} glfw glad glm imgui stb) diff --git a/engine/src/Engine.cpp b/engine/core/Engine.cpp similarity index 74% rename from engine/src/Engine.cpp rename to engine/core/Engine.cpp index 08acc3a..4e9a64d 100644 --- a/engine/src/Engine.cpp +++ b/engine/core/Engine.cpp @@ -18,9 +18,7 @@ Engine::~Engine() void Engine::Run() { - while (!glfwWindowShouldClose(m_Window->GetWindow())) { - Renderer::Render(); - } + Renderer::Render(); } void Engine::Shutdown() diff --git a/engine/include/Engine.h b/engine/core/Engine.h similarity index 70% rename from engine/include/Engine.h rename to engine/core/Engine.h index b5d6341..47acd79 100644 --- a/engine/include/Engine.h +++ b/engine/core/Engine.h @@ -4,7 +4,6 @@ #include #include "Renderer.h" #include "Window.h" -#include "UI.h" class Engine { @@ -12,9 +11,9 @@ public: Engine(); ~Engine(); - Window& GetWindow() const { return *m_Window; } + Window* GetWindow() const { return m_Window; } - static Engine& Get() { return *s_Instance; } + static Engine* Get() { return s_Instance; } void Run(); void Render(); diff --git a/engine/src/Window.cpp b/engine/core/Window.cpp similarity index 96% rename from engine/src/Window.cpp rename to engine/core/Window.cpp index 56769ce..0cb5c67 100644 --- a/engine/src/Window.cpp +++ b/engine/core/Window.cpp @@ -49,7 +49,7 @@ Window Window::Create(const std::string& title, int width, int height) return Window{title, width, height}; } -GLFWwindow* Window::GetWindow() const +GLFWwindow* Window::GetNativeWindow() const { return m_Window; } diff --git a/engine/include/Window.h b/engine/core/Window.h similarity index 95% rename from engine/include/Window.h rename to engine/core/Window.h index 10b6aca..0eade8f 100644 --- a/engine/include/Window.h +++ b/engine/core/Window.h @@ -35,7 +35,7 @@ public: void Init(); - GLFWwindow* GetWindow() const; + GLFWwindow* GetNativeWindow() const; const std::string& GetTitle() const; WindowSize GetSize(); diff --git a/engine/src/Input.cpp b/engine/core/input/Input.cpp similarity index 59% rename from engine/src/Input.cpp rename to engine/core/input/Input.cpp index 34c6702..3ce9271 100644 --- a/engine/src/Input.cpp +++ b/engine/core/input/Input.cpp @@ -3,18 +3,18 @@ bool Input::IsKeyPressed(const KeyCode key) { - return glfwGetKey(Engine::Get().GetWindow().GetWindow(), key) == GLFW_PRESS; + return glfwGetKey(Engine::Get()->GetWindow()->GetNativeWindow(), key) == GLFW_PRESS; } bool Input::IsMouseButtonPressed(const MouseCode button) { - return glfwGetMouseButton(Engine::Get().GetWindow().GetWindow(), button) == GLFW_PRESS; + return glfwGetMouseButton(Engine::Get()->GetWindow()->GetNativeWindow(), button) == GLFW_PRESS; } glm::vec2 Input::GetMousePosition() { double xPos, yPos; - glfwGetCursorPos(Engine::Get().GetWindow().GetWindow(), &xPos, &yPos); + glfwGetCursorPos(Engine::Get()->GetWindow()->GetNativeWindow(), &xPos, &yPos); return { static_cast(xPos), static_cast(yPos) }; } diff --git a/engine/include/Input.h b/engine/core/input/Input.h similarity index 100% rename from engine/include/Input.h rename to engine/core/input/Input.h diff --git a/engine/include/KeyCodes.h b/engine/core/input/KeyCodes.h similarity index 100% rename from engine/include/KeyCodes.h rename to engine/core/input/KeyCodes.h diff --git a/engine/include/MouseCodes.h b/engine/core/input/MouseCodes.h similarity index 100% rename from engine/include/MouseCodes.h rename to engine/core/input/MouseCodes.h diff --git a/engine/src/FrameBuffer.cpp b/engine/rendering/FrameBuffer.cpp similarity index 100% rename from engine/src/FrameBuffer.cpp rename to engine/rendering/FrameBuffer.cpp diff --git a/engine/include/FrameBuffer.h b/engine/rendering/FrameBuffer.h similarity index 100% rename from engine/include/FrameBuffer.h rename to engine/rendering/FrameBuffer.h diff --git a/engine/src/IndexBuffer.cpp b/engine/rendering/IndexBuffer.cpp similarity index 100% rename from engine/src/IndexBuffer.cpp rename to engine/rendering/IndexBuffer.cpp diff --git a/engine/include/IndexBuffer.h b/engine/rendering/IndexBuffer.h similarity index 100% rename from engine/include/IndexBuffer.h rename to engine/rendering/IndexBuffer.h diff --git a/engine/src/Renderer.cpp b/engine/rendering/Renderer.cpp similarity index 54% rename from engine/src/Renderer.cpp rename to engine/rendering/Renderer.cpp index e986121..c11958a 100644 --- a/engine/src/Renderer.cpp +++ b/engine/rendering/Renderer.cpp @@ -3,32 +3,12 @@ RendererData Renderer::s_Data; -float Renderer::deltaTime = 0.0f; -float Renderer::lastFrame = 0.0f; +float Renderer::s_DeltaTime = 0.0f; +float Renderer::s_LastFrame = 0.0f; -bool Renderer::firstMouse = true; -float Renderer::lastX; -float Renderer::lastY; - -float vertices[] = { - -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, - 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, - 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, - -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, - -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, - 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f, - 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, - -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, -}; - -unsigned int indices[] = { - 0, 1, 2, 2, 3, 0, - 4, 5, 6, 6, 7, 4, - 0, 1, 5, 5, 4, 0, - 2, 3, 7, 7, 6, 2, - 0, 3, 7, 7, 4, 0, - 1, 2, 6, 6, 5, 1 -}; +bool Renderer::s_FirstMouse = true; +float Renderer::s_LastX; +float Renderer::s_LastY; Renderer::Renderer() = default; @@ -44,15 +24,23 @@ void Renderer::Init() return; } - UI::Init(Engine::Get().GetWindow().GetWindow()); - s_Data.m_Camera = new Camera(glm::vec3(0.0f, 0.0f, 3.0f)); - + SetVariables(); LoadShaders(); SetupBuffers(); SetCallbacks(); } -RendererData Renderer::GetData() +void Renderer::SetVariables() +{ + s_Data.m_Scene = new Scene(); + s_Data.m_Camera = new Camera(glm::vec3(0.0f, 0.0f, 3.0f)); + s_Data.m_Cube = new Cube("Cube"); + s_Data.m_ClearColor = new glm::vec3(0.0f, 0.1f, 0.2f); + + s_Data.m_Scene->AddCube(std::shared_ptr(s_Data.m_Cube)); +} + +RendererData& Renderer::GetData() { return s_Data; } @@ -70,8 +58,8 @@ void Renderer::SetupBuffers() s_Data.m_VAO->Bind(); - s_Data.m_VBO->SetData(sizeof(vertices), vertices); - s_Data.m_IBO->SetData(sizeof(indices), indices); + s_Data.m_VBO->SetData(sizeof(float) * Cube::GetVertices().size(), Cube::GetVertices().data()); + s_Data.m_IBO->SetData(sizeof(unsigned int) * Cube::GetIndices().size(), Cube::GetIndices().data()); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), nullptr); glEnableVertexAttribArray(0); @@ -84,22 +72,22 @@ void Renderer::SetupBuffers() IndexBuffer::Unbind(); s_Data.m_FBO = new FrameBuffer(); - WindowSize windowSize = Engine::Get().GetWindow().GetSize(); + WindowSize windowSize = Engine::Get()->GetWindow()->GetSize(); s_Data.m_FBO->AttachTexture(windowSize.Width, windowSize.Height); FrameBuffer::Unbind(); } void Renderer::SetCallbacks() { - glfwSetWindowSizeCallback(Engine::Get().GetWindow().GetWindow(), [](GLFWwindow* window, int width, int height) + glfwSetWindowSizeCallback(Engine::Get()->GetWindow()->GetNativeWindow(), [](GLFWwindow* window, int width, int height) { SetupBuffers(); }); - glfwSetFramebufferSizeCallback(Engine::Get().GetWindow().GetWindow(), [](GLFWwindow* window, int width, int height) + glfwSetFramebufferSizeCallback(Engine::Get()->GetWindow()->GetNativeWindow(), [](GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }); - glfwSetScrollCallback(Engine::Get().GetWindow().GetWindow(), [](GLFWwindow* window, double xOffset, double yOffset) + glfwSetScrollCallback(Engine::Get()->GetWindow()->GetNativeWindow(), [](GLFWwindow* window, double xOffset, double yOffset) { s_Data.m_Camera->ProcessMouseScroll(static_cast(yOffset)); }); @@ -107,37 +95,35 @@ void Renderer::SetCallbacks() void Renderer::Render() { auto currentFrame = static_cast(glfwGetTime()); - deltaTime = currentFrame - lastFrame; - lastFrame = currentFrame; + s_DeltaTime = currentFrame - s_LastFrame; + s_LastFrame = currentFrame; s_Data.m_FBO->Bind(); glfwPollEvents(); - ProcessInput(Engine::Get().GetWindow().GetWindow()); + ProcessInput(Engine::Get()->GetWindow()->GetNativeWindow()); glEnable(GL_DEPTH_TEST); - glm::vec3 color = UI::GetData().m_BgColor; - glClearColor(color.x, color.y, color.z, 1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - UI::Run(); + glClearColor(s_Data.m_ClearColor->x, s_Data.m_ClearColor->y, s_Data.m_ClearColor->z, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); s_Data.m_Shader->Use(); - auto model = glm::mat4(1.0f); - model = translate(model, UI::GetData().m_Position); - if(length(UI::GetData().m_Rotation) != 0) - model = rotate(model, glm::radians(length(UI::GetData().m_Rotation)), normalize(UI::GetData().m_Rotation)); - model = scale(model, UI::GetData().m_Scale); - glm::mat4 view = s_Data.m_Camera->GetViewMatrix(); + s_Data.m_Cube->Draw(); - WindowSize size = Engine::Get().GetWindow().GetSize(); + const glm::mat4& model = *s_Data.m_Cube->GetModelMatrix(); + const glm::mat4& view = s_Data.m_Camera->GetViewMatrix(); + + WindowSize size = Engine::Get()->GetWindow()->GetSize(); glm::mat4 projection = glm::perspective(glm::radians(45.0f), static_cast(size.Width) / static_cast(size.Height), 0.1f, 100.0f); glUniformMatrix4fv(s_Data.m_Shader->GetUniformLocation("model"), 1, GL_FALSE, glm::value_ptr(model)); glUniformMatrix4fv(s_Data.m_Shader->GetUniformLocation("view"), 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(s_Data.m_Shader->GetUniformLocation("projection"), 1, GL_FALSE, glm::value_ptr(projection)); - glUniform3fv(s_Data.m_Shader->GetUniformLocation("color"), 1, glm::value_ptr(UI::GetData().m_ShaderColor)); + glUniform3fv(s_Data.m_Shader->GetUniformLocation("color"), 1, glm::value_ptr(*s_Data.m_Cube->GetShaderColor())); + + *s_Data.m_Cube->GetModelMatrix() = glm::mat4(1.0f); s_Data.m_VAO->Bind(); @@ -145,9 +131,7 @@ void Renderer::Render() { glBindFramebuffer(GL_FRAMEBUFFER, 0); - UI::Render(*s_Data.m_FBO); - - glfwSwapBuffers(Engine::Get().GetWindow().GetWindow()); + glfwSwapBuffers(Engine::Get()->GetWindow()->GetNativeWindow()); } void Renderer::ProcessInput(GLFWwindow *window) @@ -156,18 +140,18 @@ void Renderer::ProcessInput(GLFWwindow *window) { auto mousePos = Input::GetMousePosition(); - if(firstMouse) + if(s_FirstMouse) { - lastX = mousePos.x; - lastY = mousePos.y; - firstMouse = false; + s_LastX = mousePos.x; + s_LastY = mousePos.y; + s_FirstMouse = false; } - float xOffset = mousePos.x - lastX; - float yOffset = lastY - mousePos.y; + float xOffset = mousePos.x - s_LastX; + float yOffset = s_LastY - mousePos.y; - lastX = mousePos.x; - lastY = mousePos.y; + s_LastX = mousePos.x; + s_LastY = mousePos.y; s_Data.m_Camera->ProcessMouseMovement(xOffset, yOffset); @@ -179,17 +163,17 @@ void Renderer::ProcessInput(GLFWwindow *window) } if (Input::IsKeyPressed(W)) - s_Data.m_Camera->ProcessKeyboard(FORWARD, deltaTime); + s_Data.m_Camera->ProcessKeyboard(FORWARD, s_DeltaTime); if (Input::IsKeyPressed(S)) - s_Data.m_Camera->ProcessKeyboard(BACKWARD, deltaTime); + s_Data.m_Camera->ProcessKeyboard(BACKWARD, s_DeltaTime); if (Input::IsKeyPressed(A)) - s_Data.m_Camera->ProcessKeyboard(LEFT, deltaTime); + s_Data.m_Camera->ProcessKeyboard(LEFT, s_DeltaTime); if (Input::IsKeyPressed(D)) - s_Data.m_Camera->ProcessKeyboard(RIGHT, deltaTime); + s_Data.m_Camera->ProcessKeyboard(RIGHT, s_DeltaTime); if (Input::IsKeyPressed(Q)) - s_Data.m_Camera->ProcessKeyboard(DOWN, deltaTime); + s_Data.m_Camera->ProcessKeyboard(DOWN, s_DeltaTime); if (Input::IsKeyPressed(E)) - s_Data.m_Camera->ProcessKeyboard(UP, deltaTime); + s_Data.m_Camera->ProcessKeyboard(UP, s_DeltaTime); } void Renderer::Shutdown() diff --git a/engine/include/Renderer.h b/engine/rendering/Renderer.h similarity index 71% rename from engine/include/Renderer.h rename to engine/rendering/Renderer.h index dd91411..124cd21 100644 --- a/engine/include/Renderer.h +++ b/engine/rendering/Renderer.h @@ -9,10 +9,11 @@ #include "FrameBuffer.h" #include "Window.h" #include "Input.h" +#include "Scene.h" #include "Shader.h" #include "Texture.h" -#include "UI.h" #include "Camera.h" +#include "Cube.h" struct RendererData { @@ -20,8 +21,12 @@ struct RendererData VertexBuffer* m_VBO; IndexBuffer* m_IBO; FrameBuffer* m_FBO; - Camera* m_Camera; + Scene* m_Scene; Shader* m_Shader; + Camera* m_Camera; + Cube* m_Cube; + + glm::vec3* m_ClearColor; }; class Renderer @@ -34,20 +39,21 @@ public: static void Render(); static void Shutdown(); - static RendererData GetData(); + static RendererData& GetData(); private: static RendererData s_Data; + static void SetVariables(); static void LoadShaders(); static void SetupBuffers(); static void SetCallbacks(); static void ProcessInput(GLFWwindow* window); - static float deltaTime; - static float lastFrame; + static float s_DeltaTime; + static float s_LastFrame; - static bool firstMouse; - static float lastX; - static float lastY; + static bool s_FirstMouse; + static float s_LastX; + static float s_LastY; }; \ No newline at end of file diff --git a/engine/src/Shader.cpp b/engine/rendering/Shader.cpp similarity index 100% rename from engine/src/Shader.cpp rename to engine/rendering/Shader.cpp diff --git a/engine/include/Shader.h b/engine/rendering/Shader.h similarity index 100% rename from engine/include/Shader.h rename to engine/rendering/Shader.h diff --git a/engine/src/Texture.cpp b/engine/rendering/Texture.cpp similarity index 95% rename from engine/src/Texture.cpp rename to engine/rendering/Texture.cpp index ee5e3f3..118305c 100644 --- a/engine/src/Texture.cpp +++ b/engine/rendering/Texture.cpp @@ -1,4 +1,3 @@ -#include "UI.h" #include "Texture.h" Texture::Texture() @@ -43,7 +42,7 @@ void Texture::GenerateFromImage(const std::string& path) } else { - UI::Print("Failed to load texture: " + path); + std::cerr << "Failed to load texture: " << path; } stbi_image_free(m_Data); diff --git a/engine/include/Texture.h b/engine/rendering/Texture.h similarity index 97% rename from engine/include/Texture.h rename to engine/rendering/Texture.h index 875abb4..0f63bf1 100644 --- a/engine/include/Texture.h +++ b/engine/rendering/Texture.h @@ -2,6 +2,7 @@ #include #include +#include #include class Texture diff --git a/engine/src/VertexArray.cpp b/engine/rendering/VertexArray.cpp similarity index 100% rename from engine/src/VertexArray.cpp rename to engine/rendering/VertexArray.cpp diff --git a/engine/include/VertexArray.h b/engine/rendering/VertexArray.h similarity index 100% rename from engine/include/VertexArray.h rename to engine/rendering/VertexArray.h diff --git a/engine/src/VertexBuffer.cpp b/engine/rendering/VertexBuffer.cpp similarity index 100% rename from engine/src/VertexBuffer.cpp rename to engine/rendering/VertexBuffer.cpp diff --git a/engine/include/VertexBuffer.h b/engine/rendering/VertexBuffer.h similarity index 100% rename from engine/include/VertexBuffer.h rename to engine/rendering/VertexBuffer.h diff --git a/engine/scene/Scene.cpp b/engine/scene/Scene.cpp new file mode 100644 index 0000000..c80e4e9 --- /dev/null +++ b/engine/scene/Scene.cpp @@ -0,0 +1,25 @@ +#include "Scene.h" + +void Scene::AddCube(const std::shared_ptr& cube) +{ + cubes.push_back(cube); +} + +std::shared_ptr Scene::GetCubeByName(const std::string& name) +{ + for (const auto& cube : cubes) + { + if(cube->name == name) + { + return cube; + } + } + return nullptr; +} + +const std::vector>& Scene::GetCubes() const +{ + return cubes; +} + + diff --git a/engine/scene/Scene.h b/engine/scene/Scene.h new file mode 100644 index 0000000..9eea8ff --- /dev/null +++ b/engine/scene/Scene.h @@ -0,0 +1,18 @@ +#pragma once + +#include +#include +#include "Cube.h" + +class Scene { +public: + Scene()=default; + ~Scene(); + + void AddCube(const std::shared_ptr& cube); + std::shared_ptr GetCubeByName(const std::string& name); + const std::vector>& GetCubes() const; + +private: + std::vector> cubes; // TODO: Change into entities +}; \ No newline at end of file diff --git a/engine/src/Camera.cpp b/engine/scene/components/Camera.cpp similarity index 100% rename from engine/src/Camera.cpp rename to engine/scene/components/Camera.cpp diff --git a/engine/include/Camera.h b/engine/scene/components/Camera.h similarity index 100% rename from engine/include/Camera.h rename to engine/scene/components/Camera.h diff --git a/engine/scene/components/Cube.cpp b/engine/scene/components/Cube.cpp new file mode 100644 index 0000000..5e9867d --- /dev/null +++ b/engine/scene/components/Cube.cpp @@ -0,0 +1,35 @@ +#include "Cube.h" + +std::vector Cube::s_Vertices = { + -0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, + 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f, + 0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, + -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, + -0.5f, -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, + 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 1.0f, + 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 1.0f, + -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f +}; + +std::vector Cube::s_Indices = { + 0, 1, 2, 2, 3, 0, + 4, 5, 6, 6, 7, 4, + 0, 1, 5, 5, 4, 0, + 2, 3, 7, 7, 6, 2, + 0, 3, 7, 7, 4, 0, + 1, 2, 6, 6, 5, 1 +}; + +Cube::Cube(const std::string& cubeName) +{ + name = cubeName; +} + +void Cube::Draw() const +{ + *m_ModelMatrix = translate(*m_ModelMatrix, *m_Position); + if(length(*m_Rotation) != 0) + *m_ModelMatrix = rotate(*m_ModelMatrix, glm::radians(length(*m_Rotation)), normalize(*m_Rotation)); + *m_ModelMatrix = scale(*m_ModelMatrix, *m_Scale); +} + diff --git a/engine/scene/components/Cube.h b/engine/scene/components/Cube.h new file mode 100644 index 0000000..ef7ad52 --- /dev/null +++ b/engine/scene/components/Cube.h @@ -0,0 +1,41 @@ +#pragma once + +#include +#include +#include + +#include + +class Cube{ +public: + Cube(const std::string& cubeName); + + void Draw() const; + + void SetPosition(const glm::vec3& newPosition){ *m_Position = newPosition; } + glm::vec3* GetPosition() const{ return m_Position; } + + void SetRotation(const glm::vec3& newRotation){ *m_Rotation = newRotation; } + glm::vec3* GetRotation() const{ return m_Rotation; } + + void SetScale(const glm::vec3& newScale){ *m_Scale = newScale; } + glm::vec3* GetScale() const{ return m_Scale; } + + glm::mat4* GetModelMatrix() const{ return m_ModelMatrix; } + glm::vec3* GetShaderColor() const{ return m_ShaderColor; } + static std::vector& GetVertices() { return s_Vertices; } + static std::vector& GetIndices() { return s_Indices; } + + std::string name; + +private: + static std::vector s_Vertices; + static std::vector s_Indices; + + glm::vec3* m_Position = new glm::vec3{0.0f, 0.0f, 0.0f}; + glm::vec3* m_Rotation = new glm::vec3{0.0f, 0.0f, 0.0f}; + glm::vec3* m_Scale = new glm::vec3{1.0f, 1.0f, 1.0f}; + + glm::mat4* m_ModelMatrix = new glm::mat4(1.0f); + glm::vec3* m_ShaderColor = new glm::vec3(1.0f, 1.0f, 1.0f); +};