Skip to content

Commit

Permalink
Merge branch 'main' into chapulina/main/rm/bionic
Browse files Browse the repository at this point in the history
  • Loading branch information
scpeters committed Mar 15, 2022
2 parents 897fad9 + 8cc48ac commit 20511d6
Show file tree
Hide file tree
Showing 17 changed files with 452 additions and 163 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ set(IGN_MATH_VER ${ignition-math7_VERSION_MAJOR})
#--------------------------------------
# Find ignition-common
ign_find_package(ignition-common5 REQUIRED
COMPONENTS graphics events)
COMPONENTS graphics events geospatial)
set(IGN_COMMON_VER ${ignition-common5_VERSION_MAJOR})

#--------------------------------------
Expand Down
294 changes: 195 additions & 99 deletions examples/heightmap/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
#include <vector>

#include <ignition/common/Console.hh>
#include <ignition/common/HeightmapData.hh>
#include <ignition/common/ImageHeightmap.hh>
#include <ignition/common/geospatial/Dem.hh>
#include <ignition/common/geospatial/HeightmapData.hh>
#include <ignition/common/geospatial/ImageHeightmap.hh>
#include <ignition/rendering.hh>

#include "example_config.hh"
Expand All @@ -42,7 +43,178 @@ const std::string RESOURCE_PATH =
common::joinPaths(std::string(PROJECT_BINARY_PATH), "media");

//////////////////////////////////////////////////
void buildScene(ScenePtr _scene)
void createImageHeightmaps(const ScenePtr _scene, VisualPtr _root)
{
//! [create an image heightmap]
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);
//! [create an image heightmap]

//! [create another image heightmap]
auto data2 = std::make_shared<common::ImageHeightmap>();
data2->Load(common::joinPaths(RESOURCE_PATH, "city_terrain.jpg"));

HeightmapDescriptor desc2;
desc2.SetName("example_city");
desc2.SetData(data2);
desc2.SetSize({26, 26, 20});
desc2.SetSampling(2u);
desc2.SetUseTerrainPaging(true);

HeightmapTexture textureA2;
textureA2.SetSize(1.0);
textureA2.SetDiffuse("../media/fungus_diffusespecular.png");
textureA2.SetNormal("../media/flat_normal.png");
desc2.AddTexture(textureA2);

HeightmapBlend blendA2;
blendA2.SetMinHeight(2.0);
blendA2.SetFadeDistance(5.0);
desc2.AddBlend(blendA2);

HeightmapTexture textureB2;
textureB2.SetSize(1.0);
textureB2.SetDiffuse("../media/grass_diffusespecular.png");
textureB2.SetNormal("../media/flat_normal.png");
desc2.AddTexture(textureB2);

HeightmapBlend blendB2;
blendB2.SetMinHeight(8.0);
blendB2.SetFadeDistance(5.0);
desc2.AddBlend(blendB2);

HeightmapTexture textureC2;
textureC2.SetSize(1.0);
textureC2.SetDiffuse("../media/dirt_diffusespecular.png");
textureC2.SetNormal("../media/flat_normal.png");
desc2.AddTexture(textureC2);
desc2.SetPosition({30, 0, 0});
auto heightmapGeom2 = _scene->CreateHeightmap(desc2);

auto vis2 = _scene->CreateVisual();
vis2->AddGeometry(heightmapGeom2);
_root->AddChild(vis2);
//! [create another image heightmap]
}

//////////////////////////////////////////////////
void createDemHeightmaps(const ScenePtr _scene, VisualPtr _root)
{
//! [create a dem heightmap]
auto data = std::make_shared<common::Dem>();
data->Load(common::joinPaths(RESOURCE_PATH, "volcano.tif"));

HeightmapDescriptor desc;
desc.SetName("example_volcano");
desc.SetData(data);
desc.SetSize({20, 20, 18});
desc.SetSampling(2u);
desc.SetUseTerrainPaging(true);

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);
desc.SetPosition({30, 0, 0});

