67 lines
2.2 KiB
C++
67 lines
2.2 KiB
C++
#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
|