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

Cherry-pick #2927 to gazebo11: Lens flare cleanup and colorization #2954

Merged
merged 2 commits into from
Apr 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions gazebo/rendering/LensFlare.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ namespace gazebo
namespace rendering
{
/// \brief We'll create an instance of this class for each camera, to be
/// used to inject lens flare uniforms and time (for animating flare)
/// in each render call.
/// used to inject lens flare uniforms in each render call.
class LensFlareCompositorListener
: public Ogre::CompositorInstance::Listener
{
Expand Down Expand Up @@ -72,6 +71,13 @@ namespace gazebo
this->scale = _scale;
}

/// \brief Set the color of lens flare.
/// \param[in] _color Color of lens flare
public: void SetColor(const ignition::math::Vector3d &_color)
{
this->color = _color;
}

/// \brief Callback that OGRE will invoke for us on each render call
/// \param[in] _passID OGRE material pass ID.
/// \param[in] _mat Pointer to OGRE material.
Expand All @@ -97,9 +103,6 @@ namespace gazebo
pass->getFragmentProgramParameters();
GZ_ASSERT(!params.isNull(), "Null OGRE material GPU parameters");

// used for animating flare
params->setNamedConstant("time", static_cast<Ogre::Real>(
common::Time::GetWallTime().Double()));
// for adjusting aspect ratio of flare
params->setNamedConstant("viewport",
Ogre::Vector3(static_cast<double>(this->camera->ViewportWidth()),
Expand Down Expand Up @@ -133,6 +136,8 @@ namespace gazebo
params->setNamedConstant("lightPos", Conversions::Convert(pos));
params->setNamedConstant("scale",
static_cast<Ogre::Real>(lensFlareScale));
params->setNamedConstant("color",
Ogre::Vector3(this->color.X(), this->color.Y(), this->color.Z()));
}

/// \brief Get the lens flare position and scale for a normal camera
Expand Down Expand Up @@ -338,6 +343,10 @@ namespace gazebo

/// \brief Scale of lens flare.
private: double scale = 1.0;

/// \brief Color of lens flare.
private: ignition::math::Vector3d color
= ignition::math::Vector3d(1.0, 1.0, 1.0);
};

/// \brief Private data class for LensFlare
Expand Down Expand Up @@ -373,6 +382,10 @@ namespace gazebo

/// \brief Scale of lens flare.
public: double lensFlareScale = 1.0;

/// \brief Color of lens flare.
public: ignition::math::Vector3d lensFlareColor
= ignition::math::Vector3d(1.0, 1.0, 1.0);
};
}
}
Expand Down Expand Up @@ -415,6 +428,8 @@ void LensFlare::SetCamera(CameraPtr _camera)
LensFlareCompositorListener(this->dataPtr->camera, nullptr));
this->dataPtr->lensFlareCompositorListener->SetScale(
this->dataPtr->lensFlareScale);
this->dataPtr->lensFlareCompositorListener->SetColor(
this->dataPtr->lensFlareColor);

this->dataPtr->lensFlareInstance =
Ogre::CompositorManager::getSingleton().addCompositor(
Expand Down Expand Up @@ -442,6 +457,19 @@ void LensFlare::SetScale(const double _scale)
}
}

//////////////////////////////////////////////////
void LensFlare::SetColor(const ignition::math::Vector3d &_color)
{
// lensFlareColor is intentionally not clamped so the user can work in
// HDR color spaces or be artistic.
this->dataPtr->lensFlareColor = _color;
if (this->dataPtr->lensFlareCompositorListener)
{
this->dataPtr->lensFlareCompositorListener->SetColor(
this->dataPtr->lensFlareColor);
}
}

//////////////////////////////////////////////////
void LensFlare::Update()
{
Expand Down
4 changes: 4 additions & 0 deletions gazebo/rendering/LensFlare.hh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ namespace gazebo
/// \param[in] _scale Scale of lens flare
public: void SetScale(const double _scale);

/// \brief Set the color of lens flare.
/// \param[in] _color Color of lens flare
public: void SetColor(const ignition::math::Vector3d &_color);

/// \brief Update function to search light source
private: void Update();

Expand Down
29 changes: 4 additions & 25 deletions media/materials/programs/camera_lens_flare_fs.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// The input texture, which is set up by the Ogre Compositor infrastructure.
uniform sampler2D RT;
uniform sampler2D noiseRGBA;

uniform float time;
uniform vec3 viewport;

// light pos in clip space
Expand All @@ -11,16 +9,8 @@ uniform vec3 lightPos;
// lens flare scale
uniform float scale;

float noise(float t)
{
// 256 is the size of noiseRGBA texture
return texture2D(noiseRGBA, vec2(t, 0.0) / vec2(256.0)).x;
}

float noise(vec2 t)
{
return texture2D(noiseRGBA,(t + vec2(time)) / vec2(256.0)).x;
}
// lens flare color
uniform vec3 color;

vec3 lensflare(vec2 uv,vec2 pos)
{
Expand All @@ -30,12 +20,9 @@ vec3 lensflare(vec2 uv,vec2 pos)

float ang = atan(main.y, main.x);
float dist = length(main); dist = pow(dist,.1);
float n = noise(vec2((ang-time/9.0)*16.0,dist*32.0));

float f0 = 1.0/(length(uv-pos)*16.0/scale+1.0);

f0 = f0+f0*(sin((ang+time/18.0 + noise(abs(ang)+n/2.0)*2.0)*12.0)*.1+dist*.1+.8);

float f2 = max(1.0/(1.0+32.0*pow(length(uvd+0.8*pos),2.0)),.0)*00.25;
float f22 = max(1.0/(1.0+32.0*pow(length(uvd+0.85*pos),2.0)),.0)*00.23;
float f23 = max(1.0/(1.0+32.0*pow(length(uvd+0.9*pos),2.0)),.0)*00.21;
Expand Down Expand Up @@ -67,13 +54,6 @@ vec3 lensflare(vec2 uv,vec2 pos)
return c;
}

// color modifier
vec3 cc(vec3 color, float factor, float factor2)
{
float w = color.x+color.y+color.z;
return mix(color,vec3(w)*factor,w*factor2);
}

void main()
{
// return if light is behind the view
Expand All @@ -97,9 +77,8 @@ void main()
pos.x *= aspect;

// compute lens flare
vec3 color = vec3(1.4,1.2,1.0)*lensflare(uv, pos.xy);
color = cc(color,.5,.1);
vec3 finalColor = color * lensflare(uv, pos.xy);

// apply lens flare
gl_FragColor = texture2D(RT, gl_TexCoord[0].xy) + vec4(color, 1.0);
gl_FragColor = texture2D(RT, gl_TexCoord[0].xy) + vec4(finalColor, 1.0);
}
15 changes: 5 additions & 10 deletions media/materials/scripts/gazebo.material
Original file line number Diff line number Diff line change
Expand Up @@ -1533,23 +1533,22 @@ material Gazebo/WideLensMap
}


vertex_program Gazebo/CameraLesnFlareVS glsl
vertex_program Gazebo/CameraLensFlareVS glsl
{
source camera_lens_flare_vs.glsl
}

fragment_program Gazebo/CameraLesnFlareFS glsl
fragment_program Gazebo/CameraLensFlareFS glsl
{
source camera_lens_flare_fs.glsl

default_params
{
param_named RT int 0
param_named noiseRGBA int 1
param_named time float 0.0
param_named viewport float3 0.0 0.0 0.0
param_named lightPos float3 0 0 0
param_named scale float 1.0
param_named color float3 1.4 1.2 1.0
}
}

Expand All @@ -1559,19 +1558,15 @@ material Gazebo/CameraLensFlare
{
pass
{
vertex_program_ref Gazebo/CameraLesnFlareVS { }
fragment_program_ref Gazebo/CameraLesnFlareFS { }
vertex_program_ref Gazebo/CameraLensFlareVS { }
fragment_program_ref Gazebo/CameraLensFlareFS { }

texture_unit RT
{
tex_coord_set 0
tex_address_mode border
filtering linear linear linear
}
texture_unit noiseRGBA
{
texture noise_rgba.png
}
}
}
}
Expand Down
1 change: 0 additions & 1 deletion media/materials/textures/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ set (files
heightmap_bowl.png
heightmap_valley.png
motorway.jpg
noise_rgba.png
paintedWall.jpg
pioneerBody.jpg
primary.jpg
Expand Down
Binary file removed media/materials/textures/noise_rgba.png
Binary file not shown.
32 changes: 32 additions & 0 deletions plugins/LensFlareSensorPlugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ namespace gazebo

/// \brief Lens flare scale
public: double scale = 1.0;

/// \brief Lens flare color
public: ignition::math::Vector3d color
= ignition::math::Vector3d(1.4, 1.2, 1.0);
};
}

