Skip to content

Commit 101129f

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

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
@@ -367,29 +367,33 @@ void AssimpLoader::setLightColorsFromAssimp(
367367
void AssimpLoader::loadEmbeddedTexture(
368368
const aiTexture * texture, const std::string & resource_path)
369369
{
370-
if (texture == nullptr) {
371-
RVIZ_RENDERING_LOG_ERROR_STREAM("null texture!");
372-
return;
373-
}
370+
// Check if the embedded texture already exists within TextureManager before proceeding to
371+
// load it.
372+
if (!Ogre::TextureManager::getSingleton().resourceExists(resource_path, ROS_PACKAGE_NAME)) {
373+
if (texture == nullptr) {
374+
RVIZ_RENDERING_LOG_ERROR_STREAM("null texture!");
375+
return;
376+
}
377+
378+
// use the format hint to try to load the image
379+
std::string format_hint(
380+
texture->achFormatHint,
381+
strnlen(texture->achFormatHint, sizeof(texture->achFormatHint)));
374382

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

0 commit comments

Comments
 (0)