일단은 멀티로 접속이 됨..
This commit is contained in:
@@ -170,7 +170,7 @@ void Graphics::RenderIndexedBuffer(BufferHandle vertex_buffer,
|
||||
vkCmdDrawIndexed(frames_[current_frame_].command_buffer, count, 1, 0, 0, 0);
|
||||
SetModelMatrix(glm::mat4(1.f));
|
||||
}
|
||||
void Graphics::RenderModel(Model* model) {
|
||||
void Graphics::RenderModel(std::shared_ptr<Model> model) {
|
||||
if (!model->visible) return;
|
||||
SetTexture(model->material.texture_handle);
|
||||
SetModelMatrix(model->transform);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "precomp.h"
|
||||
#include "socket/packet.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
namespace veng {
|
||||
|
||||
@@ -36,16 +37,18 @@ const Model* Engine::GetStaticModel(std::string name) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Model* Engine::SpawnLifedModel(std::string asset_name, std::string name,
|
||||
std::float_t lifespan) {
|
||||
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::move(model_to_spawn);
|
||||
return &models_[name];
|
||||
models_[name] = std::make_shared<Model>(model_to_spawn);
|
||||
model_to_spawn.owner = models_[name];
|
||||
return models_[name];
|
||||
}
|
||||
|
||||
if (models_.find(name) == models_.end()) {
|
||||
@@ -53,8 +56,9 @@ Model* Engine::SpawnLifedModel(std::string asset_name, std::string name,
|
||||
model_to_spawn.asset_name = asset_name;
|
||||
model_to_spawn.name = name;
|
||||
model_to_spawn.lifespan = lifespan;
|
||||
models_[name] = std::move(model_to_spawn);
|
||||
return &models_[name];
|
||||
models_[name] = std::make_shared<Model>(model_to_spawn);
|
||||
model_to_spawn.owner = models_[name];
|
||||
return models_[name];
|
||||
}
|
||||
|
||||
std::uint32_t i = 0;
|
||||
@@ -64,7 +68,10 @@ Model* Engine::SpawnLifedModel(std::string asset_name, std::string 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::move(model_to_spawn);
|
||||
|
||||
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++;
|
||||
@@ -73,12 +80,12 @@ Model* Engine::SpawnLifedModel(std::string asset_name, std::string name,
|
||||
if (i == std::numeric_limits<std::uint32_t>::max() - 1)
|
||||
return nullptr;
|
||||
else
|
||||
return &models_[name + std::to_string(i)];
|
||||
return models_[name + std::to_string(i)];
|
||||
}
|
||||
|
||||
Model* Engine::GetSpawnedObject(std::string name) {
|
||||
std::shared_ptr<Model> Engine::GetSpawnedObject(std::string name) {
|
||||
for (auto it = models_.begin(); it != models_.end();) {
|
||||
if (it->first == name) return &it->second;
|
||||
if (it->first == name) return it->second;
|
||||
++it;
|
||||
}
|
||||
|
||||
@@ -110,149 +117,131 @@ void Engine::Update() {
|
||||
|
||||
if (Tick != nullptr) Tick(*this, delta_time);
|
||||
|
||||
std::vector<Model*> models;
|
||||
std::vector<std::shared_ptr<Model>> models;
|
||||
models.reserve(models_.size());
|
||||
|
||||
for (auto it = models_.begin(); it != models_.end();) {
|
||||
auto& model = it->second;
|
||||
|
||||
if (std::abs(model.lifespan + 1.f) <
|
||||
if (std::abs(model->lifespan + 1.f) <
|
||||
std::numeric_limits<float>::epsilon()) {
|
||||
models.push_back(&model);
|
||||
models.push_back(model);
|
||||
++it;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (model.lifespan <= 0.f) {
|
||||
if (model->lifespan <= 0.f) {
|
||||
it = models_.erase(it);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (model.shouldBeDestroyed) {
|
||||
if (model->shouldBeDestroyed) {
|
||||
it = models_.erase(it);
|
||||
continue;
|
||||
}
|
||||
|
||||
model.lifespan -= delta_time;
|
||||
model->lifespan -= delta_time;
|
||||
|
||||
models.push_back(&model);
|
||||
models.push_back(model);
|
||||
++it;
|
||||
}
|
||||
|
||||
for (auto& it : models_) {
|
||||
it.second.Update(delta_time);
|
||||
vulkan_graphics->RenderModel(&it.second);
|
||||
it.second->Update(it.second->lastUpdatedTime + delta_time);
|
||||
vulkan_graphics->RenderModel(it.second);
|
||||
}
|
||||
|
||||
physics_controller_.invokeOnColisionEvent(thread_pool_,
|
||||
{models.data(), models.size()});
|
||||
physics_controller_.invokeOnColisionEvent(thread_pool_, models);
|
||||
|
||||
vulkan_graphics->EndFrame();
|
||||
}
|
||||
}
|
||||
void Engine::NetUpdate(std::shared_ptr<Network::Socket> sock) {
|
||||
NetworkUpload(sock);
|
||||
ResponseToServerAndRefresh(sock);
|
||||
}
|
||||
|
||||
void Engine::NetworkUpload(std::shared_ptr<Network::Socket> sock) {
|
||||
std::vector<char> data;
|
||||
for (auto& it : models_) {
|
||||
if (!it.second.networkReplicated) continue;
|
||||
if (!it.second.needsUpdate) continue;
|
||||
std::lock_guard lock(it.second->modding);
|
||||
if (!it.second->networkReplicated) continue;
|
||||
if (!it.second->needsUpdate) continue;
|
||||
|
||||
Packet::Header header;
|
||||
header.opcode = Packet::Opcode::UPDATEMODEL;
|
||||
std::vector<char> model = it.second.Serialize();
|
||||
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());
|
||||
it.second.needsUpdate = false;
|
||||
it.second->needsUpdate = false;
|
||||
spdlog::debug("{} uploaded", it.second->name);
|
||||
}
|
||||
iocp_->send(*sock, data);
|
||||
iocp_->send(sock, data);
|
||||
}
|
||||
|
||||
int recv_partial(Network::IOCP* iocp, Network::Socket& sock,
|
||||
std::vector<char>& buffer) {
|
||||
size_t total_received = 0;
|
||||
size_t expected = buffer.size();
|
||||
|
||||
while (total_received < expected) {
|
||||
if (total_received != 0)
|
||||
Sleep(100); // 너무 빨리 재시도해서 큐가 채워질 틈이 없는 듯?
|
||||
if (sock.sock == 0) return 0;
|
||||
std::vector<char> temp(expected - total_received);
|
||||
|
||||
size_t received = iocp->recv(sock, temp);
|
||||
if (received == 0) return 0;
|
||||
|
||||
std::copy(temp.begin(), temp.begin() + received,
|
||||
buffer.begin() + total_received);
|
||||
total_received += received;
|
||||
}
|
||||
|
||||
return total_received;
|
||||
float GetAlpha(double old_time, double new_time) {
|
||||
return (glfwGetTime() - old_time) / (new_time - old_time);
|
||||
}
|
||||
|
||||
void recv_fully(Network::IOCP* iocp, Network::Socket& sock,
|
||||
std::vector<char>& buffer) {
|
||||
size_t total_received = 0;
|
||||
size_t expected = buffer.size();
|
||||
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));
|
||||
|
||||
while (total_received < expected) {
|
||||
if (total_received != 0)
|
||||
Sleep(100); // 너무 빨리 재시도해서 큐가 채워질 틈이 없는 듯?
|
||||
if (sock.sock == 0) return;
|
||||
std::vector<char> temp(expected - total_received);
|
||||
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));
|
||||
|
||||
size_t received = iocp->recv(sock, temp);
|
||||
local->scale = glm::mix(local->scale, remote->scale,
|
||||
GetAlpha(local->lastUpdatedTime, header.timestamp));
|
||||
|
||||
std::copy(temp.begin(), temp.begin() + received,
|
||||
buffer.begin() + total_received);
|
||||
total_received += received;
|
||||
}
|
||||
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;
|
||||
std::vector<char> recv_data(6);
|
||||
if (recv_partial(iocp_, *sock, recv_data) == 0) return;
|
||||
|
||||
auto result = iocp_->recv(sock, 14);
|
||||
auto recv_data = utils::CvtListToVector(result.get()); // 여기서 막혀서 프로그램이 리턴을 못하는 문제가 있음..
|
||||
|
||||
Packet::Header header;
|
||||
header.Deserialize(recv_data);
|
||||
recv_data.resize(header.body_length);
|
||||
recv_fully(iocp_, *sock, recv_data);
|
||||
|
||||
result = iocp_->recv(sock, header.body_length);
|
||||
recv_data = utils::CvtListToVector(result.get());
|
||||
|
||||
switch (header.opcode) {
|
||||
case Packet::Opcode::UPDATEMODEL: {
|
||||
veng::Model model;
|
||||
model.Deserialize(recv_data);
|
||||
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) {
|
||||
it.second.position = model.position;
|
||||
it.second.linear_velocity = model.linear_velocity;
|
||||
it.second.linear_acceleration = model.linear_acceleration;
|
||||
|
||||
it.second.rotation = model.rotation;
|
||||
it.second.angular_velocity = model.angular_velocity;
|
||||
it.second.angular_acceleration = model.angular_acceleration;
|
||||
|
||||
it.second.scale = model.scale;
|
||||
it.second.transform = model.transform;
|
||||
|
||||
it.second.original_offset = model.original_offset;
|
||||
it.second.radius = model.radius;
|
||||
it.second.lifespan = model.lifespan;
|
||||
|
||||
it.second.visible = model.visible;
|
||||
it.second.colision = model.colision;
|
||||
|
||||
spdlog::info("model updated: {}", model.name);
|
||||
spdlog::info("model pos: ({},{},{})", model.position.x,
|
||||
model.position.y, model.position.z);
|
||||
if (it.second->ID == model->ID) {
|
||||
Interpolation(header, it.second, model);
|
||||
spdlog::debug("model updated: [{}:{}]", model->name, model->ID.snowflake);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
@@ -260,32 +249,38 @@ void Engine::ResponseToServerAndRefresh(std::shared_ptr<Network::Socket> sock) {
|
||||
|
||||
if (!found) {
|
||||
auto spawnedModel =
|
||||
SpawnLifedModel(model.asset_name, model.name, model.lifespan);
|
||||
spawnedModel->ID = model.ID;
|
||||
spawnedModel->position = model.position;
|
||||
spawnedModel->linear_velocity = model.linear_velocity;
|
||||
spawnedModel->linear_acceleration = model.linear_acceleration;
|
||||
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->rotation = model->rotation;
|
||||
spawnedModel->angular_velocity = model->angular_velocity;
|
||||
spawnedModel->angular_acceleration = model->angular_acceleration;
|
||||
|
||||
spawnedModel->scale = model.scale;
|
||||
spawnedModel->transform = model.transform;
|
||||
spawnedModel->scale = model->scale;
|
||||
spawnedModel->transform = model->transform;
|
||||
|
||||
spawnedModel->original_offset = model.original_offset;
|
||||
spawnedModel->radius = model.radius;
|
||||
spawnedModel->lifespan = model.lifespan;
|
||||
spawnedModel->original_offset = model->original_offset;
|
||||
spawnedModel->radius = model->radius;
|
||||
spawnedModel->lifespan = model->lifespan;
|
||||
|
||||
spawnedModel->visible = model.visible;
|
||||
spawnedModel->colision = model.colision;
|
||||
spawnedModel->visible = model->visible;
|
||||
spawnedModel->colision = model->colision;
|
||||
spawnedModel->networkReplicated = false;
|
||||
spdlog::info("model spawned: {}", model.ID.snowflake);
|
||||
spdlog::info("model spawned: {}", model->ID.snowflake);
|
||||
}
|
||||
} break;
|
||||
default:
|
||||
spdlog::error("unknown data type");
|
||||
}
|
||||
thread_pool_->enqueueJob(
|
||||
[this, sock](utils::ThreadPool* tp, std::uint32_t __) {
|
||||
ResponseToServerAndRefresh(sock);
|
||||
},
|
||||
0);
|
||||
}
|
||||
|
||||
} // namespace veng
|
||||
|
||||
@@ -5,34 +5,36 @@
|
||||
|
||||
namespace veng {
|
||||
void Physics::invokeOnColisionEvent(
|
||||
gsl::not_null<utils::ThreadPool*> thread_pool, gsl::span<Model*> models) {
|
||||
gsl::not_null<utils::ThreadPool*> thread_pool, std::vector<std::shared_ptr<Model>> models) {
|
||||
constexpr std::float_t EPSILON = std::numeric_limits<std::float_t>::epsilon();
|
||||
|
||||
for (int first = 0; first < models.size(); first++) {
|
||||
if (!models[first]->colision) continue;
|
||||
auto first_model = models[first];
|
||||
if (first_model != nullptr || !first_model->colision) continue;
|
||||
for (int second = first + 1; second < models.size(); second++) {
|
||||
if (!models[second]->colision) continue;
|
||||
auto second_model = models[second];
|
||||
if (second_model != nullptr || !second_model->colision) continue;
|
||||
std::float_t distance =
|
||||
glm::distance(models[first]->position, models[second]->position);
|
||||
std::float_t model1_radius =
|
||||
models[first]->radius * models[first]->scale.x;
|
||||
std::float_t model2_radius =
|
||||
models[second]->radius * models[second]->scale.x;
|
||||
glm::distance(first_model->position, second_model->position);
|
||||
std::float_t model1_radius = first_model->radius * first_model->scale.x;
|
||||
std::float_t model2_radius = second_model->radius * second_model->scale.x;
|
||||
if (distance <= model1_radius + model2_radius) {
|
||||
if (models[first]->OnColision != nullptr)
|
||||
if (first_model->OnColision != nullptr)
|
||||
thread_pool->enqueueJob(
|
||||
[OnColision = models[first]->OnColision](
|
||||
utils::ThreadPool* thread_pool, Model* self, Model* other) {
|
||||
[OnColision = first_model->OnColision](
|
||||
utils::ThreadPool* thread_pool, std::shared_ptr<Model> self,
|
||||
std::shared_ptr<Model> other) {
|
||||
OnColision(self, other);
|
||||
},
|
||||
models[first], models[second]);
|
||||
if (models[second]->OnColision != nullptr)
|
||||
first_model, second_model);
|
||||
if (second_model->OnColision != nullptr)
|
||||
thread_pool->enqueueJob(
|
||||
[OnColision = models[second]->OnColision](
|
||||
utils::ThreadPool* thread_pool, Model* self, Model* other) {
|
||||
[OnColision = second_model->OnColision](
|
||||
utils::ThreadPool* thread_pool, std::shared_ptr<Model> self,
|
||||
std::shared_ptr<Model> other) {
|
||||
OnColision(self, other);
|
||||
},
|
||||
models[second], models[first]);
|
||||
first_model, second_model);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user