auto heightmapGeom = _scene->CreateHeightmap(desc);

auto vis = _scene->CreateVisual();
vis->AddGeometry(heightmapGeom);
_root->AddChild(vis);
//! [create a dem heightmap]

//! [create another dem heightmap]
auto data2 = std::make_shared<common::Dem>();
data2->Load(common::joinPaths(RESOURCE_PATH, "moon.tif"));

HeightmapDescriptor desc2;
desc2.SetName("example_moon");
desc2.SetData(data2);
desc2.SetSize({20, 20, 6.85});
desc2.SetSampling(2u);
desc2.SetUseTerrainPaging(false);

HeightmapTexture textureA2;
textureA2.SetSize(20.0);
textureA2.SetDiffuse("../media/moon_diffuse.png");
textureA2.SetNormal("../media/moon_normal.png");
desc2.AddTexture(textureA2);
desc2.SetPosition({0, 0, std::abs(data2->MinElevation())});
auto heightmapGeom2 = _scene->CreateHeightmap(desc2);

auto vis2 = _scene->CreateVisual();
vis2->AddGeometry(heightmapGeom2);
_root->AddChild(vis2);
//! [create another dem heightmap]
}

//////////////////////////////////////////////////
void buildScene(ScenePtr _scene, bool _buildDemScene)
{
// initialize _scene
_scene->SetAmbientLight(0.3, 0.3, 0.3);
Expand All @@ -68,97 +240,10 @@ void buildScene(ScenePtr _scene)
light1->SetCastShadows(true);
root->AddChild(light1);

//! [create a heightmap]
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);
//! [create a heightmap]

//! [create another heightmap]
auto data2 = std::make_shared<common::ImageHeightmap>();
data2->Load(common::joinPaths(RESOURCE_PATH, "city_terrain.jpg"));

HeightmapDescriptor desc2;
desc2.SetName("example_city");
desc2.SetData(data2);
desc2.SetSize({26, 26, 20});
desc2.SetSampling(2u);
desc2.SetUseTerrainPaging(true);

HeightmapTexture textureA2;
textureA2.SetSize(1.0);
textureA2.SetDiffuse("../media/fungus_diffusespecular.png");
textureA2.SetNormal("../media/flat_normal.png");
desc2.AddTexture(textureA2);

HeightmapBlend blendA2;
blendA2.SetMinHeight(2.0);
blendA2.SetFadeDistance(5.0);
desc2.AddBlend(blendA2);

HeightmapTexture textureB2;
textureB2.SetSize(1.0);
textureB2.SetDiffuse("../media/grass_diffusespecular.png");
textureB2.SetNormal("../media/flat_normal.png");
desc2.AddTexture(textureB2);

HeightmapBlend blendB2;
blendB2.SetMinHeight(8.0);
blendB2.SetFadeDistance(5.0);
desc2.AddBlend(blendB2);

HeightmapTexture textureC2;
textureC2.SetSize(1.0);
textureC2.SetDiffuse("../media/dirt_diffusespecular.png");
textureC2.SetNormal("../media/flat_normal.png");
desc2.AddTexture(textureC2);
desc2.SetPosition({30, 0, 0});
auto heightmapGeom2 = _scene->CreateHeightmap(desc2);

auto vis2 = _scene->CreateVisual();
vis2->AddGeometry(heightmapGeom2);
root->AddChild(vis2);
//! [create another heightmap]
if (_buildDemScene)
createDemHeightmaps(_scene, root);
else
createImageHeightmaps(_scene, root);

// create gray material
MaterialPtr gray = _scene->CreateMaterial();
Expand Down Expand Up @@ -193,7 +278,8 @@ void buildScene(ScenePtr _scene)

