diff --git a/Editor/Editor/Editor.cpp b/Editor/Editor/Editor.cpp index b6e5e9c..0605834 100644 --- a/Editor/Editor/Editor.cpp +++ b/Editor/Editor/Editor.cpp @@ -17,6 +17,7 @@ namespace engine3d{ void EditorApplication::OnApplicationUpdate(){ //! @note Handle Events. + // m_EditorScene->OnCameraUpdate(); auto& objects = m_EditorScene->GetSceneObjects(); Renderer::RecordSceneGameObjects(objects); } diff --git a/Editor/Editor/EditorScene.cpp b/Editor/Editor/EditorScene.cpp index e81cfe4..c5db934 100644 --- a/Editor/Editor/EditorScene.cpp +++ b/Editor/Editor/EditorScene.cpp @@ -78,8 +78,10 @@ namespace engine3d{ //! @note Setting our properties auto& transform = cube->SceneGetComponent(); - transform.m_Position = {.0f, .0f, .5f}; - transform.m_Scale = {.5f, .5f, .5f}; + // negative-float closer, positive-float z further + transform.m_Position = {.0f, .0f, .75f}; + // transform.m_Scale = {.5f, .5f, 0.5}; + transform.m_Scale = {.5f, .5f, 0.5}; // cube->SetModel(cube_mesh); cube->SetModal(cube_mesh); @@ -89,4 +91,10 @@ namespace engine3d{ void EditorScene::OnCreate(){ } + + // void EditorScene::OnCameraUpdate(){ + // for(const auto& obj : m_SceneObjects){ + // obj->SceneGetComponent().OnUpdate(); + // } + // } }; \ No newline at end of file diff --git a/engine3d/Core/Renderer/Renderer.hpp b/engine3d/Core/Renderer/Renderer.hpp index 7a51c4d..74e99bc 100644 --- a/engine3d/Core/Renderer/Renderer.hpp +++ b/engine3d/Core/Renderer/Renderer.hpp @@ -12,11 +12,11 @@ namespace engine3d{ public: static void Initialize(const std::string& p_DebugName); - static VkCommandBuffer BeginFrame(); + static void BeginFrame(); //! @note Currently the record command buffers just does what "simple_render_system.hpp/.cpp" does already //! @note In the future I'll add API's for submitting draw calls. - static void RecordCommandBuffers(VkCommandBuffer p_CommandBuffer); + // static void RecordCommandBuffers(VkCommandBuffer p_CommandBuffer); static void RecordGameObjects(std::vector& p_Objects); static void RecordSceneGameObjects(std::vector& p_SceneObjects); static void EndFrame(); diff --git a/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp b/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp new file mode 100644 index 0000000..1bad067 --- /dev/null +++ b/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.hpp @@ -0,0 +1,79 @@ +#pragma once + +#include +#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 SetInitialProperties(float p_Fov, float p_NearClip, float p_FarClip); + 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(); + + glm::quat GetOrientation() const; + glm::vec3 GetUpDirection() const; + glm::vec3 GetRightDirection() const; + glm::vec3 GetForwardDirection() const; + + glm::mat4 GetProjection() const { return m_ProjectionMatrix; } + glm::mat4 GetView() const { return m_ViewMatrix; } + + + //! @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/sim_shader_transforms/simple_shader.vert b/sim_shader_transforms/simple_shader.vert index f7881b9..f594eb5 100644 --- a/sim_shader_transforms/simple_shader.vert +++ b/sim_shader_transforms/simple_shader.vert @@ -20,8 +20,8 @@ void main(){ // gl_Position = vec4(p * Position + push.offset, 0.0, 1.0); mat4 p = push.transform; p[0] /= (push.iResolution.x / push.iResolution.y); - p[1] /= (push.iResolution.x / push.iResolution.y); - p[2] /= (push.iResolution.x / push.iResolution.y); + // p[1] /= (push.iResolution.x / push.iResolution.y); + // p[2] /= (push.iResolution.x / push.iResolution.y); gl_Position = p * vec4(Position, 1.0); fragColor = Color; } diff --git a/sim_shader_transforms/simple_shader.vert.spv b/sim_shader_transforms/simple_shader.vert.spv index c7ba936..6bd7aa3 100644 Binary files a/sim_shader_transforms/simple_shader.vert.spv and b/sim_shader_transforms/simple_shader.vert.spv differ diff --git a/src/engine3d/Core/ApplicationInstance.cpp b/src/engine3d/Core/ApplicationInstance.cpp index 7487b17..41032a1 100644 --- a/src/engine3d/Core/ApplicationInstance.cpp +++ b/src/engine3d/Core/ApplicationInstance.cpp @@ -38,7 +38,8 @@ namespace engine3d{ // Renderer::EndFrame(); // Renderer::BeginFrame(); - Renderer::RecordCommandBuffers(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 8686a4b..8500f27 100644 --- a/src/engine3d/Core/Renderer/Renderer.cpp +++ b/src/engine3d/Core/Renderer/Renderer.cpp @@ -1,12 +1,14 @@ // #include "internal/VulkanCpp/Vulkan.hpp" #include "EngineLogger.hpp" #include "GraphicDrivers/Shader.hpp" +#include "TimeManagement/UpdateManagers/SyncUpdateManager.hpp" #include "internal/Vulkan2Showcase/Shaders/VulkanShader.hpp" #include "internal/Vulkan2Showcase/VulkanContext.hpp" #include "internal/Vulkan2Showcase/VulkanModel.hpp" #include "internal/Vulkan2Showcase/helper_functions.hpp" #include #include +#include #include #include @@ -105,7 +107,7 @@ namespace engine3d{ VkClearValue clearColorValue = {{p_Rgba[0], p_Rgba[1], p_Rgba[2], p_Rgba[3]}}; } - VkCommandBuffer Renderer::BeginFrame(){ + void Renderer::BeginFrame(){ g_CurrentFrameIndex = ApplicationInstance::GetWindow().GetCurrentSwapchain()->AcquireNextImage(); VkCommandBufferBeginInfo cmd_buffer_begin_info = { .sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO @@ -134,7 +136,8 @@ namespace engine3d{ vkCmdBeginRenderPass(cmd_buffer, &rp_begin_info, VK_SUBPASS_CONTENTS_INLINE); g_IsFrameStarted = true; - return cmd_buffer; + // return cmd_buffer; + g_CommandBuffers[g_CurrentFrameIndex] = cmd_buffer; } void Renderer::RecordGameObjects(std::vector& p_Objects){ @@ -146,6 +149,7 @@ namespace engine3d{ //! @note Only for testing purposes for mesh data. for(auto& obj : p_Objects){ obj.m_Transform2D.rotation.y = glm::mod(obj.GetTransform().rotation.y + 0.001f, glm::two_pi()); + obj.m_Transform2D.rotation.x = glm::mod(obj.GetTransform().rotation.x + 0.001f, glm::two_pi()); SimplePushConstantData push = { .Transform = obj.GetTransform().mat4(), @@ -171,14 +175,20 @@ namespace engine3d{ //! @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); //! @note Only for testing purposes for mesh data. for(auto& obj : p_Objects){ // 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.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; - obj->SetRotation({x, y, z}); + + glm::vec3 new_position = {x, y, z}; + // new_position = glm::normalize(new_position); + obj->SetRotation(new_position); SimplePushConstantData push = { // .Transform = obj.GetTransform().mat4(), @@ -186,6 +196,7 @@ namespace engine3d{ .iResolution = {ApplicationInstance::GetWindow().GetWidth(), ApplicationInstance::GetWindow().GetHeight()}, // .Color = obj.GetColor(), }; + vkCmdPushConstants( current_cmd_buffer, g_PipelineLayout, diff --git a/src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp b/src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp new file mode 100644 index 0000000..0efbaa4 --- /dev/null +++ b/src/engine3d/Core/SceneManagment/Components/SPComps/EditorCamera.cpp @@ -0,0 +1,131 @@ +// #include "EditorCamera.hpp" +#include +#include +#include +#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(){ + } + + 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(); + } + + void EditorCamera::SetInitialProperties(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(); + } + + 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/src/engine3d/Core/SceneManagment/SceneObjects/SceneObject.cpp b/src/engine3d/Core/SceneManagment/SceneObjects/SceneObject.cpp index f3e0e2a..be84f1d 100644 --- a/src/engine3d/Core/SceneManagment/SceneObjects/SceneObject.cpp +++ b/src/engine3d/Core/SceneManagment/SceneObjects/SceneObject.cpp @@ -31,6 +31,10 @@ namespace engine3d { auto transform_component = SceneGetComponent(); auto transform = glm::translate(glm::mat4{1.f}, transform_component.m_Position); + transform[0][0] *= transform_component.m_Scale.x; + transform[1][1] *= transform_component.m_Scale.y; + transform[2][2] *= transform_component.m_Scale.z; + transform = glm::rotate(transform, transform_component.m_AxisRotation.y, {0.f, 1.f, 0.f}); transform = glm::rotate(transform, transform_component.m_AxisRotation.x, {1.f, 0.f, 0.f}); transform = glm::rotate(transform, transform_component.m_AxisRotation.z, {0.f, 0.f, 1.f}); diff --git a/src/engine3d/Core/TimeManagement/GlobalUpdateManager.cpp b/src/engine3d/Core/TimeManagement/GlobalUpdateManager.cpp index aa61a86..0505f85 100644 --- a/src/engine3d/Core/TimeManagement/GlobalUpdateManager.cpp +++ b/src/engine3d/Core/TimeManagement/GlobalUpdateManager.cpp @@ -33,6 +33,7 @@ namespace engine3d m_GlobalDeltaTime = 0.0; m_UpdateTime = m_GlobalTimer->GetCurrentTime(); + // m_MaxFPS = 5000; m_MaxFPS = 500; m_FPSCounter = 1; diff --git a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp index 0f55eeb..12a2857 100644 --- a/src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp +++ b/src/engine3d/Core/internal/Vulkan2Showcase/VulkanModel.cpp @@ -48,6 +48,7 @@ namespace engine3d::vk{ vkBindBufferMemory(VulkanContext::GetDriver(), m_VertexBuffer, m_VertexBufferDeviceMemory, 0); //! @note Mapping memory data. + //! @note THIS is how we map our vertices data to our VkBuffer (essentially it is our vertex buffer) void* data; vkMapMemory(VulkanContext::GetDriver(), m_VertexBufferDeviceMemory, 0, buffer_size, 0, &data); memcpy(data, p_Vertices.data(), static_cast(buffer_size));