Skip to content

Commit

Permalink
Added publisher for shadow caster material name
Browse files Browse the repository at this point in the history
Signed-off-by: William Lew <WilliamMilesLew@gmail.com>
  • Loading branch information
WilliamLewww committed Jul 16, 2021
1 parent 6cc547d commit 9e58ed1
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 13 deletions.
28 changes: 28 additions & 0 deletions gazebo/physics/World.cc
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ void World::Load(sdf::ElementPtr _sdf)
msgs::SceneFromSDF(this->dataPtr->sdf->GetElement("scene")));
this->dataPtr->sceneMsg.set_name(this->Name());

if (this->dataPtr->sdf->GetElement("scene")->
HasElement("shadow_caster_material_name"))
{
this->dataPtr->shadowCasterMaterialName =
this->dataPtr->sdf->GetElement("scene")->
Get<std::string>("shadow_caster_material_name");
}
else
{
this->dataPtr->shadowCasterMaterialName = "Gazebo/shadow_caster";
}

// The period at which messages are processed
this->dataPtr->processMsgsPeriod = common::Time(0, 200000000);

Expand Down Expand Up @@ -284,6 +296,14 @@ void World::Load(sdf::ElementPtr _sdf)
<< std::endl;
}

std::string shadowCasterService("/shadow_caster_material_name");
if (!this->dataPtr->ignNode.Advertise(shadowCasterService,
&World::ShadowCasterService, this))
{
gzerr << "Error advertising service [" << shadowCasterService << "]"
<< std::endl;
}

// This should come before loading of entities
sdf::ElementPtr physicsElem = this->dataPtr->sdf->GetElement("physics");

Expand Down Expand Up @@ -3337,3 +3357,11 @@ bool World::PluginInfoService(const ignition::msgs::StringMsg &_req,

return false;
}

//////////////////////////////////////////////////
bool World::ShadowCasterService(const ignition::msgs::StringMsg &_req,
ignition::msgs::StringMsg &_res)
{
_res.set_data(this->dataPtr->shadowCasterMaterialName.c_str());
return true;
}
3 changes: 3 additions & 0 deletions gazebo/physics/World.hh
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ namespace gazebo
private: bool PluginInfoService(const ignition::msgs::StringMsg &_request,
ignition::msgs::Plugin_V &_plugins);

private: bool ShadowCasterService(const ignition::msgs::StringMsg &_request,
ignition::msgs::StringMsg &_response);

/// \internal
/// \brief Private data pointer.
private: std::unique_ptr<WorldPrivate> dataPtr;
Expand Down
3 changes: 3 additions & 0 deletions gazebo/physics/WorldPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ namespace gazebo

/// \brief SDF World DOM object
public: std::unique_ptr<sdf::World> worldSDFDom;

/// \brief Shadow caster material name from scene SDF
public: std::string shadowCasterMaterialName;
};
}
}
Expand Down
9 changes: 1 addition & 8 deletions gazebo/rendering/RTShaderSystem.cc
Original file line number Diff line number Diff line change
Expand Up @@ -514,13 +514,7 @@ void RTShaderSystem::RemoveShadows(ScenePtr _scene)
/////////////////////////////////////////////////
void RTShaderSystem::ApplyShadows(ScenePtr _scene)
{
bool matchingShadowCasterName =
this->dataPtr->shadowCasterMaterialName ==
_scene->ShadowCasterMaterialName();

if ((!this->dataPtr->initialized || this->dataPtr->shadowsApplied) &&
matchingShadowCasterName)

if (!this->dataPtr->initialized || this->dataPtr->shadowsApplied)
return;

Ogre::SceneManager *sceneMgr = _scene->OgreSceneManager();
Expand Down Expand Up @@ -807,5 +801,4 @@ void RTShaderSystem::UpdateShadows(ScenePtr _scene)
#else
sceneMgr->setShadowTextureCasterMaterial(_scene->ShadowCasterMaterialName());
#endif
this->dataPtr->shadowCasterMaterialName = _scene->ShadowCasterMaterialName();
}
2 changes: 0 additions & 2 deletions gazebo/rendering/RTShaderSystemPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ namespace gazebo

/// \brief Mutex to protect shaders and shadows update
public: std::mutex updateMutex;

public: std::string shadowCasterMaterialName;
};
}
}
Expand Down
21 changes: 18 additions & 3 deletions gazebo/rendering/Scene.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ Scene::Scene(const std::string &_name, const bool _enableVisualizations,

this->dataPtr->sceneSimTimePosesApplied = common::Time();
this->dataPtr->sceneSimTimePosesReceived = common::Time();

ignition::transport::Node node;
// Prepare the input parameters.
ignition::msgs::StringMsg req;
req.set_data("HELLO");
ignition::msgs::StringMsg rep;
bool result;
unsigned int timeout = 5000;
bool executed = node.Request("/shadow_caster_material_name", req, timeout, rep, result);
if (executed)
{
if (result)
this->dataPtr->shadowCasterMaterialName = rep.data();
else
std::cout << "Service call failed" << std::endl;
}
else
std::cerr << "Service call timed out" << std::endl;
}

//////////////////////////////////////////////////
Expand Down Expand Up @@ -1624,7 +1642,6 @@ bool Scene::ProcessSceneMsg(ConstScenePtr &_msg)
elem->Get<double>("start"),
elem->Get<double>("end"));
}

return true;
}

Expand Down Expand Up @@ -1900,8 +1917,6 @@ void Scene::PreRender()
{
if (!this->dataPtr->initialized)
RTShaderSystem::Instance()->UpdateShaders();

RTShaderSystem::Instance()->ApplyShadows(shared_from_this());
this->dataPtr->initialized = true;
sceneMsgsCopy.erase(sIter++);
}
Expand Down

0 comments on commit 9e58ed1

Please sign in to comment.