Skip to content

Commit

Permalink
VisibleLayer: add depth_offset layer option as a simpler version of t…
Browse files Browse the repository at this point in the history
…he render symbol
  • Loading branch information
gwaldron committed Dec 13, 2024
1 parent 85a6722 commit 20aa7d3
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 65 deletions.
64 changes: 0 additions & 64 deletions src/osgEarth/Feature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -783,70 +783,6 @@ void Feature::transform( const SpatialReference* srs )
setSRS( srs );
}

#if 0
namespace
{
template<class RING>
void split(const RING* input, osg::ref_ptr<RING>& left, osg::ref_ptr<RING>& right, bool& is_left, int& ptr)
{
// check the current side's back() against the next point in the input.
RING* current = is_left? left.get() : right.get();
auto& p0 = current->back();
auto& p1 = (*input)[ptr];

if (p0.x() > 90.0 && p1.x() < -90.0)
{
// we're crossing the dateline. Split the segment.
double t = (180.0 - p0.x()) / ((p1.x()+360.0) - p0.x());
double y = p0.y() + t * (p1.y() - p0.y());
left->push_back(osg::Vec3d(180.0, y, 0.0));
right->push_back(osg::Vec3d(-180.0, y, 0.0));
is_left = !is_left;
if (is_left) left->push_back(p1); else right->push_back(p1);
while(ptr < input->size()-1)
split(input, left, right, is_left, ++ptr);
}
else if (p0.x() < -90.0 && p1.x() > 90.0)
{
// we're crossing the dateline. Split the segment.
double t = (180.0 - p1.x()) / ((p0.x() + 360.0) - p1.x());
double y = p1.y() + t * (p0.y() - p1.y());
left->push_back(osg::Vec3d(180.0, y, 0.0));
right->push_back(osg::Vec3d(-180.0, y, 0.0));
is_left = !is_left;
if (is_left) left->push_back(p1); else right->push_back(p1);
while(ptr < input->size()-1)
split(input, left, right, is_left, ++ptr);
}
else
{
current->push_back(p1);
}
}

template<class RING>
void split(const RING* input, osg::ref_ptr<RING>& left, osg::ref_ptr<RING>& right)
{
if (input == nullptr || input->size() < 3)
return;

left = new RING();
right = new RING();
int ptr = 0;
bool is_left = (*input)[0].x() > 0.0;
if (is_left)
left->push_back((*input)[0]);
else
right->push_back((*input)[0]);

while(ptr < input->size()-1)
{
split(input, left, right, is_left, ++ptr);
}
}
}
#endif

void
Feature::splitAcrossAntimeridian()
{
Expand Down
7 changes: 6 additions & 1 deletion src/osgEarth/VisibleLayer
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ namespace osgEarth
OE_OPTION(float, maxVisibleRange, FLT_MAX);
OE_OPTION(float, attenuationRange, 0.0f);
OE_OPTION(ColorBlending, blend, BLEND_INTERPOLATE);
OE_OPTION(float, depthOffset, 0.0f);
OE_OPTION(osg::Node::NodeMask, mask, 0xffffffff);
OE_OPTION(bool, debugView, false);
OE_OPTION(bool, useNVGL, false);
Expand All @@ -68,6 +69,10 @@ namespace osgEarth
virtual void setOpacity(float value);
float getOpacity() const;

//! Depth offset in meters
virtual void setDepthOffset(float value_meters);
float getDepthOffset() const;

//! Minimum camera range at which this image layer is visible (if supported)
float getMinVisibleRange() const;
void setMinVisibleRange( float minVisibleRange );
Expand Down Expand Up @@ -116,7 +121,7 @@ namespace osgEarth
bool _canSetVisible = true;

private:
osg::ref_ptr<osg::Uniform> _opacityU;
osg::ref_ptr<osg::Uniform> _opacityU, _depthOffsetU;
osg::ref_ptr<osg::Uniform> _rangeU;
osg::ref_ptr<osg::NodeCallback> _noDrawCallback;
bool _minMaxRangeShaderAdded;
Expand Down
31 changes: 31 additions & 0 deletions src/osgEarth/VisibleLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ namespace
}
)";

const char* depthOffsetVS = R"(
uniform float oe_VisibleLayer_depthOffset = 0.0;
void oe_VisibleLayer_applyDepthOffset(inout vec4 vertex_view)
{
vec3 vert_dir = normalize(vertex_view.xyz);
vertex_view.xyz = vertex_view.xyz - vert_dir * oe_VisibleLayer_depthOffset;
}
)";

// Shader that incorporates range-based opacity (min/max range with attenuation)
const char* rangeOpacityVS = R"(
#pragma import_defines(OE_DISABLE_RANGE_OPACITY)
Expand Down Expand Up @@ -127,6 +136,7 @@ VisibleLayer::Options::getConfig() const
conf.set( "attenuation_range", attenuationRange() );
conf.set( "blend", "interpolate", _blend, BLEND_INTERPOLATE );
conf.set( "blend", "modulate", _blend, BLEND_MODULATE );
conf.set( "depth_offset", depthOffset() );
conf.set( "nvgl", useNVGL() );
return conf;
}
Expand All @@ -142,6 +152,7 @@ VisibleLayer::Options::fromConfig(const Config& conf)
conf.get( "mask", _mask );
conf.get( "blend", "interpolate", _blend, BLEND_INTERPOLATE );
conf.get( "blend", "modulate", _blend, BLEND_MODULATE );
conf.get( "depth_offset", depthOffset() );
conf.get( "nvgl", useNVGL());
}

Expand Down Expand Up @@ -303,6 +314,12 @@ VisibleLayer::initializeUniforms()

vp->setFunction("oe_VisibleLayer_initOpacity", opacityVS, VirtualProgram::LOCATION_VERTEX_MODEL);


_depthOffsetU = new osg::Uniform("oe_VisibleLayer_depthOffset", (float)options().depthOffset().get());
stateSet->addUniform(_depthOffsetU.get());

vp->setFunction("oe_VisibleLayer_applyDepthOffset", depthOffsetVS, VirtualProgram::LOCATION_VERTEX_VIEW, 1.1f);

if (options().blend() == BLEND_MODULATE)
{
vp->setFunction("oe_VisibleLayer_setOpacity",
Expand Down Expand Up @@ -373,6 +390,20 @@ VisibleLayer::getOpacity() const
return options().opacity().get();
}

void
VisibleLayer::setDepthOffset(float value)
{
options().depthOffset() = value;
initializeUniforms();
_depthOffsetU->set(value);
}

float
VisibleLayer::getDepthOffset() const
{
return options().depthOffset().get();
}

void
VisibleLayer::setMinVisibleRange( float minVisibleRange )
{
Expand Down
7 changes: 7 additions & 0 deletions src/osgEarthImGui/LayersGUI
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,13 @@ namespace osgEarth
visibleLayer->setAttenuationRange(value);
}

if (visibleLayer->getNode() != nullptr)
{
float value = visibleLayer->getDepthOffset();
if (ImGuiLTable::SliderFloat("Depth offset", &value, -1.0f, 1.0f, "%.3f", 0))
visibleLayer->setDepthOffset(value);
}

bool debugView = visibleLayer->getEnableDebugView();
if (ImGuiLTable::Checkbox("Highlight", &debugView)) {
visibleLayer->setEnableDebugView(debugView);
Expand Down

0 comments on commit 20aa7d3

Please sign in to comment.