Recast: allow loading OBJ and PLY geometry from project files

Deduplicated mesh load function and implemented a simple type handler instead. Code can now load OBJ and PLY geometry from the user interface and project files.
This commit is contained in:
Kawe Mazidjatari 2024-10-11 01:17:52 +02:00
parent b4b0e22d61
commit d9432129b2
2 changed files with 21 additions and 46 deletions

View File

@ -125,7 +125,7 @@ InputGeom::~InputGeom()
delete m_mesh; delete m_mesh;
} }
bool InputGeom::loadMesh(rcContext* ctx, const std::string& filepath) bool InputGeom::loadMesh(rcContext* ctx, const std::string& filepath, const MeshFormat format)
{ {
if (m_mesh) if (m_mesh)
{ {
@ -137,49 +137,18 @@ bool InputGeom::loadMesh(rcContext* ctx, const std::string& filepath)
m_offMeshConCount = 0; m_offMeshConCount = 0;
m_volumeCount = 0; m_volumeCount = 0;
switch (format)
{
case MESH_OBJ:
m_mesh = new rcMeshLoaderObj; m_mesh = new rcMeshLoaderObj;
if (!m_mesh) break;
{ case MESH_PLY:
ctx->log(RC_LOG_ERROR, "loadMesh: Out of memory 'm_mesh'.");
return false;
}
if (!m_mesh->load(filepath))
{
ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not load '%s'", filepath.c_str());
return false;
}
rcCalcBounds(m_mesh->getVerts(), m_mesh->getVertCount(), m_meshBMin, m_meshBMax);
rdVcopy(m_navMeshBMin, m_meshBMin);
rdVcopy(m_navMeshBMax, m_meshBMax);
m_chunkyMesh = new rcChunkyTriMesh;
if (!m_chunkyMesh)
{
ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Out of memory 'm_chunkyMesh'.");
return false;
}
if (!rcCreateChunkyTriMesh(m_mesh->getVerts(), m_mesh->getTris(), m_mesh->getTriCount(), 256, m_chunkyMesh))
{
ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Failed to build chunky mesh.");
return false;
}
return true;
}
bool InputGeom::loadPlyMesh(rcContext* ctx, const std::string& filepath)
{
if (m_mesh)
{
delete m_chunkyMesh;
m_chunkyMesh = 0;
delete m_mesh;
m_mesh = 0;
}
m_offMeshConCount = 0;
m_volumeCount = 0;
m_mesh = new rcMeshLoaderPly; m_mesh = new rcMeshLoaderPly;
break;
default: // Unhandled mesh format.
assert(0);
}
if (!m_mesh) if (!m_mesh)
{ {
ctx->log(RC_LOG_ERROR, "loadMesh: Out of memory 'm_mesh'."); ctx->log(RC_LOG_ERROR, "loadMesh: Out of memory 'm_mesh'.");
@ -209,6 +178,7 @@ bool InputGeom::loadPlyMesh(rcContext* ctx, const std::string& filepath)
return true; return true;
} }
bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath) bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath)
{ {
//NB(warmist): tf2 not implemented here //NB(warmist): tf2 not implemented here
@ -271,7 +241,7 @@ bool InputGeom::loadGeomSet(rcContext* ctx, const std::string& filepath)
name++; name++;
if (*name) if (*name)
{ {
if (!loadMesh(ctx, name)) if (!load(ctx, name))
{ {
delete [] buf; delete [] buf;
return false; return false;
@ -410,9 +380,9 @@ bool InputGeom::load(rcContext* ctx, const std::string& filepath)
if (extension == ".gset") if (extension == ".gset")
return loadGeomSet(ctx, filepath); return loadGeomSet(ctx, filepath);
if (extension == ".obj") if (extension == ".obj")
return loadMesh(ctx, filepath); return loadMesh(ctx, filepath, MESH_OBJ);
if (extension == ".ply") if (extension == ".ply")
return loadPlyMesh(ctx, filepath); return loadMesh(ctx, filepath, MESH_PLY);
return false; return false;
} }

View File

@ -108,6 +108,12 @@ struct BuildSettings
class InputGeom class InputGeom
{ {
enum MeshFormat
{
MESH_OBJ,
MESH_PLY
};
rcChunkyTriMesh* m_chunkyMesh; rcChunkyTriMesh* m_chunkyMesh;
IMeshLoader* m_mesh; IMeshLoader* m_mesh;
float m_meshBMin[3], m_meshBMax[3]; float m_meshBMin[3], m_meshBMax[3];
@ -138,8 +144,7 @@ class InputGeom
int m_volumeCount; int m_volumeCount;
///@} ///@}
bool loadMesh(class rcContext* ctx, const std::string& filepath); bool loadMesh(class rcContext* ctx, const std::string& filepath, const MeshFormat format);
bool loadPlyMesh(class rcContext* ctx, const std::string& filepath);
bool loadGeomSet(class rcContext* ctx, const std::string& filepath); bool loadGeomSet(class rcContext* ctx, const std::string& filepath);
public: public:
InputGeom(); InputGeom();