Skip to content

Commit

Permalink
terrainshader: change spec to use <code>
Browse files Browse the repository at this point in the history
  • Loading branch information
gwaldron committed Feb 9, 2015
1 parent b97d038 commit 75b7e9e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 67 deletions.
47 changes: 22 additions & 25 deletions src/osgEarthExtensions/terrainshader/TerrainShaderExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,23 @@ namespace
class GLSLEffect : public TerrainEffect
{
public:
GLSLEffect(const std::string& vs, const std::string& fs)
: _vs(vs), _fs(fs)
GLSLEffect(const std::vector<std::string>& code) : _code(code)
{
if ( !_vs.empty() )
_package.add( "$vs", _vs );
if ( !_fs.empty() )
_package.add( "$fs", _fs );
}

void onInstall(TerrainEngineNode* engine)
{

if ( !_vs.empty() )
for(unsigned i=0; i<code.size(); ++i)
{
VirtualProgram* vp = VirtualProgram::getOrCreate(engine->getOrCreateStateSet());
ShaderLoader::loadFunction(vp, "$vs", _package);
_package.add( "$code."+i, code[i] );
}
}

if ( !_fs.empty() )
void onInstall(TerrainEngineNode* engine)
{
for(unsigned i=0; i<_code.size(); ++i)
{
VirtualProgram* vp = VirtualProgram::getOrCreate(engine->getOrCreateStateSet());
ShaderLoader::loadFunction(vp, "$fs", _package);
if ( !_code[i].empty() )
{
VirtualProgram* vp = VirtualProgram::getOrCreate(engine->getOrCreateStateSet());
ShaderLoader::loadFunction(vp, "$code."+i, _package);
}
}
}

Expand All @@ -64,17 +59,19 @@ namespace
VirtualProgram* vp = VirtualProgram::get(engine->getStateSet());
if ( vp )
{
if ( !_vs.empty() )
ShaderLoader::unloadFunction( vp, "$vs", _package );

if ( !_fs.empty() )
ShaderLoader::unloadFunction( vp, "$fs", _package );
for(unsigned i=0; i<_code.size(); ++i)
{
if ( !_code[i].empty() )
{
ShaderLoader::unloadFunction(vp, "$code."+i, _package);
}
}
}
}
}

std::string _vs, _fs;
ShaderPackage _package;
std::vector<std::string> _code;
ShaderPackage _package;
};
}

Expand Down Expand Up @@ -109,7 +106,7 @@ TerrainShaderExtension::connect(MapNode* mapNode)
OE_WARN << LC << "Illegal: MapNode cannot be null." << std::endl;
return false;
}
_effect = new GLSLEffect( _options.vertex().get(), _options.fragment().get() );
_effect = new GLSLEffect( _options.code() );
mapNode->getTerrainEngine()->addEffect( _effect.get() );

OE_INFO << LC << "Installed.\n";
Expand Down
25 changes: 12 additions & 13 deletions src/osgEarthExtensions/terrainshader/TerrainShaderOptions
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#ifndef OSGEARTH_EXT_TERRAIN_SHADER_OPTIONS
#define OSGEARTH_EXT_TERRAIN_SHADER_OPTIONS 1

#include <osgEarth/Common>
#include <osgEarth/URI>
#include <osgEarth/Config>
#include <vector>

namespace osgEarth { namespace TerrainShader
{
Expand All @@ -32,11 +32,8 @@ namespace osgEarth { namespace TerrainShader
class TerrainShaderOptions : public DriverConfigOptions // NO EXPORT; header only
{
public:
optional<std::string>& vertex() { return _vertex; }
const optional<std::string>& vertex() const { return _vertex; }

optional<std::string>& fragment() { return _fragment; }
const optional<std::string>& fragment() const { return _fragment; }
std::vector<std::string>& code() { return _code; }
const std::vector<std::string>& code() const { return _code; }

public:
TerrainShaderOptions( const ConfigOptions& opt =ConfigOptions() ) : DriverConfigOptions( opt )
Expand All @@ -50,8 +47,9 @@ namespace osgEarth { namespace TerrainShader
public:
Config getConfig() const {
Config conf = DriverConfigOptions::getConfig();
conf.updateIfSet("vertex", _vertex);
conf.updateIfSet("fragment", _fragment);
for(unsigned i=0; i<_code.size(); ++i) {
conf.add("code", _code[i]);
}
return conf;
}

Expand All @@ -63,12 +61,13 @@ namespace osgEarth { namespace TerrainShader

private:
void fromConfig( const Config& conf ) {
conf.getIfSet("vertex", _vertex);
conf.getIfSet("fragment", _fragment);
ConfigSet s = conf.children("code");
for(ConfigSet::const_iterator i = s.begin(); i != s.end(); ++i) {
_code.push_back( i->value() );
}
}

optional<std::string> _vertex;
optional<std::string> _fragment;
std::vector<std::string> _code;
};

} } // namespace osgEarth::TerrainShader
Expand Down
64 changes: 35 additions & 29 deletions tests/feature_offset_polygons.earth
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,41 @@ the feature data.
<options>
<lighting>false</lighting>
<overlay_blending>false</overlay_blending>
<terrain min_lod="8"/>
<terrain min_lod="8"/>
</options>

<extensions>
<terrainshader>
<description>
A GLSL snippet that pushes the terrain back to prevent high-altitude z fighting.
</description>
<vertex>
<![CDATA[
#version 110
#pragma vp_entryPoint "dp_vert"
#pragma vp_location "vertex_view"
void dp_vert(inout vec4 v) {
const float R = 6371000.0; // Approximate radius of the earth
const float dmin = 100000.0; // Minimum vertex distance at which to apply effect
const float dmax = R; // Vertex distance at which to apply maximum effect
const float maxOffset = 250000.0; // Maximum offset (applied at dmax)

float d = length(v.xyz);
float t = clamp( (d-dmin)/(dmax-dmin), 0.0, 1.0 );
float offset = t*maxOffset;
vec3 n = normalize(v.xyz);
v.xyz = v.xyz + n*offset;
}
]]>
</vertex>
</terrainshader>
</extensions>

<extensions>
<terrainshader>
<description>
A GLSL snippet that pushes the terrain back to prevent high-altitude z fighting.
</description>
<code>
<![CDATA[
#version 110
#pragma vp_entryPoint "dp_vert" // name of entry point function
#pragma vp_location "vertex_view" // where to insert this shader in the pipeline

varying out float tt;

void dp_vert(inout vec4 v)
{
const float R = 6371000.0; // Approximate radius of the earth
const float dmin = 100000.0; // Minimum vertex distance at which to apply effect
const float dmax = R; // Vertex distance at which to apply maximum effect
const float maxOffset = 250000.0; // Maximum offset (applied at dmax)

float d = length(v.xyz);
float t = clamp( (d-dmin)/(dmax-dmin), 0.0, 1.0 );
float offset = t*maxOffset;
vec3 n = normalize(v.xyz);
v.xyz = v.xyz + n*offset;

tt = t;
}
]]>
</code>
</terrainshader>
</extensions>

<image name="world" driver="gdal">
<url>../data/world.tif</url>
Expand All @@ -52,7 +58,7 @@ the feature data.
<features name="states" driver="ogr">
<url>../data/world.shp</url>
<buffer distance="-0.05"/>
<resample max_length="5.0"/>
<resample max_length="5.0"/>
</features>

<styles>
Expand Down

0 comments on commit 75b7e9e

Please sign in to comment.