Начинаю добавлять управление камерой. (Сейчас только её позиция)
This commit is contained in:
@@ -50,6 +50,7 @@ public:
|
|||||||
object->SetActorLocate(loc);
|
object->SetActorLocate(loc);
|
||||||
object->SetActorRotate(rot);
|
object->SetActorRotate(rot);
|
||||||
static_cast<Actor*>(object)->world_ = this;
|
static_cast<Actor*>(object)->world_ = this;
|
||||||
|
static_cast<Actor*>(object)->name_ = name;
|
||||||
actors_map.try_emplace(name, object);
|
actors_map.try_emplace(name, object);
|
||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
@@ -77,7 +78,11 @@ public:
|
|||||||
template<class T>
|
template<class T>
|
||||||
std::weak_ptr<T> GetActorByID(const std::string& id)
|
std::weak_ptr<T> 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<T>(it->second);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -13,5 +13,5 @@ template<class T> struct Meta;
|
|||||||
// Enum type classes
|
// Enum type classes
|
||||||
enum class Classes
|
enum class Classes
|
||||||
{
|
{
|
||||||
IRTTI, IResource, Actor, TestActor, Mesh, StaticMesh, World, TestWorld
|
IRTTI, IResource, Actor, World, Mesh, StaticMesh, Camera, TestWorld, TestActor
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,8 +3,33 @@
|
|||||||
"dtime": 5.000000,
|
"dtime": 5.000000,
|
||||||
"Parent parameters": {
|
"Parent parameters": {
|
||||||
"Class name":"World",
|
"Class name":"World",
|
||||||
"vsActorsIDs": ["TestMesh1","TestMesh2"],
|
"vsActorsIDs": ["MainCamera", "TestMesh1","TestMesh2"],
|
||||||
"loActors": [
|
"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",
|
"Class name": "StaticMesh",
|
||||||
"Parent parameters": {
|
"Parent parameters": {
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
#include "Game/ObjectFactory.hpp"
|
#include "Game/ObjectFactory.hpp"
|
||||||
|
|
||||||
#include "../Actors/TestActor.h"
|
#include "../Actors/TestActor.h"
|
||||||
|
#include "Game/Actors/Camera.hpp"
|
||||||
#include "Game/Actors/Mesh/StaticMesh.hpp"
|
#include "Game/Actors/Mesh/StaticMesh.hpp"
|
||||||
|
|
||||||
FACTORIES_LIST{
|
FACTORIES_LIST{
|
||||||
GENERATE_FACTORY_OBJECT(StaticMesh)
|
GENERATE_FACTORY_OBJECT(StaticMesh)
|
||||||
GENERATE_FACTORY_OBJECT(TestActor)
|
GENERATE_FACTORY_OBJECT(TestActor)
|
||||||
|
GENERATE_FACTORY_OBJECT(Camera)
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "TestWorld.h"
|
#include "TestWorld.h"
|
||||||
|
|
||||||
#include "Game/GameInstance.hpp"
|
#include "Game/GameInstance.hpp"
|
||||||
|
#include "Game/Actors/Camera.hpp"
|
||||||
#include "Game/SaveMap/SaveMap.hpp"
|
#include "Game/SaveMap/SaveMap.hpp"
|
||||||
#include "Log/Log.hpp"
|
#include "Log/Log.hpp"
|
||||||
#include "Game/Actors/Mesh/Mesh.hpp"
|
#include "Game/Actors/Mesh/Mesh.hpp"
|
||||||
@@ -16,12 +17,19 @@ void TestWorld::BeginPlay()
|
|||||||
{
|
{
|
||||||
Loging::Log("Load actor: " + i.lock()->GetName());
|
Loging::Log("Load actor: " + i.lock()->GetName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GetActorsByClass<Camera>()[0].lock()->SetActive();
|
||||||
}
|
}
|
||||||
|
|
||||||
void TestWorld::Tick(double delta_time)
|
void TestWorld::Tick(double delta_time)
|
||||||
{
|
{
|
||||||
World::Tick(delta_time);
|
World::Tick(delta_time);
|
||||||
|
|
||||||
|
static double time1 = 0.0;
|
||||||
|
time1 += delta_time;
|
||||||
|
double delta = sin(time1);
|
||||||
|
GetActorsByClass<Camera>()[0].lock()->SetActorLocate(Vector3D{2 + delta, 2 + delta, 2 + delta});
|
||||||
|
|
||||||
time += delta_time;
|
time += delta_time;
|
||||||
if (time >= 10.0)
|
if (time >= 10.0)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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()));
|
||||||
|
}
|
||||||
@@ -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)
|
void StaticMesh::load_model(const std::string& model_name)
|
||||||
{
|
{
|
||||||
model_name_ = 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);
|
SetModelName(model_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -32,5 +32,5 @@ void StaticMesh::OnDestroy()
|
|||||||
{
|
{
|
||||||
Mesh::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 <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
#include "GPU_GarbageCollector.h"
|
#include "GPU_GarbageCollector.h"
|
||||||
|
#include "../Game/Actors/Camera.hpp"
|
||||||
#include "Core/CoreInstance.hpp"
|
#include "Core/CoreInstance.hpp"
|
||||||
#include "Log/Log.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
|
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();
|
static auto start_time = std::chrono::high_resolution_clock::now();
|
||||||
auto current_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();
|
float time = std::chrono::duration_cast<std::chrono::duration<float>>(current_time - start_time).count();
|
||||||
UniformBufferObject ubo;
|
UniformBufferObject ubo;
|
||||||
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f));
|
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 = 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;
|
ubo.proj[1][1] *= -1;
|
||||||
memcpy(uniform_buffers_mapped_[current_frame], &ubo, sizeof(ubo));
|
memcpy(uniform_buffers_mapped_[current_frame], &ubo, sizeof(ubo));
|
||||||
@@ -1062,3 +1071,9 @@ void UwURenderEngine::stop_render() const
|
|||||||
if (!glfwWindowShouldClose(get_window()))
|
if (!glfwWindowShouldClose(get_window()))
|
||||||
glfwSetWindowShouldClose(get_window(), true);
|
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>
|
#include <memory>
|
||||||
|
|
||||||
#define GLM_FORCE_RADIANS
|
#define GLM_FORCE_RADIANS
|
||||||
|
#include <mutex>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
class Camera;
|
||||||
class GPU_GarbageCollector;
|
class GPU_GarbageCollector;
|
||||||
|
|
||||||
|
#define GET_RENDER_ENGINE static_cast<UwURenderEngine*>(GetWorld()->GetGameInstance().GetCore().GetRenderEngine())
|
||||||
|
|
||||||
class UwURenderEngine : public RenderEngineBase
|
class UwURenderEngine : public RenderEngineBase
|
||||||
{
|
{
|
||||||
struct QueueFamilyIndices
|
struct QueueFamilyIndices
|
||||||
@@ -62,6 +66,8 @@ class UwURenderEngine : public RenderEngineBase
|
|||||||
CoreInstance& core_;
|
CoreInstance& core_;
|
||||||
std::shared_ptr<ModelManager> model_manager_;
|
std::shared_ptr<ModelManager> model_manager_;
|
||||||
std::shared_ptr<GPU_GarbageCollector> garbage_collector_;
|
std::shared_ptr<GPU_GarbageCollector> garbage_collector_;
|
||||||
|
std::weak_ptr<Camera> active_camera_;
|
||||||
|
std::mutex m_active_camera_;
|
||||||
|
|
||||||
VkInstance instance_ = VK_NULL_HANDLE;
|
VkInstance instance_ = VK_NULL_HANDLE;
|
||||||
VkSurfaceKHR surface_ = VK_NULL_HANDLE;
|
VkSurfaceKHR surface_ = VK_NULL_HANDLE;
|
||||||
@@ -142,4 +148,6 @@ public:
|
|||||||
void stop_render() const override;
|
void stop_render() const override;
|
||||||
|
|
||||||
std::shared_ptr<ModelManager> GetModelManager() const { return model_manager_; }
|
std::shared_ptr<ModelManager> GetModelManager() const { return model_manager_; }
|
||||||
|
|
||||||
|
void SetActiveCamera(std::weak_ptr<Camera> camera);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user