113 lines
3.0 KiB
C++
113 lines
3.0 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>
|
|
void Append(std::vector<char>& buffer, const T& value) {
|
|
const char* data = reinterpret_cast<const char*>(&value);
|
|
buffer.insert(buffer.end(), data, data + sizeof(T));
|
|
}
|
|
|
|
template <typename T>
|
|
void AppendVector(std::vector<char>& buffer, const std::vector<T>& vec) {
|
|
std::uint32_t size = static_cast<std::uint32_t>(vec.size());
|
|
Append(buffer, size);
|
|
buffer.insert(buffer.end(), reinterpret_cast<const char*>(vec.data()),
|
|
reinterpret_cast<const char*>(vec.data() + size));
|
|
}
|
|
|
|
template <typename T>
|
|
void Read(const std::vector<char>& buffer, size_t& offset, T& out) {
|
|
std::memcpy(&out, buffer.data() + offset, sizeof(T));
|
|
offset += sizeof(T);
|
|
}
|
|
|
|
template <typename T>
|
|
void ReadVector(const std::vector<char>& buffer, size_t& offset,
|
|
std::vector<T>& out) {
|
|
std::uint32_t size = 0;
|
|
Read(buffer, offset, size);
|
|
out.resize(size);
|
|
std::memcpy(out.data(), buffer.data() + offset, sizeof(T) * size);
|
|
offset += sizeof(T) * size;
|
|
}
|
|
|
|
std::vector<char> Model::Serialize() {
|
|
std::vector<char> buffer;
|
|
|
|
AppendVector(buffer, vertices);
|
|
AppendVector(buffer, indices);
|
|
|
|
Append(buffer, position);
|
|
Append(buffer, linear_velocity);
|
|
Append(buffer, linear_acceleration);
|
|
|
|
Append(buffer, rotation);
|
|
Append(buffer, angular_velocity);
|
|
Append(buffer, angular_acceleration);
|
|
|
|
Append(buffer, scale);
|
|
Append(buffer, transform);
|
|
|
|
Append(buffer, original_offset);
|
|
Append(buffer, radius);
|
|
Append(buffer, lifespan);
|
|
|
|
Append(buffer, visible);
|
|
Append(buffer, colision);
|
|
|
|
return buffer;
|
|
}
|
|
|
|
void Model::Deserialize(std::vector<char> data) {
|
|
size_t offset = 0;
|
|
|
|
ReadVector(data, offset, vertices);
|
|
ReadVector(data, offset, indices);
|
|
|
|
Read(data, offset, position);
|
|
Read(data, offset, linear_velocity);
|
|
Read(data, offset, linear_acceleration);
|
|
|
|
Read(data, offset, rotation);
|
|
Read(data, offset, angular_velocity);
|
|
Read(data, offset, angular_acceleration);
|
|
|
|
Read(data, offset, scale);
|
|
Read(data, offset, transform);
|
|
|
|
Read(data, offset, original_offset);
|
|
Read(data, offset, radius);
|
|
Read(data, offset, lifespan);
|
|
|
|
Read(data, offset, visible);
|
|
Read(data, offset, colision);
|
|
}
|
|
|
|
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
|