Skip to content
This repository has been archived by the owner on Feb 3, 2025. It is now read-only.

Fix multiple reflectance maps and improve performance #2742

Merged
merged 3 commits into from
May 28, 2020
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
5 changes: 5 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## Gazebo 9

## Gazebo 9.XX.X (20XX-XX-XX)

1. Fix multiple reflectance maps and improve performance
* [Pull request #2724](https://github.com/osrf/gazebo/pull/2742)

## Gazebo 9.13.0 (2020-04-03)

1. Use target based compile options to specify C++ standard
Expand Down
73 changes: 43 additions & 30 deletions gazebo/rendering/DepthCamera.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ namespace gazebo
private: std::string materialScheme;
};


/// \class ReflectanceRenderTargetListener
/// \brief Ogre render target listener.
class ReflectanceRenderTargetListener : public Ogre::RenderTargetListener
Expand Down Expand Up @@ -137,6 +136,12 @@ namespace gazebo

/// \brief Scene pointer
private: ScenePtr scene;

/// \brief Default reflectance material
private: Ogre::MaterialPtr reflectanceMaterial;

/// \brief Black material for objects with no reflectance map
private: Ogre::MaterialPtr blackMaterial;
};
}
}
Expand Down Expand Up @@ -334,11 +339,11 @@ void DepthCamera::CreateReflectanceTexture(const std::string &_textureName)
Ogre::ColourValue(Ogre::ColourValue(0, 0, 0)));

this->dataPtr->reflectanceViewport->setOverlaysEnabled(false);
this->dataPtr->reflectanceViewport->setSkiesEnabled(false);
this->dataPtr->reflectanceViewport->setShadowsEnabled(false);
this->dataPtr->reflectanceViewport->setVisibilityMask(
GZ_VISIBILITY_ALL & ~(GZ_VISIBILITY_GUI | GZ_VISIBILITY_SELECTABLE));

this->dataPtr->reflectanceViewport->setMaterialScheme("reflectance_map");

this->dataPtr->reflectanceMaterialSwitcher.reset(
new ReflectanceMaterialSwitcher(this->scene,
this->dataPtr->reflectanceViewport));
Expand Down Expand Up @@ -789,14 +794,29 @@ void ReflectanceRenderTargetListener::postRenderTargetUpdate(
ReflectanceMaterialListener::ReflectanceMaterialListener(ScenePtr _scene)
:scene(_scene)
{
// load the reflectance and black material
std::string material = "Gazebo/Reflectance";
Ogre::MaterialManager::getSingleton().load(material,
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
this->reflectanceMaterial =
Ogre::MaterialManager::getSingleton().getByName(material);

material = "Gazebo/Black";
Ogre::MaterialManager::getSingleton().load(material,
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
this->blackMaterial =
Ogre::MaterialManager::getSingleton().getByName("Gazebo/Black");
}

/////////////////////////////////////////////////
Ogre::Technique *ReflectanceMaterialListener::handleSchemeNotFound(
uint16_t /*_schemeIndex*/, const Ogre::String & /*_schemeName*/,
uint16_t /*_schemeIndex*/, const Ogre::String & _schemeName,
Ogre::Material *_originalMaterial, uint16_t /*_lodIndex*/,
const Ogre::Renderable *_rend)
{
if (_schemeName != "reflectance_map")
return _originalMaterial->getBestTechnique();

if (_rend && typeid(*_rend) == typeid(Ogre::SubEntity))
{
std::string material = "";
Expand Down Expand Up @@ -851,41 +871,34 @@ Ogre::Technique *ReflectanceMaterialListener::handleSchemeNotFound(
if (!visual)
return nullptr;

Ogre::MaterialPtr mat;
const Ogre::Any reflectanceMapAny = visual->GetSceneNode()->
getUserObjectBindings().getUserAny("reflectance_map");
getUserObjectBindings().getUserAny("reflectance_map");
if (!reflectanceMapAny.isEmpty())
{
material = "Gazebo/Reflectance";
reflectanceMap = Ogre::any_cast<std::string>(reflectanceMapAny);

// clone the material for each unique reflectance map
std::string materialUnique = "Gazebo/Reflectance_" + reflectanceMap;
mat = Ogre::MaterialManager::getSingleton().getByName(materialUnique);
if (mat.isNull())
{
mat = this->reflectanceMaterial->clone(materialUnique);
Ogre::Technique *technique = mat->getTechnique(0);
if (!reflectanceMap.empty())
{
Ogre::TextureUnitState *tus = technique->getPass(0)->
getTextureUnitState(0);
tus->setTextureName(reflectanceMap);
}
}
}
else
{
material = "Gazebo/Black";
}

// set the material for the models
Ogre::ResourcePtr res =
Ogre::MaterialManager::getSingleton().getByName(material);
if (res.isNull())
{
Ogre::MaterialManager::getSingleton().load(material,
Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
mat = this->blackMaterial;
}
Ogre::MaterialPtr mat;
// OGRE 1.9 changes the shared pointer definition
#if (OGRE_VERSION < ((1 << 16) | (9 << 8) | 0))
mat = static_cast<Ogre::MaterialPtr>(res);
#else
mat = res.staticCast<Ogre::Material>();
#endif

GZ_ASSERT(!mat.isNull(), "Reflectance material is null");
Ogre::Technique *technique = mat->getTechnique(0);
if (!reflectanceMap.empty())
{
Ogre::TextureUnitState *tus = technique->getPass(0)->
getTextureUnitState(0);
tus->setTextureName(reflectanceMap);
}

return technique;
}
Expand Down
2 changes: 1 addition & 1 deletion media/materials/scripts/gazebo.material
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ material Gazebo/Reflectance
texture_unit
{
}
}
}
}
}

vertex_program Gazebo/XYZNormalsVS glsl
{
Expand Down