Skip to content

Instantly share code, notes, and snippets.

@gwaldron
Created April 30, 2024 12:39
Show Gist options
  • Save gwaldron/43a311af3fbed6a2a9ccc55945fc0d11 to your computer and use it in GitHub Desktop.
Save gwaldron/43a311af3fbed6a2a9ccc55945fc0d11 to your computer and use it in GitHub Desktop.
Test - toggle the alpha value of featurenode symbols
#include <osgViewer/Viewer>
#include <osgEarth/Notify>
#include <osgEarth/EarthManipulator>
#include <osgEarth/ExampleResources>
#include <osgEarth/MapNode>
#include <osgEarth/FeatureNode>
#include <osgEarth/TMS>
#include <iostream>
using namespace osgEarth;
using namespace osgEarth::Util;
FeatureNode* feature_node = nullptr;
bool lowlight = false;
int
main(int argc, char** argv)
{
osgEarth::initialize();
osgViewer::Viewer viewer;
viewer.setCameraManipulator(new EarthManipulator());
auto mapnode = new MapNode();
auto imagery = new TMSImageLayer();
imagery->setURL("https://readymap.org/readymap/tiles/1.0.0/7/");
mapnode->getMap()->addLayer(imagery);
const char* style_xml = R"(
<feature name="Testing">
<srs>wgs84</srs>
<geometry>
POLYGON((-100 47, -100 49, -95 48, -96 45, -98 42))
</geometry>
<style type="text/css">
Basic_Polygon_Style {
stroke: #ffff00;
stroke-width: 3px;
stroke-tessellation-size: 10km;
stroke-smooth: true;
fill: #00ff00;
text-content: "PRESS 'H' TO TOGGLE ME";
text-halo: #000000;
text-size: 48;
render-depth-offset: true;
render-max-tess-angle: 0.25;
}
</style>
</feature>
)";
Config conf;
conf.fromXML(std::istringstream(style_xml));
feature_node = new FeatureNode(conf.child("feature"), nullptr);
mapnode->addChild(feature_node);
viewer.setSceneData(mapnode);
EventRouter::get(&viewer).onKeyPress(osgGA::GUIEventAdapter::KEY_H,
[&](osg::View* view, float mx, float my)
{
lowlight = !lowlight;
float alpha = lowlight ? 0.25f : 1.0f;
// Make all other nodes transparent
osgEarth::Style style = feature_node->getStyle();
osgEarth::LineSymbol* lineSymbol =
style.getSymbol<osgEarth::LineSymbol>();
if (lineSymbol)
{
osgEarth::Color strokeColor = lineSymbol->stroke()->color();
strokeColor.a() = alpha;
lineSymbol->stroke()->color() = strokeColor;
}
osgEarth::PolygonSymbol* polygonSymbol =
style.getSymbol<osgEarth::PolygonSymbol>();
if (polygonSymbol)
{
osgEarth::Color fillColor = polygonSymbol->fill()->color();
fillColor.a() = alpha;
polygonSymbol->fill()->color() = fillColor;
}
osgEarth::TextSymbol* textSymbol =
style.getSymbol<osgEarth::TextSymbol>();
if (textSymbol)
{
osgEarth::Color textColor = textSymbol->fill()->color();
textColor.a() = alpha;
textSymbol->fill()->color() = textColor;
}
feature_node->setStyle(style);
feature_node->dirty(); // Force redraw
});
return viewer.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment