Gazebo Gazebo

API Reference

6.16.0
Light config

This tutorial gives an introduction to Ignition Gazebo's service /world/<world name>/light_config. This service will allow to modify lights in the scene.

Modifying lights

To modify lights inside the scene we need to use the service /world/<world name>/light_config and fill the message ignition::msgs::Light. In particular this example modifies the point light that we introduced with the function createLight().

ignition::msgs::EntityFactory entityFactoryRequest;
entityFactoryRequest.mutable_light()->set_name("point");
entityFactoryRequest.mutable_light()->set_range(4);
entityFactoryRequest.mutable_light()->set_attenuation_linear(0.5);
entityFactoryRequest.mutable_light()->set_attenuation_constant(0.2);
entityFactoryRequest.mutable_light()->set_attenuation_quadratic(0.01);
entityFactoryRequest.mutable_light()->set_cast_shadows(false);
entityFactoryRequest.mutable_light()->mutable_direction(),
ignition::math::Vector3d(directionX, directionY, directionZ));
ignition::msgs::Set(entityFactoryRequest.mutable_light()->mutable_pose(),
ignition::math::Pose3d(0.0, 0, 3.0, 0.0, 0.0, 0.0));

NOTE:: You can check the entity creation tutorial to learn how to include models and lights in the scene.

As you can see in the snippet we modify the specular and diffuse colors of the light in the scene.

lightRequest.set_name("point");
lightRequest.set_range(4);
lightRequest.set_attenuation_linear(0.5);
lightRequest.set_attenuation_constant(0.2);
lightRequest.set_attenuation_quadratic(0.01);
lightRequest.set_cast_shadows(false);
lightRequest.set_type(ignition::msgs::Light::POINT);
// direction field only affects spot / directional lights
ignition::msgs::Set(lightRequest.mutable_direction(),
ignition::math::Vector3d(directionX, directionY, directionZ));
ignition::msgs::Set(lightRequest.mutable_pose(),
ignition::math::Pose3d(0.0, -1.5, 3.0, 0.0, 0.0, 0.0));
ignition::msgs::Set(lightRequest.mutable_diffuse(),
ignition::math::Color(r, g, b, 1));
ignition::msgs::Set(lightRequest.mutable_specular(),
ignition::math::Color(r, g, b, 1));

In this case we are creating random numbers to fill the diffuse and specular.

while (m < epsilon)
{
r = distr(twister);
g = distr(twister);
b = distr(twister);
m = std::sqrt(r*r + b*b + g*g);
}
r /= m;
g /= m;
b /= m;

Run the example

To run this example you should cd into examples/standalone/light_control and build the code:

mkdir build
cd build
cmake ..
make

Then you should open two terminals and run:

  • Terminal one:
    ign gazebo -r -v 4 empty.sdf
  • Terminal two:
    ./light_control
void set_attenuation_quadratic(float value)
void set_name(const ::std::string &value)
void Set(msgs::AxisAlignedBox *_b, const math::AxisAlignedBox &_v)
::ignition::msgs::Vector3d * mutable_direction()
T sqrt(T... args)
void set_cast_shadows(bool value)
void set_attenuation_linear(float value)
void set_range(float value)
static const LightType POINT
void set_attenuation_constant(float value)
void set_type(::ignition::msgs::Light_LightType value)
::ignition::msgs::Light * mutable_light()
::ignition::msgs::Pose * mutable_pose()