Expand Down Expand Up @@ -75,6 +79,11 @@ void LensFlareSensorPlugin::Load(sensors::SensorPtr _sensor,
gzerr << "Lens flare scale must be greater than 0" << std::endl;
}

if (_sdf->HasElement("color"))
{
this->dataPtr->color = _sdf->Get<ignition::math::Vector3d>("color");
}

sensors::CameraSensorPtr cameraSensor =
std::dynamic_pointer_cast<sensors::CameraSensor>(_sensor);

Expand All @@ -101,6 +110,28 @@ void LensFlareSensorPlugin::Load(sensors::SensorPtr _sensor,
return;
}

/////////////////////////////////////////////////
void LensFlareSensorPlugin::SetScale(const double _scale)
{
this->dataPtr->scale = _scale;

for (auto flare : this->dataPtr->lensFlares)
{
flare->SetScale(_scale);
}
}

/////////////////////////////////////////////////
void LensFlareSensorPlugin::SetColor(const ignition::math::Vector3d &_color)
{
this->dataPtr->color = _color;

for (auto flare : this->dataPtr->lensFlares)
{
flare->SetColor(_color);
}
}

/////////////////////////////////////////////////
void LensFlareSensorPlugin::AddLensFlare(rendering::CameraPtr _camera)
{
Expand All @@ -111,5 +142,6 @@ void LensFlareSensorPlugin::AddLensFlare(rendering::CameraPtr _camera)
lensFlare.reset(new rendering::LensFlare);
lensFlare->SetCamera(_camera);
lensFlare->SetScale(this->dataPtr->scale);
lensFlare->SetColor(this->dataPtr->color);
this->dataPtr->lensFlares.push_back(lensFlare);
}
13 changes: 12 additions & 1 deletion plugins/LensFlareSensorPlugin.hh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ namespace gazebo
/// \brief Plugin that adds lens flare effect to a camera or multicamera
/// sensor
/// The plugin has the following optional parameter:
/// <scale> Scale of lens flare. Must be greater than 0
/// <scale> Scale of lens flare. Must be greater than 0
/// <color> Color of lens flare.
/// \todo A potentially useful feature would be an option for constantly
/// updating the flare color to match the light source color.
class GZ_PLUGIN_VISIBLE LensFlareSensorPlugin : public SensorPlugin
{
/// \brief Constructor.
Expand All @@ -41,6 +44,14 @@ namespace gazebo
public: virtual void Load(sensors::SensorPtr _sensor,
sdf::ElementPtr _sdf);

/// \brief Set the scale of lens flare.
/// \param[in] _scale Scale of lens flare
public: void SetScale(const double _scale);

/// \brief Set the color of lens flare.
/// \param[in] _color Color of lens flare
public: void SetColor(const ignition::math::Vector3d &_color);

/// \brief Add lens flare effect to a camera
/// \param[in] _camera Camera to add the lens flare effect to.
private: void AddLensFlare(rendering::CameraPtr _camera);
Expand Down
17 changes: 14 additions & 3 deletions test/integration/ogre_log.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,20 @@ TEST_F(OgreLog, LogError)
if (line.find(" GL_EXTENSIONS =") < 12)
continue;

EXPECT_EQ(line.find("Error"), std::string::npos);
EXPECT_EQ(line.find("error"), std::string::npos);
EXPECT_EQ(line.find("ERROR"), std::string::npos);
// A GLX extension may have the word "error" in its name. For example:
// GLX_ARB_create_context_no_error.
// We will skip the line that lists all the extensions. This line starts
// with a date, so we just check that "Supported GLX extensions:" is
// toward the beginning.
// False positive cppcheck
// https://sourceforge.net/p/cppcheck/discussion/general/thread/0c113d65/
// cppcheck-suppress stlIfStrFind
if (line.find(" Supported GLX extensions: ") < 12)
continue;

EXPECT_EQ(line.find("Error"), std::string::npos) << line;
EXPECT_EQ(line.find("error"), std::string::npos) << line;
EXPECT_EQ(line.find("ERROR"), std::string::npos) << line;
}
}

Expand Down