Skip to content

Commit f6da51b

Browse files
add resourceExists check to loadEmbeddedTexture before loading texture (#1542) (#1552)
(cherry picked from commit af656e3) Co-authored-by: John TGZ <[email protected]>
1 parent 1cdfce2 commit f6da51b

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

rviz_rendering/src/rviz_rendering/mesh_loader_helpers/assimp_loader.cpp

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -362,29 +362,33 @@ void AssimpLoader::setLightColorsFromAssimp(
362362
void AssimpLoader::loadEmbeddedTexture(
363363
const aiTexture * texture, const std::string & resource_path)
364364
{
365-
if (texture == nullptr) {
366-
RVIZ_RENDERING_LOG_ERROR_STREAM("null texture!");
367-
return;
368-
}
365+
// Check if the embedded texture already exists within TextureManager before proceeding to
366+
// load it.
367+
if (!Ogre::TextureManager::getSingleton().resourceExists(resource_path, ROS_PACKAGE_NAME)) {
368+
if (texture == nullptr) {
369+
RVIZ_RENDERING_LOG_ERROR_STREAM("null texture!");
370+
return;
371+
}
372+
373+
// use the format hint to try to load the image
374+
std::string format_hint(
375+
texture->achFormatHint,
376+
strnlen(texture->achFormatHint, sizeof(texture->achFormatHint)));
369377

370-
// use the format hint to try to load the image
371-
std::string format_hint(
372-
texture->achFormatHint,
373-
strnlen(texture->achFormatHint, sizeof(texture->achFormatHint)));
374-
375-
Ogre::DataStreamPtr stream(
376-
new Ogre::MemoryDataStream(
377-
(unsigned char *)texture->pcData, texture->mWidth));
378-
379-
try {
380-
Ogre::Image image;
381-
image.load(stream, format_hint.c_str());
382-
Ogre::TextureManager::getSingleton().loadImage(
383-
resource_path, ROS_PACKAGE_NAME, image);
384-
} catch (Ogre::Exception & e) {
385-
RVIZ_RENDERING_LOG_ERROR_STREAM(
386-
"Could not load texture [" << resource_path.c_str() <<
387-
"] with format hint [" << format_hint << "]: " << e.what());
378+
Ogre::DataStreamPtr stream(
379+
new Ogre::MemoryDataStream(
380+
(unsigned char *)texture->pcData, texture->mWidth));
381+
382+
try {
383+
Ogre::Image image;
384+
image.load(stream, format_hint.c_str());
385+
Ogre::TextureManager::getSingleton().loadImage(
386+
resource_path, ROS_PACKAGE_NAME, image);
387+
} catch (Ogre::Exception & e) {
388+
RVIZ_RENDERING_LOG_ERROR_STREAM(
389+
"Could not load texture [" << resource_path.c_str() <<
390+
"] with format hint [" << format_hint << "]: " << e.what());
391+
}
388392
}
389393
}
390394

0 commit comments

Comments
 (0)