Heightmap
This example shows how to add a heigntmap to the scene.
It loads 2 different heightmaps with different parameters.
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/ignitionrobotics/ign-rendering
cd ign-rendering/examples/heightmap
mkdir build
cd build
cmake ..
make
Execute the example:
./heightmap
You'll see:
[Msg] Loading plugin [ignition-rendering6-ogre]
[Msg] Loading heightmap: scene::Heightmap(65528)
[Msg] Heightmap loaded. Process took 217 ms.
===============================
TAB - Switch render engines
ESC - Exit
===============================
Code
A heightmap is a terrain defined by a 2D grid of height values. This demo loads the heights from a grayscale image, where the color black represents the lowest point, and white represents the highest.
The heightmap's information is stored in the HeightmapDescriptor
class. In addition to the height data, the heightmap descriptor also exposes functionality such as:
- Its size in meters in XYZ space.
- The position of its origin in the world.
- The textures to use according to the height.
- How to blend these textures.
Here's the snippet of code from examples/heightmap/Main.cc
that adds a heightmap to the scene:
auto data = std::make_shared<common::ImageHeightmap>();
data->Load(common::joinPaths(RESOURCE_PATH, "heightmap_bowl.png"));
HeightmapDescriptor desc;
desc.SetName("example_bowl");
desc.SetData(data);
desc.SetSize({17, 17, 10});
desc.SetSampling(2u);
desc.SetUseTerrainPaging(false);
HeightmapTexture textureA;
textureA.SetSize(1.0);
textureA.SetDiffuse("../media/dirt_diffusespecular.png");
textureA.SetNormal("../media/flat_normal.png");
desc.AddTexture(textureA);
HeightmapBlend blendA;
blendA.SetMinHeight(2.0);
blendA.SetFadeDistance(5.0);
desc.AddBlend(blendA);
HeightmapTexture textureB;
textureB.SetSize(1.0);
textureB.SetDiffuse("../media/grass_diffusespecular.png");
textureB.SetNormal("../media/flat_normal.png");
desc.AddTexture(textureB);
HeightmapBlend blendB;
blendB.SetMinHeight(4.0);
blendB.SetFadeDistance(5.0);
desc.AddBlend(blendB);
HeightmapTexture textureC;
textureC.SetSize(1.0);
textureC.SetDiffuse("../media/fungus_diffusespecular.png");
textureC.SetNormal("../media/flat_normal.png");
desc.AddTexture(textureC);
auto heightmapGeom = _scene->CreateHeightmap(desc);
auto vis = _scene->CreateVisual();
vis->AddGeometry(heightmapGeom);
root->AddChild(vis);