Skip to content

Commit

Permalink
Merge branch 'master' of github.com:gwaldron/osgearth
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonbeverage committed Sep 11, 2023
2 parents ede26bf + 0e88ef4 commit 3bee971
Show file tree
Hide file tree
Showing 22 changed files with 601 additions and 106 deletions.
1 change: 1 addition & 0 deletions src/osgEarth/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ set(TARGET_GLSL
DrawInstancedAttribute.glsl
GPUClamping.glsl
GPUClamping.lib.glsl
HexTiling.glsl
Instancing.glsl
LineDrawable.glsl
MetadataNode.glsl
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#pragma vp_name osgEarth Hex Tiling Library

#ifdef VP_STAGE_FRAGMENT

// Adapted and ported to GLSL from:
// https://github.com/mmikk/hextile-demo

const float ht_g_fallOffContrast = 0.6;
const float ht_g_exp = 7;
float ht_g_fallOffContrast = 0.6;
float ht_g_exp = 7;

#ifndef mul
#define mul(X, Y) ((X)*(Y))
Expand All @@ -24,7 +28,7 @@ void ht_TriangleGrid(
st *= HEX_SCALE; // 2 * 1.sqrt(3);

// Skew input space into simplex triangle grid
const mat2 gridToSkewedGrid = mat2(1.0, -0.57735027, 0.0, 1.15470054);
mat2 gridToSkewedGrid = mat2(1.0, -0.57735027, 0.0, 1.15470054);
vec2 skewedCoord = mul(gridToSkewedGrid, st);

ivec2 baseId = ivec2(floor(skewedCoord));
Expand Down Expand Up @@ -53,7 +57,7 @@ void ht_TriangleGrid_f(
st *= HEX_SCALE; // 2 * 1.sqrt(3);

// Skew input space into simplex triangle grid
const mat2 gridToSkewedGrid = mat2(1.0, -0.57735027, 0.0, 1.15470054);
mat2 gridToSkewedGrid = mat2(1.0, -0.57735027, 0.0, 1.15470054);
vec2 skewedCoord = mul(gridToSkewedGrid, st);

vec2 baseId = floor(skewedCoord);
Expand All @@ -80,7 +84,7 @@ vec2 ht_hash(vec2 p)

vec2 ht_MakeCenST(ivec2 Vertex)
{
const mat2 invSkewMat = mat2(1.0, 0.5, 0.0, 1.0 / 1.15470054);
mat2 invSkewMat = mat2(1.0, 0.5, 0.0, 1.0 / 1.15470054);
return mul(invSkewMat, Vertex) / HEX_SCALE;
}

Expand Down Expand Up @@ -137,16 +141,16 @@ vec3 ht_ProduceHexWeights(vec3 W, ivec2 vertex1, ivec2 vertex2, ivec2 vertex3)
// Output: convert vM to a derivative.
vec2 ht_TspaceNormalToDerivative(in vec3 vM)
{
const float scale = 1.0 / 128.0;
float scale = 1.0 / 128.0;

// Ensure vM delivers a positive third component using abs() and
// constrain vM.z so the range of the derivative is [-128; 128].
const vec3 vMa = abs(vM);
const float z_ma = max(vMa.z, scale*max(vMa.x, vMa.y));
vec3 vMa = abs(vM);
float z_ma = max(vMa.z, scale*max(vMa.x, vMa.y));

// Set to match positive vertical texture coordinate axis.
const bool gFlipVertDeriv = true;
const float s = gFlipVertDeriv ? -1.0 : 1.0;
bool gFlipVertDeriv = true;
float s = gFlipVertDeriv ? -1.0 : 1.0;
return -vec2(vM.x, s*vM.y) / z_ma;
}

Expand All @@ -164,10 +168,11 @@ vec2 ht_sampleDeriv(sampler2D nmap, vec2 st, vec2 dSTdx, vec2 dSTdy)
// Output:\ weights shows the weight of each hex tile
void bumphex2derivNMap(
out vec2 deriv, out vec3 weights,
sampler2D nmap, vec2 st,
sampler2D nmap, in vec2 st,
float rotStrength, float r)
{
vec2 dSTdx = dFdx(st), dSTdy = dFdy(st);
vec2 dSTdx = dFdx(st);
vec2 dSTdy = dFdy(st);

// Get triangle info
float w1, w2, w3;
Expand Down Expand Up @@ -210,16 +215,17 @@ void bumphex2derivNMap(
weights = ht_ProduceHexWeights(W.xyz, vertex1, vertex2, vertex3);
}

float ht_get_lod(in ivec2 dim, in vec2 x, in vec2 y)
{
vec2 ddx = x * float(dim.x), ddy = y * float(dim.y);
return 0.5 * log2(max(dot(ddx, ddx), dot(ddy, ddy)));
}

// Input:\ tex is a texture with color
// Input:\ r increase contrast when r>0.5
// Output:\ color is the blended result
// Output:\ weights shows the weight of each hex tile
void ht_hex2colTex(
out vec4 color,
sampler2D tex,
vec2 st,
float rotStrength)
// tex = sampler to sample
// st = texture coordinates
// rotStrength = amount of rotation offset
// transStrength = amount of translation offset
vec4 ht_hex2col(in sampler2D tex, in vec2 st, in float rotStrength, in float transStength)
{
vec2 dSTdx = dFdx(st), dSTdy = dFdy(st);

Expand All @@ -236,29 +242,33 @@ void ht_hex2colTex(
vec2 cen2 = ht_MakeCenST(vertex2);
vec2 cen3 = ht_MakeCenST(vertex3);

vec2 st1 = mul(st - cen1, rot1) + cen1 + ht_hash(vertex1);
vec2 st2 = mul(st - cen2, rot2) + cen2 + ht_hash(vertex2);
vec2 st3 = mul(st - cen3, rot3) + cen3 + ht_hash(vertex3);
vec2 st1 = mul(st - cen1, rot1) + cen1 + ht_hash(vertex1) * transStength;
vec2 st2 = mul(st - cen2, rot2) + cen2 + ht_hash(vertex2) * transStength;
vec2 st3 = mul(st - cen3, rot3) + cen3 + ht_hash(vertex3) * transStength;

// Fetch input
vec4 c1 = textureGrad(tex, st1, dSTdx*rot1, dSTdy*rot1);
vec4 c2 = textureGrad(tex, st2, dSTdx*rot2, dSTdy*rot2);
vec4 c3 = textureGrad(tex, st3, dSTdx*rot3, dSTdy*rot3);
ivec2 dim = textureSize(tex, 0);
vec4 c1 = textureLod(tex, st1, ht_get_lod(dim, dSTdx*rot1, dSTdy*rot1));
vec4 c2 = textureLod(tex, st2, ht_get_lod(dim, dSTdx*rot2, dSTdy*rot2));
vec4 c3 = textureLod(tex, st3, ht_get_lod(dim, dSTdx*rot3, dSTdy*rot3));

//vec4 c1 = textureGrad(tex, st1, dSTdx*rot1, dSTdy*rot1);
//vec4 c2 = textureGrad(tex, st2, dSTdx*rot2, dSTdy*rot2);
//vec4 c3 = textureGrad(tex, st3, dSTdx*rot3, dSTdy*rot3);

// use luminance as weight
const vec3 Lw = vec3(0.299, 0.587, 0.114);
vec3 Lw = vec3(0.299, 0.587, 0.114);
vec3 Dw = vec3(dot(c1.xyz, Lw), dot(c2.xyz, Lw), dot(c3.xyz, Lw));

Dw = mix(vec3(1.0), Dw, ht_g_fallOffContrast); // 0.6
vec3 W = Dw * pow(vec3(w1, w2, w3), vec3(ht_g_exp)); // 7
W /= (W.x + W.y + W.z);
//if (r != 0.5) W = Gain3(W, r);

color = W.x * c1 + W.y * c2 + W.z * c3;
vec4 color = W.x * c1 + W.y * c2 + W.z * c3;
//weights = ProduceHexWeights(W.xyz, vertex1, vertex2, vertex3);
}

#define HT_HASH(X) fract(sin(mat2(127.1, 311.7, 269.5, 183.3) * X)*43758.5453)
return color;
}

// Hextiling function optimized for no rotations and to
// sample and interpolate both color and material vectors
Expand All @@ -267,7 +277,8 @@ void ht_hex2colTex_optimized(
in sampler2D material_tex,
in vec2 st,
out vec4 color,
out vec4 material)
out vec4 material,
inout vec3 weighting)
{
// Get triangle info
vec3 weights;
Expand Down Expand Up @@ -322,13 +333,20 @@ void ht_hex2colTex_optimized(
vec4 m3 = textureGrad(material_tex, st3, ddx, ddy);
#endif

// Use color's luminance as weighting factor
const vec3 Lw = vec3(0.299, 0.587, 0.114);
vec3 Dw = vec3(dot(c1.xyz, Lw), dot(c2.xyz, Lw), dot(c3.xyz, Lw));
Dw = mix(vec3(1.0), Dw, ht_g_fallOffContrast);
vec3 W = Dw * pow(weights, vec3(ht_g_exp));
W /= (W.x + W.y + W.z);

vec3 W = weighting;
if (W == vec3(0))
{
// Use color's luminance as weighting factor
vec3 Lw = vec3(0.299, 0.587, 0.114);
vec3 Dw = vec3(dot(c1.xyz, Lw), dot(c2.xyz, Lw), dot(c3.xyz, Lw));
Dw = mix(vec3(1.0), Dw, ht_g_fallOffContrast);
W = Dw * pow(weights, vec3(ht_g_exp));
W /= (W.x + W.y + W.z);
}

weighting = W;
color = W.x * c1 + W.y * c2 + W.z * c3;
material = W.x * m1 + W.y * m2 + W.z * m3;
}

#endif // VP_STAGE_FRAGMENT
5 changes: 4 additions & 1 deletion src/osgEarth/LayerShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
#include <osgEarth/LayerShader>
#include <osgEarth/ShaderLoader>
#include <osgEarth/ShaderUtils>
#include <osgEarth/VirtualProgram>
#include <osgEarth/Color>
#include <osgEarth/Layer>
Expand Down Expand Up @@ -142,7 +143,9 @@ LayerShader::install(Layer* layer, TerrainResources* res)
vp->setName(layer->getName());
ShaderPackage package;
package.add("", _options.code());
package.loadAll(vp, layer->getReadOptions());

ShaderLoader::load(vp, "", package, layer->getReadOptions());
//package.loadAll(vp, layer->getReadOptions());

for (int i = 0; i < _options.samplers().size(); ++i)
{
Expand Down
18 changes: 13 additions & 5 deletions src/osgEarth/MapNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,16 +334,14 @@ MapNode::open()
HTTPClient::setProxySettings( options().proxySettings().get() );
}

// load and attach the terrain engine.
_terrainEngine = TerrainEngineNode::create(options().terrain().get());

// Install a callback that lets this MapNode know about any changes
// to the map, and invoke it manually now so they start out in sync.
_mapCallback = new MapNodeMapCallbackProxy(this);
_map->addMapCallback( _mapCallback.get() );
_mapCallback->invokeOnLayerAdded(_map.get());

// Give the terrain engine a map to render.
// load and attach the terrain engine.
_terrainEngine = TerrainEngineNode::create(options().terrain().get());
if ( _terrainEngine )
{
_terrainEngine->setMap(_map.get(), options().terrain().get());
Expand All @@ -353,6 +351,12 @@ MapNode::open()
OE_WARN << "FAILED to create a terrain engine for this map" << std::endl;
}

// now that the terrain engine exists, we can invoke the rendering callback
LayerVector layers;
_map->getLayers(layers, [&](const Layer* layer) { return layer->isOpen(); });
for (auto& layer : layers)
layer->invoke_prepareForRendering(getTerrainEngine());

// initialize terrain-level lighting:
if ( options().terrain()->enableLighting().isSet() )
{
Expand Down Expand Up @@ -446,9 +450,12 @@ MapNode::open()
VirtualProgram* vp = VirtualProgram::getOrCreate(stateset);
vp->setName(className());
Shaders shaders;

shaders.load(vp, shaders.PBR);
stateset->setDefine("OE_USE_PBR");

shaders.load(vp, shaders.HexTilingLib);
stateset->setDefine("OE_HAVE_HEX_TILING");

dirtyBound();

Expand Down Expand Up @@ -768,7 +775,8 @@ MapNode::onLayerAdded(Layer* layer, unsigned index)
if (!layer || !layer->isOpen())
return;

layer->invoke_prepareForRendering(getTerrainEngine());
if (getTerrainEngine())
layer->invoke_prepareForRendering(getTerrainEngine());

// Create the layer's node, if it has one:
osg::Node* node = layer->getNode();
Expand Down
45 changes: 31 additions & 14 deletions src/osgEarth/NativeProgramAdapter
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace osgEarth { namespace Util
{
public:
/** Create a program adapter under the current state that wraps the provided glProgram handle. */
NativeProgramAdapter(const osg::State* state, GLint handle, const char* prefix, const std::string& name)
NativeProgramAdapter(const osg::State* state, GLint handle, const std::vector<const char*> prefixes, const std::string& name)
{
OE_LOCAL << LC << "Create adapter for glProgram " << name << " (handle=" << handle << ")" << std::endl;

Expand All @@ -61,30 +61,47 @@ namespace osgEarth { namespace Util
_ext->glGetProgramiv( _handle, GL_ACTIVE_UNIFORMS, &numUniforms );
_ext->glGetProgramiv( _handle, GL_ACTIVE_UNIFORM_MAX_LENGTH, &maxLen );

if ( (numUniforms > 0) && (maxLen > 1) )
std::vector<std::string> uniformNames;

if ((numUniforms > 0) && (maxLen > 1))
{
GLint size = 0;
GLenum type = 0;
GLchar* name = new GLchar[maxLen];

for( GLint i = 0; i < numUniforms; ++i )
for (GLint i = 0; i < numUniforms; ++i)
{
_ext->glGetActiveUniform(_handle, i, maxLen, 0, &size, &type, name);
uniformNames.push_back(std::string(name));
}
}

for(auto& uniformName : uniformNames)
{
bool use_uniform = true;

if (!prefixes.empty())
{
_ext->glGetActiveUniform( _handle, i, maxLen, 0, &size, &type, name );
if ( !prefix || (::strlen(name) >= ::strlen(prefix) && ::strncmp(name, prefix, ::strlen(prefix)) == 0) )
use_uniform = false;
for (auto& prefix : prefixes)
{
GLint loc = _ext->glGetUniformLocation( _handle, name );
if ( loc != -1 )
if (Strings::startsWith(uniformName, prefix))
{
_uniformLocations[osg::Uniform::getNameID(reinterpret_cast<const char*>(name))] = loc;

OE_LOCAL << LC << " Uniform = \"" << name << "\", location = " << loc << std::endl;
use_uniform = true;
break;
}
}
}
}
else
{
OE_LOCAL << LC << " No uniforms found." << std::endl;

if (use_uniform)
{
GLint loc = _ext->glGetUniformLocation(_handle, uniformName.c_str());
if ( loc != -1 )
{
_uniformLocations[osg::Uniform::getNameID(uniformName)] = loc;
OE_LOCAL << LC << " Uniform = \"" << uniformName << "\", location = " << loc << std::endl;
}
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/osgEarth/Shaders
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ namespace osgEarth { namespace Util
std::string Draping;
std::string DrawInstancedAttribute;
std::string GPUClamping, GPUClampingLib;
std::string HexTilingLib;
std::string Instancing;
std::string LineDrawable;
std::string WireLines;
Expand Down
3 changes: 3 additions & 0 deletions src/osgEarth/Shaders.cpp.in
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ namespace osgEarth { namespace Util
GPUClampingLib = "GPUClamping.lib.glsl";
_sources[GPUClampingLib] = @GPUClamping.lib.glsl@;

HexTilingLib = "HexTiling.glsl";
_sources[HexTilingLib] = @HexTiling.glsl@;

// DrawInstanced
Instancing = "Instancing.glsl";
_sources[Instancing] = @Instancing.glsl@;
Expand Down
1 change: 1 addition & 0 deletions src/osgEarth/earcut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <algorithm>
#include <cassert>
#include <cmath>
#include <limits>
#include <memory>
#include <vector>

Expand Down
2 changes: 1 addition & 1 deletion src/osgEarthDrivers/engine_rex/RexEngine.SDK.GL4.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ vec4 oe_terrain_getNormalAndCurvature(in uint64_t handle, in vec2 uv)
return vec4(normalize(n.xyz), curv);
}

#ifndef VP_FRAGMENT_STAGE
#ifndef VP_STAGE_FRAGMENT
/**
* Scales repeating texture coordinate such that they are [0..1]
* at a specific reference tile LOD.
Expand Down
2 changes: 1 addition & 1 deletion src/osgEarthDrivers/engine_rex/RexEngine.SDK.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ vec4 oe_terrain_getNormalAndCurvature()
return oe_terrain_getNormalAndCurvature(uv_scaledBiased);
}

#ifndef VP_FRAGMENT_STAGE
#ifndef VP_STAGE_FRAGMENT
/**
* Scales repeating texture coordinate such that they are [0..1]
* at a specific reference tile LOD.
Expand Down
2 changes: 1 addition & 1 deletion src/osgEarthProcedural/Biome
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ namespace osgEarth
private:
int _index;
Biome* _parentBiome;
bool _implicit; // whether this biome really exists or was derived by filters
bool _implicit; // whether this biome really exists or was derived by traits
friend class BiomeCatalog;
};

Expand Down
Loading

0 comments on commit 3bee971

Please sign in to comment.