Skip to content

Commit

Permalink
Add SetSize API for LidarVisual (gazebosim#392)
Browse files Browse the repository at this point in the history
* add set size for lidar visual

Signed-off-by: Ian Chen <[email protected]>

* update unit test

Signed-off-by: Ian Chen <[email protected]>

* fix warnings

Signed-off-by: Ian Chen <[email protected]>

* ogre2 working

Signed-off-by: Ian Chen <[email protected]>

* testing homewbrew

Signed-off-by: Ian Chen <[email protected]>

* more testing homebrew

Signed-off-by: Ian Chen <[email protected]>

* test arb extension

Signed-off-by: Ian Chen <[email protected]>

* testing no extension

Signed-off-by: Ian Chen <[email protected]>

* fixing windows

Signed-off-by: Ian Chen <[email protected]>

* fixing windows

Signed-off-by: Ian Chen <[email protected]>

* update doc

Signed-off-by: Ian Chen <[email protected]>

* fixes

Signed-off-by: Ian Chen <[email protected]>

* update doc

Signed-off-by: Ian Chen <[email protected]>

* Add SetSize API for Marker point size (gazebosim#403)

Signed-off-by: Ian Chen <[email protected]>

Co-authored-by: Alejandro Hernández Cordero <[email protected]>
Co-authored-by: Louise Poubel <[email protected]>
Signed-off-by: William Lew <[email protected]>
  • Loading branch information
3 people authored and WilliamLewww committed Dec 7, 2021
1 parent d53a24f commit 46d8011
Show file tree
Hide file tree
Showing 18 changed files with 352 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/lidar_visual/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ LidarVisualPtr createLidar(ScenePtr _scene)
lidar->SetMaxVerticalAngle(vMaxAngle);
lidar->SetMaxRange(maxRange);
lidar->SetMinRange(minRange);
lidar->SetSize(5.0);

// the types can be set as follows:-
// LVT_POINTS -> Lidar Points at the range value
Expand Down
2 changes: 1 addition & 1 deletion examples/lidar_visual/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ By default, the demo uses the Ogre 1 engine:

./lidar_visual

It's also possible to use Ogre 2, i.e. (Currently disabled)
It's also possible to use Ogre 2:

./lidar_visual ogre2

10 changes: 10 additions & 0 deletions include/ignition/rendering/LidarVisual.hh
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,16 @@ namespace ignition
/// \return The type for lidar visual
public: virtual LidarVisualType Type() const = 0;

/// \brief Set size of the lidar visualization,
/// e.g. size of rasterized lidar points in pixels
/// \param[in] _size Size of the lidar visualization.
public: virtual void SetSize(double _size) = 0;

/// \brief Get size of the lidar visualization
/// \return Size of the lidar visualization.
/// \sa SetSize
public: virtual double Size() const = 0;

/// \brief Set if non-hitting rays will be displayed
/// (this does not work for TRIANGLE_STRIPS visual)
/// \param[in] _display Boolean value to display non hitting visuals
Expand Down
10 changes: 10 additions & 0 deletions include/ignition/rendering/Marker.hh
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ namespace ignition
/// \return The render type of the marker
public: virtual ignition::rendering::MarkerType Type() const = 0;

/// \brief Set size of the marker. Only affects MT_POINTS.
/// e.g. size of rasterized points in pixels
/// \param[in] _size Size of the marker
public: virtual void SetSize(double _size) = 0;

/// \brief Get the size of the marker.
/// \return The size of the marker
/// \sa SetSize
public: virtual double Size() const = 0;

/// \brief Clear the points of the marker, if applicable
public: virtual void ClearPoints() = 0;

Expand Down
23 changes: 23 additions & 0 deletions include/ignition/rendering/base/BaseLidarVisual.hh
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ namespace ignition
// Documentation inherited
public: virtual LidarVisualType Type() const override;

// Documentation inherited
public: virtual void SetSize(double _size) override;

// Documentation inherited
public: virtual double Size() const override;

/// \brief Create predefined materials for lidar visual
public: virtual void CreateMaterials();

Expand Down Expand Up @@ -186,6 +192,9 @@ namespace ignition
/// \brief Type of lidar visualisation
protected: LidarVisualType lidarVisualType =
LidarVisualType::LVT_TRIANGLE_STRIPS;

/// \brief Size of lidar visualisation
protected: double size = 1.0;
};

/////////////////////////////////////////////////
Expand Down Expand Up @@ -432,6 +441,20 @@ namespace ignition
return this->lidarVisualType;
}

/////////////////////////////////////////////////
template <class T>
void BaseLidarVisual<T>::SetSize(double _size)
{
this->size = _size;
}

/////////////////////////////////////////////////
template <class T>
double BaseLidarVisual<T>::Size() const
{
return this->size;
}

