Gazebo Rendering

API Reference

6.6.3
Render order

This example shows how to set the render order for coplanar polygons.

The material API allows changing the render order. When polygons are coplanar, you can get problems with depth fighting (also known as z fighting) where the pixels from the two polys compete for the same screen pixel. As you can see in the following image:

The method SetRenderOrder in the Material class allows you to avoid this issue. The higher value will be rendered on top of other coplanar polygons. This method will set the depth bias value of objects that the material is assigned to.

In the simple_demo example you can find two materials with different render orders. The red material (SetRenderOrder(3)) has a higher value than the white material (SetRenderOrder(3)).

// create red material
MaterialPtr red = _scene->CreateMaterial();
red->SetAmbient(0.5, 0.0, 0.0);
red->SetDiffuse(1.0, 0.0, 0.0);
red->SetSpecular(0.5, 0.5, 0.5);
red->SetShininess(50);
red->SetReflectivity(0);
red->SetRenderOrder(3);
// create white material
MaterialPtr white = _scene->CreateMaterial();
white->SetAmbient(0.5, 0.5, 0.5);
white->SetDiffuse(0.8, 0.8, 0.8);
white->SetReceiveShadows(true);
white->SetReflectivity(0);
white->SetRenderOrder(0);

As you can see in the following image the depth fighting issue is resolved.

You can set this in your SDF file including in the material tag a new tag called render_order with a float value:

<material>
<render_order>5</render_order>
<ambient>0 0 1 1</ambient>
<diffuse>0 0 1 1</diffuse>
<specular>0 0 1 1</specular>
</material>

Clone the source code, create a build directory and use cmake and make to compile the code:

git clone https://github.com/ignitionrobotics/ign-rendering
cd ign-rendering/examples/simple_demo
mkdir build
cd build
cmake ..
make

Execute the example:

./simple_demo