//////////////////////////////////////////////////
CameraPtr createCamera(const std::string &_engineName,
const std::map<std::string, std::string>& _params)
const std::map<std::string, std::string>& _params,
bool _buildDemScene)
{
// create and populate scene
RenderEngine *engine = rendering::engine(_engineName, _params);
Expand All @@ -204,7 +290,7 @@ CameraPtr createCamera(const std::string &_engineName,
return CameraPtr();
}
ScenePtr scene = engine->CreateScene("scene");
buildScene(scene);
buildScene(scene, _buildDemScene);

// return camera sensor
SensorPtr sensor = scene->SensorByName("camera");
Expand All @@ -220,16 +306,26 @@ int main(int _argc, char** _argv)
std::vector<std::string> engineNames;
std::vector<CameraPtr> cameras;

int buildDemScene = 0;
for (int i = 1; i < _argc; ++i)
{
if (std::string(_argv[i]) == "--dem")
{
buildDemScene = i;
break;
}
}

// Expose engine name to command line because we can't instantiate both
// ogre and ogre2 at the same time
std::string ogreEngineName("ogre2");
if (_argc > 1)
if (_argc > 1 && buildDemScene != 1)
{
ogreEngineName = _argv[1];
}

GraphicsAPI graphicsApi = GraphicsAPI::OPENGL;
if (_argc > 2)
if (_argc > 2 && buildDemScene != 2)
{
graphicsApi = GraphicsAPIUtils::Set(std::string(_argv[2]));
}
Expand All @@ -247,7 +343,7 @@ int main(int _argc, char** _argv)
params["metal"] = "1";
}

CameraPtr camera = createCamera(engineName, params);
CameraPtr camera = createCamera(engineName, params, buildDemScene);
if (camera)
{
cameras.push_back(camera);
Expand Down
Binary file added examples/heightmap/media/moon.tif
Binary file not shown.
Binary file added examples/heightmap/media/moon_diffuse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/heightmap/media/moon_normal.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/heightmap/media/volcano.tif
Binary file not shown.
8 changes: 4 additions & 4 deletions include/ignition/rendering/HeightmapDescriptor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include <memory>
#include <string>
#include <ignition/common/HeightmapData.hh>
#include <ignition/common/geospatial/HeightmapData.hh>
#include <ignition/utils/SuppressWarning.hh>

#include "ignition/rendering/config.hh"
Expand Down Expand Up @@ -186,12 +186,12 @@ inline namespace IGNITION_RENDERING_VERSION_NAMESPACE {
/// \param[in] _data New data.
public: void SetData(const std::shared_ptr<common::HeightmapData> &_data);

/// \brief Get the heightmap's scaling factor.
/// \brief Get the heightmap's final size in world units.
/// \return The heightmap's size.
public: ignition::math::Vector3d Size() const;

/// \brief Set the heightmap's size. Defaults to 1x1x1.
/// \param[in] _size The heightmap's size.
/// \brief Set the heightmap's final size in world units. Defaults to 1x1x1.
/// \return The heightmap's size factor.
public: void SetSize(const ignition::math::Vector3d &_size);

/// \brief Get the heightmap's position offset.
Expand Down
8 changes: 3 additions & 5 deletions ogre/src/OgreHeightmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,9 @@ void OgreHeightmap::Init()
// There is an issue with OGRE terrain LOD if heights are not relative to 0.
// So we move the heightmap so that its min elevation = 0 before feeding to
// ogre. It is later translated back by the setOrigin call.
double minElevation = 0.0;

// TODO(chapulina) add a virtual HeightmapData::MinElevation function to
// avoid the ifdef check. i.e. heightmapSizeZ = MaxElevation - MinElevation
double heightmapSizeZ = this->descriptor.Data()->MaxElevation();
double minElevation = this->descriptor.Data()->MinElevation();
double heightmapSizeZ =
this->descriptor.Data()->MaxElevation() - minElevation;

// \todo These parameters shouldn't be hardcoded, and instead parametrized so
// that they can be made consistent across different libraries (like
Expand Down
Loading

0 comments on commit 20511d6

Please sign in to comment.