Commit 06aea5bc authored by Sebastian Eichelbaum's avatar Sebastian Eichelbaum

[ADD] added depth-based shading to the preprocessor. Its a mostly trivial...

[ADD] added depth-based shading to the preprocessor. Its a mostly trivial implementation but can produce some nice looking images.
parent a2efe030
......@@ -46,6 +46,25 @@ WGEPostprocessingNode::WGEPostprocessingNode( osg::ref_ptr< WGECamera > referenc
m_possibleSelections->addItem( "None", "No postprocessing." );
m_showHud = m_properties->addProperty( "Texture Debug", "If set, all intermediate texture are shown on screen for debugging.", false );
m_depthGroup = WPropGroup( new WPropertyGroup( "Additional Depth Shading", "Allows to emphasize depth by shading." ) );
m_shadeByDepth = m_depthGroup->addProperty( "Enable", "Enable depth-based shading.", false );
m_depthThresholdL = m_depthGroup->addProperty( "Lower Depth Threshold", "All depths in front of this are assumed to be foreground.", 0.25 );
m_depthThresholdU = m_depthGroup->addProperty( "Upper Depth Threshold",
"All depths behind this are assumed to be background.", 0.75 );
m_depthThresholdU->setMin( 0.0 );
m_depthThresholdU->setMax( 1.0 );
m_depthThresholdL->setMin( 0.0 );
m_depthThresholdL->setMax( 1.0 );
m_depthShadeL = m_depthGroup->addProperty( "Lower Shading Intensity", "How much to shade the background.", 0.75 );
m_depthShadeU = m_depthGroup->addProperty( "Upper Shading Intensity",
"How much to shade the foreground. Use values higher than one to brighten the foreground.", 1.0 );
m_depthShadeL->setMin( 0.0 );
m_depthShadeL->setMax( 1.0 );
m_depthShadeU->setMin( 0.0 );
m_depthShadeU->setMax( 3.0 );
m_active = m_properties->addProperty( "Enable", "If set, post-processing is enabled.", false, true );
m_activePostprocessor = m_properties->addProperty( "Postprocessor", "Selection one of the postprocessors.",
m_possibleSelections->getSelectorFirst(),
......@@ -88,8 +107,18 @@ WGEPostprocessingNode::WGEPostprocessingNode( osg::ref_ptr< WGECamera > referenc
m_possibleSelections->addItem( processor->getName(), processor->getDescription() );
// the final step
osg::ref_ptr< WGEOffscreenFinalPass > output = offscreen->addFinalOnScreenPass( new WGEShader( "WGEPostprocessorCombiner" ),
"Output" );
osg::ref_ptr< WGEShader > combinerShader( new WGEShader( "WGEPostprocessorCombiner" ) );
osg::ref_ptr< WGEOffscreenFinalPass > output = offscreen->addFinalOnScreenPass( combinerShader, "Output" );
// register the depth-shading values:
output->getOrCreateStateSet()->addUniform( new WGEPropertyUniform< WPropDouble >( "u_depthThresholdU", m_depthThresholdU ) );
output->getOrCreateStateSet()->addUniform( new WGEPropertyUniform< WPropDouble >( "u_depthThresholdL", m_depthThresholdL ) );
output->getOrCreateStateSet()->addUniform( new WGEPropertyUniform< WPropDouble >( "u_depthShadeU", m_depthShadeU ) );
output->getOrCreateStateSet()->addUniform( new WGEPropertyUniform< WPropDouble >( "u_depthShadeL", m_depthShadeL ) );
// Enable depth-shade via compile-time options:
combinerShader->addPreprocessor( WGEShaderPreprocessor::SPtr(
new WGEShaderPropertyDefineOptions< WPropBool >( m_shadeByDepth, "DEPTH_SHADING_DISABLED", "DEPTH_SHADING_ENABLED" ) ) );
osg::ref_ptr< osg::Texture2D > colorTex = processor->getOutput();
colorTex->setFilter( osg::Texture::MIN_FILTER, osg::Texture::LINEAR_MIPMAP_LINEAR );
......@@ -108,6 +137,8 @@ WGEPostprocessingNode::WGEPostprocessingNode( osg::ref_ptr< WGECamera > referenc
addChild( offscreen );
}
m_properties->addProperty( m_depthGroup );
// let the props control some stuff
addUpdateCallback( new WGESwitchCallback< WPropSelection >( m_activePostprocessor ) );
}
......@@ -199,4 +230,7 @@ void WGEPostprocessingNode::postprocessorSelected()
{
m_postprocs[ i ]->getProperties()->setHidden( i != ( active - 1 ) );
}
// Show depth shading?
m_depthGroup->setHidden( active == 0 );
}
......@@ -165,6 +165,36 @@ private:
*/
WPropBool m_showHud;
/**
* Activate depth based darkening
*/
WPropBool m_shadeByDepth;
/**
* Lower depth threshold -> lower depth is background.
*/
WPropDouble m_depthThresholdL;
/**
* Upper depth threshold -> higher depth is foreground.
*/
WPropDouble m_depthThresholdU;
/**
* Lower depth shade threshold -> how dark to get
*/
WPropDouble m_depthShadeL;
/**
* Upper depth threshold -> how bright to get.
*/
WPropDouble m_depthShadeU;
/**
* Group these depth-shading props.
*/
WPropGroup m_depthGroup;
/**
* The property containing the currently active method or a combination.
*/
......
......@@ -44,6 +44,15 @@ uniform sampler2D u_texture0Sampler;
uniform sampler2D u_texture1Sampler;
#define u_depthSampler u_texture1Sampler
/**
* Depth shading parameters
*/
uniform float u_depthShadeL = 0.75;
uniform float u_depthShadeU = 1.0;
uniform float u_depthThresholdL = 0.25;
uniform float u_depthThresholdU = 0.75;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Varying
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
......@@ -64,6 +73,19 @@ vec2 pixelCoord = gl_TexCoord[0].st;
// * Blending utilities
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* Returns the original unprocessed color value at the specified point
*
* \param where the pixel to grab
* \param lod the level of detail to get. 0 for unfiltered texture.
*
* \return the color
*/
vec4 getColor( in vec2 where, in float lod )
{
return texture2DLod( u_colorSampler, where, lod );
}
/**
* Returns the original unprocessed color value at the specified point
*
......@@ -134,8 +156,53 @@ void main()
}
#endif
vec4 finalColor = getColor( coord );
#ifdef DEPTH_SHADING_ENABLED
// Add some depth-based un-sharpness?
float dNorm = clamp( ( depth - u_depthThresholdL ) / ( u_depthThresholdU - u_depthThresholdL ), 0.0, 1.0 );
// Use this to experiment with camera focus and depth of field effects
/*
if( depth < u_depthThresholdU )
{
finalColor = getColor( coord, 0.0 );
}
else
{
float numLevels = 4.0;
if( dNorm < 0.25 )
{
finalColor = getColor( coord, 0.0 );
}
else
{
if ( dNorm < 0.5 )
{
finalColor = getColor( coord, 1.0 );
}
else
{
if ( dNorm < 0.75 )
{
finalColor = getColor( coord, 2.0 );
}
else
{
finalColor = getColor( coord, 3.0 );
}
}
}
}
*/
// Darken by depth? Scale quadratically.
float dni = 2.0 * ( 1.0 - dNorm );
finalColor.rgb *= clamp( dni * dni, u_depthShadeL, u_depthShadeU );
#endif // DEPTH_SHADING_ENABLED
// output the depth and final color.
gl_FragColor = getColor( coord );
gl_FragColor = finalColor;
gl_FragDepth = depth;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment