You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

128 lines
3.2 KiB

#include <cassert>
#include <cstdlib> // calloc
#include <assimp/cimport.h>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <glm/glm.hpp>
#include <glm/geometric.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include "aixlog.hpp"
#include "mesh.h"
inline glm::vec3 copyVector(aiVector3D v_in, glm::vec3& v_out);
meMeshInfo* copyMeshInfo(const aiScene* scene, aiMesh* mesh);
void freeMesh(meMeshInfo* mesh);
bool
meInitAssimp()
{
LOG(INFO) << "Initializing Assimp\n";
/* get a handle to the predefined STDOUT log stream and attach
* it to the logging system. It remains active for all further
* calls to aiImportFile(Ex) and aiApplyPostProcessing. */
aiLogStream stream = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT,NULL);
aiAttachLogStream(&stream);
return true;
}
bool
meLoadFromFile(const char* filename, meMeshGroup& mesh_group)
{
LOG(INFO) << "Loading file: " << filename << "\n";
const aiScene* scene = aiImportFile(filename, aiProcessPreset_TargetRealtime_MaxQuality);
if (scene->mNumMeshes < 1) {
LOG(ERROR) << "Scene contains no meshes\n";
return false;
}
mesh_group.num_meshes = scene->mNumMeshes;
mesh_group.meshes = (meMeshInfo**) std::calloc(mesh_group.num_meshes, sizeof(meMeshInfo*));
mesh_group.use_normals = scene->mMeshes[0]->HasNormals();
for (uint i = 0; i < scene->mNumMeshes; i++) {
mesh_group.meshes[i] = copyMeshInfo(scene, scene->mMeshes[i]);
}
// free memeory from assimp
aiReleaseImport(scene);
return true;
}
void
meFreeMeshGroup(meMeshGroup& mesh_group)
{
for (uint i = 0; i < mesh_group.num_meshes; i++)
freeMesh(mesh_group.meshes[i]);
}
void
meShutdownAssimp()
{
aiDetachAllLogStreams();
}
inline glm::vec3
copyVector(aiVector3D v_in, glm::vec3& v_out)
{
v_out.x = v_in.x;
v_out.y = v_in.y;
v_out.z = v_in.z;
return v_out;
}
meMeshInfo*
copyMeshInfo(const aiScene* scene, aiMesh* mesh)
{
meMeshInfo* mi = (meMeshInfo*) std::calloc(1, sizeof(meMeshInfo));
mi->model_transform = glm::mat4(1);
// allocate buffers for vertex and index data from mesh
mi->num_vertices = mesh->mNumVertices;
mi->vertices = (glm::vec3 *) std::calloc(mi->num_vertices, sizeof(glm::vec3));
mi->normals = (glm::vec3 *) std::calloc(mi->num_vertices, sizeof(glm::vec3));
mi->num_indices = mesh->mNumFaces * 3; // NOTE: assume 3 vertices per face
mi->indices = (uint *) std::calloc(mi->num_indices, sizeof(uint));
// copy vertices and normals
for (uint i = 0; i < mi->num_vertices; i++) {
copyVector(mesh->mVertices[i], mi->vertices[i]);
if (mesh->HasNormals())
copyVector(mesh->mNormals[i], mi->normals[i]);
}
// 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];
// material
aiMaterial* mat = scene->mMaterials[mesh->mMaterialIndex];
aiColor3D color(0.f, 0.f, 0.f);
if (AI_SUCCESS != mat->Get(AI_MATKEY_COLOR_DIFFUSE, color)) {
LOG(ERROR) << "Some Assimp-type-error\n";
} else {
mi->diffuse_color.r = color.r;
mi->diffuse_color.g = color.g;
mi->diffuse_color.b = color.b;
}
return mi;
}
void
freeMesh(meMeshInfo* mesh)
{
utilSafeFree(mesh->vertices);
utilSafeFree(mesh->normals);
utilSafeFree(mesh->indices);
utilSafeFree(mesh);
}