This commit is contained in:
Huseyn Ismayilov 2024-11-22 17:48:22 +04:00
commit 5ad68f110e
35 changed files with 316 additions and 176 deletions

View File

@ -1,3 +1,4 @@
# Set minimum CMake version
cmake_minimum_required(VERSION 3.22) cmake_minimum_required(VERSION 3.22)
# Declare project # Declare project
@ -10,7 +11,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Define variables of directory paths # Define variables of directory paths
set(THIRDPARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty) set(THIRDPARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty)
# Add thirdparty libraries # Add third party libraries
add_subdirectory(${THIRDPARTY_DIR}/glad) add_subdirectory(${THIRDPARTY_DIR}/glad)
add_subdirectory(${THIRDPARTY_DIR}/glfw) add_subdirectory(${THIRDPARTY_DIR}/glfw)
add_subdirectory(${THIRDPARTY_DIR}/glm) add_subdirectory(${THIRDPARTY_DIR}/glm)

View File

@ -2,7 +2,7 @@
#define GLFW_INCLUDE_NONE #define GLFW_INCLUDE_NONE
#include "Engine.h" #include "Engine.h"
#include "UI.h" #include "GUI.h"
class Editor class Editor
{ {
@ -14,6 +14,10 @@ public:
static void Run(); static void Run();
static void Shutdown(); static void Shutdown();
static Editor* GetInstance();
static Engine* GetEngine();
private: private:
void InitializeImGui(); static Editor* s_Instance;
static Engine* s_Engine;
}; };

View File

