Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flip heightmap's Y position on Ogre 2 and add sanity checks for NaN #585

Merged
merged 5 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Deprecated code produces compile-time warnings. These warning serve as
notification to users that their code should be upgraded. The next major
release will remove the deprecated code.

## Ignition Rendering 6.0.1 to 6.1.0

### Modifications

1. Ogre 2 heightmaps: the Y position sign was flipped

## Ignition Rendering 5.x to 6.x

### Modifications
Expand Down
2 changes: 1 addition & 1 deletion examples/heightmap/Main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ void buildScene(ScenePtr _scene)
textureC2.SetDiffuse("../media/dirt_diffusespecular.png");
textureC2.SetNormal("../media/flat_normal.png");
desc2.AddTexture(textureC2);
desc2.SetPosition({30, 0, 0});
desc2.SetPosition({30, 10, 0});
auto heightmapGeom2 = _scene->CreateHeightmap(desc2);

auto vis2 = _scene->CreateVisual();
Expand Down
9 changes: 8 additions & 1 deletion ogre/src/OgreHeightmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,14 @@ void OgreHeightmap::Init()
for (unsigned int x = 0; x < vertSize; ++x)
{
int index = (vertSize - y - 1) * vertSize + x;
this->dataPtr->heights.push_back(lookup[index] - minElevation);

// Sanity check in case we get NaNs from ign-common, this prevents a crash
// in Ogre
auto value = lookup[index];
if (!std::isfinite(value))
value = minElevation;

this->dataPtr->heights.push_back(value - minElevation);
}
}

Expand Down
31 changes: 25 additions & 6 deletions ogre2/src/Ogre2Heightmap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,35 @@ void Ogre2Heightmap::Init()
// Obtain min and max elevation and bring everything to range [0; 1]
// Terra should support non-normalized ranges but there are a couple
// bugs preventing that, so it's just easier to normalize the data
float minElevation = 0.0;
float maxElevation = 0.0;
double minElevation = this->descriptor.Data()->MinElevation();
chapulina marked this conversation as resolved.
Show resolved Hide resolved
double maxElevation = this->descriptor.Data()->MaxElevation();

// Sanity check
if (minElevation >= maxElevation)
{
ignerr << "Internal error: min elevation [" << minElevation
<< "] >= max elevation [" << maxElevation << "]" << std::endl;
return;
}

for (unsigned int y = 0; y < newWidth; ++y)
{
for (unsigned int x = 0; x < newWidth; ++x)
{
const size_t index = y * srcWidth + x;
const float heightVal = lookup[index];
minElevation = std::min(minElevation, heightVal);
maxElevation = std::max(maxElevation, heightVal);
float heightVal = lookup[index];

// Sanity check in case we get NaNs from ign-common, this prevents a crash
// in Ogre
if (!std::isfinite(heightVal))
heightVal = minElevation;

if (heightVal < minElevation || heightVal > maxElevation)
{
ignerr << "Internal error: height [" << heightVal
<< "] is out of bounds [" << minElevation << " / "
<< maxElevation << "]" << std::endl;
}
this->dataPtr->heights.push_back(heightVal);
}
}
Expand Down Expand Up @@ -208,9 +226,10 @@ void Ogre2Heightmap::Init()
const math::Vector3d newSize = this->descriptor.Size() *
math::Vector3d(1.0, 1.0, heightDiff);

// The position's Y sign ends up flipped
math::Vector3d center(
this->descriptor.Position().X(),
this->descriptor.Position().Y(),
-this->descriptor.Position().Y(),
chapulina marked this conversation as resolved.
Show resolved Hide resolved
this->descriptor.Position().Z() + newSize.Z() * 0.5 + minElevation);

Ogre::Root *ogreRoot = Ogre2RenderEngine::Instance()->OgreRoot();
Expand Down