/////////////////////////////////////////////////
template <class T>
void BaseLidarVisual<T>::SetDisplayNonHitting(bool _display)
Expand Down
24 changes: 24 additions & 0 deletions include/ignition/rendering/base/BaseMarker.hh
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ namespace ignition
// Documentation inherited
public: virtual MarkerType Type() const override;

// Documentation inherited
public: virtual void SetSize(double _size) override;

// Documentation inherited
public: virtual double Size() const override;

// Documentation inherited
public: virtual void SetLayer(int32_t _layer) override;

Expand Down Expand Up @@ -97,6 +103,9 @@ namespace ignition
/// \brief Marker type
protected: MarkerType markerType =
ignition::rendering::MarkerType::MT_NONE;

/// \brief Marker size
protected: double size = 1.0;
};

/////////////////////////////////////////////////
Expand Down Expand Up @@ -159,6 +168,21 @@ namespace ignition
return this->markerType;
}

/////////////////////////////////////////////////
template <class T>
void BaseMarker<T>::SetSize(double _size)
{
this->size = _size;
this->markerDirty = true;
}

/////////////////////////////////////////////////
template <class T>
double BaseMarker<T>::Size() const
{
return this->size;
}

/////////////////////////////////////////////////
template <class T>
void BaseMarker<T>::PreRender()
Expand Down
12 changes: 11 additions & 1 deletion ogre/src/OgreLidarVisual.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* 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 <ignition/common/Console.hh>
Expand Down Expand Up @@ -486,6 +486,16 @@ void OgreLidarVisual::Update()
verticalAngle += this->verticalAngleStep;
}

if (this->dataPtr->lidarVisType == LidarVisualType::LVT_POINTS &&
!this->dataPtr->points.empty())
{
// get the PointCloudPoint material
Ogre::MaterialPtr mat = this->dataPtr->points[0]->getMaterial();
auto pass = mat->getTechnique(0)->getPass(0);
auto params = pass->getVertexProgramParameters();
params->setNamedConstant("size", static_cast<Ogre::Real>(this->size));
}

// The newly created dynamic lines are having default visibility as true.
// The visibility needs to be set as per the current value after the new
// renderables are created.
Expand Down
24 changes: 24 additions & 0 deletions ogre/src/OgreMarker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,30 @@ OgreMarker::~OgreMarker()
//////////////////////////////////////////////////
void OgreMarker::PreRender()
{
if (this->markerType == MarkerType::MT_POINTS &&
this->dataPtr->dynamicRenderable &&
this->dataPtr->dynamicRenderable->PointCount() > 0u)
{
std::string pointsMatName = "PointCloudPoint";
if (this->dataPtr->dynamicRenderable->getMaterial().isNull() ||
this->dataPtr->dynamicRenderable->getMaterial()->getName()
!= pointsMatName)
{
#if (OGRE_VERSION <= ((1 << 16) | (10 << 8) | 7))
this->dataPtr->dynamicRenderable->setMaterial(pointsMatName);
#else
Ogre::MaterialPtr pointsMat =
Ogre::MaterialManager::getSingleton().getByName(
pointsMatName);
this->dataPtr->dynamicRenderable->setMaterial(pointsMat);
#endif
}
auto pass = this->dataPtr->dynamicRenderable->getMaterial()->getTechnique(
0)->getPass(0);
auto vertParams = pass->getVertexProgramParameters();
vertParams->setNamedConstant("size", static_cast<Ogre::Real>(this->size));
}

this->dataPtr->dynamicRenderable->Update();
}

Expand Down
5 changes: 3 additions & 2 deletions ogre/src/media/materials/programs/point_vs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
// Works for perspective and orthographic projection.

uniform mat4 worldviewproj_matrix;
uniform float size;

void main()
{
gl_Position = worldviewproj_matrix * gl_Vertex;
gl_FrontColor = gl_Color;
gl_PointSize = 10 / gl_Position.w;
}
gl_PointSize = size;
}
4 changes: 2 additions & 2 deletions ogre/src/media/materials/scripts/point_cloud_point.material
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ vertex_program PointCloudVS glsl
default_params
{
param_named_auto worldviewproj_matrix worldviewproj_matrix
param_named size float 1.0
}
}

fragment_program PointCloudFS glsl
{
source point_fs.glsl

}

