Browse Source

rename meMeshInfo/Group to match other struct style

testing
cinnaboot 6 years ago
parent
commit
9474278220
  1. 2
      include/entity.h
  2. 10
      include/mesh.h
  3. 2
      include/render_object.h
  4. 12
      src/entity.cpp
  5. 96
      src/mesh.cpp
  6. 2
      src/render_object.cpp

2
include/entity.h

@ -13,7 +13,7 @@ struct entity
// TODO: should be a pointer into a global array of mesh_info(s) or // TODO: should be a pointer into a global array of mesh_info(s) or
// mesh_groups stored on the render_state object // mesh_groups stored on the render_state object
meMeshGroup mesh_group; mesh_group meshes;
render_object* render_objs; render_object* render_objs;
uint ro_count; uint ro_count;

10
include/mesh.h

@ -12,7 +12,7 @@
#include "util_image.h" #include "util_image.h"
struct meMeshInfo struct mesh_info
{ {
glm::mat4 model_transform; glm::mat4 model_transform;
// NOTE: vertices, normals, and tex_coords have the same count // NOTE: vertices, normals, and tex_coords have the same count
@ -26,11 +26,11 @@ struct meMeshInfo
node_animation* node_anim; node_animation* node_anim;
}; };
struct meMeshGroup struct mesh_group
{ {
// TODO: this should be one big allocation since the mesh doesn't change // TODO: this should be one big allocation since the mesh doesn't change
// while running // while running
meMeshInfo** meshes; mesh_info** meshes;
uint num_meshes; uint num_meshes;
}; };
@ -39,9 +39,9 @@ bool meInitAssimp();
// NOTE: meshes loaded from assimp require texture coordinates, and a diffuse // NOTE: meshes loaded from assimp require texture coordinates, and a diffuse
// texture, which can be a seperate file, or embedded in the input file // texture, which can be a seperate file, or embedded in the input file
bool meLoadFromFile(meMeshGroup& mesh_group, const char* filepath); bool meLoadFromFile(mesh_group& mesh_group, const char* filepath);
void meFreeMeshGroup(meMeshGroup& mesh_group); void meFreeMeshGroup(mesh_group& mesh_group);
void meShutdownAssimp(); void meShutdownAssimp();

2
include/render_object.h

@ -22,7 +22,7 @@ struct render_object
GLuint index_buffer_count; GLuint index_buffer_count;
}; };
bool roInit(meMeshInfo* mi_in, render_object& ro_out); bool roInit(mesh_info* mi_in, render_object& ro_out);
void roFree(render_object& ro); void roFree(render_object& ro);

12
src/entity.cpp

@ -22,9 +22,9 @@ entInit(entity& e, const char* model_path)
entScale(e, glm::vec3(1.0)); entScale(e, glm::vec3(1.0));
entSetWorldPosition(e, glm::vec3(0, 0, 0)); entSetWorldPosition(e, glm::vec3(0, 0, 0));
if (meLoadFromFile(e.mesh_group, model_path)) if (meLoadFromFile(e.meshes, model_path))
{ {
e.ro_count = e.mesh_group.num_meshes; e.ro_count = e.meshes.num_meshes;
e.render_objs = UTIL_ALLOC(e.ro_count, render_object); e.render_objs = UTIL_ALLOC(e.ro_count, render_object);
if (loadMeshIntoGL(e)) if (loadMeshIntoGL(e))
return true; return true;
@ -37,7 +37,7 @@ entInit(entity& e, const char* model_path)
void void
entFree(entity& e) entFree(entity& e)
{ {
meFreeMeshGroup(e.mesh_group); meFreeMeshGroup(e.meshes);
for (uint i = 0; i < e.ro_count; i++) for (uint i = 0; i < e.ro_count; i++)
roFree(e.render_objs[i]); roFree(e.render_objs[i]);
@ -66,7 +66,7 @@ entScale(entity& e, glm::vec3 v)
{ {
e.world_transform = glm::scale(e.world_transform, v); e.world_transform = glm::scale(e.world_transform, v);
for (uint i = 0; i < e.mesh_group.num_meshes; i++) { for (uint i = 0; i < e.meshes.num_meshes; i++) {
render_object& ro = e.render_objs[i]; render_object& ro = e.render_objs[i];
ro.node_xform = glm::scale(ro.node_xform, v); ro.node_xform = glm::scale(ro.node_xform, v);
} }
@ -78,10 +78,10 @@ entScale(entity& e, glm::vec3 v)
bool bool
loadMeshIntoGL(entity& e) loadMeshIntoGL(entity& e)
{ {
for (uint i = 0; i < e.mesh_group.num_meshes; i++) { for (uint i = 0; i < e.meshes.num_meshes; i++) {
assert(e.render_objs != nullptr); assert(e.render_objs != nullptr);
render_object& ro = e.render_objs[i]; render_object& ro = e.render_objs[i];
meMeshInfo* mi = e.mesh_group.meshes[i]; mesh_info* mi = e.meshes.meshes[i];
assert(mi != nullptr); assert(mi != nullptr);
if (!roInit(mi, ro)) { if (!roInit(mi, ro)) {

96
src/mesh.cpp

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

2
src/render_object.cpp

@ -18,7 +18,7 @@ inline void enableGLFloatBuffer(uint buffer_id, uint location);
// interface // interface
bool bool
roInit(meMeshInfo* mi_in, render_object& ro_out) roInit(mesh_info* mi_in, render_object& ro_out)
{ {
if (initGLFloatBuffer(mi_in->vertices, if (initGLFloatBuffer(mi_in->vertices,
mi_in->num_vertices, mi_in->num_vertices,

Loading…
Cancel
Save