diff --git a/README.md b/README.md index 85abe809..252f63c2 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,31 @@ This package contains plugins that support the simulation of waves and surface v ## Dependencies You will need a working installation of ROS and Gazebo in order to use this package. -It has been tested with: -- Gazebo version 9.4.1 + +## Ubuntu + +- Ubuntu 18.04 - ROS Melodic Morenia +- Gazebo version 9.0.0 + +Install CGAL 4.13 libraries: + +```bash +sudo apt-get install libcgal-dev +``` + +### macOS + - OSX 10.11.6 +- ROS Melodic Morenia +- Gazebo version 9.6.0 + +Install CGAL 4.13 libraries: + +```bash +brew install cgal +``` ## Installation diff --git a/asv_wave_sim/package.xml b/asv_wave_sim/package.xml index 0248ba90..3735250c 100644 --- a/asv_wave_sim/package.xml +++ b/asv_wave_sim/package.xml @@ -1,7 +1,7 @@ asv_wave_sim - 0.1.1 + 0.1.2 This package contains Gazebo plugins and models for simulating water waves for surface vessels. diff --git a/asv_wave_sim_gazebo/package.xml b/asv_wave_sim_gazebo/package.xml index 203ddbad..b2e8c68a 100644 --- a/asv_wave_sim_gazebo/package.xml +++ b/asv_wave_sim_gazebo/package.xml @@ -1,7 +1,7 @@ asv_wave_sim_gazebo - 0.1.1 + 0.1.2 This package contains Gazebo media, models and worlds for simulating water waves and dynamics for surface vessels. There are ROS diff --git a/asv_wave_sim_gazebo_plugins/CMakeLists.txt b/asv_wave_sim_gazebo_plugins/CMakeLists.txt index 7652609e..978878ae 100644 --- a/asv_wave_sim_gazebo_plugins/CMakeLists.txt +++ b/asv_wave_sim_gazebo_plugins/CMakeLists.txt @@ -73,6 +73,7 @@ add_library(Hydrodynamics SHARED src/Algorithm.cc src/Convert.cc + src/Gazebo.cc src/Geometry.cc src/Grid.cc src/MeshTools.cc diff --git a/asv_wave_sim_gazebo_plugins/include/asv_wave_sim_gazebo_plugins/Gazebo.hh b/asv_wave_sim_gazebo_plugins/include/asv_wave_sim_gazebo_plugins/Gazebo.hh new file mode 100644 index 00000000..120ecc50 --- /dev/null +++ b/asv_wave_sim_gazebo_plugins/include/asv_wave_sim_gazebo_plugins/Gazebo.hh @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2012 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +/// \file Gazebo.hh +/// \brief Support for methods not available in legacy versions of Gazebo. + +#ifndef _ASV_WAVE_SIM_GAZEBO_PLUGINS_GAZEBO_HH_ +#define _ASV_WAVE_SIM_GAZEBO_PLUGINS_GAZEBO_HH_ + +#include + +namespace gazebo +{ + namespace rendering + { + class Visual; + + /// \brief Set a shader program parameter associated to this visual's + /// material + /// \param[in] _visual Reference to a Visual + /// \param[in] _paramName Name of shader parameter + /// \param[in] _shaderType Type of shader. Supported types: + /// vertex, fragment + /// \param[in] _value Value to set the parameter to. The value string can + /// be a number (int, float) or a space delimited array of numbers + /// (floats). The value type must match the type defined in the shaders. + /// Note: Setting vec2/float2 params is only supported in ogre1.9+ + void SetMaterialShaderParam( + Visual& _visual, + const std::string &_paramName, + const std::string &_shaderType, + const std::string &_value); + + }; // namespace rendering + +} // namespace gazebo + +#endif // _ASV_WAVE_SIM_GAZEBO_PLUGINS_GAZEBO_HH_ diff --git a/asv_wave_sim_gazebo_plugins/package.xml b/asv_wave_sim_gazebo_plugins/package.xml index ea6cc0de..6c24424f 100644 --- a/asv_wave_sim_gazebo_plugins/package.xml +++ b/asv_wave_sim_gazebo_plugins/package.xml @@ -1,7 +1,7 @@ asv_wave_sim_gazebo_plugins - 0.1.1 + 0.1.2 This package contains Gazebo plugins for the simulation of water surface waves and hydrostatic and hydrodynamics forces. diff --git a/asv_wave_sim_gazebo_plugins/src/CGAL_TEST.cc b/asv_wave_sim_gazebo_plugins/src/CGAL_TEST.cc index ef3b0722..ec170c62 100644 --- a/asv_wave_sim_gazebo_plugins/src/CGAL_TEST.cc +++ b/asv_wave_sim_gazebo_plugins/src/CGAL_TEST.cc @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include diff --git a/asv_wave_sim_gazebo_plugins/src/Gazebo.cc b/asv_wave_sim_gazebo_plugins/src/Gazebo.cc new file mode 100644 index 00000000..eb39d5ba --- /dev/null +++ b/asv_wave_sim_gazebo_plugins/src/Gazebo.cc @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2012 Open Source Robotics Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * +*/ + +#include "asv_wave_sim_gazebo_plugins/Gazebo.hh" + +#include +#include +#include + +#include +#include +#include +#include + +namespace gazebo +{ + namespace rendering + { + + void SetMaterialShaderParam( + Visual& _visual, + const std::string &_paramName, + const std::string &_shaderType, + const std::string &_value) + { + // currently only vertex and fragment shaders are supported + if (_shaderType != "vertex" && _shaderType != "fragment") + { + gzerr << "Shader type: '" << _shaderType << "' is not supported" + << std::endl; + return; + } + + // set the parameter based name and type defined in material script + // and shaders + auto setNamedParam = [](Ogre::GpuProgramParametersSharedPtr _params, + const std::string &_name, const std::string &_v) + { + auto paramDef = _params->_findNamedConstantDefinition(_name); + if (!paramDef) + return; + + switch (paramDef->constType) + { + case Ogre::GCT_INT1: + { + int value = Ogre::StringConverter::parseInt(_v); + _params->setNamedConstant(_name, value); + break; + } + case Ogre::GCT_FLOAT1: + { + Ogre::Real value = Ogre::StringConverter::parseReal(_v); + _params->setNamedConstant(_name, value); + break; + } + #if (OGRE_VERSION >= ((1 << 16) | (9 << 8) | 0)) + case Ogre::GCT_INT2: + case Ogre::GCT_FLOAT2: + { + Ogre::Vector2 value = Ogre::StringConverter::parseVector2(_v); + _params->setNamedConstant(_name, value); + break; + } + #endif + case Ogre::GCT_INT3: + case Ogre::GCT_FLOAT3: + { + Ogre::Vector3 value = Ogre::StringConverter::parseVector3(_v); + _params->setNamedConstant(_name, value); + break; + } + case Ogre::GCT_INT4: + case Ogre::GCT_FLOAT4: + { + Ogre::Vector4 value = Ogre::StringConverter::parseVector4(_v); + _params->setNamedConstant(_name, value); + break; + } + case Ogre::GCT_MATRIX_4X4: + { + Ogre::Matrix4 value = Ogre::StringConverter::parseMatrix4(_v); + _params->setNamedConstant(_name, value); + break; + } + default: + break; + } + }; + + // loop through material techniques and passes to find the param + Ogre::MaterialPtr mat = Ogre::MaterialManager::getSingleton().getByName( + _visual.GetMaterialName()); + if (mat.isNull()) + { + gzerr << "Failed to find material: '" << _visual.GetMaterialName() + << std::endl; + return; + } + for (unsigned int i = 0; i < mat->getNumTechniques(); ++i) + { + Ogre::Technique *technique = mat->getTechnique(i); + if (!technique) + continue; + for (unsigned int j = 0; j < technique->getNumPasses(); ++j) + { + Ogre::Pass *pass = technique->getPass(j); + if (!pass) + continue; + + // check if pass is programmable, ie if they are using shaders + if (!pass->isProgrammable()) + continue; + + if (_shaderType == "vertex" && pass->hasVertexProgram()) + { + setNamedParam(pass->getVertexProgramParameters(), _paramName, _value); + } + else if (_shaderType == "fragment" && pass->hasFragmentProgram()) + { + setNamedParam(pass->getFragmentProgramParameters(), _paramName, _value); + } + else + { + gzerr << "Failed to retrieve shaders for material: '" + << _visual.GetMaterialName() << "', technique: '" + << technique->getName() << "', pass: '" << pass->getName() << "'" + << std::endl; + continue; + } + } + } + } + + } // namespace rendering +} // namespace gazebo + +/////////////////////////////////////////////////////////////////////////////// diff --git a/asv_wave_sim_gazebo_plugins/src/WavefieldVisualPlugin.cc b/asv_wave_sim_gazebo_plugins/src/WavefieldVisualPlugin.cc index 8ce51343..baa2c299 100644 --- a/asv_wave_sim_gazebo_plugins/src/WavefieldVisualPlugin.cc +++ b/asv_wave_sim_gazebo_plugins/src/WavefieldVisualPlugin.cc @@ -16,6 +16,7 @@ #include "asv_wave_sim_gazebo_plugins/WavefieldVisualPlugin.hh" #include "asv_wave_sim_gazebo_plugins/Convert.hh" +#include "asv_wave_sim_gazebo_plugins/Gazebo.hh" #include "asv_wave_sim_gazebo_plugins/Grid.hh" #include "asv_wave_sim_gazebo_plugins/Wavefield.hh" #include "asv_wave_sim_gazebo_plugins/Utilities.hh" @@ -285,9 +286,13 @@ namespace asv // Initialise vertex shader std::string shaderType = "vertex"; +#if 0 this->data->visual->SetMaterialShaderParam( "time", shaderType, std::to_string(0.0)); - +#else + rendering::SetMaterialShaderParam(*this->data->visual, + "time", shaderType, std::to_string(0.0)); +#endif this->SetShaderParams(); this->data->isInitialised = true; } @@ -309,8 +314,13 @@ namespace asv { std::string shaderType = "vertex"; float simTime = this->data->simTime; +#if 0 this->data->visual->SetMaterialShaderParam( "time", shaderType, std::to_string(simTime)); +#else + rendering::SetMaterialShaderParam(*this->data->visual, + "time", shaderType, std::to_string(simTime)); +#endif } } @@ -386,6 +396,7 @@ namespace asv // These parameters are updated on initialisation auto& visual = *this->data->visual; +#if 0 visual.SetMaterialShaderParam( "amplitude", shaderType, Ogre::StringConverter::toString(amplitude)); visual.SetMaterialShaderParam( @@ -400,6 +411,22 @@ namespace asv "dir1", shaderType, Ogre::StringConverter::toString(dir1)); visual.SetMaterialShaderParam( "dir2", shaderType, Ogre::StringConverter::toString(dir2)); +#else + rendering::SetMaterialShaderParam(visual, + "amplitude", shaderType, Ogre::StringConverter::toString(amplitude)); + rendering::SetMaterialShaderParam(visual, + "wavenumber", shaderType, Ogre::StringConverter::toString(wavenumber)); + rendering::SetMaterialShaderParam(visual, + "omega", shaderType, Ogre::StringConverter::toString(omega)); + rendering::SetMaterialShaderParam(visual, + "steepness", shaderType, Ogre::StringConverter::toString(steepness)); + rendering::SetMaterialShaderParam(visual, + "dir0", shaderType, Ogre::StringConverter::toString(dir0)); + rendering::SetMaterialShaderParam(visual, + "dir1", shaderType, Ogre::StringConverter::toString(dir1)); + rendering::SetMaterialShaderParam(visual, + "dir2", shaderType, Ogre::StringConverter::toString(dir2)); +#endif } } // namespace asv