material PointCloudPoint
Expand All @@ -27,4 +27,4 @@ material PointCloudPoint
fragment_program_ref PointCloudFS {}
}
}
}
}
10 changes: 10 additions & 0 deletions ogre2/src/Ogre2DynamicRenderable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ void Ogre2DynamicRenderable::UpdateBuffer()
// update item aabb
if (this->dataPtr->ogreItem)
{
bool castShadows = this->dataPtr->ogreItem->getCastShadows();
auto lowLevelMat = this->dataPtr->ogreItem->getSubItem(0)->getMaterial();

// need to rebuild ogre [sub]item because the vao was destroyed
// this updates the item's bounding box and fixes occasional crashes
// from invalid access to old vao
Expand All @@ -340,6 +343,13 @@ void Ogre2DynamicRenderable::UpdateBuffer()
this->dataPtr->ogreItem->setCastShadows(
this->dataPtr->material->CastShadows());
}
else if (lowLevelMat)
{
// the _initialise call above resets the ogre item properties so set
// them again
this->dataPtr->ogreItem->getSubItem(0)->setMaterial(lowLevelMat);
this->dataPtr->ogreItem->setCastShadows(castShadows);
}
}

this->dataPtr->dirty = false;
Expand Down
54 changes: 52 additions & 2 deletions ogre2/src/Ogre2LidarVisual.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,19 @@
*
*/

#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#include <OpenGL/gl.h>
#include <OpenGL/glext.h>
#else
#ifndef _WIN32
#include <GL/gl.h>
#endif
#endif

#include <ignition/common/Console.hh>

#include "ignition/rendering/ogre2/Ogre2Conversions.hh"
#include "ignition/rendering/ogre2/Ogre2DynamicRenderable.hh"
#include "ignition/rendering/ogre2/Ogre2LidarVisual.hh"
#include "ignition/rendering/ogre2/Ogre2Scene.hh"
Expand All @@ -26,7 +37,10 @@
#ifdef _MSC_VER
#pragma warning(push, 0)
#endif
#include <OgreItem.h>
#include <OgreMaterialManager.h>
#include <OgreSceneNode.h>
#include <OgreTechnique.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
Expand Down Expand Up @@ -63,6 +77,10 @@ class ignition::rendering::Ogre2LidarVisualPrivate

/// \brief The visibility of the visual
public: bool visible = true;

/// \brief Pointer to point cloud material.
/// Used when LidarVisualType = LVT_POINTS.
public: Ogre::MaterialPtr pointsMat;
};

using namespace ignition;
Expand Down Expand Up @@ -121,6 +139,7 @@ void Ogre2LidarVisual::Destroy()
}

this->dataPtr->lidarPoints.clear();
this->dataPtr->pointsMat.setNull();
}

//////////////////////////////////////////////////
Expand All @@ -133,6 +152,18 @@ void Ogre2LidarVisual::Init()
//////////////////////////////////////////////////
void Ogre2LidarVisual::Create()
{
// enable GL_PROGRAM_POINT_SIZE so we can set gl_PointSize in vertex shader
#ifdef __APPLE__
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);
#else
#ifndef _WIN32
glEnable(GL_PROGRAM_POINT_SIZE);
#endif
#endif
this->dataPtr->pointsMat =
Ogre::MaterialManager::getSingleton().getByName(
"PointCloudPoint");

this->ClearPoints();
this->dataPtr->receivedData = false;
}
Expand Down Expand Up @@ -300,8 +331,11 @@ void Ogre2LidarVisual::Update()
new Ogre2DynamicRenderable(this->Scene()));

renderable->SetOperationType(MT_POINTS);
MaterialPtr mat = this->Scene()->Material("Lidar/BlueRay");
renderable->SetMaterial(mat, false);

// use low level programmable material so we can customize point size
Ogre::Item *item = dynamic_cast<Ogre::Item *>(renderable->OgreObject());
item->setCastShadows(false);
item->getSubItem(0)->setMaterial(this->dataPtr->pointsMat);

this->ogreNode->attachObject(renderable->OgreObject());
this->dataPtr->points.push_back(renderable);
Expand Down Expand Up @@ -441,6 +475,22 @@ void Ogre2LidarVisual::Update()
verticalAngle += this->verticalAngleStep;
}

if (this->dataPtr->lidarVisType == LidarVisualType::LVT_POINTS &&
!this->dataPtr->points.empty())
{
// point renderables use low level materials
// get the material and set size uniform variable
auto pass = this->dataPtr->pointsMat->getTechnique(0)->getPass(0);
auto vertParams = pass->getVertexProgramParameters();
vertParams->setNamedConstant("size", static_cast<Ogre::Real>(this->size));

// support setting color only from diffuse for now
MaterialPtr mat = this->Scene()->Material("Lidar/BlueRay");
auto fragParams = pass->getFragmentProgramParameters();
fragParams->setNamedConstant("color",
Ogre2Conversions::Convert(mat->Diffuse()));
}

// The newly created dynamic lines are having default visibility as true.
// The visibility needs to be set as per the current value after the new
// renderables are created.
Expand Down
Loading

0 comments on commit 46d8011

Please sign in to comment.