361 lines
12 KiB
C++
361 lines
12 KiB
C++
#include "vulkan_engine/vulkan/engine.h"
|
|
|
|
#include "precomp.h"
|
|
#include "socket/packet.h"
|
|
#include "utils/utils.h"
|
|
|
|
#define TICKRATE 10
|
|
constexpr double TICKDELAY = (1 / TICKRATE);
|
|
|
|
namespace veng {
|
|
|
|
void Engine::init() {
|
|
glm::ivec2 window_size_ = vulkan_graphics->window->GetFramebufferSize();
|
|
view = glm::lookAt(glm::vec3(0.f, 0.f, -5.f), glm::vec3(0.f, 0.f, 0.f),
|
|
glm::vec3(0.f, -1.f, 0.f));
|
|
projection = glm::perspective(
|
|
glm::radians(103.f),
|
|
(std::float_t)window_size_.x / (std::float_t)window_size_.y, 0.1f,
|
|
1000.f);
|
|
vulkan_graphics->SetViewProjection(view, projection);
|
|
|
|
if (BeginPlay != nullptr) BeginPlay(*this);
|
|
}
|
|
|
|
void Engine::LoadModelAsset(std::string path, std::string name) {
|
|
model_assets_[name] = veng::Model(vulkan_graphics);
|
|
asset_loader_.setPath(path);
|
|
asset_loader_.loadModel(model_assets_[name]);
|
|
|
|
model_assets_[name].vertex_buffer =
|
|
vulkan_graphics->CreateVertexBuffer(model_assets_[name].vertices);
|
|
model_assets_[name].index_buffer =
|
|
vulkan_graphics->CreateIndexBuffer(model_assets_[name].indices);
|
|
model_assets_[name].material.texture_handle =
|
|
vulkan_graphics->CreateTexture(asset_loader_.readTexture());
|
|
}
|
|
|
|
const Model* Engine::GetStaticModel(std::string name) {
|
|
if (model_assets_.find(name) != model_assets_.end())
|
|
return &model_assets_[name];
|
|
return nullptr;
|
|
}
|
|
|
|
std::shared_ptr<Model> Engine::SpawnLifedModel(std::string asset_name,
|
|
std::string name,
|
|
std::float_t lifespan) {
|
|
if (asset_name == "") {
|
|
Model model_to_spawn(nullptr);
|
|
model_to_spawn.asset_name = "";
|
|
model_to_spawn.name = name;
|
|
model_to_spawn.visible = false;
|
|
model_to_spawn.lifespan = lifespan;
|
|
models_[name] = std::make_shared<Model>(model_to_spawn);
|
|
model_to_spawn.owner = models_[name];
|
|
return models_[name];
|
|
}
|
|
|
|
if (models_.find(name) == models_.end()) {
|
|
Model model_to_spawn(*GetStaticModel(asset_name));
|
|
model_to_spawn.asset_name = asset_name;
|
|
model_to_spawn.name = name;
|
|
model_to_spawn.lifespan = lifespan;
|
|
models_[name] = std::make_shared<Model>(model_to_spawn);
|
|
model_to_spawn.owner = models_[name];
|
|
return models_[name];
|
|
}
|
|
|
|
std::uint32_t i = 0;
|
|
for (i = 0; i < std::numeric_limits<std::uint32_t>::max();) {
|
|
if (models_.find(name + std::to_string(i)) == models_.end()) {
|
|
Model model_to_spawn(*GetStaticModel(asset_name));
|
|
model_to_spawn.asset_name = asset_name;
|
|
model_to_spawn.name = name + std::to_string(i);
|
|
model_to_spawn.lifespan = lifespan;
|
|
|
|
models_[name + std::to_string(i)] =
|
|
std::make_shared<Model>(model_to_spawn);
|
|
model_to_spawn.owner = models_[name + std::to_string(i)];
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
|
|
if (i == std::numeric_limits<std::uint32_t>::max() - 1)
|
|
return nullptr;
|
|
else
|
|
return models_[name + std::to_string(i)];
|
|
}
|
|
|
|
std::shared_ptr<Model> Engine::GetSpawnedObject(std::string name) {
|
|
for (auto it = models_.begin(); it != models_.end();) {
|
|
if (it->first == name) return it->second;
|
|
++it;
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
void Engine::Update() {
|
|
glm::ivec2 framebuffer_size = vulkan_graphics->window->GetFramebufferSize();
|
|
if (framebuffer_size != window_size_ && framebuffer_size.x != 0 &&
|
|
framebuffer_size.y != 0) {
|
|
window_size_ = framebuffer_size;
|
|
auto grater = (framebuffer_size.x > framebuffer_size.y)
|
|
? framebuffer_size.x
|
|
: framebuffer_size.y;
|
|
view = glm::lookAt(glm::vec3(0.f, 0.f, -5.f), glm::vec3(0.f, 0.f, 0.f),
|
|
glm::vec3(0.f, -1.f, 0.f));
|
|
projection = glm::perspective(
|
|
glm::radians(103.f),
|
|
(std::float_t)framebuffer_size.x / (std::float_t)framebuffer_size.y,
|
|
0.1f, 1000.f);
|
|
}
|
|
vulkan_graphics->SetViewProjection(view, projection);
|
|
|
|
if (vulkan_graphics->BeginFrame()) {
|
|
std::double_t current_time = glfwGetTime();
|
|
std::float_t delta_time =
|
|
static_cast<std::float_t>(current_time - last_frame_time_);
|
|
last_frame_time_ = current_time;
|
|
|
|
if (Tick != nullptr) Tick(*this, delta_time);
|
|
|
|
for (auto it = models_.begin(); it != models_.end();) {
|
|
auto& model = it->second;
|
|
|
|
if (std::abs(model->lifespan + 1.f) <
|
|
std::numeric_limits<float>::epsilon()) {
|
|
++it;
|
|
continue;
|
|
}
|
|
|
|
if (model->lifespan <= 0.f) {
|
|
it = models_.erase(it);
|
|
continue;
|
|
}
|
|
|
|
if (model->shouldBeDestroyed) {
|
|
it = models_.erase(it);
|
|
continue;
|
|
}
|
|
|
|
model->lifespan -= delta_time;
|
|
++it;
|
|
}
|
|
|
|
for (auto& it : models_) {
|
|
it.second->Update(it.second->lastUpdatedTime + delta_time);
|
|
vulkan_graphics->RenderModel(it.second);
|
|
}
|
|
|
|
// Physics::invokeOnColisionEvent(thread_pool_, models_);
|
|
|
|
vulkan_graphics->EndFrame();
|
|
}
|
|
}
|
|
void Engine::NetUpdate(std::shared_ptr<Network::Socket> sock) {
|
|
NetworkUpload(sock);
|
|
}
|
|
|
|
void Engine::NetworkUpload(std::shared_ptr<Network::Socket> sock) {
|
|
static double last_all_updated;
|
|
auto current_time = glfwGetTime();
|
|
bool needs_all_update = false;
|
|
if (current_time - last_all_updated >= TICKDELAY) {
|
|
needs_all_update = true;
|
|
last_all_updated = glfwGetTime();
|
|
}
|
|
|
|
std::vector<char> data;
|
|
for (auto& it : models_) {
|
|
std::lock_guard lock(it.second->modding);
|
|
if (!it.second->networkReplicated && !needs_all_update) continue;
|
|
if (!it.second->needsUpdate && !needs_all_update) continue;
|
|
|
|
Packet::Header header;
|
|
header.opcode = Packet::Opcode::UPDATEMODEL;
|
|
header.timestamp = glfwGetTime();
|
|
std::vector<char> model = it.second->Serialize();
|
|
header.body_length = model.size();
|
|
std::vector<char> header_serialized = header.Serialize();
|
|
|
|
data.insert(data.end(), header_serialized.begin(), header_serialized.end());
|
|
data.insert(data.end(), model.begin(), model.end());
|
|
if (it.second->updateRedundantCount <= 1) {
|
|
it.second->needsUpdate = false;
|
|
} else {
|
|
it.second->updateRedundantCount--;
|
|
}
|
|
spdlog::debug("{} uploaded", it.second->name);
|
|
}
|
|
iocp_->send(sock, data);
|
|
}
|
|
|
|
float GetAlpha(double old_time, double new_time) {
|
|
return (glfwGetTime() - old_time) / (new_time - old_time) * 0.5;
|
|
}
|
|
|
|
void Interpolation(Packet::Header header, std::shared_ptr<veng::Model> local,
|
|
std::shared_ptr<veng::Model> remote) {
|
|
constexpr float position_threshold = 5.0f; // 위치 차이 임계값
|
|
constexpr float rotation_threshold = 0.5f; // 회전 차이 임계값 (라디안 기준)
|
|
|
|
std::lock_guard lock(local->modding);
|
|
float alpha = GetAlpha(local->lastUpdatedTime, header.timestamp);
|
|
|
|
// 위치 보간 또는 강제 이동
|
|
if (glm::distance(local->position, remote->position) > position_threshold) {
|
|
local->position = remote->position;
|
|
} else {
|
|
local->position = glm::mix(local->position, remote->position, alpha);
|
|
}
|
|
|
|
// 선속도
|
|
if (glm::length(remote->linear_velocity - local->linear_velocity) >
|
|
position_threshold) {
|
|
local->linear_velocity = remote->linear_velocity;
|
|
} else {
|
|
local->linear_velocity =
|
|
glm::mix(local->linear_velocity, remote->linear_velocity, alpha);
|
|
}
|
|
|
|
local->linear_acceleration =
|
|
glm::mix(local->linear_acceleration, remote->linear_acceleration, alpha);
|
|
|
|
// 회전 보간 또는 강제 이동
|
|
if (glm::length(remote->rotation - local->rotation) > rotation_threshold) {
|
|
local->rotation = remote->rotation;
|
|
} else {
|
|
local->rotation = glm::mix(local->rotation, remote->rotation, alpha);
|
|
}
|
|
|
|
local->angular_velocity =
|
|
glm::mix(local->angular_velocity, remote->angular_velocity, alpha);
|
|
|
|
local->angular_acceleration = glm::mix(local->angular_acceleration,
|
|
remote->angular_acceleration, alpha);
|
|
|
|
// 스케일은 보간만 적용
|
|
local->scale = glm::mix(local->scale, remote->scale, alpha);
|
|
|
|
// 동기화 항목은 그대로 적용
|
|
local->original_offset = remote->original_offset;
|
|
local->radius = remote->radius;
|
|
local->lifespan = remote->lifespan;
|
|
local->visible = remote->visible;
|
|
local->colision = remote->colision;
|
|
}
|
|
|
|
|
|
//void Interpolation(Packet::Header header, std::shared_ptr<veng::Model> local,
|
|
// std::shared_ptr<veng::Model> remote) {
|
|
// std::lock_guard lock(local->modding);
|
|
// local->position =
|
|
// glm::mix(local->position, remote->position,
|
|
// GetAlpha(local->lastUpdatedTime, header.timestamp));
|
|
// local->linear_velocity =
|
|
// glm::mix(local->linear_velocity, remote->linear_velocity,
|
|
// GetAlpha(local->lastUpdatedTime, header.timestamp));
|
|
// local->linear_acceleration =
|
|
// glm::mix(local->linear_acceleration, remote->linear_acceleration,
|
|
// GetAlpha(local->lastUpdatedTime, header.timestamp));
|
|
//
|
|
// local->rotation =
|
|
// glm::mix(local->rotation, remote->rotation,
|
|
// GetAlpha(local->lastUpdatedTime, header.timestamp));
|
|
// local->angular_velocity =
|
|
// glm::mix(local->angular_velocity, remote->angular_velocity,
|
|
// GetAlpha(local->lastUpdatedTime, header.timestamp));
|
|
// local->angular_acceleration =
|
|
// glm::mix(local->angular_acceleration, remote->angular_acceleration,
|
|
// GetAlpha(local->lastUpdatedTime, header.timestamp));
|
|
//
|
|
// local->scale = glm::mix(local->scale, remote->scale,
|
|
// GetAlpha(local->lastUpdatedTime, header.timestamp));
|
|
//
|
|
// local->original_offset = remote->original_offset;
|
|
// local->radius = remote->radius;
|
|
// local->lifespan = remote->lifespan;
|
|
//
|
|
// local->visible = remote->visible;
|
|
// local->colision = remote->colision;
|
|
//}
|
|
|
|
void Engine::ResponseToServerAndRefresh(std::shared_ptr<Network::Socket> sock) {
|
|
if (sock->sock == 0) return;
|
|
|
|
auto result = iocp_->recv(sock, 14);
|
|
auto recv_data = utils::CvtListToVector(result.get()); // 여기서 막혀서 프로그램이 리턴을 못하는 문제가 있음..
|
|
|
|
Packet::Header header;
|
|
header.Deserialize(recv_data);
|
|
|
|
result = iocp_->recv(sock, header.body_length);
|
|
recv_data = utils::CvtListToVector(result.get());
|
|
|
|
switch (header.opcode) {
|
|
case Packet::Opcode::UPDATEMODEL: {
|
|
std::shared_ptr<veng::Model> model = std::make_shared<veng::Model>();
|
|
model->Deserialize(recv_data);
|
|
|
|
bool found = false;
|
|
for (auto& it : models_) {
|
|
if (it.second->ID == model->ID) {
|
|
Interpolation(header, it.second, model);
|
|
// spdlog::debug("model updated: [{}:{}]", model->name, model->ID.snowflake);
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!found) {
|
|
auto spawnedModel =
|
|
SpawnLifedModel(model->asset_name, model->name, model->lifespan);
|
|
std::lock_guard lock(spawnedModel->modding);
|
|
spawnedModel->ID = model->ID;
|
|
spawnedModel->position = model->position;
|
|
spawnedModel->linear_velocity = model->linear_velocity;
|
|
spawnedModel->linear_acceleration = model->linear_acceleration;
|
|
|
|
spawnedModel->rotation = model->rotation;
|
|
spawnedModel->angular_velocity = model->angular_velocity;
|
|
spawnedModel->angular_acceleration = model->angular_acceleration;
|
|
|
|
spawnedModel->scale = model->scale;
|
|
spawnedModel->transform = model->transform;
|
|
|
|
spawnedModel->original_offset = model->original_offset;
|
|
spawnedModel->radius = model->radius;
|
|
spawnedModel->lifespan = model->lifespan;
|
|
|
|
spawnedModel->visible = model->visible;
|
|
spawnedModel->colision = model->colision;
|
|
spawnedModel->networkReplicated = false;
|
|
spdlog::info("model spawned: {}", model->ID.snowflake);
|
|
}
|
|
} break;
|
|
case Packet::Opcode::DESPAWNMODEL: {
|
|
std::shared_ptr<veng::Model> model = std::make_shared<veng::Model>();
|
|
model->Deserialize(recv_data);
|
|
|
|
for (auto it = models_.begin(); it != models_.end();) {
|
|
if (it->second->ID == model->ID) {
|
|
models_.erase(it);
|
|
break;
|
|
}
|
|
++it;
|
|
}
|
|
} break;
|
|
default:
|
|
spdlog::error("unknown data type");
|
|
}
|
|
thread_pool_->enqueueJob(
|
|
[this, sock](utils::ThreadPool* tp, std::uint32_t __) {
|
|
ResponseToServerAndRefresh(sock);
|
|
},
|
|
0);
|
|
}
|
|
|
|
} // namespace veng
|