22 #include <gazebo/gazebo.hh> 23 #include <gazebo/common/Event.hh> 24 #include <gazebo/common/Time.hh> 25 #include <gazebo/common/Plugin.hh> 26 #include <gazebo/rendering/rendering.hh> 27 #include <gazebo/rendering/RenderTypes.hh> 28 #include <gazebo/rendering/Scene.hh> 29 #include <gazebo/rendering/Visual.hh> 30 #include <gazebo/sensors/Sensor.hh> 31 #include <gazebo/sensors/SensorManager.hh> 32 #include <gazebo/transport/transport.hh> 33 #include <gazebo/transport/Node.hh> 35 #include <ignition/math/Vector2.hh> 36 #include <ignition/math/Vector3.hh> 38 #include "gazebo/rendering/ogre_gazebo.h" 39 #include "gazebo/rendering/Camera.hh" 40 #include "gazebo/rendering/UserCamera.hh" 51 GZ_REGISTER_VISUAL_PLUGIN(WavefieldVisualPlugin)
62 _vout = Ogre::Vector2::ZERO;
65 gzerr <<
"Vector must have size 2 or less" << std::endl;
68 for (
size_t i = 0; i < _v.size(); ++i)
80 _vout = Ogre::Vector3::ZERO;
83 gzerr <<
"Vector must have size 3 or less" << std::endl;
86 for (
size_t i = 0; i < _v.size(); ++i)
96 void ToOgreVector2(
const ignition::math::Vector2d& _v, Ogre::Vector2& _vout)
106 void ToOgreVector3(
const ignition::math::Vector3d& _v, Ogre::Vector3& _vout)
114 const std::vector<ignition::math::Vector2d>& _v,
115 Ogre::Vector2& _vout0,
116 Ogre::Vector2& _vout1,
117 Ogre::Vector2& _vout2
120 _vout0 = Ogre::Vector2::ZERO;
121 _vout1 = Ogre::Vector2::ZERO;
122 _vout2 = Ogre::Vector2::ZERO;
126 gzerr <<
"Vector must have size 3 or less" << std::endl;
138 const std::vector<ignition::math::Vector3d>& _v,
139 Ogre::Vector3& _vout0,
140 Ogre::Vector3& _vout1,
141 Ogre::Vector3& _vout2
144 _vout0 = Ogre::Vector3::ZERO;
145 _vout1 = Ogre::Vector3::ZERO;
146 _vout2 = Ogre::Vector3::ZERO;
150 gzerr <<
"Vector must have size 3 or less" << std::endl;
170 planeDown(
"planeDown")
180 public: sdf::ElementPtr
sdf;
186 public:
bool isStatic =
false;
189 public:
bool enableRtt =
true;
193 public:
double refractOpacity = 0;
197 public:
double reflectOpacity = 0;
200 public:
double rttNoise = 0;
203 public:
double simTime = 0;
206 public:
bool isInitialised =
false;
209 public: gazebo::rendering::ScenePtr
scene;
210 public: Ogre::Entity* oceanEntity =
nullptr;
213 public: Ogre::TextureUnitState *reflectTex =
nullptr;
214 public: Ogre::TextureUnitState *refractTex =
nullptr;
229 WavefieldVisualPlugin::~WavefieldVisualPlugin()
232 this->data->waveParams.reset();
235 this->data->preRenderConnection.reset();
236 this->data->cameraPreRenderConnection.reset();
239 WavefieldVisualPlugin::WavefieldVisualPlugin() :
241 RenderTargetListener(),
244 this->
data->isInitialised =
false;
248 rendering::VisualPtr _visual,
249 sdf::ElementPtr _sdf)
257 GZ_ASSERT(_visual !=
nullptr,
"Visual must not be null");
258 GZ_ASSERT(_sdf !=
nullptr,
"SDF Element must not be null");
261 this->
data->visual = _visual;
262 this->
data->sdf = _sdf;
265 #if GAZEBO_MAJOR_VERSION >= 8 266 this->
data->visualName = _visual->Name();
268 this->
data->visualName = _visual->GetName();
271 gzmsg <<
"WavefieldVisualPlugin <" << this->
data->visualName
272 <<
">: Loading WaveParamaters from SDF" << std::endl;
276 #if (GAZEBO_MAJOR_VERSION == 7 && GAZEBO_MINOR_VERSION >= 16) || \ 277 (GAZEBO_MAJOR_VERSION == 9 && GAZEBO_MINOR_VERSION >= 11) 282 this->
data->enableRtt =
false;
286 this->
data->refractOpacity =
288 this->
data->reflectOpacity =
290 this->
data->rttNoise =
294 if (_sdf->HasElement(
"wave"))
296 gzmsg <<
"Found <wave> tag" << std::endl;
297 sdf::ElementPtr sdfWave = _sdf->GetElement(
"wave");
298 this->
data->waveParams->SetFromSDF(*sdfWave);
302 gzerr <<
"Missing <wave> tag" << std::endl;
306 this->
data->waveParams->DebugPrint();
309 Ogre::SceneNode *ogreNode = this->
data->visual->GetSceneNode();
310 this->
data->oceanEntity =
311 dynamic_cast<Ogre::Entity *
>(ogreNode->getAttachedObject(0));
312 if (!this->
data->oceanEntity)
314 gzerr <<
"No ocean entity found" << std::endl;
319 this->
data->oceanEntity->setRenderQueueGroup(this->
data->oceanEntity->
320 getRenderQueueGroup()+1);
323 if (this->
data->enableRtt)
327 this->
data->preRenderConnection = event::Events::ConnectPreRender(
338 if (!this->
data->isInitialised)
341 std::string shaderType =
"vertex";
343 this->
data->visual->SetMaterialShaderParam(
344 "time", shaderType, std::to_string(0.0));
347 "time", shaderType, std::to_string(0.0));
351 this->
data->isInitialised =
true;
363 if (this->
data->enableRtt)
373 if (!this->
data->isStatic)
376 this->
data->visual->SetMaterialShaderParam(
377 "time", shaderType, std::to_string(simTime));
379 auto simTime = this->
data->visual->GetScene()->SimTime();
382 std::to_string(static_cast<float>(simTime.Double())));
393 this->
data->scene = this->
data->visual->GetScene();
396 this->
data->planeUp = Ogre::MovablePlane(Ogre::Vector3::UNIT_Z,
397 Ogre::Vector3::ZERO);
398 this->
data->planeDown = Ogre::MovablePlane(-Ogre::Vector3::UNIT_Z,
399 Ogre::Vector3::ZERO);
402 Ogre::MaterialPtr material =
403 Ogre::MaterialManager::getSingleton().getByName(this->
data->visual->
405 this->
data->reflectTex = (material->getTechnique(0)->getPass(0)->
406 getTextureUnitState(2));
408 this->
data->refractTex = (material->getTechnique(0)->getPass(0)->
409 getTextureUnitState(3));
413 "refractOpacity",
"fragment",
414 std::to_string(static_cast<float>(this->data->refractOpacity)));
416 "reflectOpacity",
"fragment",
417 std::to_string(static_cast<float>(this->data->reflectOpacity)));
419 "rttNoise",
"fragment",
420 std::to_string(static_cast<float>(this->data->rttNoise)));
423 if (this->
data->scene->UserCameraCount() > 0)
426 "flipAcrossY",
"fragment",
432 "flipAcrossY",
"fragment",
436 #if (GAZEBO_MAJOR_VERSION == 7 && GAZEBO_MINOR_VERSION >= 16) || \ 437 (GAZEBO_MAJOR_VERSION == 9 && GAZEBO_MINOR_VERSION >= 11) 440 this->
data->cameraPreRenderConnection =
441 rendering::Events::ConnectCameraPreRender(
443 this, std::placeholders::_1));
449 #if GAZEBO_MAJOR_VERSION >= 8 450 ignition::math::Pose3d pose = this->
data->visual->WorldPose();
451 Ogre::Vector3 oceanPosition(pose.Pos().X(),
455 Ogre::Quaternion oceanRotation(pose.Rot().W(),
460 math::Pose pose = this->
data->visual->GetWorldPose();
461 Ogre::Vector3 oceanPosition(pose.pos.x,
465 Ogre::Quaternion oceanRotation(pose.rot.w,
470 Ogre::Vector3 oceanNormal = oceanRotation * Ogre::Vector3::UNIT_Z;
471 this->
data->planeUp.redefine(oceanNormal, oceanPosition);
472 this->
data->planeDown.redefine(-oceanNormal, oceanPosition);
478 if (this->
data->scene->UserCameraCount() > 0)
481 rendering::UserCameraPtr userCamera = this->
data->scene->GetUserCamera(0);
484 if (std::find(this->
data->cameras.begin(), this->
data->cameras.end(),
485 userCamera->OgreCamera()) == this->
data->cameras.end())
495 std::vector<rendering::CameraPtr> newCameras = this->
NewCameras();
496 for (rendering::CameraPtr c : newCameras)
506 const double kScale = 0.25;
507 const int kWidth = _camera->getViewport()->getActualWidth() * kScale;
508 const int kHeight = _camera->getViewport()->getActualHeight() * kScale;
511 Ogre::TexturePtr rttReflectionTexture =
512 Ogre::TextureManager::getSingleton().createManual(
513 this->
data->visualName +
"_" + _camera->getName() +
"_reflection",
514 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
519 Ogre::TU_RENDERTARGET);
522 Ogre::TexturePtr rttRefractionTexture =
523 Ogre::TextureManager::getSingleton().createManual(
524 this->
data->visualName +
"_" + _camera->getName() +
"_refraction",
525 Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
530 Ogre::TU_RENDERTARGET);
532 Ogre::ColourValue backgroundColor =
533 rendering::Conversions::Convert(this->
data->scene->BackgroundColor());
536 Ogre::RenderTarget* reflectionRt =
537 rttReflectionTexture->getBuffer()->getRenderTarget();
538 reflectionRt->setAutoUpdated(
false);
539 Ogre::Viewport *reflVp =
540 reflectionRt->addViewport(_camera);
541 reflVp->setClearEveryFrame(
true);
542 reflVp->setOverlaysEnabled(
false);
543 reflVp->setBackgroundColour(backgroundColor);
544 reflVp->setVisibilityMask(GZ_VISIBILITY_ALL &
545 ~(GZ_VISIBILITY_GUI | GZ_VISIBILITY_SELECTABLE));
546 reflectionRt->addListener(
this);
549 Ogre::RenderTarget* refractionRt =
550 rttRefractionTexture->getBuffer()->getRenderTarget();
551 refractionRt->setAutoUpdated(
false);
552 Ogre::Viewport *refrVp =
553 refractionRt->addViewport(_camera);
554 refrVp->setClearEveryFrame(
true);
555 refrVp->setOverlaysEnabled(
false);
556 refrVp->setBackgroundColour(backgroundColor);
557 refrVp->setVisibilityMask(GZ_VISIBILITY_ALL &
558 ~(GZ_VISIBILITY_GUI | GZ_VISIBILITY_SELECTABLE));
559 refractionRt->addListener(
this);
562 this->
data->cameras.push_back(_camera);
563 this->
data->reflectionRts.push_back(reflectionRt);
564 this->
data->refractionRts.push_back(refractionRt);
567 this->
data->reflectTex->addFrameTextureName(rttReflectionTexture
569 this->
data->refractTex->addFrameTextureName(rttRefractionTexture
575 std::vector<rendering::CameraPtr> retVal;
577 auto sensors = sensors::SensorManager::Instance()->GetSensors();
578 for (
auto sensor : sensors)
580 if (sensor->Type() ==
"camera")
582 rendering::CameraPtr c = this->
data->scene->GetCamera(
583 this->
data->scene->StripSceneName(sensor->ScopedName()));
585 if (c && std::find(this->
data->cameras.begin(),
586 this->
data->cameras.end(),
587 c->OgreCamera()) == this->
data->cameras.end())
601 const std::string shaderType =
"vertex";
604 Ogre::Vector3 amplitude = Ogre::Vector3::ZERO;
605 Ogre::Vector3 wavenumber = Ogre::Vector3::ZERO;
606 Ogre::Vector3 omega = Ogre::Vector3::ZERO;
607 Ogre::Vector3 steepness = Ogre::Vector3::ZERO;
608 Ogre::Vector2 dir0 = Ogre::Vector2::ZERO;
609 Ogre::Vector2 dir1 = Ogre::Vector2::ZERO;
610 Ogre::Vector2 dir2 = Ogre::Vector2::ZERO;
619 auto& visual = *this->
data->visual;
621 visual.SetMaterialShaderParam(
622 "amplitude", shaderType, Ogre::StringConverter::toString(amplitude));
623 visual.SetMaterialShaderParam(
624 "wavenumber", shaderType, Ogre::StringConverter::toString(wavenumber));
625 visual.SetMaterialShaderParam(
626 "omega", shaderType, Ogre::StringConverter::toString(omega));
627 visual.SetMaterialShaderParam(
628 "steepness", shaderType, Ogre::StringConverter::toString(steepness));
629 visual.SetMaterialShaderParam(
630 "dir0", shaderType, Ogre::StringConverter::toString(dir0));
631 visual.SetMaterialShaderParam(
632 "dir1", shaderType, Ogre::StringConverter::toString(dir1));
633 visual.SetMaterialShaderParam(
634 "dir2", shaderType, Ogre::StringConverter::toString(dir2));
637 "Nwaves", shaderType, std::to_string(this->
data->waveParams->Number()));
639 "amplitude", shaderType, Ogre::StringConverter::toString(amplitude));
641 "wavenumber", shaderType, Ogre::StringConverter::toString(wavenumber));
643 "omega", shaderType, Ogre::StringConverter::toString(omega));
645 "steepness", shaderType, Ogre::StringConverter::toString(steepness));
647 "dir0", shaderType, Ogre::StringConverter::toString(dir0));
649 "dir1", shaderType, Ogre::StringConverter::toString(dir1));
651 "dir2", shaderType, Ogre::StringConverter::toString(dir2));
652 float tau = this->
data->waveParams->Tau();
654 "tau", shaderType, Ogre::StringConverter::toString(tau));
664 rendering::CameraPtr camSource;
665 if (this->
data->scene->UserCameraCount() > 0)
666 camSource = this->
data->scene->GetUserCamera(0);
668 camSource = this->
data->scene->GetCamera(_camera);
671 for (
unsigned int i = 0; i < this->
data->cameras.size(); ++i)
673 if (camSource->OgreCamera() == this->
data->cameras.at(i))
675 this->
data->reflectionRts.at(i)->update();
676 this->
data->refractionRts.at(i)->update();
686 const Ogre::RenderTargetEvent& rte)
689 if (this->
data->oceanEntity)
691 this->
data->oceanEntity->setVisible(
false);
695 for (
unsigned int i = 0; i < this->
data->reflectionRts.size(); ++i)
697 Ogre::RenderTarget* rt = this->
data->reflectionRts.at(i);
698 if (rte.source == rt)
700 this->
data->cameras.at(i)->enableReflection(this->
data->planeUp);
701 this->
data->cameras.at(i)->enableCustomNearClipPlane(this->
data->
703 this->
data->reflectTex->setCurrentFrame(i);
709 for (
unsigned int i = 0; i < this->
data->refractionRts.size(); ++i)
711 Ogre::RenderTarget* rt = this->
data->refractionRts.at(i);
712 if (rte.source == rt)
714 this->
data->cameras.at(i)->enableCustomNearClipPlane(this->
data->
716 this->
data->refractTex->setCurrentFrame(i);
723 const Ogre::RenderTargetEvent& rte)
726 if (this->
data->oceanEntity)
728 this->
data->oceanEntity->setVisible(
true);
732 for (
unsigned int i = 0; i < this->
data->reflectionRts.size(); ++i)
734 Ogre::RenderTarget* rt = this->
data->reflectionRts.at(i);
735 if (rte.source == rt)
737 this->
data->cameras.at(i)->disableReflection();
738 this->
data->cameras.at(i)->disableCustomNearClipPlane();
744 for (
unsigned int i = 0; i < this->
data->refractionRts.size(); ++i)
746 Ogre::RenderTarget* rt = this->
data->refractionRts.at(i);
747 if (rte.source == rt)
749 this->
data->cameras.at(i)->disableCustomNearClipPlane();
std::shared_ptr< WaveParameters > waveParams
The wavefield parameters.
Ogre::MovablePlane planeUp
void OnCameraPreRender(const std::string &_camera)
Update rtts before cameras.
void CreateRtts(Ogre::Camera *_camera)
Create reflection refraction rtts for a given camera Stores the render target and given camera...
void AddNewCamerasForReflectionRefraction()
Check for new cameras, setup rtts for them.
This file defines utilities for extracting parameters from SDF.
void ToOgreVector3(const std::vector< double > &_v, Ogre::Vector3 &_vout)
Convert a vector containing three doubles to an Ogre Vector3.
WavefieldVisualPluginPrivate()
void SetShaderParams()
Update the vertex shader parameters.
gazebo::rendering::ScenePtr scene
std::string getName(void *handle)
event::ConnectionPtr cameraPreRenderConnection
std::string visualName
The visual's name.
std::shared_ptr< WavefieldVisualPluginPrivate > data
static bool SdfParamBool(sdf::Element &_sdf, const std::string &_paramName, const bool _defaultVal)
Extract a named bool parameter from an SDF element.
A class to manage the parameters for generating a wave.
std::vector< Ogre::RenderTarget * > reflectionRts
static double SdfParamDouble(sdf::Element &_sdf, const std::string &_paramName, const double _defaultVal)
Extract a named double parameter from an SDF element.
sdf::ElementPtr sdf
The wavefield visual plugin SDF.
std::vector< Ogre::Camera * > cameras
virtual void preRenderTargetUpdate(const Ogre::RenderTargetEvent &rte)
Hide/Show objects for reflection/refraction render eg. hide objects above water for refraction hide o...
event::ConnectionPtr preRenderConnection
Event based connections.
Support for methods not available in legacy versions of Gazebo.
void UpdateClipPlanes()
Move and rotate clip planes to match ocean pose.
void ToOgreVector2(const std::vector< ignition::math::Vector2d > &_v, Ogre::Vector2 &_vout0, Ogre::Vector2 &_vout1, Ogre::Vector2 &_vout2)
rendering::VisualPtr visual
The visual containing this plugin.
virtual void Init()
Initialize the plugin.
virtual void Reset()
Reset the plugin.
void ToOgreVector3(const std::vector< ignition::math::Vector3d > &_v, Ogre::Vector3 &_vout0, Ogre::Vector3 &_vout1, Ogre::Vector3 &_vout2)
void SetMaterialShaderParam(Visual &_visual, const std::string &_paramName, const std::string &_shaderType, const std::string &_value)
Set a shader program parameter associated to this visual's material.
std::vector< gazebo::rendering::CameraPtr > NewCameras()
Get new cameras not already contained in this->data->cameras.
void SetupReflectionRefraction()
Setup Ogre objects for reflection/refraction.
This file contains definitions for classes used to manage a wave field. This includes wave parameters...
This file defines a Gazebo VisualPlugin used to render a wave field and keep it synchronised with the...
virtual void postRenderTargetUpdate(const Ogre::RenderTargetEvent &rte)
virtual void Load(gazebo::rendering::VisualPtr _visual, sdf::ElementPtr _sdf)
Load the plugin.
void ToOgreVector2(const std::vector< double > &_v, Ogre::Vector2 &_vout)
Convert a vector containing two doubles to an Ogre Vector2.
void OnPreRender()
Called every PreRender event.
Ogre::MovablePlane planeDown
std::vector< Ogre::RenderTarget * > refractionRts