diff --git a/Effects/water.eff b/Effects/water.eff index 88bacb034..31ee0369a 100644 --- a/Effects/water.eff +++ b/Effects/water.eff @@ -298,6 +298,27 @@ <use>texture[3]/internal-format</use> </internal-format> </texture-unit> + <texture-unit> + <unit>4</unit> + <image> + <use>texture[7]/image</use> + </image> + <type> + <use>texture[7]/type</use> + </type> + <filter> + <use>texture[7]/filter</use> + </filter> + <wrap-s> + <use>texture[7]/wrap-s</use> + </wrap-s> + <wrap-t> + <use>texture[7]/wrap-t</use> + </wrap-t> + <internal-format> + <use>texture[7]/internal-format</use> + </internal-format> + </texture-unit> <!--<texture-unit> <unit>4</unit> <image> @@ -402,6 +423,11 @@ <type>sampler-2d</type> <value type="int">3</value> </uniform> + <uniform> + <name>topo_map</name> + <type>sampler-2d</type> + <value type="int">4</value> + </uniform> <uniform> <name>sea_foam</name> <type>sampler-2d</type> diff --git a/Shaders/water_lightfield.frag b/Shaders/water_lightfield.frag index 9eb8eb5c0..ff8dd59c6 100644 --- a/Shaders/water_lightfield.frag +++ b/Shaders/water_lightfield.frag @@ -13,6 +13,7 @@ uniform sampler2D water_dudvmap; uniform sampler2D sea_foam; uniform sampler2D perlin_normalmap; uniform sampler2D ice_texture; +uniform sampler2D topo_map; uniform sampler3D Noise; @@ -26,6 +27,8 @@ varying vec3 viewerdir; varying vec3 lightdir; varying vec3 relPos; varying vec3 rawPos; +varying vec2 TopoUV; + varying float earthShade; varying float yprime_alt; @@ -284,7 +287,7 @@ void main(void) { - vec3 shadedFogColor = vec3(0.65, 0.67, 0.78); + vec3 shadedFogColor = vec3(0.65, 0.67, 0.78); float effective_scattering = min(scattering, cloud_self_shading); float dist = length(relPos); @@ -298,6 +301,11 @@ void main(void) float noise_2000m = Noise3D(rawPos.xyz,2000.0); float noise_2500m = Noise3D(rawPos.xyz, 2500.0); + // get depth map + vec4 topoTexel = texture2D(topo_map, TopoUV); + float floorMixFactor = smoothstep(0.3, 0.985, topoTexel.a); + vec3 floorColour = topoTexel.rgb; + mat4 RotationMatrix; // compute direction to viewer @@ -443,7 +451,17 @@ void main(void) refl.a = 1.0; refl.g = refl.g * (0.9 + 0.2* noise_2500m); - + + if (ocean_flag ==1) // use depth information + { + refl.rgb = mix(refl.rgb, 0.65* floorColour, floorMixFactor); + refl.rgb = refl.rgb * (0.5 + 0.5 * smoothstep(0.0,0.3,topoTexel.a)); + } + else + { + refl.rgb = 1.3 * refl.rgb; + } + float intensity; // de-saturate for reduced light refl.rgb = mix(refl.rgb, vec3 (0.248, 0.248, 0.248), 1.0 - smoothstep(0.1, 0.8, ground_scattering)); diff --git a/Shaders/water_lightfield.vert b/Shaders/water_lightfield.vert index 1c014d943..65084d392 100644 --- a/Shaders/water_lightfield.vert +++ b/Shaders/water_lightfield.vert @@ -12,6 +12,7 @@ varying vec4 waterTex2; varying vec4 waterTex4; varying vec3 relPos; varying vec3 rawPos; +varying vec2 TopoUV; varying vec3 viewerdir; varying vec3 lightdir; @@ -36,6 +37,14 @@ uniform int ocean_flag; uniform mat4 osg_ViewMatrixInverse; +// constants for the cartesian to geodetic conversion. + +const float a = 6378137.0; //float a = equRad; +const float squash = 0.9966471893352525192801545; +const float latAdjust = 0.9999074159800018; //geotiff source for the depth map +const float lonAdjust = 0.9999537058469516; //actual extents: +-180.008333333333326/+-90.008333333333340 + + vec3 specular_light; // This is the value used in the skydome scattering shader - use the same here for consistency? @@ -234,6 +243,46 @@ else // the faster, full-day version without lightfields } +// Geodesy lookup for depth map + float e2 = abs(1.0 - squash * squash); + float ra2 = 1.0/(a * a); + float e4 = e2 * e2; + float XXpYY = rawPos.x * rawPos.x + rawPos.y * rawPos.y; + float Z = rawPos.z; + float sqrtXXpYY = sqrt(XXpYY); + float p = XXpYY * ra2; + float q = Z*Z*(1.0-e2)*ra2; + float r = 1.0/6.0*(p + q - e4); + float s = e4 * p * q/(4.0*r*r*r); + if ( s >= 2.0 && s <= 0.0) + s = 0.0; + float t = pow(1.0+s+sqrt(s*2.0+s*s), 1.0/3.0); + float u = r + r*t + r/t; + float v = sqrt(u*u + e4*q); + float w = (e2*u+ e2*v-e2*q)/(2.0*v); + float k = sqrt(u+v+w*w)-w; + float D = k*sqrtXXpYY/(k+e2); + + vec2 NormPosXY = normalize(rawPos.xy); + vec2 NormPosXZ = normalize(vec2(D, rawPos.z)); + float signS = sign(rawPos.y); + if (-0.00015 <= rawPos.y && rawPos.y<=.00015) + signS = 1.0; + float signT = sign(rawPos.z); + if (-0.0002 <= rawPos.z && rawPos.z<=.0002) + signT = 1.0; + float cosLon = dot(NormPosXY, vec2(1.0,0.0)); + float cosLat = dot(abs(NormPosXZ), vec2(1.0,0.0)); + TopoUV.s = signS * lonAdjust * degrees(acos(cosLon))/180.; + TopoUV.t = signT * latAdjust * degrees(acos(cosLat))/90.; + TopoUV.s = TopoUV.s * 0.5 + 0.5; + TopoUV.t = TopoUV.t * 0.5 + 0.5; + +// + + + + gl_FrontColor.rgb = specular_light; gl_BackColor.rgb = gl_FrontColor.rgb;