From ae1c6aba2aed4ad73d62dc7de0ec4729ea99613e Mon Sep 17 00:00:00 2001 From: SpinnerX Date: Fri, 22 Nov 2024 12:43:47 -0800 Subject: [PATCH] Working camera component with the cube object, and added viewport to get a perfect cube rendering added into the renderer --- Editor/Editor/Editor.cpp | 3 +- .../Editor/EditorComponents/EditorCamera.cpp | 128 ------------------ .../Editor/EditorComponents/EditorCamera.hpp | 74 ---------- Editor/Editor/EditorScene.cpp | 50 +++++-- Editor/Editor/EditorScene.hpp | 6 + .../Components/SPComps/EditorCamera.hpp | 21 +++ engine3d/Core/Window.hpp | 2 + src/CMakeLists.txt | 2 + src/engine3d/Core/ApplicationInstance.cpp | 12 +- src/engine3d/Core/Renderer/Renderer.cpp | 43 ++++-- .../Components/SPComps/EditorCamera.cpp | 122 +++++++++++++++++ src/engine3d/Core/Window.cpp | 4 + 12 files changed, 235 insertions(+), 232 deletions(-) delete mode 100644 Editor/Editor/EditorComponents/EditorCamera.cpp delete mode 100644 Editor/Editor/EditorComponents/EditorCamera.hpp diff --git a/Editor/Editor/Editor.cpp b/Editor/Editor/Editor.cpp index 0605834..729f6a5 100644 --- a/Editor/Editor/Editor.cpp +++ b/Editor/Editor/Editor.cpp @@ -17,7 +17,8 @@ namespace engine3d{ void EditorApplication::OnApplicationUpdate(){ //! @note Handle Events. - // m_EditorScene->OnCameraUpdate(); + m_EditorScene->OnUpdate(); + // m_EditorScene->OnMoveCamUpdate(); auto& objects = m_EditorScene->GetSceneObjects(); Renderer::RecordSceneGameObjects(objects); } diff --git a/Editor/Editor/EditorComponents/EditorCamera.cpp b/Editor/Editor/EditorComponents/EditorCamera.cpp deleted file mode 100644 index 81fce9f..0000000 --- a/Editor/Editor/EditorComponents/EditorCamera.cpp +++ /dev/null @@ -1,128 +0,0 @@ -#include "EditorCamera.hpp" -#include "Event/InputPoll.hpp" -#include "TimeManagement/UpdateManagers/SyncUpdateManager.hpp" -#include -#include -#include - -#define GLM_ENABLE_EXPERIMENTAL -#include - -#include -#include -#include - -namespace engine3d{ - EditorCamera::EditorCamera(const std::string& p_Tag) : m_Tag(p_Tag) {} - - void EditorCamera::OnIntegrate(float p_Fov, float p_NearClip, float p_FarClip){ - m_Fov = p_Fov; - m_AspectRatio = ((float)ApplicationInstance::GetWindow().GetWidth() / (float)ApplicationInstance::GetWindow().GetHeight()); - m_NearClip = p_NearClip; - m_FarClip = p_FarClip; - - m_ViewportSize.x = (float)ApplicationInstance::GetWindow().GetWidth(); - m_ViewportSize.y = (float)ApplicationInstance::GetWindow().GetHeight(); - - RecalculateView(); - } - - void EditorCamera::OnUpdate(){ - if(InputPoll::IsKeyPressed(ENGINE_KEY_LEFT_ALT)){ - glm::vec2 delta = (InputPoll::GetMousePosition() - m_InitialMousePosition) * 0.003f; - m_InitialMousePosition = InputPoll::GetMousePosition(); - - if(InputPoll::IsMousePressed(Mouse::ButtonMiddle)){ - CameraPan(delta); - } - else if(InputPoll::IsMousePressed(Mouse::ButtonLast)){ - CameraRotate(delta); - } - else if(InputPoll::IsMousePressed(Mouse::ButtonRight)){ - CameraZoom(delta.y); - } - } - - RecalculateView(); - } - - glm::quat EditorCamera::GetOrientation() const { - return glm::quat(glm::vec3(-m_Pitch, -m_Yaw, 1.0f)); - } - - glm::vec3 EditorCamera::GetUpDirection() const{ - return glm::rotate(GetOrientation(), glm::vec3{0.0f, 1.0f, 0.0f}); - } - - glm::vec3 EditorCamera::GetRightDirection() const { - return glm::rotate(GetOrientation(), glm::vec3(1.0f, 0.0f, 0.0f)); - } - - glm::vec3 EditorCamera::GetForwardDirection() const { - return glm::rotate(GetOrientation(), glm::vec3(0.0f, 0.0f, -1.0f)); - } - - - //! @note Getting pans. - void EditorCamera::CameraPan(const glm::vec2& p_Data){} - - void EditorCamera::CameraZoom(float delta) { - // float dt = SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime; - m_Distance -= (delta * ZoomSpeed()); - - if(m_Distance < 1.0f){ - m_FocalPoint += GetForwardDirection(); - m_Distance = 1.0f; - } - } - - void EditorCamera::CameraRotate(const glm::vec2& p_Position) { - float yawSign = GetUpDirection().y < 0 ? -1.0f : 1.0f; - m_Yaw += yawSign * p_Position.x * RotationSpeed(); - m_Pitch += p_Position.y * RotationSpeed(); - } - - float EditorCamera::RotationSpeed() const { - return 0.8f; - } - - glm::vec3 EditorCamera::CalculateNewPosition() const{ - return m_FocalPoint - GetForwardDirection() * m_Distance; - } - - float EditorCamera::ZoomSpeed() const{ - float distance = m_Distance * 0.2f; - distance = std::max(distance, 0.0f); - - float speed = distance * distance; - speed = std::min(speed, 100.0f); - return speed; - } - - void EditorCamera::RecalculateProjection() { - m_ProjectionMatrix = glm::perspective(glm::radians(m_Fov), m_AspectRatio, m_NearClip, m_FarClip); - RecalculateView(); - } - - void EditorCamera::RecalculateView() { - m_Position = CalculateNewPosition(); - glm::quat orientation = GetOrientation(); - m_ViewMatrix = glm::translate(glm::mat4(1.0f), m_Position) * glm::toMat4(orientation); - m_ViewMatrix = glm::inverse(m_ViewMatrix); - } - - glm::vec3 EditorCamera::RecalculatePosition() const { - return m_FocalPoint * GetForwardDirection() * m_Distance; - - } - - glm::vec2 EditorCamera::PanSpeed() const{ - float x = std::min(m_ViewportSize.x / 1000.0f, 2.4f); // max = 2.4.f - float xFactor = 0.0366f * (x * x) - 0.1778f * x + 0.3021f; - - float y = std::min(m_ViewportSize.y / 1000.0f, 2.4f); // max = 2.4f - float yFactor = 0.0366f * (y * y) - 0.1778f * y + 0.3021f; - - return {xFactor, yFactor}; - } -}; \ No newline at end of file diff --git a/Editor/Editor/EditorComponents/EditorCamera.hpp b/Editor/Editor/EditorComponents/EditorCamera.hpp deleted file mode 100644 index 4934b9f..0000000 --- a/Editor/Editor/EditorComponents/EditorCamera.hpp +++ /dev/null @@ -1,74 +0,0 @@ -#pragma once - -#include -#include - -namespace engine3d{ - class EditorCamera : public GameComponent{ - public: - struct CameraData{ - float PerspectiveFOV = glm::radians(45.0f); - float PerspectiveNear = 0.01; - float PerspectiveFar = 1000.0f; - float OrthographicSize = 10.0f; - float OrthographicNear = 1.0f; - float OrthographicFar = 1.0f; - }; - - enum CameraProjectionType{ - UNDEFINED = -1, - ORTHOGRAPHIC = 0, - PERSPECTIVE = 1 - }; - - - EditorCamera() = default; - EditorCamera(const std::string& p_Tag); - - void OnUpdate(); - - //! @note Eventually we will refer to OnIntegrate as OnCreate or OnPlay or something. - //! @note Come up with a better name for this for accurate API. - void OnIntegrate(float p_Fov, float p_NearClip, float p_FarClip); - - glm::quat GetOrientation() const; - glm::vec3 GetUpDirection() const; - glm::vec3 GetRightDirection() const; - glm::vec3 GetForwardDirection() const; - - - //! @note Getting pans. - void CameraPan(const glm::vec2& p_Data); - void CameraZoom(float delta); - void CameraRotate(const glm::vec2& p_Data); - - private: - glm::vec3 CalculateNewPosition() const; - void RecalculateView(); - - void RecalculateProjection(); - - glm::vec3 RecalculatePosition() const; - - glm::vec2 PanSpeed() const; - float ZoomSpeed() const; - - float RotationSpeed() const; - - private: - std::string m_Tag; - glm::vec2 m_InitialMousePosition = {0.0f, 0.0f}; - float m_Fov = 45.0f, m_AspectRatio = 1.778f, m_NearClip=0.1f, m_FarClip = 1000.0f; - - glm::mat4 m_ViewMatrix; - glm::mat4 m_ProjectionMatrix; - glm::vec3 m_Position; - glm::vec3 m_FocalPoint; - //! @note Moving this out of Editor Camera. - // glm::vec2 m_InitialMousePosition - float m_Distance = 10.0f; - float m_Pitch = 0.f, m_Yaw = 0.f; - glm::vec2 m_ViewportSize; - - }; -}; \ No newline at end of file diff --git a/Editor/Editor/EditorScene.cpp b/Editor/Editor/EditorScene.cpp index c5db934..8691a68 100644 --- a/Editor/Editor/EditorScene.cpp +++ b/Editor/Editor/EditorScene.cpp @@ -2,17 +2,21 @@ #include #include #include +#include +#include +#include +#include namespace engine3d{ Ref CreateCubeMesh(glm::vec3 offset){ std::vector vertices{ - // left face (white) - {{-.5f, -.5f, -.5f}, {.9f, .9f, .9f}}, - {{-.5f, .5f, .5f}, {.9f, .9f, .9f}}, - {{-.5f, -.5f, .5f}, {.9f, .9f, .9f}}, - {{-.5f, -.5f, -.5f}, {.9f, .9f, .9f}}, - {{-.5f, .5f, -.5f}, {.9f, .9f, .9f}}, - {{-.5f, .5f, .5f}, {.9f, .9f, .9f}}, + // left Face (white) + vk::VulkanModel::Vertex{.Position{-.5f, -.5f, -.5f}, .Color{.9f, .9f, .9f}}, + vk::VulkanModel::Vertex{.Position{-.5f, .5f, .5f}, .Color{.9f, .9f, .9f}}, + vk::VulkanModel::Vertex{.Position ={-.5f, -.5f, .5f}, .Color{.9f, .9f, .9f}}, + vk::VulkanModel::Vertex{.Position ={-.5f, -.5f, -.5f},.Color {.9f, .9f, .9f}}, + vk::VulkanModel::Vertex{.Position ={-.5f, .5f, -.5f}, .Color{.9f, .9f, .9f}}, + vk::VulkanModel::Vertex{.Position ={-.5f, .5f, .5f}, .Color{.9f, .9f, .9f}}, // right face (yellow) {{.5f, -.5f, -.5f}, {.8f, .8f, .1f}}, @@ -61,10 +65,11 @@ namespace engine3d{ return CreateRef(vertices, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT); } - EditorScene::EditorScene(){ m_Scene = new Scene(); + // SyncUpdateManager::GetInstance()->Subscribe(this, &EditorScene::OnUpdate); + auto cube_mesh = CreateCubeMesh({.0f, .0f, .0f}); //! @note Make this scene object as part of our current scene. @@ -78,8 +83,21 @@ namespace engine3d{ //! @note Setting our properties auto& transform = cube->SceneGetComponent(); + cube->AddComponent(); + + auto& camera = cube->SceneGetComponent(); + // camera.SetInitialProperties(30.0f, 0.1f, 1000.0f); + // camera.SetView(glm::vec3(0.f), glm::vec3(0.5f, 0.f, 1.f)); + auto aspect_ratio = ApplicationInstance::GetWindow().GetAspectRatio(); + // camera.SetOrthoProjection(-1, 1, -1, 1, -1, 1); + + //! @note Shows the actual cube in center of screen that works + //! @note Because z = -20.f your gonna want to increase the near clip by 100.0f + camera.SetViewTarget({-1.f, -2.f, -20.f}, {0.f, 0.f, 2.5f}); + // negative-float closer, positive-float z further - transform.m_Position = {.0f, .0f, .75f}; + // transform.m_Position = {.0f, .0f, .75f}; + transform.m_Position = {.0f, .0f, 2.5f}; // transform.m_Scale = {.5f, .5f, 0.5}; transform.m_Scale = {.5f, .5f, 0.5}; // cube->SetModel(cube_mesh); @@ -87,11 +105,25 @@ namespace engine3d{ //! @note Then we add them to our vector. m_SceneObjects.push_back(cube); + } void EditorScene::OnCreate(){ } + void EditorScene::OnUpdate(){ + // glm::vec3 m_MoveDirection{0.f}; + // glm::vec3 m_Rotation{0}; + + for(const auto& obj : m_SceneObjects){ + // auto& transform_compoent = obj->SceneGetComponent(); + auto& camera_component = obj->SceneGetComponent(); + camera_component.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 0.1f, 100.f); + } + } + + void EditorScene::OnMoveCamUpdate(){} + // void EditorScene::OnCameraUpdate(){ // for(const auto& obj : m_SceneObjects){ // obj->SceneGetComponent().OnUpdate(); diff --git a/Editor/Editor/EditorScene.hpp b/Editor/Editor/EditorScene.hpp index 3adb2c0..4a10ada 100644 --- a/Editor/Editor/EditorScene.hpp +++ b/Editor/Editor/EditorScene.hpp @@ -10,10 +10,16 @@ namespace engine3d{ void OnCreate(); + void OnUpdate(); + + void OnMoveCamUpdate(); + std::vector& GetSceneObjects() { return m_SceneObjects; } private: std::vector m_SceneObjects; Scene* m_Scene; + float m_MoveSpeed = {3.f}; + float m_LookSpeed = {1.5f}; }; }; \ No newline at end of file diff --git a/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp b/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp index 1bad067..8328216 100644 --- a/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp +++ b/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp @@ -27,6 +27,27 @@ namespace engine3d{ EditorCamera(const std::string& p_Tag); void SetInitialProperties(float p_Fov, float p_NearClip, float p_FarClip); + + // --------------------- + //! @note From tutorial. + // --------------------- + void SetOrthoProjection(float left, float right, float top, float bottom, float near, float far); + void SetPerspectiveProjection(float fovy, float aspect, float near, float far); + + //! @note Used to get camera to view at a specific direction + void SetViewDirection(glm::vec3 Position, glm::vec3 Direction, glm::vec3 Up = glm::vec3(0.f, -1.f, 0.f)); + + //! @note Used to get the camera to view at a specific target. + void SetViewTarget(glm::vec3 Position, glm::vec3 Target, glm::vec3 Up = glm::vec3(0.f, -1.f, 0.f)); + + //! @note Euler angles to specify the rotation of transforms for orienting the camera. + void SetViewXYZ(glm::vec3 Position, glm::vec3 Rotation); + // --------------------- + //! @note From tutorial. + // --------------------- + + + void OnUpdate(); //! @note Eventually we will refer to OnIntegrate as OnCreate or OnPlay or something. diff --git a/engine3d/Core/Window.hpp b/engine3d/Core/Window.hpp index b445030..0eec118 100644 --- a/engine3d/Core/Window.hpp +++ b/engine3d/Core/Window.hpp @@ -48,6 +48,8 @@ namespace engine3d{ uint32_t GetWidth() const; uint32_t GetHeight() const; std::string GetTitle() const; + + float GetAspectRatio() const; void OnUpdateAllFrames(); protected: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 739b31c..4cbb55b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -57,6 +57,7 @@ add_library( # Special Component Includes ${ENGINE_INCLUDE_NAME}/Core/SceneManagment/Components/SPComps/Transform.hpp + ${ENGINE_INCLUDE_NAME}/Core/SceneManagment/Components/SPComps/EditorCamera.hpp # Renderer Includes ${ENGINE_INCLUDE_NAME}/Core/Renderer/Renderer.hpp @@ -105,6 +106,7 @@ add_library( ${ENGINE_SRC_DIR}/Core/ApplicationManager/GameObjManager/UUID.cpp ${ENGINE_SRC_DIR}/Core/SceneManagment/Components/SPComps/Transform.cpp + ${ENGINE_SRC_DIR}/Core/SceneManagment/Components/SPComps/EditorCamera.cpp ${ENGINE_SRC_DIR}/Core/Renderer/Renderer.cpp diff --git a/src/engine3d/Core/ApplicationInstance.cpp b/src/engine3d/Core/ApplicationInstance.cpp index 41032a1..03b990d 100644 --- a/src/engine3d/Core/ApplicationInstance.cpp +++ b/src/engine3d/Core/ApplicationInstance.cpp @@ -1,5 +1,5 @@ #include "Renderer/Renderer.hpp" -#include "TimeManagement/GlobalUpdateManager.hpp" +// #include "TimeManagement/GlobalUpdateManager.hpp" #include #include #include @@ -29,17 +29,7 @@ namespace engine3d{ InputPoll::UpdateEvents(); // FrameTimer::UpdateFrameTimer(); // give us the frames in flight. - // Renderer::Presentation(); - // m_Window->GetCurrentSwapchain()->BeginFrame(); - // Renderer::BeginFrame(); - - // Renderer::SetBackgroundColor({1.0f, 0.0f, 0.0f, 0.0f}); - // UpdateCurrentApplicationInstance(); - - // Renderer::EndFrame(); - // Renderer::BeginFrame(); Renderer::BeginFrame(); - // Renderer::RecordCommandBuffers(Renderer::BeginFrame()); m_Window->OnUpdateAllFrames(); Renderer::EndFrame(); } diff --git a/src/engine3d/Core/Renderer/Renderer.cpp b/src/engine3d/Core/Renderer/Renderer.cpp index 8500f27..558d420 100644 --- a/src/engine3d/Core/Renderer/Renderer.cpp +++ b/src/engine3d/Core/Renderer/Renderer.cpp @@ -18,6 +18,8 @@ #include #include +#include + namespace engine3d{ static std::vector g_CommandBuffers; VkCommandPool g_CmdPool; @@ -136,8 +138,27 @@ namespace engine3d{ vkCmdBeginRenderPass(cmd_buffer, &rp_begin_info, VK_SUBPASS_CONTENTS_INLINE); g_IsFrameStarted = true; - // return cmd_buffer; + + VkViewport viewport = { + .x = 0.0f, + .y = 0.0f, + .width = static_cast(ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetSwapchainExtent().width), + .height = static_cast(ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetSwapchainExtent().height), + .maxDepth = 1.0f, + }; + + VkRect2D scissor = { + .offset = {0, 0}, + .extent = ApplicationInstance::GetWindow().GetCurrentSwapchain()->GetSwapchainExtent() + }; + + + g_CommandBuffers[g_CurrentFrameIndex] = cmd_buffer; + + vkCmdSetViewport(g_CommandBuffers[g_CurrentFrameIndex], 0, 1, &viewport); + vkCmdSetScissor(g_CommandBuffers[g_CurrentFrameIndex], 0, 1, &scissor); + } void Renderer::RecordGameObjects(std::vector& p_Objects){ @@ -172,28 +193,32 @@ namespace engine3d{ void Renderer::RecordSceneGameObjects(std::vector& p_Objects){ auto current_cmd_buffer = GetCurrentCommandBuffer(); + //! @note Essentially doing m_Pipeline->Bind(m_CommandBuffer[i]) //! @note Starts when to start rendering!! vkCmdBindPipeline(current_cmd_buffer, VK_PIPELINE_BIND_POINT_GRAPHICS, g_Shader->GetGraphicsPipeline()); float delta_time = SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime; // ConsoleLogWarn("Delta Time = {:.7}", delta_time); + // auto projection_view = //! @note Only for testing purposes for mesh data. for(auto& obj : p_Objects){ + auto camera_component = obj->SceneGetComponent(); + auto proj_view = camera_component.GetProjection() * camera_component.GetView(); + // obj.m_Transform2D.rotation.y = glm::mod(obj.GetTransform().rotation.y + 0.001f, glm::two_pi()); // float x = obj->GetRotation().x; - float y = glm::mod(obj->GetRotation().y + (0.1f * delta_time), glm::two_pi()); - float x = glm::mod(obj->GetRotation().x + (0.05f * delta_time), glm::two_pi()); - float z = obj->GetRotation().z; + // float y = glm::mod(obj->GetRotation().y + (0.1f * delta_time), glm::two_pi()); + // float x = glm::mod(obj->GetRotation().x + (0.05f * delta_time), glm::two_pi()); + // float z = obj->GetRotation().z; - glm::vec3 new_position = {x, y, z}; + // glm::vec3 new_position = {x, y, z}; // new_position = glm::normalize(new_position); - obj->SetRotation(new_position); + // obj->SetRotation(new_position); SimplePushConstantData push = { - // .Transform = obj.GetTransform().mat4(), - .Transform = obj->toMat4(), - .iResolution = {ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()}, + .Transform = proj_view * obj->toMat4(), + // .iResolution = {ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()}, // .Color = obj.GetColor(), }; diff --git a/src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp b/src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp index 0efbaa4..1837e4e 100644 --- a/src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp +++ b/src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp @@ -49,6 +49,128 @@ namespace engine3d{ RecalculateView(); } + + /* + void EditorCamera::SetView(glm::vec3 Position, glm::vec3 Target, glm::vec3 Up){ + } + + void EditorCamera::SetViewXYZ(glm::vec3 Position, glm::vec3 Rotation){ + const float c3 = glm::cos(Rotation.z); + const float s3 = glm::sin(Rotation.z); + const float c2 = glm::cos(Rotation.x); + const float s2 = glm::sin(Rotation.x); + const float c1 = glm::cos(Rotation.y); + const float s1 = glm::sin(Rotation.y); + const glm::vec3 u{(c1 * c3 + s1 * s2 * s3), (c2 * s3), (c1 * s2 * s3 - c3 * s1)}; + const glm::vec3 v{(c3 * s1 * s2 - c1 * s3), (c2 * c3), (c1 * c3 * s2 + s1 * s3)}; + const glm::vec3 w{(c2 * s1), (-s2), (c1 * c2)}; + m_ViewMatrix = glm::mat4{1.f}; + m_ViewMatrix[0][0] = u.x; + m_ViewMatrix[1][0] = u.y; + m_ViewMatrix[2][0] = u.z; + m_ViewMatrix[0][1] = v.x; + m_ViewMatrix[1][1] = v.y; + m_ViewMatrix[2][1] = v.z; + m_ViewMatrix[0][2] = w.x; + m_ViewMatrix[1][2] = w.y; + m_ViewMatrix[2][2] = w.z; + m_ViewMatrix[3][0] = -glm::dot(u, Position); + m_ViewMatrix[3][1] = -glm::dot(v, Position); + m_ViewMatrix[3][2] = -glm::dot(w, Position); + } + + void EditorCamera::SetViewTarget(glm::vec3 Position, glm::vec3 Target, glm::vec3 Up){ + SetView(Position, Target - Position, Up); + } + */ + + void EditorCamera::SetViewDirection(glm::vec3 Position, glm::vec3 Direction, glm::vec3 Up){ + const glm::vec3 w{glm::normalize(Direction)}; + const glm::vec3 u{glm::normalize(glm::cross(w, Up))}; + const glm::vec3 v{glm::cross(w, u)}; + + m_ViewMatrix = glm::mat4{1.f}; + m_ViewMatrix[0][0] = u.x; + m_ViewMatrix[1][0] = u.y; + m_ViewMatrix[2][0] = u.z; + m_ViewMatrix[0][1] = v.x; + m_ViewMatrix[1][1] = v.y; + m_ViewMatrix[2][1] = v.z; + m_ViewMatrix[0][2] = w.x; + m_ViewMatrix[1][2] = w.y; + m_ViewMatrix[2][2] = w.z; + m_ViewMatrix[3][0] = -glm::dot(u, Position); + m_ViewMatrix[3][1] = -glm::dot(v, Position); + m_ViewMatrix[3][2] = -glm::dot(w, Position); + } + + //! @note Used to get the camera to view at a specific target. + void EditorCamera::SetViewTarget(glm::vec3 Position, glm::vec3 Target, glm::vec3 Up){ + SetViewDirection(Position, Target - Position, Up); + } + + //! @note Euler angles to specify the rotation of transforms for orienting the camera. + void EditorCamera::SetViewXYZ(glm::vec3 Position, glm::vec3 Rotation){ + const float c3 = glm::cos(Rotation.z); + const float s3 = glm::sin(Rotation.z); + const float c2 = glm::cos(Rotation.x); + const float s2 = glm::sin(Rotation.x); + const float c1 = glm::cos(Rotation.y); + const float s1 = glm::sin(Rotation.y); + const glm::vec3 u{(c1 * c3 + s1 * s2 * s3), (c2 * s3), (c1 * s2 * s3 - c3 * s1)}; + const glm::vec3 v{(c3 * s1 * s2 - c1 * s3), (c2 * c3), (c1 * c3 * s2 + s1 * s3)}; + const glm::vec3 w{(c2 * s1), (-s2), (c1 * c2)}; + m_ViewMatrix = glm::mat4{1.f}; + m_ViewMatrix[0][0] = u.x; + m_ViewMatrix[1][0] = u.y; + m_ViewMatrix[2][0] = u.z; + m_ViewMatrix[0][1] = v.x; + m_ViewMatrix[1][1] = v.y; + m_ViewMatrix[2][1] = v.z; + m_ViewMatrix[0][2] = w.x; + m_ViewMatrix[1][2] = w.y; + m_ViewMatrix[2][2] = w.z; + m_ViewMatrix[3][0] = -glm::dot(u, Position); + m_ViewMatrix[3][1] = -glm::dot(v, Position); + m_ViewMatrix[3][2] = -glm::dot(w, Position); + } + // --------------------- + + + void EditorCamera::SetOrthoProjection(float left, float right, float top, float bottom, float near, float far){ + m_ProjectionMatrix = glm::mat4{1.0f}; + m_ProjectionMatrix[0][0] = 2.f / (right - left); + m_ProjectionMatrix[1][1] = 2.f / (bottom - top); + m_ProjectionMatrix[2][2] = 1.f / (far - near); + m_ProjectionMatrix[3][0] = -(right + left) / (right - left); + m_ProjectionMatrix[3][1] = -(bottom + top) / (bottom - top); + m_ProjectionMatrix[3][2] = -near / (far - near); + } + + + void EditorCamera::SetPerspectiveProjection(float fovy, float aspect, float near, float far){ + assert(glm::abs(aspect - std::numeric_limits::epsilon()) > 0.0f); + const float tanHalfFovy = tan(fovy / 2.f); + m_ProjectionMatrix = glm::mat4{0.0f}; + m_ProjectionMatrix[0][0] = 1.f / (aspect * tanHalfFovy); + m_ProjectionMatrix[1][1] = 1.f / (tanHalfFovy); + m_ProjectionMatrix[2][2] = far / (far - near); + m_ProjectionMatrix[2][3] = 1.f; + m_ProjectionMatrix[3][2] = -(far * near) / (far - near); + } + + + + + + + + + + + + + glm::quat EditorCamera::GetOrientation() const { return glm::quat(glm::vec3(-m_Pitch, -m_Yaw, 1.0f)); } diff --git a/src/engine3d/Core/Window.cpp b/src/engine3d/Core/Window.cpp index 2ff3d3d..e16f30f 100644 --- a/src/engine3d/Core/Window.cpp +++ b/src/engine3d/Core/Window.cpp @@ -52,6 +52,10 @@ namespace engine3d{ return Title(); } + float Window::GetAspectRatio() const{ + return (float)GetWidth() / (float)GetHeight(); + } + void Window::OnUpdateAllFrames(){ GlobalUpdateManager::GetInstance()->GlobalOnTickUpdate();