refactor: Seperate GUI from engine

This commit is contained in:
Huseyn Ismayilov 2024-11-22 04:37:06 +04:00
parent 44e6cc3c4e
commit ab49f1e533
18 changed files with 282 additions and 169 deletions

View File

@ -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;
};

View File

@ -7,20 +7,11 @@
#include <string>
#include <IconsFontAwesome6.h>
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;
};

View File

@ -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(){
}

View File

@ -1,19 +1,19 @@
#include <imgui.h>
#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl3.h>
#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");

View File

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

View File

@ -4,7 +4,6 @@
#include <GLFW/glfw3.h>
#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();

View File

@ -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;
}

View File

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

View File

@ -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<float>(xPos), static_cast<float>(yPos) };
}

View File

@ -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<Cube>(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<float>(yOffset));
});
@ -107,37 +95,35 @@ void Renderer::SetCallbacks()
void Renderer::Render() {
auto currentFrame = static_cast<float>(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<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("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()

View File

@ -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;
};

View File

@ -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);

View File

@ -2,6 +2,7 @@
#include <glad/glad.h>
#include <stb_image.h>
#include <iostream>
#include <string>
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);
};