Skip to content

Commit

Permalink
Introduces GZ_MESH_FORCE_ASSIMP flag (#403)
Browse files Browse the repository at this point in the history
Signed-off-by: Onur Berk Tore <[email protected]>
  • Loading branch information
Onur Berk Töre authored Jul 29, 2022
1 parent 4484e5e commit afc47c8
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
12 changes: 10 additions & 2 deletions graphics/include/gz/common/MeshManager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ namespace gz
class SubMesh;

/// \class MeshManager MeshManager.hh gz/common/MeshManager.hh
/// \brief Maintains and manages all meshes
/// \brief Maintains and manages all meshes. Supported mesh formats are
/// STL (STLA, STLB), COLLADA, OBJ, GLTF (GLB) and FBX. By default only GLTF
/// and FBX are loaded using assimp loader, however if GZ_MESH_FORCE_ASSIMP
/// environment variable is set, then MeshManager will use assimp loader for
/// all supported mesh formats.
class GZ_COMMON_GRAPHICS_VISIBLE MeshManager
: public SingletonT<MeshManager>
{
Expand Down Expand Up @@ -240,8 +244,12 @@ namespace gz
const gz::math::Vector2d &_segments,
const gz::math::Vector2d &_uvTile);

/// \brief Sets the forceAssimp flag by reading the GZ_MESH_FORCE_ASSIMP
/// environment variable. If forceAssimp true, MeshManager uses Assimp
/// for loading all mesh formats, otherwise only for GLTF and FBX.
public: void SetAssimpEnvs();

/// \brief Tesselate a 2D mesh
///
/// Makes a zigzag pattern compatible with strips
/// \param[in] _sm the mesh to tesselate
/// \param[in] _meshWith mesh width
Expand Down
43 changes: 33 additions & 10 deletions graphics/src/MeshManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ class gz::common::MeshManager::Implementation

/// \brief Mutex to protect the mesh map
public: std::mutex mutex;

/// \brief True if assimp is used for loading all supported mesh formats
public: bool forceAssimp;
#ifdef _WIN32
#pragma warning(pop)
#endif
Expand Down Expand Up @@ -146,19 +149,26 @@ const Mesh *MeshManager::Load(const std::string &_filename)
std::transform(extension.begin(), extension.end(),
extension.begin(), ::tolower);
MeshLoader *loader = nullptr;

if (extension == "stl" || extension == "stlb" || extension == "stla")
loader = &this->dataPtr->stlLoader;
else if (extension == "dae")
loader = &this->dataPtr->colladaLoader;
else if (extension == "obj")
loader = &this->dataPtr->objLoader;
else if (extension == "gltf" || extension == "glb" || extension == "fbx")
this->SetAssimpEnvs();
if (this->dataPtr->forceAssimp)
{
loader = &this->dataPtr->assimpLoader;
}
else
{
gzerr << "Unsupported mesh format for file[" << _filename << "]\n";
return nullptr;
if (extension == "stl" || extension == "stlb" || extension == "stla")
loader = &this->dataPtr->stlLoader;
else if (extension == "dae")
loader = &this->dataPtr->colladaLoader;
else if (extension == "obj")
loader = &this->dataPtr->objLoader;
else if (extension == "gltf" || extension == "glb" || extension == "fbx")
loader = &this->dataPtr->assimpLoader;
else
{
gzerr << "Unsupported mesh format for file[" << _filename << "]\n";
return nullptr;
}
}
// This mutex prevents two threads from loading the same mesh at the
// same time.
Expand Down Expand Up @@ -1619,3 +1629,16 @@ void MeshManager::ConvertPolylinesToVerticesAndEdges(
}
}
}

//////////////////////////////////////////////////
void MeshManager::SetAssimpEnvs()
{
std::string forceAssimpEnv;
common::env("GZ_MESH_FORCE_ASSIMP", forceAssimpEnv);
this->dataPtr->forceAssimp = false;
if (forceAssimpEnv == "true")
{
gzmsg << "Using assimp to load all mesh formats" << std::endl;
this->dataPtr->forceAssimp = true;
}
}

0 comments on commit afc47c8

Please sign in to comment.