|
|
|
|
@ -14,12 +14,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// forward declarations
|
|
|
|
|
mesh_info* allocateMeshInfo(uint num_vertices, uint num_indices); |
|
|
|
|
void assimpLogCB(const char* message, char* user); |
|
|
|
|
meMeshInfo* allocateMeshInfo(uint num_vertices, uint num_indices); |
|
|
|
|
void freeMesh(meMeshInfo* mesh); |
|
|
|
|
mesh_info* copyMeshInfo(const aiScene* scene, aiMesh* mesh); |
|
|
|
|
inline glm::vec3 copyVector(aiVector3D v_in, glm::vec3& v_out); |
|
|
|
|
meMeshInfo* copyMeshInfo(const aiScene* scene, aiMesh* mesh); |
|
|
|
|
bool loadDiffuseTexture(const aiScene* scene, aiMesh* mesh, meMeshInfo* mi); |
|
|
|
|
void freeMesh(mesh_info* mesh); |
|
|
|
|
bool loadDiffuseTexture(const aiScene* scene, aiMesh* mesh, mesh_info* mi); |
|
|
|
|
bool validateScene(const aiScene* scene, const char* filepath); |
|
|
|
|
|
|
|
|
|
// interface
|
|
|
|
|
@ -36,7 +36,7 @@ meInitAssimp()
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
meLoadFromFile(meMeshGroup& mesh_group, const char* filepath) |
|
|
|
|
meLoadFromFile(mesh_group& mesh_group, const char* filepath) |
|
|
|
|
{ |
|
|
|
|
LOG(Info) << "Loading file: " << filepath << "\n"; |
|
|
|
|
const aiScene* scene = aiImportFile(filepath, aiProcessPreset_TargetRealtime_MaxQuality); |
|
|
|
|
@ -45,11 +45,11 @@ meLoadFromFile(meMeshGroup& mesh_group, const char* filepath)
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
mesh_group.num_meshes = scene->mNumMeshes; |
|
|
|
|
mesh_group.meshes = UTIL_ALLOC(mesh_group.num_meshes, meMeshInfo*); |
|
|
|
|
mesh_group.meshes = UTIL_ALLOC(mesh_group.num_meshes, mesh_info*); |
|
|
|
|
|
|
|
|
|
for (uint i = 0; i < scene->mNumMeshes; i++) { |
|
|
|
|
aiMesh* mesh = scene->mMeshes[i]; |
|
|
|
|
meMeshInfo* mi = copyMeshInfo(scene, mesh); |
|
|
|
|
mesh_info* mi = copyMeshInfo(scene, mesh); |
|
|
|
|
|
|
|
|
|
if (!mesh->HasTextureCoords(0) || |
|
|
|
|
!loadDiffuseTexture(scene, mesh, mi)) |
|
|
|
|
@ -68,7 +68,7 @@ meLoadFromFile(meMeshGroup& mesh_group, const char* filepath)
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
meFreeMeshGroup(meMeshGroup& mesh_group) |
|
|
|
|
meFreeMeshGroup(mesh_group& mesh_group) |
|
|
|
|
{ |
|
|
|
|
for (uint i = 0; i < mesh_group.num_meshes; i++) |
|
|
|
|
freeMesh(mesh_group.meshes[i]); |
|
|
|
|
@ -87,18 +87,10 @@ meShutdownAssimp()
|
|
|
|
|
|
|
|
|
|
// internal
|
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
assimpLogCB(const char* message, char* user) |
|
|
|
|
{ |
|
|
|
|
// NOTE: filter 'info' messages from assimp
|
|
|
|
|
if (!utilMatchPrefix(message, "Info,", 5)) |
|
|
|
|
LOG(Info) << message << "\n"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
meMeshInfo* |
|
|
|
|
mesh_info* |
|
|
|
|
allocateMeshInfo(uint num_vertices, uint num_indices) |
|
|
|
|
{ |
|
|
|
|
meMeshInfo* mi = UTIL_ALLOC(1, meMeshInfo); |
|
|
|
|
mesh_info* mi = UTIL_ALLOC(1, mesh_info); |
|
|
|
|
mi->model_transform = glm::mat4(1); |
|
|
|
|
|
|
|
|
|
// allocate buffers for vertex and index data from mesh
|
|
|
|
|
@ -113,14 +105,33 @@ allocateMeshInfo(uint num_vertices, uint num_indices)
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
freeMesh(meMeshInfo* mesh) |
|
|
|
|
assimpLogCB(const char* message, char* user) |
|
|
|
|
{ |
|
|
|
|
utilFreeImage(mesh->diffuse_texture); |
|
|
|
|
utilSafeFree(mesh->vertices); |
|
|
|
|
utilSafeFree(mesh->normals); |
|
|
|
|
utilSafeFree(mesh->texture_coords); |
|
|
|
|
utilSafeFree(mesh->indices); |
|
|
|
|
utilSafeFree(mesh); |
|
|
|
|
// NOTE: filter 'info' messages from assimp
|
|
|
|
|
if (!utilMatchPrefix(message, "Info,", 5)) |
|
|
|
|
LOG(Info) << message << "\n"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
mesh_info* |
|
|
|
|
copyMeshInfo(const aiScene* scene, aiMesh* mesh) |
|
|
|
|
{ |
|
|
|
|
mesh_info* mi = allocateMeshInfo(mesh->mNumVertices, mesh->mNumFaces * 3); |
|
|
|
|
|
|
|
|
|
// copy vertices, normals, and texture coords
|
|
|
|
|
for (uint i = 0; i < mi->num_vertices; i++) { |
|
|
|
|
copyVector(mesh->mVertices[i], mi->vertices[i]); |
|
|
|
|
copyVector(mesh->mNormals[i], mi->normals[i]); |
|
|
|
|
mi->texture_coords[i].x = mesh->mTextureCoords[0][i].x; |
|
|
|
|
mi->texture_coords[i].y = mesh->mTextureCoords[0][i].y; |
|
|
|
|
mi->texture_coords[i].z = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// copy indices
|
|
|
|
|
for (uint i = 0; i < mesh->mNumFaces; i++) |
|
|
|
|
for (uint j = 0; j < 3; j++) |
|
|
|
|
mi->indices[i * 3 + j] = mesh->mFaces[i].mIndices[j]; |
|
|
|
|
|
|
|
|
|
return mi; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
inline glm::vec3 |
|
|
|
|
@ -132,8 +143,19 @@ copyVector(aiVector3D v_in, glm::vec3& v_out)
|
|
|
|
|
return v_out; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void |
|
|
|
|
freeMesh(mesh_info* mesh) |
|
|
|
|
{ |
|
|
|
|
utilFreeImage(mesh->diffuse_texture); |
|
|
|
|
utilSafeFree(mesh->vertices); |
|
|
|
|
utilSafeFree(mesh->normals); |
|
|
|
|
utilSafeFree(mesh->texture_coords); |
|
|
|
|
utilSafeFree(mesh->indices); |
|
|
|
|
utilSafeFree(mesh); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
loadDiffuseTexture(const aiScene* scene, aiMesh* mesh, meMeshInfo* mi) |
|
|
|
|
loadDiffuseTexture(const aiScene* scene, aiMesh* mesh, mesh_info* mi) |
|
|
|
|
{ |
|
|
|
|
aiMaterial* mat = scene->mMaterials[mesh->mMaterialIndex]; |
|
|
|
|
aiString file_name; |
|
|
|
|
@ -166,28 +188,6 @@ loadDiffuseTexture(const aiScene* scene, aiMesh* mesh, meMeshInfo* mi)
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
meMeshInfo* |
|
|
|
|
copyMeshInfo(const aiScene* scene, aiMesh* mesh) |
|
|
|
|
{ |
|
|
|
|
meMeshInfo* mi = allocateMeshInfo(mesh->mNumVertices, mesh->mNumFaces * 3); |
|
|
|
|
|
|
|
|
|
// copy vertices, normals, and texture coords
|
|
|
|
|
for (uint i = 0; i < mi->num_vertices; i++) { |
|
|
|
|
copyVector(mesh->mVertices[i], mi->vertices[i]); |
|
|
|
|
copyVector(mesh->mNormals[i], mi->normals[i]); |
|
|
|
|
mi->texture_coords[i].x = mesh->mTextureCoords[0][i].x; |
|
|
|
|
mi->texture_coords[i].y = mesh->mTextureCoords[0][i].y; |
|
|
|
|
mi->texture_coords[i].z = 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// copy indices
|
|
|
|
|
for (uint i = 0; i < mesh->mNumFaces; i++) |
|
|
|
|
for (uint j = 0; j < 3; j++) |
|
|
|
|
mi->indices[i * 3 + j] = mesh->mFaces[i].mIndices[j]; |
|
|
|
|
|
|
|
|
|
return mi; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool |
|
|
|
|
validateScene(const aiScene* scene, const char* filepath) |
|
|
|
|
{ |
|
|
|
|
|