Skip to content

Commit

Permalink
MP: add support for min and max range with fading
Browse files Browse the repository at this point in the history
  • Loading branch information
gwaldron committed Feb 11, 2015
1 parent aafa96f commit 19e380c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/osgEarth/ImageLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ImageLayerOptions::setDefaults()
{
_opacity.init( 1.0f );
_transparentColor.init( osg::Vec4ub(0,0,0,0) );
_minRange.init( -FLT_MAX );
_minRange.init( 0.0 );
_maxRange.init( FLT_MAX );
_lodBlending.init( false );
_featherPixels.init( false );
Expand Down
9 changes: 8 additions & 1 deletion src/osgEarthDrivers/engine_mp/MPEngine.frag.glsl
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#version $GLSL_VERSION_STR
$GLSL_DEFAULT_PRECISION_FLOAT

#pragma vp_entryPoint "oe_mp_apply_coloring"
#pragma vp_location "fragment_coloring"
#pragma vp_order "0"

#define MP_USE_TERRAIN_COLOR
#define MP_USE_BLENDING

Expand All @@ -10,11 +14,14 @@ uniform int oe_layer_order;
uniform float oe_layer_opacity;

varying vec4 oe_layer_texc;
varying float oe_terrain_rangeOpacity;

#ifdef MP_USE_TERRAIN_COLOR
uniform vec4 oe_terrain_color;
#endif

uniform float m;

void oe_mp_apply_coloring(inout vec4 color)
{
#ifdef MP_USE_TERRAIN_COLOR
Expand All @@ -23,7 +30,7 @@ void oe_mp_apply_coloring(inout vec4 color)

float applyImagery = oe_layer_uid >= 0 ? 1.0 : 0.0;
vec4 texel = mix(color, texture2D(oe_layer_tex, oe_layer_texc.st), applyImagery);
texel.a = mix(texel.a, texel.a*oe_layer_opacity, applyImagery);
texel.a = mix(texel.a, texel.a*oe_layer_opacity*oe_terrain_rangeOpacity, applyImagery);

#ifdef MP_USE_BLENDING
float firstLayer = oe_layer_order == 0 ? 1.0 : 0.0;
Expand Down
23 changes: 22 additions & 1 deletion src/osgEarthDrivers/engine_mp/MPEngine.vert.glsl
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
#version $GLSL_VERSION_STR
$GLSL_DEFAULT_PRECISION_FLOAT

#pragma vp_entryPoint "oe_mp_setup_coloring"
#pragma vp_location "vertex_view"
#pragma vp_order "0"

uniform float oe_layer_minRange;
uniform float oe_layer_maxRange;

varying vec4 oe_layer_texc;
varying vec4 oe_layer_tilec;
varying float oe_terrain_rangeOpacity;

void oe_mp_setup_coloring(inout vec4 VertexModel)
void oe_mp_setup_coloring(inout vec4 vertexView)
{
oe_layer_texc = gl_MultiTexCoord$MP_PRIMARY_UNIT;
oe_layer_tilec = gl_MultiTexCoord$MP_SECONDARY_UNIT;

float range = -vertexView.z;

float rangeSpanSlice = 0.1*(oe_layer_maxRange-oe_layer_minRange);
float maxFadeSpan = min(rangeSpanSlice, oe_layer_maxRange*0.1);
float minFadeSpan = min(rangeSpanSlice, oe_layer_minRange*0.1);

oe_terrain_rangeOpacity =
range > oe_layer_maxRange + maxFadeSpan ? 0.0 :
range > oe_layer_maxRange ? 1.0-(range-oe_layer_maxRange)/maxFadeSpan :
range > oe_layer_minRange + minFadeSpan ? 1.0 :
range > oe_layer_minRange ? (range-oe_layer_minRange)/minFadeSpan :
0.0;
}
2 changes: 2 additions & 0 deletions src/osgEarthDrivers/engine_mp/MPGeometry
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ namespace osgEarth { namespace Drivers { namespace MPTerrainEngine
unsigned _opacityUniformNameID;
unsigned _texMatParentUniformNameID;
unsigned _tileKeyUniformNameID;
unsigned _minRangeUniformNameID;
unsigned _maxRangeUniformNameID;

// Data stored for each graphics context:
struct PerContextData {
Expand Down
37 changes: 37 additions & 0 deletions src/osgEarthDrivers/engine_mp/MPGeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ _imageUnit ( imageUnit )
_orderUniformNameID = osg::Uniform::getNameID( "oe_layer_order" );
_opacityUniformNameID = osg::Uniform::getNameID( "oe_layer_opacity" );
_texMatParentUniformNameID = osg::Uniform::getNameID( "oe_layer_parent_texmat" );
_minRangeUniformNameID = osg::Uniform::getNameID( "oe_layer_minRange" );
_maxRangeUniformNameID = osg::Uniform::getNameID( "oe_layer_maxRange" );

// we will set these later (in TileModelCompiler)
this->setUseVertexBufferObjects(false);
Expand Down Expand Up @@ -123,6 +125,8 @@ MPGeometry::renderPrimitiveSets(osg::State& state,
GLint uidLocation = -1;
GLint orderLocation = -1;
GLint texMatParentLocation = -1;
GLint minRangeLocation = -1;
GLint maxRangeLocation = -1;

// The PCP can change (especially in a VirtualProgram environment). So we do need to
// requery the uni locations each time unfortunately. TODO: explore optimizations.
Expand Down Expand Up @@ -186,6 +190,8 @@ MPGeometry::renderPrimitiveSets(osg::State& state,

// remember whether we applied a parent texture.
bool usedTexParent = false;
bool useMinVisibleRange = false;
bool useMaxVisibleRange = false;

if ( _layers.size() > 0 )
{
Expand Down Expand Up @@ -230,9 +236,28 @@ MPGeometry::renderPrimitiveSets(osg::State& state,
// no texture LOD blending for shared layers for now. maybe later.
}
}

// check for min/rax range usage.
const ImageLayerOptions& layerOptions = layer._imageLayer->getImageLayerOptions();
if ( layerOptions.minVisibleRange().isSet() )
useMinVisibleRange = true;
if ( layerOptions.maxVisibleRange().isSet() )
useMaxVisibleRange = true;
}
}

// look up the minRange uniform if necessary
if ( useMinVisibleRange && pcp )
{
minRangeLocation = pcp->getUniformLocation( _minRangeUniformNameID );
}

// look up the maxRange uniform if necessary
if ( useMaxVisibleRange && pcp )
{
maxRangeLocation = pcp->getUniformLocation( _maxRangeUniformNameID );
}

if (renderColor)
{
// find the first opaque layer, top-down, and start there:
Expand Down Expand Up @@ -316,6 +341,18 @@ MPGeometry::renderPrimitiveSets(osg::State& state,
{
ext->glUniformMatrix4fv( texMatParentLocation, 1, GL_FALSE, layer._texMatParent.ptr() );
}

// assign the min range
if ( minRangeLocation >= 0 )
{
ext->glUniform1f( minRangeLocation, layer._imageLayer->getImageLayerOptions().minVisibleRange().get() );
}

// assign the max range
if ( maxRangeLocation >= 0 )
{
ext->glUniform1f( maxRangeLocation, layer._imageLayer->getImageLayerOptions().maxVisibleRange().get() );
}
}

// draw the primitive sets.
Expand Down
7 changes: 6 additions & 1 deletion src/osgEarthDrivers/engine_mp/MPTerrainEngineNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,8 @@ MPTerrainEngineNode::updateState()
osgEarth::replaceIn( vs, "$MP_PRIMARY_UNIT", Stringify() << _primaryUnit );
osgEarth::replaceIn( vs, "$MP_SECONDARY_UNIT", Stringify() << _secondaryUnit );

vp->setFunction( "oe_mp_setup_coloring", vs, ShaderComp::LOCATION_VERTEX_MODEL, 0.0 );
//vp->setFunction( "oe_mp_setup_coloring", vs, ShaderComp::LOCATION_VERTEX_MODEL, 0.0 );
vp->setFunction( "oe_mp_setup_coloring", vs, ShaderComp::LOCATION_VERTEX_VIEW, 0.0 );

// Fragment shader:
std::string fs = ShaderLoader::load(
Expand Down Expand Up @@ -998,6 +999,10 @@ MPTerrainEngineNode::updateState()
terrainStateSet->getOrCreateUniform(
"oe_layer_order", osg::Uniform::INT )->set( 0 );

// default min/max range uniforms.
terrainStateSet->addUniform( new osg::Uniform("oe_layer_minRange", 0.0f) );
terrainStateSet->addUniform( new osg::Uniform("oe_layer_maxRange", FLT_MAX) );

// base terrain color.
if ( useTerrainColor )
{
Expand Down
4 changes: 3 additions & 1 deletion src/osgEarthDrivers/engine_mp/TileModelCompiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,9 @@ namespace
layer._opaque =
(r->_layer.getMapLayer()->getColorFilters().size() == 0 ) &&
(layer._tex.valid() && !r->_layer.hasAlpha()) &&
(!layer._texParent.valid() || !r->_layerParent.hasAlpha());
(!layer._texParent.valid() || !r->_layerParent.hasAlpha()) &&
(layer._imageLayer.valid() && layer._imageLayer->getMinVisibleRange() == 0.0f) &&
(layer._imageLayer.valid() && layer._imageLayer->getMaxVisibleRange() == FLT_MAX);

// texture matrix: scale/bias matrix of the texture. Currently we don't use
// this for rendering because the scale/bias is already baked into the
Expand Down
8 changes: 5 additions & 3 deletions tests/min_max_range.earth
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ TIP: set your OSG_NUM_HTTP_DATABASE_THREADS to 4 or more!
<map name="MapQuest Open Aerial" type="geocentric" version="2">

<image name="mapquest_open_aerial" driver="xyz">
<url>http://oatile[1234].mqcdn.com/naip/{z}/{x}/{y}.jpg</url>
<url>http://oatile[1234].mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpg</url>
<profile>spherical-mercator</profile>
<cache_policy usage="no_cache"/>
<nodata_image>http://oatile3.mqcdn.com/tiles/1.0.0/sat/13/636/6210.jpg</nodata_image>
</image>
<image name="mapquest_osm" driver="xyz" max_range="5e6" min_range="1e6">
<image name="mapquest_osm" driver="xyz" max_range="1e7" min_range="1e6">
<url>http://otile[1234].mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.jpg</url>
<profile>global-mercator</profile>
</image>
Expand Down

0 comments on commit 19e380c

Please sign in to comment.