Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SetSize API for LidarVisual #392

Merged
merged 18 commits into from
Sep 15, 2021
Merged
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);
adlarkin marked this conversation as resolved.
Show resolved Hide resolved

// the types can be set as follows:-
// LVT_POINTS -> Lidar Points at the range value
Expand Down
9 changes: 9 additions & 0 deletions include/ignition/rendering/LidarVisual.hh
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ 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 lidar points
chapulina marked this conversation as resolved.
Show resolved Hide resolved
/// \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
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
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
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 1.0
}
}

fragment_program PointCloudFS glsl
{
source point_fs.glsl

}

material PointCloudPoint
Expand All @@ -27,4 +27,4 @@ material PointCloudPoint
fragment_program_ref PointCloudFS {}
}
}
}
}
8 changes: 8 additions & 0 deletions ogre2/src/Ogre2DynamicRenderable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,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 @@ -356,6 +359,11 @@ void Ogre2DynamicRenderable::UpdateBuffer()
this->dataPtr->ogreItem->setCastShadows(
this->dataPtr->material->CastShadows());
}
else if (lowLevelMat)
{
this->dataPtr->ogreItem->getSubItem(0)->setMaterial(lowLevelMat);
this->dataPtr->ogreItem->setCastShadows(castShadows);
}
adlarkin marked this conversation as resolved.
Show resolved Hide resolved
}

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

#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/Ogre2DynamicRenderable.hh"
Expand All @@ -26,7 +35,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 +75,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 +137,7 @@ void Ogre2LidarVisual::Destroy()
}

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

//////////////////////////////////////////////////
Expand All @@ -133,6 +150,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 +329,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 +473,16 @@ 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 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
29 changes: 29 additions & 0 deletions ogre2/src/media/materials/programs/point_fs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2021 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.
*
*/

#version 330

out vec4 fragColor;

void main()
{
// hardcode to blue color for now
// todo(anyone) undate Ogre2DynamicRenderable to support vertex coloring
// so we can set color using the line below
// fragColor = gl_Color;
fragColor = vec4(0.0f, 0.0f, 1.0f, 1.0f);
}
34 changes: 34 additions & 0 deletions ogre2/src/media/materials/programs/point_vs.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (C) 2021 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.
*
*/

#version 330

in vec4 vertex;
uniform mat4 worldViewProj;
uniform float size;

out gl_PerVertex
{
vec4 gl_Position;
};

void main()
{
// Calculate output position
gl_Position = worldViewProj * vertex;
gl_PointSize = size;
}
46 changes: 46 additions & 0 deletions ogre2/src/media/materials/scripts/point_cloud_point.material
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2021 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.
*
*/

vertex_program PointCloudVS glsl
{
source point_vs.glsl

default_params
{
param_named_auto worldViewProj worldviewproj_matrix
param_named size 1.0
}
}

fragment_program PointCloudFS glsl
{
source point_fs.glsl
}

material PointCloudPoint
{
technique
{
pass
{
point_size_attenuation on
point_sprites on
vertex_program_ref PointCloudVS {}
fragment_program_ref PointCloudFS {}
}
}
}
3 changes: 3 additions & 0 deletions src/LidarVisual_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ void LidarVisualTest::LidarVisual(const std::string &_renderEngine)
lidar->SetType(LVT_TRIANGLE_STRIPS);
EXPECT_EQ(lidar->Type(), LVT_TRIANGLE_STRIPS);

EXPECT_DOUBLE_EQ(1.0, lidar->Size());
lidar->SetSize(12.0);
EXPECT_DOUBLE_EQ(12.0, lidar->Size());

ignition::math::Pose3d p(0.5, 2.56, 3.67, 1.4, 2, 4.5);
lidar->SetOffset(p);
Expand Down