@ -7,20 +7,11 @@
#include <string> #include <string>
#include <IconsFontAwesome6.h> #include <IconsFontAwesome6.h>
struct InspectorData class GUI
{
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
{ {
public: public:
UI(); GUI();
~UI(); ~GUI();
static void Init(GLFWwindow* window); static void Init(GLFWwindow* window);
static void LoadConfigs(); static void LoadConfigs();
@ -29,7 +20,6 @@ public:
static void Shutdown(); static void Shutdown();
static void Print(const std::string& message); static void Print(const std::string& message);
static InspectorData GetData();
static void ShowMenu(); static void ShowMenu();
static void ShowEntities(); static void ShowEntities();
@ -39,8 +29,6 @@ public:
static void ShowProperties(); static void ShowProperties();
private: private:
static InspectorData s_Data; static std::string s_Log;
static ImVec4* s_StyleColors;
static std::string m_Log;
static ImVec4* m_StyleColors;
}; };

View File

@ -1,6 +1,12 @@
#include "Editor.h" #include "Editor.h"
Editor::Editor()= default; Editor* Editor::s_Instance = nullptr;
Engine* Editor::s_Engine = nullptr;
Editor::Editor()
{
s_Instance = this;
}
Editor::~Editor() Editor::~Editor()
{ {
@ -9,14 +15,22 @@ Editor::~Editor()
void Editor::Init() 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(){
} }

View File

@ -1,19 +1,19 @@
#include <imgui.h> #include <imgui.h>
#include <imgui_impl_glfw.h> #include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h> #include <imgui_impl_opengl3.h>
#include "UI.h" #include "GUI.h"
#include "FrameBuffer.h" #include "FrameBuffer.h"
#include "Window.h" #include "Window.h"
InspectorData UI::s_Data; std::string GUI::s_Log;
std::string UI::m_Log; ImVec4* GUI::s_StyleColors;
ImVec4* UI::m_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_CHECKVERSION();
ImGui::CreateContext(); ImGui::CreateContext();
@ -25,7 +25,7 @@ void UI::Init(GLFWwindow* window)
ImGui_ImplOpenGL3_Init(); ImGui_ImplOpenGL3_Init();
} }
void UI::LoadConfigs() void GUI::LoadConfigs()
{ {
ImGuiIO& io = ImGui::GetIO(); (void)io; ImGuiIO& io = ImGui::GetIO(); (void)io;
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard;
@ -46,7 +46,7 @@ void UI::LoadConfigs()
ImGui::StyleColorsDark(); ImGui::StyleColorsDark();
ImGuiStyle* style = &ImGui::GetStyle(); ImGuiStyle* style = &ImGui::GetStyle();
m_StyleColors = style->Colors; s_StyleColors = style->Colors;
style->WindowMenuButtonPosition = ImGuiDir_None; style->WindowMenuButtonPosition = ImGuiDir_None;
style->WindowBorderSize = 0.0f; style->WindowBorderSize = 0.0f;
@ -60,47 +60,44 @@ void UI::LoadConfigs()
style->SeparatorTextPadding = ImVec2(5.0f, 5.0f); style->SeparatorTextPadding = ImVec2(5.0f, 5.0f);
style->TabBarBorderSize = 2.0f; style->TabBarBorderSize = 2.0f;
m_StyleColors[ImGuiCol_WindowBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.0f); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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); s_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_TextSelectedBg] = ImVec4(0.08f, 0.42f, 0.14f, 0.35f);
} }
InspectorData UI::GetData() void GUI::Run()
{
return s_Data;
}
void UI::Run()
{ {
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame(); ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame(); ImGui::NewFrame();
ImGui::DockSpaceOverViewport(ImGui::GetMainViewport()); 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(); ShowConsole();
ShowEntities(); ShowEntities();
@ -113,23 +110,23 @@ void UI::Render(const FrameBuffer& sceneBuffer)
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
} }
void UI::Shutdown() void GUI::Shutdown()
{ {
ImGui_ImplOpenGL3_Shutdown(); ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown(); ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext(); 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"); ImGui::Begin(ICON_FA_TERMINAL" Console");
if(ImGui::Button("Clear")){ if(ImGui::Button("Clear")){
m_Log.clear(); s_Log.clear();
} }
ImGui::SameLine(); 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("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(); ImGui::End();
} }
void UI::ShowEntities() void GUI::ShowEntities()
{ {
ImGui::Begin(ICON_FA_CUBE" Entities"); ImGui::Begin(ICON_FA_CUBE" Entities");
@ -154,16 +151,19 @@ void UI::ShowEntities()
ImGui::End(); ImGui::End();
} }
void UI::ShowProperties() void GUI::ShowProperties()
{ {
ImGui::Begin(ICON_FA_BARS_STAGGERED" Properties"); ImGui::Begin(ICON_FA_BARS_STAGGERED" Properties");
if(ImGui::CollapsingHeader("Transform")) if(ImGui::CollapsingHeader("Transform"))
{ {
ImGui::BeginGroup(); ImGui::BeginGroup();
ImGui::DragFloat3("Position", glm::value_ptr(s_Data.m_Position), 0.2f); glm::vec3* position = Renderer::GetData().m_Cube->GetPosition();
ImGui::DragFloat3("Rotation", glm::value_ptr(s_Data.m_Rotation), 0.4f); glm::vec3* rotation = Renderer::GetData().m_Cube->GetRotation();
ImGui::DragFloat3("Scale", glm::value_ptr(s_Data.m_Scale), 0.1f); 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(); ImGui::EndGroup();
} }
@ -171,14 +171,15 @@ void UI::ShowProperties()
ImGui::BeginGroup(); ImGui::BeginGroup();
ImGui::Text("Colors"); ImGui::Text("Colors");
ImGui::ColorEdit3("Shader Color", glm::value_ptr(s_Data.m_ShaderColor)); glm::vec3* shaderColor = Renderer::GetData().m_Cube->GetShaderColor();
ImGui::ColorEdit3("Background Color", glm::value_ptr(s_Data.m_BgColor)); ImGui::ColorEdit3("Shader Color", glm::value_ptr(*shaderColor));
ImGui::ColorEdit3("Background Color", glm::value_ptr(*Renderer::GetData().m_ClearColor));
ImGui::EndGroup(); ImGui::EndGroup();
ImGui::End(); ImGui::End();
} }
void UI::ShowMenu() void GUI::ShowMenu()
{ {
if(ImGui::BeginMainMenuBar()){ if(ImGui::BeginMainMenuBar()){
if(ImGui::BeginMenu("File")){ if(ImGui::BeginMenu("File")){
@ -191,14 +192,14 @@ void UI::ShowMenu()
} }
} }
void UI::ShowFiles() void GUI::ShowFiles()
{ {
ImGui::Begin(ICON_FA_FOLDER" Files"); ImGui::Begin(ICON_FA_FOLDER" Files");
ImGui::End(); ImGui::End();
} }
void UI::ShowScene(const FrameBuffer& sceneBuffer) void GUI::ShowScene(const FrameBuffer& sceneBuffer)
{ {
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{0, 0}); ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2{0, 0});
ImGui::Begin(ICON_FA_CLAPPERBOARD" Scene"); ImGui::Begin(ICON_FA_CLAPPERBOARD" Scene");

View File

@ -1,19 +1,45 @@
# Set minimum CMake version
cmake_minimum_required(VERSION 3.22)
# Declare project
project(engine) project(engine)
set(ENGINE_SOURCE_DIR src) # Define variables of source files and include directories
set(ENGINE_INCLUDE_DIR include) 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_library(${PROJECT_NAME})
# Add resources folder
if(CMAKE_BUILD_TYPE STREQUAL "Release") if(CMAKE_BUILD_TYPE STREQUAL "Release")
target_compile_definitions(${PROJECT_NAME} PUBLIC RESOURCES_PATH="./resources/") target_compile_definitions(${PROJECT_NAME} PUBLIC RESOURCES_PATH="./resources/")
else() else()
target_compile_definitions(${PROJECT_NAME} PUBLIC RESOURCES_PATH="${CMAKE_SOURCE_DIR}/editor/resources/") target_compile_definitions(${PROJECT_NAME} PUBLIC RESOURCES_PATH="${CMAKE_SOURCE_DIR}/editor/resources/")
endif() endif()
target_sources(${PROJECT_NAME} PRIVATE ${ENGINE_SOURCES}) # Link sources, include directories, and third party libraries
target_include_directories(${PROJECT_NAME} PUBLIC ${ENGINE_INCLUDE_DIR}) 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) target_link_libraries(${PROJECT_NAME} glfw glad glm imgui stb)

View File

@ -18,9 +18,7 @@ Engine::~Engine()
void Engine::Run() void Engine::Run()
{ {
while (!glfwWindowShouldClose(m_Window->GetWindow())) { Renderer::Render();
Renderer::Render();
}
} }
void Engine::Shutdown() void Engine::Shutdown()

View File

@ -4,7 +4,6 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "Renderer.h" #include "Renderer.h"
#include "Window.h" #include "Window.h"
#include "UI.h"
class Engine class Engine
{ {
@ -12,9 +11,9 @@ public:
Engine(); Engine();
~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 Run();
void Render(); void Render();

View File

@ -49,7 +49,7 @@ Window Window::Create(const std::string& title, int width, int height)
return Window{title, width, height}; return Window{title, width, height};
} }
GLFWwindow* Window::GetWindow() const GLFWwindow* Window::GetNativeWindow() const
{ {
return m_Window; return m_Window;
} }

View File

@ -35,7 +35,7 @@ public:
void Init(); void Init();
GLFWwindow* GetWindow() const; GLFWwindow* GetNativeWindow() const;
const std::string& GetTitle() const; const std::string& GetTitle() const;
WindowSize GetSize(); WindowSize GetSize();

View File

@ -3,18 +3,18 @@
bool Input::IsKeyPressed(const KeyCode key) 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) 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() glm::vec2 Input::GetMousePosition()
{ {
double xPos, yPos; double xPos, yPos;
glfwGetCursorPos(Engine::Get().GetWindow().GetWindow(), &xPos, &yPos); glfwGetCursorPos(Engine::Get()->GetWindow()->GetNativeWindow(), &xPos, &yPos);
return { static_cast<float>(xPos), static_cast<float>(yPos) }; return { static_cast<float>(xPos), static_cast<float>(yPos) };
} }

View File

@ -3,32 +3,12 @@
RendererData Renderer::s_Data; RendererData Renderer::s_Data;
float Renderer::deltaTime = 0.0f; float Renderer::s_DeltaTime = 0.0f;
float Renderer::lastFrame = 0.0f; float Renderer::s_LastFrame = 0.0f;
bool Renderer::firstMouse = true; bool Renderer::s_FirstMouse = true;
float Renderer::lastX; float Renderer::s_LastX;
float Renderer::lastY; float Renderer::s_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
};
Renderer::Renderer() = default; Renderer::Renderer() = default;
@ -44,15 +24,23 @@ void Renderer::Init()
return; return;
} }
UI::Init(Engine::Get().GetWindow().GetWindow()); SetVariables();
s_Data.m_Camera = new Camera(glm::vec3(0.0f, 0.0f, 3.0f));
LoadShaders(); LoadShaders();
SetupBuffers(); SetupBuffers();
SetCallbacks(); 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<Cube>(s_Data.m_Cube));
}
RendererData& Renderer::GetData()
{ {
return s_Data; return s_Data;
} }
@ -70,8 +58,8 @@ void Renderer::SetupBuffers()
s_Data.m_VAO->Bind(); s_Data.m_VAO->Bind();
s_Data.m_VBO->SetData(sizeof(vertices), vertices); s_Data.m_VBO->SetData(sizeof(float) * Cube::GetVertices().size(), Cube::GetVertices().data());
s_Data.m_IBO->SetData(sizeof(indices), indices); 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); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), nullptr);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
@ -84,22 +72,22 @@ void Renderer::SetupBuffers()
IndexBuffer::Unbind(); IndexBuffer::Unbind();
s_Data.m_FBO = new FrameBuffer(); 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); s_Data.m_FBO->AttachTexture(windowSize.Width, windowSize.Height);
FrameBuffer::Unbind(); FrameBuffer::Unbind();
} }
void Renderer::SetCallbacks() 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(); 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); 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<float>(yOffset)); s_Data.m_Camera->ProcessMouseScroll(static_cast<float>(yOffset));
}); });
@ -107,37 +95,35 @@ void Renderer::SetCallbacks()
void Renderer::Render() { void Renderer::Render() {
auto currentFrame = static_cast<float>(glfwGetTime()); auto currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame; s_DeltaTime = currentFrame - s_LastFrame;
lastFrame = currentFrame; s_LastFrame = currentFrame;
s_Data.m_FBO->Bind(); s_Data.m_FBO->Bind();
glfwPollEvents(); glfwPollEvents();
ProcessInput(Engine::Get().GetWindow().GetWindow()); ProcessInput(Engine::Get()->GetWindow()->GetNativeWindow());
glEnable(GL_DEPTH_TEST); 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(); s_Data.m_Shader->Use();
auto model = glm::mat4(1.0f); s_Data.m_Cube->Draw();
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();
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<float>(size.Width) / static_cast<float>(size.Height), 0.1f, 100.0f); glm::mat4 projection = glm::perspective(glm::radians(45.0f), static_cast<float>(size.Width) / static_cast<float>(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("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("view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(s_Data.m_Shader->GetUniformLocation("projection"), 1, GL_FALSE, glm::value_ptr(projection)); 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(); s_Data.m_VAO->Bind();
@ -145,9 +131,7 @@ void Renderer::Render() {
glBindFramebuffer(GL_FRAMEBUFFER, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0);
UI::Render(*s_Data.m_FBO); glfwSwapBuffers(Engine::Get()->GetWindow()->GetNativeWindow());
glfwSwapBuffers(Engine::Get().GetWindow().GetWindow());
} }
void Renderer::ProcessInput(GLFWwindow *window) void Renderer::ProcessInput(GLFWwindow *window)
@ -156,18 +140,18 @@ void Renderer::ProcessInput(GLFWwindow *window)
{ {
auto mousePos = Input::GetMousePosition(); auto mousePos = Input::GetMousePosition();
if(firstMouse) if(s_FirstMouse)
{ {
lastX = mousePos.x; s_LastX = mousePos.x;
lastY = mousePos.y; s_LastY = mousePos.y;
firstMouse = false; s_FirstMouse = false;
} }
float xOffset = mousePos.x - lastX; float xOffset = mousePos.x - s_LastX;
float yOffset = lastY - mousePos.y; float yOffset = s_LastY - mousePos.y;
lastX = mousePos.x; s_LastX = mousePos.x;
lastY = mousePos.y; s_LastY = mousePos.y;
s_Data.m_Camera->ProcessMouseMovement(xOffset, yOffset); s_Data.m_Camera->ProcessMouseMovement(xOffset, yOffset);
@ -179,17 +163,17 @@ void Renderer::ProcessInput(GLFWwindow *window)
} }
if (Input::IsKeyPressed(W)) if (Input::IsKeyPressed(W))
s_Data.m_Camera->ProcessKeyboard(FORWARD, deltaTime); s_Data.m_Camera->ProcessKeyboard(FORWARD, s_DeltaTime);
if (Input::IsKeyPressed(S)) if (Input::IsKeyPressed(S))
s_Data.m_Camera->ProcessKeyboard(BACKWARD, deltaTime); s_Data.m_Camera->ProcessKeyboard(BACKWARD, s_DeltaTime);
if (Input::IsKeyPressed(A)) if (Input::IsKeyPressed(A))
s_Data.m_Camera->ProcessKeyboard(LEFT, deltaTime); s_Data.m_Camera->ProcessKeyboard(LEFT, s_DeltaTime);
if (Input::IsKeyPressed(D)) if (Input::IsKeyPressed(D))
s_Data.m_Camera->ProcessKeyboard(RIGHT, deltaTime); s_Data.m_Camera->ProcessKeyboard(RIGHT, s_DeltaTime);
if (Input::IsKeyPressed(Q)) if (Input::IsKeyPressed(Q))
s_Data.m_Camera->ProcessKeyboard(DOWN, deltaTime); s_Data.m_Camera->ProcessKeyboard(DOWN, s_DeltaTime);
if (Input::IsKeyPressed(E)) if (Input::IsKeyPressed(E))
s_Data.m_Camera->ProcessKeyboard(UP, deltaTime); s_Data.m_Camera->ProcessKeyboard(UP, s_DeltaTime);
} }
void Renderer::Shutdown() void Renderer::Shutdown()

View File

@ -9,10 +9,11 @@
#include "FrameBuffer.h" #include "FrameBuffer.h"
#include "Window.h" #include "Window.h"
#include "Input.h" #include "Input.h"
#include "Scene.h"
#include "Shader.h" #include "Shader.h"
#include "Texture.h" #include "Texture.h"
#include "UI.h"
#include "Camera.h" #include "Camera.h"
#include "Cube.h"
struct RendererData struct RendererData
{ {
@ -20,8 +21,12 @@ struct RendererData
VertexBuffer* m_VBO; VertexBuffer* m_VBO;
IndexBuffer* m_IBO; IndexBuffer* m_IBO;
FrameBuffer* m_FBO; FrameBuffer* m_FBO;
Camera* m_Camera; Scene* m_Scene;
Shader* m_Shader; Shader* m_Shader;
Camera* m_Camera;
Cube* m_Cube;
glm::vec3* m_ClearColor;
}; };
class Renderer class Renderer
@ -34,20 +39,21 @@ public:
static void Render(); static void Render();
static void Shutdown(); static void Shutdown();
static RendererData GetData(); static RendererData& GetData();
private: private:
static RendererData s_Data; static RendererData s_Data;
static void SetVariables();
static void LoadShaders(); static void LoadShaders();
static void SetupBuffers(); static void SetupBuffers();
static void SetCallbacks(); static void SetCallbacks();
static void ProcessInput(GLFWwindow* window); static void ProcessInput(GLFWwindow* window);
static float deltaTime; static float s_DeltaTime;
static float lastFrame; static float s_LastFrame;
static bool firstMouse; static bool s_FirstMouse;
static float lastX; static float s_LastX;
static float lastY; static float s_LastY;
}; };

