From e1db9fd5888eacae639cd7d577317d07f3a3fe65 Mon Sep 17 00:00:00 2001 From: Jiga228 Date: Sun, 31 May 2026 22:37:26 +0700 Subject: [PATCH] =?UTF-8?q?=D0=9D=D0=B0=D1=87=D0=B8=D0=BD=D0=B0=D1=8E=20?= =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F=D1=82=D1=8C=20=D1=83?= =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA?= =?UTF-8?q?=D0=B0=D0=BC=D0=B5=D1=80=D0=BE=D0=B9.=20(=D0=A1=D0=B5=D0=B9?= =?UTF-8?q?=D1=87=D0=B0=D1=81=20=D1=82=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20?= =?UTF-8?q?=D0=B5=D1=91=20=D0=BF=D0=BE=D0=B7=D0=B8=D1=86=D0=B8=D1=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Game/World/World.hpp | 7 ++++- FastRTTI/RTTI_Meta.h | 2 +- TestGame/Worlds/TestWorld.world | 27 ++++++++++++++++++- TestGame/src/Factories/ObjectFactory.cpp | 2 ++ TestGame/src/Worlds/TestWorld.cpp | 8 ++++++ UwURenderEngine/Game/Actors/Camera.cpp | 27 +++++++++++++++++++ UwURenderEngine/Game/Actors/Camera.hpp | 16 +++++++++++ .../Game/Actors/Mesh/StaticMesh.cpp | 4 +-- .../RenderEngine/UwURenderEngine.cpp | 17 +++++++++++- .../RenderEngine/UwURenderEngine.hpp | 8 ++++++ 10 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 UwURenderEngine/Game/Actors/Camera.cpp create mode 100644 UwURenderEngine/Game/Actors/Camera.hpp diff --git a/Core/Game/World/World.hpp b/Core/Game/World/World.hpp index e5b5f03..5a11917 100644 --- a/Core/Game/World/World.hpp +++ b/Core/Game/World/World.hpp @@ -50,6 +50,7 @@ public: object->SetActorLocate(loc); object->SetActorRotate(rot); static_cast(object)->world_ = this; + static_cast(object)->name_ = name; actors_map.try_emplace(name, object); return object; } @@ -77,7 +78,11 @@ public: template std::weak_ptr GetActorByID(const std::string& id) { - return actors_map[id]; + auto it = actors_map.find(id); + if (it == actors_map.end() || it->second == nullptr) + return {}; + + return std::static_pointer_cast(it->second); } }; diff --git a/FastRTTI/RTTI_Meta.h b/FastRTTI/RTTI_Meta.h index d348cee..964cb47 100644 --- a/FastRTTI/RTTI_Meta.h +++ b/FastRTTI/RTTI_Meta.h @@ -13,5 +13,5 @@ template struct Meta; // Enum type classes enum class Classes { - IRTTI, IResource, Actor, TestActor, Mesh, StaticMesh, World, TestWorld + IRTTI, IResource, Actor, World, Mesh, StaticMesh, Camera, TestWorld, TestActor }; diff --git a/TestGame/Worlds/TestWorld.world b/TestGame/Worlds/TestWorld.world index 0a5e7f2..614de31 100644 --- a/TestGame/Worlds/TestWorld.world +++ b/TestGame/Worlds/TestWorld.world @@ -3,8 +3,33 @@ "dtime": 5.000000, "Parent parameters": { "Class name":"World", - "vsActorsIDs": ["TestMesh1","TestMesh2"], + "vsActorsIDs": ["MainCamera", "TestMesh1","TestMesh2"], "loActors": [ + { + "Class name": "Camera", + "Parent parameters": { + "Class name":"Actor", + "oloc": { + "Class name":"Vector3D", + "dx":2.0, + "dy":2.0, + "dz":2.0 + }, + "orot": { + "Class name":"Vector3D", + "dx":0.0, + "dy":0.0, + "dz":0.0 + }, + "oscale": { + "Class name": "Vector3D", + "dx": 1.0, + "dy": 1.0, + "dz": 1.0 + }, + "tags":[] + } + }, { "Class name": "StaticMesh", "Parent parameters": { diff --git a/TestGame/src/Factories/ObjectFactory.cpp b/TestGame/src/Factories/ObjectFactory.cpp index d0ed568..153fafc 100644 --- a/TestGame/src/Factories/ObjectFactory.cpp +++ b/TestGame/src/Factories/ObjectFactory.cpp @@ -1,9 +1,11 @@ #include "Game/ObjectFactory.hpp" #include "../Actors/TestActor.h" +#include "Game/Actors/Camera.hpp" #include "Game/Actors/Mesh/StaticMesh.hpp" FACTORIES_LIST{ GENERATE_FACTORY_OBJECT(StaticMesh) GENERATE_FACTORY_OBJECT(TestActor) + GENERATE_FACTORY_OBJECT(Camera) }; diff --git a/TestGame/src/Worlds/TestWorld.cpp b/TestGame/src/Worlds/TestWorld.cpp index f15442a..9cdbf37 100644 --- a/TestGame/src/Worlds/TestWorld.cpp +++ b/TestGame/src/Worlds/TestWorld.cpp @@ -1,6 +1,7 @@ #include "TestWorld.h" #include "Game/GameInstance.hpp" +#include "Game/Actors/Camera.hpp" #include "Game/SaveMap/SaveMap.hpp" #include "Log/Log.hpp" #include "Game/Actors/Mesh/Mesh.hpp" @@ -16,12 +17,19 @@ void TestWorld::BeginPlay() { Loging::Log("Load actor: " + i.lock()->GetName()); } + + GetActorsByClass()[0].lock()->SetActive(); } void TestWorld::Tick(double delta_time) { World::Tick(delta_time); + static double time1 = 0.0; + time1 += delta_time; + double delta = sin(time1); + GetActorsByClass()[0].lock()->SetActorLocate(Vector3D{2 + delta, 2 + delta, 2 + delta}); + time += delta_time; if (time >= 10.0) { diff --git a/UwURenderEngine/Game/Actors/Camera.cpp b/UwURenderEngine/Game/Actors/Camera.cpp new file mode 100644 index 0000000..6187c01 --- /dev/null +++ b/UwURenderEngine/Game/Actors/Camera.cpp @@ -0,0 +1,27 @@ +#include "Camera.hpp" + +#include "../../RenderEngine/UwURenderEngine.hpp" +#include "Core/CoreInstance.hpp" +#include "Game/GameInstance.hpp" +#include "Game/SaveMap/SaveMap.hpp" +#include "Game/World/World.hpp" + +Camera::Camera() +{ + SetType(Classes::Camera); +} + +std::shared_ptr Camera::save() +{ + return std::make_shared("Camera")->connect_to(Actor::save()); +} + +void Camera::load(std::shared_ptr save) +{ + Actor::load(save->getParent()); +} + +void Camera::SetActive() const noexcept +{ + GET_RENDER_ENGINE->SetActiveCamera(GetWorld()->GetActorByID(GetName())); +} diff --git a/UwURenderEngine/Game/Actors/Camera.hpp b/UwURenderEngine/Game/Actors/Camera.hpp new file mode 100644 index 0000000..4c7f189 --- /dev/null +++ b/UwURenderEngine/Game/Actors/Camera.hpp @@ -0,0 +1,16 @@ +#pragma once +#include "RTTI_Meta.h" +#include "Game/Actors/Actor.hpp" + +GENERATE_META(Camera) + +class Camera : public Actor +{ +public: + Camera(); + + std::shared_ptr save() override; + void load(std::shared_ptr save) override; + + void SetActive() const noexcept; +}; diff --git a/UwURenderEngine/Game/Actors/Mesh/StaticMesh.cpp b/UwURenderEngine/Game/Actors/Mesh/StaticMesh.cpp index 5cc3d23..8acc43f 100644 --- a/UwURenderEngine/Game/Actors/Mesh/StaticMesh.cpp +++ b/UwURenderEngine/Game/Actors/Mesh/StaticMesh.cpp @@ -24,7 +24,7 @@ void StaticMesh::load(std::shared_ptr save) void StaticMesh::load_model(const std::string& model_name) { model_name_ = model_name; - model_ = static_cast(GetWorld()->GetGameInstance().GetCore().GetRenderEngine())->GetModelManager()->LoadModel(model_name); + model_ = GET_RENDER_ENGINE->GetModelManager()->LoadModel(model_name); SetModelName(model_name); } @@ -32,5 +32,5 @@ void StaticMesh::OnDestroy() { Mesh::OnDestroy(); - static_cast(GetWorld()->GetGameInstance().GetCore().GetRenderEngine())->GetModelManager()->FreeModel(model_name_); + GET_RENDER_ENGINE->GetModelManager()->FreeModel(model_name_); } diff --git a/UwURenderEngine/RenderEngine/UwURenderEngine.cpp b/UwURenderEngine/RenderEngine/UwURenderEngine.cpp index cf00c64..31a5960 100644 --- a/UwURenderEngine/RenderEngine/UwURenderEngine.cpp +++ b/UwURenderEngine/RenderEngine/UwURenderEngine.cpp @@ -9,6 +9,7 @@ #include #include "GPU_GarbageCollector.h" +#include "../Game/Actors/Camera.hpp" #include "Core/CoreInstance.hpp" #include "Log/Log.hpp" @@ -870,12 +871,20 @@ void UwURenderEngine::record_command_buffer(VkCommandBuffer command_buffer, uint void UwURenderEngine::update_uniform_buffer(uint32_t current_frame) const { + glm::vec3 eye{0, 0, 0}; + if (active_camera_.expired() == false) + { + std::shared_ptr camera = active_camera_.lock(); + Vector3D loc = camera->GetActorLocate(); + eye = {loc.x, loc.y, loc.z}; + } + static auto start_time = std::chrono::high_resolution_clock::now(); auto current_time = std::chrono::high_resolution_clock::now(); float time = std::chrono::duration_cast>(current_time - start_time).count(); UniformBufferObject ubo; ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f)); - ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)); + ubo.view = glm::lookAt(eye, glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)); ubo.proj = glm::perspective(glm::radians(45.0f), static_cast(swapchain_extent_.width) / static_cast(swapchain_extent_.height), 0.1f, 256.0f); ubo.proj[1][1] *= -1; memcpy(uniform_buffers_mapped_[current_frame], &ubo, sizeof(ubo)); @@ -1062,3 +1071,9 @@ void UwURenderEngine::stop_render() const if (!glfwWindowShouldClose(get_window())) glfwSetWindowShouldClose(get_window(), true); } + +void UwURenderEngine::SetActiveCamera(std::weak_ptr camera) +{ + std::scoped_lock lock(m_active_camera_); + active_camera_ = camera; +} diff --git a/UwURenderEngine/RenderEngine/UwURenderEngine.hpp b/UwURenderEngine/RenderEngine/UwURenderEngine.hpp index 5ece50b..24ec707 100644 --- a/UwURenderEngine/RenderEngine/UwURenderEngine.hpp +++ b/UwURenderEngine/RenderEngine/UwURenderEngine.hpp @@ -8,10 +8,14 @@ #include #define GLM_FORCE_RADIANS +#include #include +class Camera; class GPU_GarbageCollector; +#define GET_RENDER_ENGINE static_cast(GetWorld()->GetGameInstance().GetCore().GetRenderEngine()) + class UwURenderEngine : public RenderEngineBase { struct QueueFamilyIndices @@ -62,6 +66,8 @@ class UwURenderEngine : public RenderEngineBase CoreInstance& core_; std::shared_ptr model_manager_; std::shared_ptr garbage_collector_; + std::weak_ptr active_camera_; + std::mutex m_active_camera_; VkInstance instance_ = VK_NULL_HANDLE; VkSurfaceKHR surface_ = VK_NULL_HANDLE; @@ -142,4 +148,6 @@ public: void stop_render() const override; std::shared_ptr GetModelManager() const { return model_manager_; } + + void SetActiveCamera(std::weak_ptr camera); };