Browse Source

split up copyMeshInfo() function

testing
cinnaboot 6 years ago
parent
commit
309e5103f0
  1. 7
      examples/assimp_loading/main.cpp
  2. 68
      src/mesh.cpp

7
examples/assimp_loading/main.cpp

@ -6,13 +6,15 @@
#include "mesh.h"
typedef void (*frame_callback_fn) (render_state*);
void
doFrameCallback(render_state* rs)
{
}
void
doRenderLoop(render_state* rs, void (*callback_fn) (render_state*))
doRenderLoop(render_state* rs, frame_callback_fn callback_fn)
{
const uint TARGET_FPS = 60;
const uint FRAME_DELAY = 1000 / TARGET_FPS;
@ -45,10 +47,9 @@ main()
{
render_state* rs = renInit();
meInitAssimp();
meMeshGroup mg;
if (meLoadFromFile(mg, "../data", "../data/spaceship.glb")) {
if (meLoadFromFile(mg, "../data", "../data/spaceship.glb")) {
doRenderLoop(rs, doFrameCallback);
} else {
LOG(Error) << "Error loading mesh, exiting\n";

68
src/mesh.cpp

@ -19,6 +19,7 @@ meMeshInfo* allocateMeshInfo(uint num_vertices, uint num_indices, bool has_norma
void freeMesh(meMeshInfo* mesh);
inline glm::vec3 copyVector(aiVector3D v_in, glm::vec3& v_out);
meMeshInfo* copyMeshInfo(const char* data_dir, const aiScene* scene, aiMesh* mesh);
bool loadDiffuseTexture(const char* data_dir, const aiScene* scene, aiMesh* mesh, meMeshInfo* mi);
// interface
@ -54,10 +55,22 @@ meLoadFromFile(meMeshGroup& mesh_group, const char* data_dir, const char* filena
mesh_group.use_normals = scene->mMeshes[0]->HasNormals();
for (uint i = 0; i < scene->mNumMeshes; i++) {
mesh_group.meshes[i] = copyMeshInfo(data_dir, scene, scene->mMeshes[i]);
//mesh_group.meshes[i] = copyMeshInfo(data_dir, scene, scene->mMeshes[i]);
aiMesh* mesh = scene->mMeshes[i];
meMeshInfo* mi = copyMeshInfo(data_dir, scene, mesh);
if (mesh->HasTextureCoords(0)) {
if (!loadDiffuseTexture(data_dir, scene, mesh, mi)) {
LOG(Error) << "Error loading texture, cleaning up import\n";
freeMesh(mi);
aiReleaseImport(scene);
return false;
}
}
mesh_group.meshes[i] = mi;
}
// free memeory from assimp
aiReleaseImport(scene);
return true;
}
@ -125,6 +138,35 @@ copyVector(aiVector3D v_in, glm::vec3& v_out)
return v_out;
}
bool
loadDiffuseTexture(const char* data_dir, const aiScene* scene, aiMesh* mesh, meMeshInfo* mi)
{
aiMaterial* mat = scene->mMaterials[mesh->mMaterialIndex];
aiString file_name;
if (mat->GetTextureCount(aiTextureType_DIFFUSE) < 1)
return false;
if (AI_SUCCESS != mat->GetTexture(
aiTextureType_DIFFUSE, 0, &file_name, NULL, NULL, NULL, NULL, NULL))
{
LOG(Error) << "No diffuse texture from assimp\n";
return false;
} else {
const aiTexture* tex = scene->GetEmbeddedTexture(file_name.C_Str());
if (tex != nullptr) {
LOG(Info) << "has embedded texture\n";
// TODO: load binary texture data with stb_image
} else {
LOG(Info) << "Loading texture file: " << file_name.C_Str() << "\n";
mi->diffuse_texture = utilLoadImage(data_dir, file_name.C_Str());
mi->use_texture = true;
}
}
return true;
}
meMeshInfo*
copyMeshInfo(const char* data_dir, const aiScene* scene, aiMesh* mesh)
{
@ -134,6 +176,7 @@ copyMeshInfo(const char* data_dir, const aiScene* scene, aiMesh* mesh)
// copy vertices, normals, and texture coords
for (uint i = 0; i < mi->num_vertices; i++) {
//TODO: can probably use glm::vec3 constructor here and remove copyVector helper
copyVector(mesh->mVertices[i], mi->vertices[i]);
if (has_normals)
@ -150,26 +193,5 @@ copyMeshInfo(const char* data_dir, const aiScene* scene, aiMesh* mesh)
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)) {
mi->diffuse_color.r = color.r;
mi->diffuse_color.g = color.g;
mi->diffuse_color.b = color.b;
}
if (has_tex && mat->GetTextureCount(aiTextureType_DIFFUSE) > 0) {
aiString file_name;
if (AI_SUCCESS != mat->GetTexture(aiTextureType_DIFFUSE, 0, &file_name, NULL, NULL, NULL, NULL, NULL)) {
LOG(Error) << "No diffuse texture from assimp\n";
} else {
LOG(Info) << "Loading texture file: " << file_name.C_Str() << "\n";
mi->diffuse_texture = utilLoadImage(data_dir, file_name.C_Str());
mi->use_texture = true;
}
}
return mi;
}

Loading…
Cancel
Save