직렬화 함수 작성 끝
This commit is contained in:
66
impl/vulkan_engine/asset/loader.cpp
Normal file
66
impl/vulkan_engine/asset/loader.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include "vulkan_engine/asset/loader.h"
|
||||
|
||||
#include "stb/stb_image.h"
|
||||
#include "vulkan_engine/utilities.h"
|
||||
|
||||
namespace veng {
|
||||
|
||||
void Loader::setPath(std::string path) {
|
||||
scene_ = importer_.ReadFile(
|
||||
path.c_str(), aiProcess_CalcTangentSpace | aiProcess_Triangulate |
|
||||
aiProcess_JoinIdenticalVertices |
|
||||
aiProcess_SortByPType | aiProcess_FlipUVs);
|
||||
if (scene_ == nullptr || !scene_->HasMeshes())
|
||||
throw std::runtime_error(importer_.GetErrorString());
|
||||
}
|
||||
void Loader::loadModel(inModel model) {
|
||||
aiMesh* mesh = scene_->mMeshes[0];
|
||||
|
||||
for (std::uint32_t i = 0; i < mesh->mNumVertices; i++) {
|
||||
glm::vec2 uv = {mesh->mTextureCoords[0][i].x, mesh->mTextureCoords[0][i].y};
|
||||
model.vertices.emplace_back(
|
||||
glm::vec3{mesh->mVertices[i].x, mesh->mVertices[i].y,
|
||||
mesh->mVertices[i].z},
|
||||
uv);
|
||||
}
|
||||
|
||||
for (const Vertex& it : model.vertices) {
|
||||
model.original_offset.x += it.position.x / model.vertices.size();
|
||||
model.original_offset.y += it.position.y / model.vertices.size();
|
||||
model.original_offset.z += it.position.z / model.vertices.size();
|
||||
}
|
||||
|
||||
for (Vertex& vertex : model.vertices) {
|
||||
vertex.position -= model.original_offset;
|
||||
model.radius = (model.radius < glm::length(vertex.position))
|
||||
? glm::length(vertex.position)
|
||||
: model.radius;
|
||||
}
|
||||
|
||||
for (std::uint32_t i = 0; i < mesh->mNumFaces; i++) {
|
||||
aiFace face = mesh->mFaces[i];
|
||||
for (unsigned int j = 0; j < face.mNumIndices; ++j) {
|
||||
model.indices.push_back(face.mIndices[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<std::uint8_t> Loader::readTexture() {
|
||||
aiString texture_path;
|
||||
if (scene_->mMaterials[scene_->mMeshes[0]->mMaterialIndex]->GetTexture(
|
||||
aiTextureType_DIFFUSE, 0, &texture_path) != AI_SUCCESS) {
|
||||
spdlog::warn("No texture");
|
||||
}
|
||||
|
||||
if (scene_->mNumTextures > 0 || texture_path.C_Str()[0] == '*') {
|
||||
const aiTexture* texture = scene_->GetEmbeddedTexture(texture_path.C_Str());
|
||||
|
||||
return std::vector<std::uint8_t>(
|
||||
(std::uint8_t*)texture->pcData,
|
||||
(std::uint8_t*)texture->pcData + texture->mWidth);
|
||||
}
|
||||
|
||||
return ReadFile(texture_path.C_Str());
|
||||
}
|
||||
|
||||
} // namespace veng
|
||||
91
impl/vulkan_engine/asset/object/model.cpp
Normal file
91
impl/vulkan_engine/asset/object/model.cpp
Normal file
@@ -0,0 +1,91 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user