osgearth icon indicating copy to clipboard operation
osgearth copied to clipboard

Why does the land image area also show water when using Triton library and using ocean_mask.tif image to mask the land area?

Open mirro187 opened this issue 2 years ago • 3 comments

{ //mask ocean osgEarth::Drivers::GDALOptions layerOpt; layerOpt.url() = osgEarth::URI("E:/shareLIB/ResData/ocean_mask.tif"); ImageLayerOptions imageOpts("ocean_mask", layerOpt); // osg::ref_ptrosgEarth::ImageLayer imageLayer = new osgEarth::ImageLayer(imageOpts); imageLayer->setVisible(false); _mapNode->getMap()->addLayer(imageLayer.get()); } // { osgEarthTriton::TritonOptions tritonOptions; tritonOptions.user() = "my_user_name"; tritonOptions.licenseCode() = "my_license_code"; tritonOptions.maxAltitude() = 10000; tritonOptions.maskLayer() = "ocean_mask"; osg::ref_ptr<TritonLayer> tritonLayer = new TritonLayer(tritonOptions); _mapNode->getMap()->addLayer(tritonLayer); } // Create TritonNode from TritonOptions osgEarthTriton::TritonOptions tritonOptions; tritonOptions.user() = "my_user_name"; tritonOptions.licenseCode() = "my_license_code"; tritonOptions.maxAltitude() = 10000; tritonOptions.useHeightMap() = true; // const char* ev_t = ::getenv("TRITON_PATH"); if (ev_t) { tritonOptions.resourcePath() = osgDB::concatPaths( std::string(ev_t), "Resources"); } // _pTritonLayer = new TritonLayer(tritonOptions, new TritonCallback()); _pTritonLayer->setOpacity(0.6); _mapNode->getMap()->addLayer(_pTritonLayer.get());

mirro187 avatar Oct 11 '21 07:10 mirro187

Try making your mask layer shared:

imageLayer->setShared(true);

gwaldron avatar Nov 15 '21 18:11 gwaldron

Try making your mask layer shared:

imageLayer->setShared(true);

//Why did I set true to ImageLayer in this triton.earth file and it still doesn't work?

E:/shareLIB/ResData/ocean_mask.tif ----------------------------------------------------
<image name="readymap_imagery" driver="tms" visible="true">
    <url>E:/workplace/platform-standard-5007/branches/ParallelCloud/X64/bin/Debug/data/BasicImage/3DImage/ttt/tms.xml</url>
</image>

<elevation name="readymap_elevation" driver="tms">
    <url>E:/workplace/platform-standard-5007/branches/ParallelCloud/X64/bin/Debug/data/BasicImage/Dem2/tms.xml</url>
</elevation>

<!-- Triton masking layer. By default Triton will look at the elevation of
     the terrain and will draw water where the elevation is less than zero.
     But this will cause land below sea level to get water as well. You can
     use a mask like this to prevent that from happening. -->
     
<image name="ocean_mask" driver="gdal" shared="true" visible="false">
    <url>E:/shareLIB/ResData/ocean_mask.tif</url>
</image>
<viewpoints>
    <viewpoint name="Start" heading="-29.7876" pitch="-12" range="4200" long="-156.537" lat="20.6702" />
    <viewpoint name="Above" heading="-35.42227636584842" height="-188.2792971581221" lat="20.68154179570284" long="-156.5452311560784" pitch="-30" range="5469"/>
    <viewpoint name="Near clip" heading="-0.618211rad" height="-190.1852927561849" lat="20.67586333023495" long="-156.5418074743535" pitch="-0.2546rad" range="1154.32m"/>
    <viewpoint name="Horizon"  heading="-27.1911" height="-206.3652788307518" lat="20.69785423327782" long="-156.5550697849549" pitch="-16.0293" range="68323m"/>
    <viewpoint>
        <name>Positano</name>
        <heading>6.84601</heading>
        <pitch>-1.08752</pitch>
        <range>11078.6m</range>
        <long>14.49135978754529</long>
        <lat>40.62397054670792</lat>
        <height>55.47445763554424</height>
        <srs>+proj=longlat +datum=WGS84 +no_defs </srs>
    </viewpoint>

</viewpoints>

<annotations>
    <model name="Object in the water">
        <url>../data/red_flag.osg.40.scale</url>
        <position lat="20.6714" long="-156.5384" alt="0"/>
    </model>
</annotations>
---------------------------------------------------------------------------------------- /* -*-c++-*- */ /* osgEarth - Geospatial SDK for OpenSceneGraph * Copyright 2008-2014 Pelican Mapping * http://osgearth.org * * osgEarth is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see */ #include #include #include #include #include #include #include #include #include #include

#define LC "[osgearth_triton] "

using namespace osgEarth; using namespace osgEarth::Util; using namespace osgEarth::Triton; namespace ui = osgEarth::Util::Controls;

struct Settings { optional chop; optional seaState; optional alpha; osg::observer_ptr<TritonLayer> tritonLayer;

void apply(Environment& env, Ocean& ocean)
{
    if (chop.isSet())
    {
        ocean.SetChoppiness(chop.get());
        chop.clear();
    }

    if (seaState.isSet())
    {
        env.SimulateSeaState(seaState.get(), 0.0);
        seaState.clear();
    }

    osg::ref_ptr<TritonLayer> layer;
    if (alpha.isSet() && tritonLayer.lock(layer))
    {
        layer->setOpacity(alpha.value());
        alpha.clear();
    }
}

};

class TritonCallback : public osgEarth::Triton::Callback { public: TritonCallback(Settings& settings) : _settings(settings) { }

void onInitialize(Environment& env, Ocean& ocean)
{
    //todo
}

void onDrawOcean(Environment& env, Ocean& ocean)
{
    _settings.apply(env, ocean);
}

Settings& _settings;

};

struct App { App() { tritonLayer = NULL; map = NULL; }

Map*         map;
TritonLayer* tritonLayer;
Settings     settings;

void addTriton()
{
	{
		osgEarth::Triton::TritonOptions tritonOptions;
		tritonOptions.user() = "my_user_name";
		tritonOptions.licenseCode() = "my_license_code";
		tritonOptions.maxAltitude() = 100000;
		tritonOptions.maskLayer()   = "ocean_mask";
		tritonLayer = new TritonLayer(tritonOptions, new TritonCallback(settings));
		map->addLayer(tritonLayer);
	}
    // Create TritonNode from TritonOptions
    osgEarth::Triton::TritonOptions tritonOptions;
    tritonOptions.user()         = "my_user_name";
    tritonOptions.licenseCode()  = "my_license_code";
    tritonOptions.maxAltitude()  = 100000;
    tritonOptions.useHeightMap() = true;
    const char* ev_t = ::getenv("TRITON_PATH");
    if ( ev_t )
    {
        tritonOptions.resourcePath() = osgDB::concatPaths(
            std::string(ev_t),
            "Resources" );

        OE_INFO << LC 
            << "Setting resource path to << " << tritonOptions.resourcePath().get()
            << std::endl;
    }
    else
    {
        OE_WARN << LC
            << "No resource path! Triton might not initialize properly. "
            << "Consider setting the TRITON_PATH environment variable."
            << std::endl;
    }


    tritonLayer = new TritonLayer(tritonOptions, new TritonCallback(settings));
    map->addLayer(tritonLayer);
    settings.tritonLayer = tritonLayer;
}

void removeTriton()
{
    if (tritonLayer)
        map->removeLayer(tritonLayer);
    tritonLayer = 0L;
}

};

App s_app;

template<typename T> struct Set : public ui::ControlEventHandler { optional<T>& _var; Set(optional<T>& var) : _var(var) { } void onValueChanged(ui::Control*, double value) { _var = value; } };

struct Toggle : public ui::ControlEventHandler { void onValueChanged(ui::Control*, bool value) { if (s_app.tritonLayer) s_app.removeTriton(); else s_app.addTriton(); } };

Container* createUI() { VBox* box = new VBox(); box->setBackColor(0,0,0,0.5); Grid* grid = box->addControl(new Grid()); int r=0; grid->setControl(0, r, new LabelControl("Chop")); grid->setControl(1, r, new HSliderControl(0, 3, 0, new Set(s_app.settings.chop))); ++r; grid->setControl(0, r, new LabelControl("Sea State")); grid->setControl(1, r, new HSliderControl(0, 12, 5, new Set(s_app.settings.seaState))); ++r;
grid->setControl(0, r, new LabelControl("Alpha")); grid->setControl(1, r, new HSliderControl(0, 1.0, 1.0, new Set(s_app.settings.alpha))); ++r; grid->setControl(0, r, new LabelControl("Toggle")); grid->setControl(1, r, new CheckBoxControl(false, new Toggle()));

grid->getControl(1, r-1)->setHorizFill(true,200);

return box;

}

int usage(const char* name) { OE_DEBUG << "\nUsage: " << name << " file.earth" << std::endl << osgEarth::Util::MapNodeHelper().usage() << std::endl;

return 0;

}

int main(int argc, char** argv) { osg::ArgumentParser arguments(&argc,argv);

// help?
if ( arguments.read("--help") )
    return usage(argv[0]);

// create a viewer:
osgViewer::Viewer viewer(arguments);

// Tell the database pager to not modify the unref settings
viewer.getDatabasePager()->setUnrefImageDataAfterApplyPolicy( false, false );

// install our default manipulator (do this before calling load)
viewer.setCameraManipulator( new osgEarth::Util::EarthManipulator() );

// load an earth file, and support all or our example command-line options
// and earth file <external> tags    
osg::Group* node = osgEarth::Util::MapNodeHelper().load(arguments, &viewer, createUI());
if ( node )
{        
    viewer.getCamera()->setNearFarRatio(0.00002);
    viewer.getCamera()->setSmallFeatureCullingPixelSize(-1.0f);

    viewer.setSceneData( node );

    s_app.map = MapNode::get( node )->getMap();
    s_app.addTriton();

    return viewer.run();
}
else
{
    return usage(argv[0]);
}

}

mirro187 avatar Jan 05 '22 11:01 mirro187

Try making your mask layer shared:

imageLayer->setShared(true);

//Why did I set true to ImageLayer in this triton.earth file and it still doesn't work?

E:/shareLIB/ResData/ocean_mask.tif ----------------------------------------------------
<image name="readymap_imagery" driver="tms" visible="true">
    <url>E:/workplace/platform-standard-5007/branches/ParallelCloud/X64/bin/Debug/data/BasicImage/3DImage/ttt/tms.xml</url>
</image>

<elevation name="readymap_elevation" driver="tms">
    <url>E:/workplace/platform-standard-5007/branches/ParallelCloud/X64/bin/Debug/data/BasicImage/Dem2/tms.xml</url>
</elevation>

<!-- Triton masking layer. By default Triton will look at the elevation of
     the terrain and will draw water where the elevation is less than zero.
     But this will cause land below sea level to get water as well. You can
     use a mask like this to prevent that from happening. -->
     
<image name="ocean_mask" driver="gdal" shared="true" visible="false">
    <url>E:/shareLIB/ResData/ocean_mask.tif</url>
</image>
<viewpoints>
    <viewpoint name="Start" heading="-29.7876" pitch="-12" range="4200" long="-156.537" lat="20.6702" />
    <viewpoint name="Above" heading="-35.42227636584842" height="-188.2792971581221" lat="20.68154179570284" long="-156.5452311560784" pitch="-30" range="5469"/>
    <viewpoint name="Near clip" heading="-0.618211rad" height="-190.1852927561849" lat="20.67586333023495" long="-156.5418074743535" pitch="-0.2546rad" range="1154.32m"/>
    <viewpoint name="Horizon"  heading="-27.1911" height="-206.3652788307518" lat="20.69785423327782" long="-156.5550697849549" pitch="-16.0293" range="68323m"/>
    <viewpoint>
        <name>Positano</name>
        <heading>6.84601</heading>
        <pitch>-1.08752</pitch>
        <range>11078.6m</range>
        <long>14.49135978754529</long>
        <lat>40.62397054670792</lat>
        <height>55.47445763554424</height>
        <srs>+proj=longlat +datum=WGS84 +no_defs </srs>
    </viewpoint>

</viewpoints>

<annotations>
    <model name="Object in the water">
        <url>../data/red_flag.osg.40.scale</url>
        <position lat="20.6714" long="-156.5384" alt="0"/>
    </model>
</annotations>
---------------------------------------------------------------------------------------- /* -*-c++-*- */ /* osgEarth - Geospatial SDK for OpenSceneGraph * Copyright 2008-2014 Pelican Mapping * http://osgearth.org * * osgEarth is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see */ #include #include #include #include #include #include #include #include #include #include

#define LC "[osgearth_triton] "

using namespace osgEarth; using namespace osgEarth::Util; using namespace osgEarth::Triton; namespace ui = osgEarth::Util::Controls;

struct Settings { optional chop; optional seaState; optional alpha; osg::observer_ptr<TritonLayer> tritonLayer;

void apply(Environment& env, Ocean& ocean)
{
    if (chop.isSet())
    {
        ocean.SetChoppiness(chop.get());
        chop.clear();
    }

    if (seaState.isSet())
    {
        env.SimulateSeaState(seaState.get(), 0.0);
        seaState.clear();
    }

    osg::ref_ptr<TritonLayer> layer;
    if (alpha.isSet() && tritonLayer.lock(layer))
    {
        layer->setOpacity(alpha.value());
        alpha.clear();
    }
}

};

class TritonCallback : public osgEarth::Triton::Callback { public: TritonCallback(Settings& settings) : _settings(settings) { }

void onInitialize(Environment& env, Ocean& ocean)
{
    //todo
}

void onDrawOcean(Environment& env, Ocean& ocean)
{
    _settings.apply(env, ocean);
}

Settings& _settings;

};

struct App { App() { tritonLayer = NULL; map = NULL; }

Map*         map;
TritonLayer* tritonLayer;
Settings     settings;

void addTriton()
{
	{
		osgEarth::Triton::TritonOptions tritonOptions;
		tritonOptions.user() = "my_user_name";
		tritonOptions.licenseCode() = "my_license_code";
		tritonOptions.maxAltitude() = 100000;
		tritonOptions.maskLayer()   = "ocean_mask";
		tritonLayer = new TritonLayer(tritonOptions, new TritonCallback(settings));
		map->addLayer(tritonLayer);
	}
    // Create TritonNode from TritonOptions
    osgEarth::Triton::TritonOptions tritonOptions;
    tritonOptions.user()         = "my_user_name";
    tritonOptions.licenseCode()  = "my_license_code";
    tritonOptions.maxAltitude()  = 100000;
    tritonOptions.useHeightMap() = true;
    const char* ev_t = ::getenv("TRITON_PATH");
    if ( ev_t )
    {
        tritonOptions.resourcePath() = osgDB::concatPaths(
            std::string(ev_t),
            "Resources" );

        OE_INFO << LC 
            << "Setting resource path to << " << tritonOptions.resourcePath().get()
            << std::endl;
    }
    else
    {
        OE_WARN << LC
            << "No resource path! Triton might not initialize properly. "
            << "Consider setting the TRITON_PATH environment variable."
            << std::endl;
    }


    tritonLayer = new TritonLayer(tritonOptions, new TritonCallback(settings));
    map->addLayer(tritonLayer);
    settings.tritonLayer = tritonLayer;
}

void removeTriton()
{
    if (tritonLayer)
        map->removeLayer(tritonLayer);
    tritonLayer = 0L;
}

};

App s_app;

template<typename T> struct Set : public ui::ControlEventHandler { optional<T>& _var; Set(optional<T>& var) : _var(var) { } void onValueChanged(ui::Control*, double value) { _var = value; } };

struct Toggle : public ui::ControlEventHandler { void onValueChanged(ui::Control*, bool value) { if (s_app.tritonLayer) s_app.removeTriton(); else s_app.addTriton(); } };

Container* createUI() { VBox* box = new VBox(); box->setBackColor(0,0,0,0.5); Grid* grid = box->addControl(new Grid()); int r=0; grid->setControl(0, r, new LabelControl("Chop")); grid->setControl(1, r, new HSliderControl(0, 3, 0, new Set(s_app.settings.chop))); ++r; grid->setControl(0, r, new LabelControl("Sea State")); grid->setControl(1, r, new HSliderControl(0, 12, 5, new Set(s_app.settings.seaState))); ++r;
grid->setControl(0, r, new LabelControl("Alpha")); grid->setControl(1, r, new HSliderControl(0, 1.0, 1.0, new Set(s_app.settings.alpha))); ++r; grid->setControl(0, r, new LabelControl("Toggle")); grid->setControl(1, r, new CheckBoxControl(false, new Toggle()));

grid->getControl(1, r-1)->setHorizFill(true,200);

return box;

}

int usage(const char* name) { OE_DEBUG << "\nUsage: " << name << " file.earth" << std::endl << osgEarth::Util::MapNodeHelper().usage() << std::endl;

return 0;

}

int main(int argc, char** argv) { osg::ArgumentParser arguments(&argc,argv);

// help?
if ( arguments.read("--help") )
    return usage(argv[0]);

// create a viewer:
osgViewer::Viewer viewer(arguments);

// Tell the database pager to not modify the unref settings
viewer.getDatabasePager()->setUnrefImageDataAfterApplyPolicy( false, false );

// install our default manipulator (do this before calling load)
viewer.setCameraManipulator( new osgEarth::Util::EarthManipulator() );

// load an earth file, and support all or our example command-line options
// and earth file <external> tags    
osg::Group* node = osgEarth::Util::MapNodeHelper().load(arguments, &viewer, createUI());
if ( node )
{        
    viewer.getCamera()->setNearFarRatio(0.00002);
    viewer.getCamera()->setSmallFeatureCullingPixelSize(-1.0f);

    viewer.setSceneData( node );

    s_app.map = MapNode::get( node )->getMap();
    s_app.addTriton();

    return viewer.run();
}
else
{
    return usage(argv[0]);
}

}

mirro187 avatar Jan 05 '22 11:01 mirro187