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.
 
 
 
 

98 lines
2.4 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"
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;
}
meMeshInfo*
meLoadFromFile(const char* filename)
{
const aiScene* scene = aiImportFile(filename, aiProcessPreset_TargetRealtime_MaxQuality);
if (scene->mNumMeshes != 1) {
LOG(ERROR) << "We Can only handle 1 mesh per entity atm\n";
return nullptr;
}
meMeshInfo* mesh = (meMeshInfo*) std::calloc(1, sizeof(meMeshInfo));
// TODO: using hard model transform for now
mesh->model_transform = glm::scale(glm::mat4(1), glm::vec3(100, 100, 100));
// allocate buffers for vertex and index data from mesh
mesh->num_vertices = scene->mMeshes[0]->mNumVertices;
mesh->vertices = (glm::vec3 *) std::calloc(mesh->num_vertices, sizeof(glm::vec3));
mesh->num_indices = scene->mMeshes[0]->mNumFaces * 3; // NOTE: assume 3 vertices per face
mesh->indices = (uint *) std::calloc(mesh->num_indices, sizeof(uint));
// copy vertices
for (uint i = 0; i < mesh->num_vertices; i++) {
aiVector3D v_in = scene->mMeshes[0]->mVertices[i];
glm::vec3& v_out = mesh->vertices[i];
v_out.x = v_in.x;
v_out.y = v_in.y;
v_out.z = v_in.z;
}
// copy indices
for (uint i = 0; i < scene->mMeshes[0]->mNumFaces; i++)
for (uint j = 0; j < 3; j++)
mesh->indices[i * 3 + j] = scene->mMeshes[0]->mFaces[i].mIndices[j];
#if 1
// material
aiMaterial* mat = scene->mMaterials[0];
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 {
mesh->diffuse_color.r = color.r;
mesh->diffuse_color.g = color.g;
mesh->diffuse_color.b = color.b;
}
#endif
// free memeory from assimp
aiReleaseImport(scene);
return mesh;
}
void
meFreeMesh(meMeshInfo* mesh)
{
std::free(mesh->vertices);
mesh->vertices = nullptr;
std::free(mesh->indices);
mesh->indices = nullptr;
std::free(mesh);
mesh = nullptr;
}
void
meShutdownAssimp()
{
aiDetachAllLogStreams();
}