SP3-103 Implemented library to load GLTF with assimp and processmesh

This commit is contained in:
Xiao Qi 2022-09-22 11:26:06 +08:00
parent 4446d0a60d
commit 8ac6ef95d6
3 changed files with 110 additions and 3 deletions

View File

@ -0,0 +1,16 @@
#pragma once
#include <vector>
#include "Math/SHMath.h"
namespace SHADE
{
struct SHMeshAsset
{
std::vector<SHVec3> vertexPosition;
std::vector<SHVec2> texCoords;
std::vector<SHVec3> vertexTangent;
std::vector<SHVec3> vertexNormal;
std::vector<uint32_t> indices;
};
}

View File

@ -1,13 +1,83 @@
#include "SHpch.h"
#include "SHMeshLoader.h"
#include <assimp/scene.h>
#include <assimp/postprocess.h>
namespace SHADE
{
Assimp::Importer SHMeshLoader::aiImporter;
bool SHMeshLoader::LoadMesh(AssetPath path)
void SHMeshLoader::ProcessNode(aiNode const& node, aiScene const& scene, std::vector<SHMeshAsset>& meshes)
{
for (size_t i {0}; i < node.mNumMeshes; ++i)
{
aiMesh* mesh = scene.mMeshes[node.mMeshes[i]];
meshes.push_back(ProcessMesh(*mesh, scene));
}
for (size_t i{ 0 }; i < node.mNumMeshes; ++i)
{
ProcessNode(*node.mChildren[i], scene, meshes);
}
}
SHMeshAsset SHMeshLoader::ProcessMesh(aiMesh const& mesh, aiScene const& scene)
{
(void)scene;
SHMeshAsset result;
for (size_t i{0}; i < mesh.mNumVertices; ++i)
{
// Vertex position
SHVec3 vertex;
vertex.x = mesh.mVertices[i].x;
vertex.y = mesh.mVertices[i].y;
vertex.z = mesh.mVertices[i].z;
result.vertexPosition.push_back(vertex);
// Tex coords
SHVec2 texCoord{0.f, 0.f};
if (mesh.mTextureCoords[0])
{
texCoord.x = mesh.mTextureCoords[0][i].x;
texCoord.y = mesh.mTextureCoords[0][i].y;
}
result.texCoords.push_back(texCoord);
// Normals
SHVec3 normal{0.f, 0.f, 0.f};
if (mesh.mNormals)
{
normal.x = mesh.mNormals[i].x;
normal.y = mesh.mNormals[i].y;
normal.z = mesh.mNormals[i].z;
}
result.vertexNormal.push_back(normal);
// Tangent
SHVec3 tangent{0.f, 0.f, 0.f};
if (mesh.mTangents)
{
tangent.x = mesh.mTangents[i].x;
tangent.y = mesh.mTangents[i].y;
tangent.z = mesh.mTangents[i].z;
}
result.vertexTangent.push_back(tangent);
}
for (size_t i {0}; i < mesh.mNumFaces; ++i)
{
aiFace face = mesh.mFaces[i];
for (size_t j{0}; j < face.mNumIndices; ++j)
{
result.indices.push_back(face.mIndices[j]);
}
}
return result;
}
bool SHMeshLoader::LoadMesh(std::vector<SHMeshAsset>& meshes, std::vector<AssetPath>& images, AssetPath path)
{
const aiScene* scene = aiImporter.ReadFile(path.string().c_str(),
aiProcess_Triangulate
@ -27,5 +97,20 @@ namespace SHADE
| aiProcess_PreTransformVertices // pre-transform all vertices
| aiProcess_FlipUVs // flip the V to match the Vulkans way of doing UVs
);
if (!scene || !scene->HasMeshes())
{
SHLOG_ERROR("ERROR in GLTF::ASSIMP: {}\nFile: {}", aiImporter.GetErrorString(), path.string());
return false;
}
for (size_t i {0}; i < scene->mNumTextures; ++i)
{
images.push_back(AssetPath(scene->mTextures[i]->mFilename.C_Str()));
}
ProcessNode(*scene->mRootNode, *scene, meshes);
return true;
}
}

View File

@ -1,6 +1,9 @@
#pragma once
#include "../SHAssetMacros.h"
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include "../Asset Types/SHMeshAsset.h"
#include <vector>
namespace SHADE
{
@ -9,7 +12,10 @@ namespace SHADE
private:
static Assimp::Importer aiImporter;
static void ProcessNode(aiNode const& node, aiScene const& scene, std::vector<SHMeshAsset>& meshes);
static SHMeshAsset ProcessMesh(aiMesh const& mesh, aiScene const& scene);
public:
static bool LoadMesh(AssetPath path);
static bool LoadMesh(std::vector<SHMeshAsset>& meshes, std::vector<AssetPath>& images, AssetPath path);
};
}