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
// mesh_groups stored on the render_state object
meMeshGroup mesh_group;
mesh_group meshes;
render_object* render_objs;
uint ro_count;

10
include/mesh.h

@ -12,7 +12,7 @@
#include "util_image.h"
struct meMeshInfo
struct mesh_info
{
glm::mat4 model_transform;
// NOTE: vertices, normals, and tex_coords have the same count
@ -26,11 +26,11 @@ struct meMeshInfo
node_animation* node_anim;
};
struct meMeshGroup
struct mesh_group
{
// TODO: this should be one big allocation since the mesh doesn't change
// while running
meMeshInfo** meshes;
mesh_info** meshes;
uint num_meshes;
};
@ -39,9 +39,9 @@ bool meInitAssimp();
// NOTE: meshes loaded from assimp require texture coordinates, and a diffuse
// 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();

2
include/render_object.h

@ -22,7 +22,7 @@ struct render_object
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);

12
src/entity.cpp

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

96
src/mesh.cpp

@ -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)
{

2
src/render_object.cpp

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

Loading…
Cancel
Save