Skip to content

Commit

Permalink
GeoTransform: fix a bug: terrain callback was not reinstalled after t…
Browse files Browse the repository at this point in the history
…he terrain observer_ptr went null and a new terrain was found
  • Loading branch information
gwaldron committed Dec 20, 2024
1 parent f1d7948 commit b7525e4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
19 changes: 8 additions & 11 deletions src/osgEarth/GeoTransform
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef OSGEARTH_GEO_TRANSFORM
#define OSGEARTH_GEO_TRANSFORM
#pragma once

#include <osgEarth/Common>
#include <osgEarth/Config>
Expand Down Expand Up @@ -123,16 +122,14 @@ namespace osgEarth
protected:
virtual ~GeoTransform();

GeoPoint _position; // Current position
osg::observer_ptr<Terrain> _terrain; // Terrain for relative height resolution
bool _terrainCallbackInstalled; // Whether the Terrain callback is in
bool _autoRecomputeHeights; // Whether to resolve relative position Z's
bool _findTerrainInUpdateTraversal; // True is we need _terrain but don't have it
bool _clampInUpdateTraversal; // Whether a terrain clamp is required

GeoPoint _position; // Current position
osg::observer_ptr<Terrain> _terrain; // Terrain for relative height resolution
bool _terrainCallbackInstalled; // Whether the Terrain callback is in
bool _autoRecomputeHeights; // Whether to resolve relative position Z's
bool _findTerrainInUpdateTraversal; // True is we need _terrain but don't have it
bool _clampInUpdateTraversal; // Whether a terrain clamp is required
osg::ref_ptr<ComputeMatrixCallback> _computeMatrixCallback;
osg::ref_ptr<TerrainCallbackAdapter<GeoTransform>> _terrainCallback; // Callback for terrain updates
};

} // namespace osgEarth

#endif // OSGEARTH_GEO_TRANSFORM
33 changes: 25 additions & 8 deletions src/osgEarth/GeoTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@
using namespace osgEarth;

GeoTransform::GeoTransform() :
_findTerrainInUpdateTraversal(false),
_terrainCallbackInstalled(false),
_autoRecomputeHeights(true),
_clampInUpdateTraversal(false)
_findTerrainInUpdateTraversal(false),
_terrainCallbackInstalled(false),
_autoRecomputeHeights(true),
_clampInUpdateTraversal(false)
{
//nop
//nop
}

GeoTransform::GeoTransform(const GeoTransform& rhs, const osg::CopyOp& op) :
osg::MatrixTransform(rhs, op)
osg::MatrixTransform(rhs, op)
{
_position = rhs._position;
_terrain = rhs._terrain.get();
Expand All @@ -58,9 +58,24 @@ GeoTransform::setTerrain(Terrain* terrain)
if (terrain)
{
if (_terrain.valid())
{
if (_terrainCallback.valid())
{
_terrain->removeTerrainCallback(_terrainCallback.get());
_terrainCallbackInstalled = false;
}
_terrain->removeObserver(this);
}

_terrain = terrain;
_terrain->addObserver(this);

if (_terrainCallback.valid())
{
_terrain->addTerrainCallback(_terrainCallback);
_terrainCallbackInstalled = true;
}

setPosition(_position);
}
}
Expand Down Expand Up @@ -152,8 +167,10 @@ GeoTransform::setPosition(const GeoPoint& position)
!_terrainCallbackInstalled &&
terrain.valid())
{
// The Adapter template auto-destructs, so we never need to remote it manually.
terrain->addTerrainCallback( new TerrainCallbackAdapter<GeoTransform>(this) );
// The Adapter template auto-destructs, so we never need to remove it manually.
if (!_terrainCallback.valid())
_terrainCallback = new TerrainCallbackAdapter<GeoTransform>(this);
terrain->addTerrainCallback(_terrainCallback);
_terrainCallbackInstalled = true;
}

Expand Down

0 comments on commit b7525e4

Please sign in to comment.