Gazebo Rendering

API Reference

8.2.1
Mesh Viewer

Compile and run the example

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

git clone https://github.com/gazebosim/gz-rendering
cd gz-rendering/examples/mesh_viewer
mkdir build
cd build
cmake ..
make

To run the example:

./mesh_viewer

Code

In the the Main.cc file there is a function called buildScene which is in charge of creating the objects in the scene (for example: the grid or the duck). The steps to visualize a mesh are the following:

  • Create a visual
  • Attach mesh to the visual
  • Include the visual in the scene
MeshDescriptor descriptor;
descriptor.meshName = common::joinPaths(RESOURCE_PATH, "duck.dae");
common::MeshManager *meshManager = common::MeshManager::Instance();
descriptor.mesh = meshManager->Load(descriptor.meshName);
if (descriptor.mesh)
{
VisualPtr mesh = _scene->CreateVisual();
mesh->SetLocalPosition(3, 0, 0);
mesh->SetLocalRotation(1.5708, 0, 2.0);
MeshPtr meshGeom = _scene->CreateMesh(descriptor);
mesh->AddGeometry(meshGeom);
root->AddChild(mesh);
}
else
{
gzerr << "Failed load mesh: " << descriptor.meshName << std::endl;
}

Similarly, we can add a grid to the floor:

VisualPtr grid = _scene->CreateVisual();
GridPtr gridGeom = _scene->CreateGrid();
gridGeom->SetCellCount(20);
gridGeom->SetCellLength(1);
gridGeom->SetVerticalCellCount(0);
grid->AddGeometry(gridGeom);
grid->SetLocalPosition(3, 0, 0.0);
grid->SetMaterial(gray);
root->AddChild(grid);

Or we can include a camera and set up the position, orientation, dimensions and other relevant features of cameras:

CameraPtr camera = _scene->CreateCamera("camera");
camera->SetLocalPosition(0.0, 1.0, 0.5);
camera->SetLocalRotation(0.0, 0.0, 0.0);
camera->SetImageWidth(800);
camera->SetImageHeight(600);
camera->SetAntiAliasing(2);
camera->SetAspectRatio(1.333);
camera->SetHFOV(GZ_PI / 2);
root->AddChild(camera);