92 lines
2.4 KiB
C++
92 lines
2.4 KiB
C++
#include "vulkan_engine/asset/object/model.h"
|
|
|
|
#include "vulkan_engine/vulkan/graphics.h"
|
|
|
|
namespace veng {
|
|
|
|
Model::~Model() {
|
|
if (graphics_ == nullptr) return;
|
|
|
|
graphics_->DestroyTexture(material.texture_handle);
|
|
graphics_->DestroyBuffer(vertex_buffer);
|
|
graphics_->DestroyBuffer(index_buffer);
|
|
}
|
|
|
|
template <typename T>
|
|
static void Append(std::vector<char>& dst, const T& data) {
|
|
const char* ptr = reinterpret_cast<const char*>(&data);
|
|
dst.insert(dst.end(), ptr, ptr + sizeof(T));
|
|
}
|
|
|
|
std::vector<char> Model::Serialize() {
|
|
std::vector<char> serialized;
|
|
|
|
Append(serialized, ID);
|
|
|
|
Append(serialized, transform);
|
|
Append(serialized, position);
|
|
Append(serialized, linear_velocity);
|
|
Append(serialized, linear_acceleration);
|
|
Append(serialized, rotation);
|
|
Append(serialized, angular_velocity);
|
|
Append(serialized, angular_acceleration);
|
|
Append(serialized, scale);
|
|
Append(serialized, original_offset);
|
|
|
|
Append(serialized, radius);
|
|
|
|
uint8_t vis = visible ? 1 : 0;
|
|
uint8_t col = colision ? 1 : 0;
|
|
Append(serialized, vis);
|
|
Append(serialized, col);
|
|
|
|
return serialized;
|
|
}
|
|
|
|
template <typename T>
|
|
static void Extract(const std::vector<char>& src, size_t& offset, T& out) {
|
|
std::memcpy(&out, src.data() + offset, sizeof(T));
|
|
offset += sizeof(T);
|
|
}
|
|
|
|
void Model::Deserialize(std::vector<char> data) {
|
|
size_t offset = 0;
|
|
|
|
Extract(data, offset, ID);
|
|
|
|
Extract(data, offset, transform);
|
|
Extract(data, offset, position);
|
|
Extract(data, offset, linear_velocity);
|
|
Extract(data, offset, linear_acceleration);
|
|
Extract(data, offset, rotation);
|
|
Extract(data, offset, angular_velocity);
|
|
Extract(data, offset, angular_acceleration);
|
|
Extract(data, offset, scale);
|
|
Extract(data, offset, original_offset);
|
|
|
|
Extract(data, offset, radius);
|
|
|
|
uint8_t vis = 0, col = 0;
|
|
Extract(data, offset, vis);
|
|
Extract(data, offset, col);
|
|
visible = vis != 0;
|
|
colision = col != 0;
|
|
}
|
|
|
|
void veng::Model::Update(float dt) {
|
|
linear_velocity += linear_acceleration * dt;
|
|
position += linear_velocity * dt;
|
|
|
|
angular_velocity += angular_acceleration * dt;
|
|
if (glm::length(angular_velocity) > 1e-6f) {
|
|
rotation =
|
|
glm::normalize(glm::rotate(rotation, glm::length(angular_velocity * dt),
|
|
glm::normalize(angular_velocity)));
|
|
}
|
|
|
|
transform = glm::translate(glm::mat4(1.0f), position) *
|
|
glm::mat4_cast(rotation) * glm::scale(glm::mat4(1.0f), scale);
|
|
}
|
|
|
|
} // namespace veng
|