Начинаю добавлять управление камерой. (Сейчас только её позиция)

This commit is contained in:
2026-05-31 22:37:26 +07:00
parent 6389e6b25f
commit e1db9fd588
10 changed files with 112 additions and 6 deletions
+27
View File
@@ -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<SaveMap> Camera::save()
{
return std::make_shared<SaveMap>("Camera")->connect_to(Actor::save());
}
void Camera::load(std::shared_ptr<SaveMap> save)
{
Actor::load(save->getParent());
}
void Camera::SetActive() const noexcept
{
GET_RENDER_ENGINE->SetActiveCamera(GetWorld()->GetActorByID<Camera>(GetName()));
}
+16
View File
@@ -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<SaveMap> save() override;
void load(std::shared_ptr<SaveMap> save) override;
void SetActive() const noexcept;
};
@@ -24,7 +24,7 @@ void StaticMesh::load(std::shared_ptr<SaveMap> save)
void StaticMesh::load_model(const std::string& model_name)
{
model_name_ = model_name;
model_ = static_cast<UwURenderEngine*>(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<UwURenderEngine*>(GetWorld()->GetGameInstance().GetCore().GetRenderEngine())->GetModelManager()->FreeModel(model_name_);
GET_RENDER_ENGINE->GetModelManager()->FreeModel(model_name_);
}
@@ -9,6 +9,7 @@
#include <glm/gtc/matrix_transform.hpp>
#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> 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<std::chrono::duration<float>>(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<float>(swapchain_extent_.width) / static_cast<float>(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> camera)
{
std::scoped_lock lock(m_active_camera_);
active_camera_ = camera;
}
@@ -8,10 +8,14 @@
#include <memory>
#define GLM_FORCE_RADIANS
#include <mutex>
#include <glm/glm.hpp>
class Camera;
class GPU_GarbageCollector;
#define GET_RENDER_ENGINE static_cast<UwURenderEngine*>(GetWorld()->GetGameInstance().GetCore().GetRenderEngine())
class UwURenderEngine : public RenderEngineBase
{
struct QueueFamilyIndices
@@ -62,6 +66,8 @@ class UwURenderEngine : public RenderEngineBase
CoreInstance& core_;
std::shared_ptr<ModelManager> model_manager_;
std::shared_ptr<GPU_GarbageCollector> garbage_collector_;
std::weak_ptr<Camera> 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<ModelManager> GetModelManager() const { return model_manager_; }
void SetActiveCamera(std::weak_ptr<Camera> camera);
};