Skip to content

Commit

Permalink
[ign-common3] Support fuel URLs for textures (#102)
Browse files Browse the repository at this point in the history
Resolves gazebosim/gz-sim#343

Signed-off-by: John Shepherd <john@openrobotics.org>
Signed-off-by: Michael Carroll <michael@openrobotics.org>
  • Loading branch information
John Shepherd authored and mjcarroll committed Oct 19, 2020
1 parent ba80f90 commit f5ee75a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
15 changes: 11 additions & 4 deletions graphics/src/Material.cc
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,23 @@ void Material::SetTextureImage(const std::string &_tex)
void Material::SetTextureImage(const std::string &_tex,
const std::string &_resourcePath)
{
this->dataPtr->texImage = _resourcePath + "/" + _tex;
this->dataPtr->texImage = common::joinPaths(_resourcePath, _tex);

// If the texture image doesn't exist then try the next most likely path.
if (!exists(this->dataPtr->texImage))
{
this->dataPtr->texImage = _resourcePath + "/../materials/textures/" + _tex;
// Try to resolve this texture image to a locally cached path in a
// separate directory
this->dataPtr->texImage = common::findFile(_tex);
if (!exists(this->dataPtr->texImage))
{
ignerr << "Unable to find texture[" << _tex << "] in path["
<< _resourcePath << "]\n";
this->dataPtr->texImage = common::joinPaths(_resourcePath, "..",
"materials", "textures", _tex);
if (!exists(this->dataPtr->texImage))
{
ignerr << "Unable to find texture [" << _tex << "] as a locally"
" cached texture or in path ["<< _resourcePath << "]\n";
}
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions graphics/src/Material_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <gtest/gtest.h>

#include "test_config.h"
#include "ignition/common/Material.hh"
#include "ignition/common/Pbr.hh"
#include "test/util.hh"
Expand All @@ -35,8 +36,15 @@ TEST_F(MaterialTest, Material)
EXPECT_STREQ("texture_image", mat.TextureImage().c_str());

mat.SetTextureImage("texture_image", "/path");
EXPECT_STREQ("/path/../materials/textures/texture_image",
mat.TextureImage().c_str());
std::string texturePath = common::joinPaths("/path", "..",
"materials", "textures", "texture_image");
EXPECT_STREQ(texturePath.c_str(), mat.TextureImage().c_str());

texturePath = common::joinPaths(std::string(PROJECT_SOURCE_PATH), "test",
"data", "box.dae");

mat.SetTextureImage(texturePath, "bad_path");
EXPECT_STREQ(texturePath.c_str(), mat.TextureImage().c_str());

mat.SetAmbient(math::Color(0.1f, 0.2f, 0.3f, 0.4f));
EXPECT_TRUE(mat.Ambient() == math::Color(0.1f, 0.2f, 0.3f, 0.4f));
Expand Down

0 comments on commit f5ee75a

Please sign in to comment.