Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skeleton import functionality to mesh_loader #1654

Merged
merged 3 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions src/rviz/mesh_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@
*/

#include "mesh_loader.h"
#include <OGRE/OgrePrerequisites.h>
#include <resource_retriever/retriever.h>

#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>

#include <OGRE/OgreSkeleton.h>
#include <OGRE/OgreSkeletonManager.h>
#include <OGRE/OgreSkeletonSerializer.h>
#include <OGRE/OgreMeshManager.h>
#include <OGRE/OgreTextureManager.h>
#include <OGRE/OgreMaterialManager.h>
Expand All @@ -56,6 +60,7 @@
#include <assimp/postprocess.h>
#include <assimp/IOStream.hpp>
#include <assimp/IOSystem.hpp>
#include <boost/filesystem/operations.hpp>

namespace fs = boost::filesystem;

Expand Down Expand Up @@ -699,6 +704,7 @@ Ogre::MeshPtr loadMeshFromResource(const std::string& resource_path)
{
return Ogre::MeshPtr();
}
loadSkeletonFromResource(resource_path); // load skeleton to the resource manager

Ogre::MeshSerializer ser;
Ogre::DataStreamPtr stream(new Ogre::MemoryDataStream(res.data.get(), res.size));
Expand Down Expand Up @@ -729,4 +735,46 @@ Ogre::MeshPtr loadMeshFromResource(const std::string& resource_path)
return Ogre::MeshPtr();
}

Ogre::SkeletonPtr loadSkeletonFromResource(const std::string& resource_path)
{
std::string skeleton_resource_path = resource_path.substr(0, resource_path.length() - 4);
skeleton_resource_path.append("skeleton");

if (Ogre::SkeletonManager::getSingleton().resourceExists(skeleton_resource_path))
{
return Ogre::SkeletonManager::getSingleton().getByName(skeleton_resource_path);
}
else
{
resource_retriever::Retriever retriever;
resource_retriever::MemoryResource res;
try
{
res = retriever.get(skeleton_resource_path);
}
catch (resource_retriever::Exception& e)
{
ROS_ERROR("%s", e.what());
return Ogre::SkeletonPtr();
}

if (res.size == 0)
{
return Ogre::SkeletonPtr();
}

fs::path skeleton_path(skeleton_resource_path);

Ogre::SkeletonSerializer ser;
Ogre::DataStreamPtr stream(new Ogre::MemoryDataStream(res.data.get(), res.size));
Ogre::SkeletonPtr skeleton = Ogre::SkeletonManager::getSingleton().create(
skeleton_path.filename().c_str(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, true);
ser.importSkeleton(stream, skeleton.get());

return skeleton;
}

return Ogre::SkeletonPtr();
}

} // namespace rviz
3 changes: 2 additions & 1 deletion src/rviz/mesh_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
#define RVIZ_MESH_LOADER_H

#include <OGRE/OgreMesh.h>
#include <OGRE/OgreSkeleton.h>

namespace rviz
{
Ogre::MeshPtr loadMeshFromResource(const std::string& resource_path);

Ogre::SkeletonPtr loadSkeletonFromResource(const std::string& resource_path);
} // namespace rviz

#endif // RVIZ_MESH_LOADER_H