View File

@ -1,4 +1,3 @@
#include "UI.h"
#include "Texture.h" #include "Texture.h"
Texture::Texture() Texture::Texture()
@ -43,7 +42,7 @@ void Texture::GenerateFromImage(const std::string& path)
} }
else else
{ {
UI::Print("Failed to load texture: " + path); std::cerr << "Failed to load texture: " << path;
} }
stbi_image_free(m_Data); stbi_image_free(m_Data);

View File

@ -2,6 +2,7 @@
#include <glad/glad.h> #include <glad/glad.h>
#include <stb_image.h> #include <stb_image.h>
#include <iostream>
#include <string> #include <string>
class Texture class Texture

25
engine/scene/Scene.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "Scene.h"
void Scene::AddCube(const std::shared_ptr<Cube>& cube)
{
cubes.push_back(cube);
}
std::shared_ptr<Cube> Scene::GetCubeByName(const std::string& name)
{
for (const auto& cube : cubes)
{
if(cube->name == name)
{
return cube;
}
}
return nullptr;
}
const std::vector<std::shared_ptr<Cube>>& Scene::GetCubes() const
{
return cubes;
}

18
engine/scene/Scene.h Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include <memory>
#include <vector>
#include "Cube.h"
class Scene {
public:
Scene()=default;
~Scene();
void AddCube(const std::shared_ptr<Cube>& cube);
std::shared_ptr<Cube> GetCubeByName(const std::string& name);
const std::vector<std::shared_ptr<Cube>>& GetCubes() const;
private:
std::vector<std::shared_ptr<Cube>> cubes; // TODO: Change into entities
};

View File

@ -0,0 +1,35 @@
#include "Cube.h"
std::vector<float> 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<unsigned int> 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);
}

View File

@ -0,0 +1,41 @@
#pragma once
#include <string>
#include <glm/vec3.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/glm.hpp>
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<float>& GetVertices() { return s_Vertices; }
static std::vector<unsigned int>& GetIndices() { return s_Indices; }
std::string name;
private:
static std::vector<float> s_Vertices;
static std::vector<unsigned int> 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);
};