diff --git a/Editor/Editor/Editor.cpp b/Editor/Editor/Editor.cpp index 729f6a5..ada3e8c 100644 --- a/Editor/Editor/Editor.cpp +++ b/Editor/Editor/Editor.cpp @@ -17,10 +17,9 @@ namespace engine3d{ void EditorApplication::OnApplicationUpdate(){ //! @note Handle Events. - m_EditorScene->OnUpdate(); - // m_EditorScene->OnMoveCamUpdate(); - auto& objects = m_EditorScene->GetSceneObjects(); - Renderer::RecordSceneGameObjects(objects); + // m_EditorScene->OnUpdate(); + m_EditorScene->OnMoveCamUpdate(); + Renderer::RecordSceneGameObjects(m_EditorScene->GetSceneObjects(), m_EditorScene->GetCameraObject()); } ApplicationInstance* InitializeApplication(){ diff --git a/Editor/Editor/EditorScene.cpp b/Editor/Editor/EditorScene.cpp index 8691a68..81a60ef 100644 --- a/Editor/Editor/EditorScene.cpp +++ b/Editor/Editor/EditorScene.cpp @@ -75,17 +75,25 @@ namespace engine3d{ //! @note Make this scene object as part of our current scene. // SceneObject* cube = new SceneObject(m_Scene); SceneObject* cube = new SceneObject(m_Scene); + m_CameraObject = new SceneObject(m_Scene); + + m_CameraObject->AddComponent(); + // auto cube = SceneObject::Create(); // cube.SetModel(m_CubeMesh); // cube.GetTransform().Translation = {.0f, .0f, .5f}; // cube.GetTransform().scale = {.5f, .5f, .5f}; + auto& camera_transform = m_CameraObject->SceneGetComponent(); + camera_transform.m_Position = {-1.f, -2.f, -20.f}; + + auto camera = m_CameraObject->SceneGetComponent(); //! @note Setting our properties auto& transform = cube->SceneGetComponent(); - cube->AddComponent(); + // cube->AddComponent(); - auto& camera = cube->SceneGetComponent(); + // 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(); @@ -93,11 +101,15 @@ namespace engine3d{ //! @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}); + // camera.SetViewTarget({-1.f, -2.f, -20.f}, {0.f, 0.f, 2.5f}); + // camera.SetViewTarget(glm::vec3(-1.f, -2.f, -20.f), glm::vec3(0.f, 0.f, 2.5f)); // negative-float closer, positive-float z further // transform.m_Position = {.0f, .0f, .75f}; - transform.m_Position = {.0f, .0f, 2.5f}; + // transform.m_Position = {.0f, .0f, 2.5f}; + transform.m_Position = {.0f, .0f, 2.5}; + // transform.m_Position.z = transform.m_Position.z - 2.5f; + // camera.SetViewXYZ(transform.m_Position, transform.m_AxisRotation); // transform.m_Scale = {.5f, .5f, 0.5}; transform.m_Scale = {.5f, .5f, 0.5}; // cube->SetModel(cube_mesh); @@ -115,14 +127,126 @@ namespace engine3d{ // 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, 50.f); + // } + } + + void EditorScene::OnMoveCamUpdate(){ + auto& transform = m_CameraObject->SceneGetComponent(); + auto& camera = m_CameraObject->SceneGetComponent(); + auto cube_transform = m_SceneObjects[0]->SceneGetComponent(); + // float tempDt_Y; + glm::vec2 temp_position = {0.f, 0.f}; + constexpr float sensitivity = 5.0f; + constexpr glm::vec2 invert_pos = {-1, 1}; + // ConsoleLogInfo("x = {}, y = {}, z = {}", transform.m_Position.x, transform.m_Position.y, transform.m_Position.z); + // ConsoleLogInfo("x = {}, y = {}, z = {}\n", cube_transform.m_Position.x, cube_transform.m_Position.y, cube_transform.m_Position.z); + + /* + move-right = D + move-left = A + move-forward = W + move-backward = S + + move-up = E + move-down = Q + + look-left = LEFT + look-right = RIGHT + look-up = UP + look-down = DOWN + */ + + /* 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); + glm::vec3 rotate{0}; + if(InputPoll::IsKeyPressed(ENGINE_KEY_LEFT)) rotate.y += 1.f; + if(InputPoll::IsKeyPressed(ENGINE_KEY_RIGHT)) rotate.y -= 1.f; + if(InputPoll::IsKeyPressed(ENGINE_KEY_UP)) rotate.x += 1.f; + if(InputPoll::IsKeyPressed(ENGINE_KEY_DOWN)) rotate.x -= 1.f; + + auto& transform = obj->SceneGetComponent(); + auto& camera = obj->SceneGetComponent(); + + if(glm::dot(rotate, rotate) > std::numeric_limits::epsilon()){ + transform.m_AxisRotation += m_LookSpeed * SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime * glm::normalize(rotate); + } + + transform.m_AxisRotation.x = glm::clamp(transform.m_AxisRotation.x, -1.5f, 1.5f); + transform.m_AxisRotation.y = glm::mod(transform.m_AxisRotation.y, glm::two_pi()); + + float yaw = transform.m_AxisRotation.y; + const glm::vec3 forward_dir{sin(yaw), 0.f, cos(yaw)}; + const glm::vec3 right_dir{forward_dir.z, 0.f, -forward_dir.y}; + const glm::vec3 up_dir{0.f, -1.f, 0.f}; + + glm::vec3 move_dir{0.f}; + + if(InputPoll::IsKeyPressed(ENGINE_KEY_W)) move_dir += forward_dir; // FORWARD + if(InputPoll::IsKeyPressed(ENGINE_KEY_S)) move_dir -= forward_dir; // BACKWARD + if(InputPoll::IsKeyPressed(ENGINE_KEY_D)) move_dir += right_dir; // RIGHT + if(InputPoll::IsKeyPressed(ENGINE_KEY_A)) move_dir -= right_dir; // LEFT + if(InputPoll::IsKeyPressed(ENGINE_KEY_E)) move_dir += up_dir; // UP + if(InputPoll::IsKeyPressed(ENGINE_KEY_Q)) move_dir -= up_dir; // DOWN + + if(glm::dot(move_dir, move_dir) > std::numeric_limits::epsilon()){ + transform.m_Position += m_MoveSpeed * (SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime) * glm::normalize(move_dir); + } + + // camera.SetViewTarget({-1.f, -2.f, -20.f}, transform.m_Position); + // camera.SetViewTarget({0.f, 0.f, 0.f}, transform.m_Position); + camera.SetViewXYZ(transform.m_Position, transform.m_AxisRotation); } - } + */ + + glm::vec3 rotate{0}; + + if(InputPoll::IsMousePressed(Mouse::ButtonRight)){ + // temp_position.x = m_MousePosition.x - InputPoll::GetMouseX(); + rotate.y += (m_MousePosition.x - InputPoll::GetMouseX()) * invert_pos.y; + rotate.x += (m_MousePosition.y - InputPoll::GetMouseY()) * invert_pos.x; + } + // if(InputPoll::IsKeyPressed(ENGINE_KEY_LEFT)) rotate.y += 1.f; + // if(InputPoll::IsKeyPressed(ENGINE_KEY_RIGHT)) rotate.y -= 1.f; + // if(InputPoll::IsKeyPressed(ENGINE_KEY_UP)) rotate.x += 1.f; + // if(InputPoll::IsKeyPressed(ENGINE_KEY_DOWN)) rotate.x -= 1.f; + + m_MousePosition = InputPoll::GetMousePosition(); + + if(glm::dot(rotate, rotate) > std::numeric_limits::epsilon()){ + transform.m_AxisRotation += m_LookSpeed * SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime * glm::normalize(rotate) * sensitivity; + } + + transform.m_AxisRotation.x = glm::clamp(transform.m_AxisRotation.x, -1.5f, 1.5f); + transform.m_AxisRotation.y = glm::mod(transform.m_AxisRotation.y, glm::two_pi()); + + float yaw = transform.m_AxisRotation.y; + const glm::vec3 forward_dir{sin(yaw), 0.f, cos(yaw)}; + const glm::vec3 right_dir{forward_dir.z, 0.f, -forward_dir.y}; + const glm::vec3 up_dir{0.f, -1.f, 0.f}; - void EditorScene::OnMoveCamUpdate(){} + glm::vec3 move_dir{0.f}; + + if(InputPoll::IsKeyPressed(ENGINE_KEY_W)) move_dir += forward_dir; // FORWARD + if(InputPoll::IsKeyPressed(ENGINE_KEY_S)) move_dir -= forward_dir; // BACKWARD + if(InputPoll::IsKeyPressed(ENGINE_KEY_D)) move_dir += right_dir; // RIGHT + if(InputPoll::IsKeyPressed(ENGINE_KEY_A)) move_dir -= right_dir; // LEFT + if(InputPoll::IsKeyPressed(ENGINE_KEY_E)) move_dir += up_dir; // UP + if(InputPoll::IsKeyPressed(ENGINE_KEY_Q)) move_dir -= up_dir; // DOWN + + if(glm::dot(move_dir, move_dir) > std::numeric_limits::epsilon()){ + transform.m_Position += m_MoveSpeed * (SyncUpdateManager::GetInstance()->m_SyncLocalDeltaTime) * glm::normalize(move_dir); + } + + camera.SetViewXYZ(transform.m_Position, transform.m_AxisRotation); + + + camera.SetPerspectiveProjection(glm::radians(50.f), ApplicationInstance::GetWindow().GetAspectRatio(), 0.1f, 100.f); + + } // void EditorScene::OnCameraUpdate(){ // for(const auto& obj : m_SceneObjects){ diff --git a/Editor/Editor/EditorScene.hpp b/Editor/Editor/EditorScene.hpp index 4a10ada..86a8247 100644 --- a/Editor/Editor/EditorScene.hpp +++ b/Editor/Editor/EditorScene.hpp @@ -16,9 +16,13 @@ namespace engine3d{ std::vector& GetSceneObjects() { return m_SceneObjects; } + SceneObject* GetCameraObject() const { return m_CameraObject; } + private: std::vector m_SceneObjects; Scene* m_Scene; + SceneObject* m_CameraObject; + glm::vec2 m_MousePosition; float m_MoveSpeed = {3.f}; float m_LookSpeed = {1.5f}; }; diff --git a/engine3d/Core/Renderer/Renderer.hpp b/engine3d/Core/Renderer/Renderer.hpp index 74e99bc..8c76a54 100644 --- a/engine3d/Core/Renderer/Renderer.hpp +++ b/engine3d/Core/Renderer/Renderer.hpp @@ -18,7 +18,7 @@ namespace engine3d{ //! @note In the future I'll add API's for submitting draw calls. // static void RecordCommandBuffers(VkCommandBuffer p_CommandBuffer); static void RecordGameObjects(std::vector& p_Objects); - static void RecordSceneGameObjects(std::vector& p_SceneObjects); + static void RecordSceneGameObjects(std::vector& p_SceneObjects, SceneObject* p_CameraObject); static void EndFrame(); template diff --git a/sim_shader_transforms/simple_shader.vert b/sim_shader_transforms/simple_shader.vert index f594eb5..82c4385 100644 --- a/sim_shader_transforms/simple_shader.vert +++ b/sim_shader_transforms/simple_shader.vert @@ -8,6 +8,7 @@ layout(location = 0) out vec3 fragColor; layout(push_constant) uniform Push { mat4 transform; + mat4 Projection; vec2 iResolution; vec3 color; } push; @@ -18,11 +19,12 @@ void main(){ // p[0] *= (push.iResolution.y / push.iResolution.x); // p[1] *= (push.iResolution.y / push.iResolution.x); // gl_Position = vec4(p * Position + push.offset, 0.0, 1.0); - mat4 p = push.transform; - p[0] /= (push.iResolution.x / push.iResolution.y); + // 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); - gl_Position = p * vec4(Position, 1.0); + // gl_Position = p * vec4(Position, 1.0); + gl_Position = push.transform * 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 6bd7aa3..1689897 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/Renderer/Renderer.cpp b/src/engine3d/Core/Renderer/Renderer.cpp index 558d420..06b59c6 100644 --- a/src/engine3d/Core/Renderer/Renderer.cpp +++ b/src/engine3d/Core/Renderer/Renderer.cpp @@ -191,7 +191,7 @@ namespace engine3d{ } } - void Renderer::RecordSceneGameObjects(std::vector& p_Objects){ + void Renderer::RecordSceneGameObjects(std::vector& p_Objects, SceneObject* p_CameraObject){ auto current_cmd_buffer = GetCurrentCommandBuffer(); //! @note Essentially doing m_Pipeline->Bind(m_CommandBuffer[i]) @@ -201,9 +201,11 @@ namespace engine3d{ // ConsoleLogWarn("Delta Time = {:.7}", delta_time); // auto projection_view = + auto camera_component = p_CameraObject->SceneGetComponent(); + //! @note Only for testing purposes for mesh data. for(auto& obj : p_Objects){ - auto camera_component = obj->SceneGetComponent(); + // 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());