2010-03-22 22:00:44 +00:00
|
|
|
// -*- mode: C; -*-
|
|
|
|
// Licence: GPL v2
|
2010-09-25 21:28:56 +00:00
|
|
|
// Author: Frederic Bouvier.
|
2010-03-22 22:00:44 +00:00
|
|
|
// Adapted from the paper by F. Policarpo et al. : Real-time Relief Mapping on Arbitrary Polygonal Surfaces
|
|
|
|
|
2010-03-12 22:25:19 +00:00
|
|
|
#version 120
|
|
|
|
|
|
|
|
varying vec4 rawpos;
|
|
|
|
varying vec4 ecPosition;
|
|
|
|
varying vec3 VNormal;
|
|
|
|
varying vec3 VTangent;
|
|
|
|
varying vec3 VBinormal;
|
|
|
|
varying vec3 Normal;
|
|
|
|
varying vec4 constantColor;
|
|
|
|
|
2010-04-10 17:32:07 +00:00
|
|
|
uniform sampler3D NoiseTex;
|
2010-03-12 22:25:19 +00:00
|
|
|
uniform sampler2D BaseTex;
|
|
|
|
uniform sampler2D NormalTex;
|
|
|
|
uniform float depth_factor;
|
2010-03-22 22:00:44 +00:00
|
|
|
uniform float tile_size;
|
2010-04-10 15:08:29 +00:00
|
|
|
uniform float quality_level; // From /sim/rendering/quality-level
|
2010-04-10 17:32:07 +00:00
|
|
|
uniform float snowlevel; // From /sim/rendering/snow-level-m
|
2010-03-28 17:20:04 +00:00
|
|
|
uniform vec3 night_color;
|
2010-03-12 22:25:19 +00:00
|
|
|
|
2010-04-10 17:32:07 +00:00
|
|
|
const float scale = 1.0;
|
2010-04-10 14:35:02 +00:00
|
|
|
int linear_search_steps = 10;
|
2010-03-12 22:25:19 +00:00
|
|
|
|
|
|
|
float ray_intersect(sampler2D reliefMap, vec2 dp, vec2 ds)
|
|
|
|
{
|
|
|
|
float size = 1.0 / float(linear_search_steps);
|
|
|
|
float depth = 0.0;
|
|
|
|
float best_depth = 1.0;
|
|
|
|
|
|
|
|
for(int i = 0; i < linear_search_steps - 1; ++i)
|
|
|
|
{
|
|
|
|
depth += size;
|
2010-09-25 21:28:56 +00:00
|
|
|
float t = step(0.95, texture2D(reliefMap, dp + ds * depth).a);
|
2010-03-12 22:25:19 +00:00
|
|
|
if(best_depth > 0.996)
|
|
|
|
if(depth >= t)
|
|
|
|
best_depth = depth;
|
|
|
|
}
|
|
|
|
depth = best_depth;
|
|
|
|
|
|
|
|
const int binary_search_steps = 5;
|
|
|
|
|
|
|
|
for(int i = 0; i < binary_search_steps; ++i)
|
|
|
|
{
|
|
|
|
size *= 0.5;
|
2010-09-25 21:28:56 +00:00
|
|
|
float t = step(0.95, texture2D(reliefMap, dp + ds * depth).a);
|
2010-03-12 22:25:19 +00:00
|
|
|
if(depth >= t)
|
|
|
|
{
|
|
|
|
best_depth = depth;
|
|
|
|
depth -= 2.0 * size;
|
|
|
|
}
|
|
|
|
depth += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(best_depth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void main (void)
|
|
|
|
{
|
2010-04-10 14:35:02 +00:00
|
|
|
if ( quality_level >= 3.5 ) {
|
|
|
|
linear_search_steps = 20;
|
|
|
|
}
|
2010-03-22 22:00:44 +00:00
|
|
|
vec3 ecPos3 = ecPosition.xyz / ecPosition.w;
|
|
|
|
vec3 V = normalize(ecPos3);
|
|
|
|
vec3 s = vec3(dot(V, VTangent), dot(V, VBinormal), dot(VNormal, -V));
|
2010-03-12 22:25:19 +00:00
|
|
|
vec2 ds = s.xy * depth_factor / s.z;
|
2010-03-22 22:00:44 +00:00
|
|
|
vec2 dp = gl_TexCoord[0].st - ds;
|
2010-03-12 22:25:19 +00:00
|
|
|
float d = ray_intersect(NormalTex, dp, ds);
|
|
|
|
|
|
|
|
vec2 uv = dp + ds * d;
|
|
|
|
vec3 N = texture2D(NormalTex, uv).xyz * 2.0 - 1.0;
|
|
|
|
|
|
|
|
|
2010-03-22 22:00:44 +00:00
|
|
|
float emis = N.z;
|
|
|
|
N.z = sqrt(1.0 - min(1.0,dot(N.xy, N.xy)));
|
2010-04-10 17:32:07 +00:00
|
|
|
float Nz = N.z;
|
2010-03-12 22:25:19 +00:00
|
|
|
N = normalize(N.x * VTangent + N.y * VBinormal + N.z * VNormal);
|
|
|
|
|
|
|
|
vec3 l = gl_LightSource[0].position.xyz;
|
|
|
|
vec3 diffuse = gl_Color.rgb * max(0.0, dot(N, l));
|
2010-04-01 20:59:17 +00:00
|
|
|
float shadow_factor = 1.0;
|
2010-04-10 14:35:02 +00:00
|
|
|
|
2010-04-10 15:08:29 +00:00
|
|
|
// Shadow
|
2010-04-10 14:35:02 +00:00
|
|
|
if ( quality_level >= 3.0 ) {
|
|
|
|
dp += ds * d;
|
|
|
|
vec3 sl = normalize( vec3( dot( l, VTangent ), dot( l, VBinormal ), dot( -l, VNormal ) ) );
|
|
|
|
ds = sl.xy * depth_factor / sl.z;
|
|
|
|
dp -= ds * d;
|
|
|
|
float dl = ray_intersect(NormalTex, dp, ds);
|
|
|
|
if ( dl < d - 0.05 )
|
|
|
|
shadow_factor = dot( constantColor.xyz, vec3( 1.0, 1.0, 1.0 ) ) * 0.25;
|
|
|
|
}
|
2010-04-10 15:08:29 +00:00
|
|
|
// end shadow
|
2010-04-10 14:35:02 +00:00
|
|
|
|
2010-03-12 22:25:19 +00:00
|
|
|
vec4 ambient_light = constantColor + gl_LightSource[0].diffuse * vec4(diffuse, 1.0);
|
2010-03-22 22:00:44 +00:00
|
|
|
float reflectance = ambient_light.r * 0.3 + ambient_light.g * 0.59 + ambient_light.b * 0.11;
|
|
|
|
if ( shadow_factor < 1.0 )
|
|
|
|
ambient_light = constantColor + gl_LightSource[0].diffuse * shadow_factor * vec4(diffuse, 1.0);
|
2010-03-23 22:20:24 +00:00
|
|
|
float emission_factor = (1.0 - smoothstep(0.15, 0.25, reflectance)) * emis;
|
2010-03-28 16:54:26 +00:00
|
|
|
vec4 tc = texture2D(BaseTex, uv);
|
2010-08-23 06:53:18 +00:00
|
|
|
emission_factor *= 0.5*pow(tc.r+0.8*tc.g+0.2*tc.b, 2.0) -0.2;
|
2010-03-28 17:20:04 +00:00
|
|
|
ambient_light += (emission_factor * vec4(night_color, 0.0));
|
2010-03-12 22:25:19 +00:00
|
|
|
|
2010-03-23 22:20:24 +00:00
|
|
|
float fogFactor;
|
|
|
|
float fogCoord = ecPos3.z / (1.0 + smoothstep(0.3, 0.7, emission_factor));
|
|
|
|
const float LOG2 = 1.442695;
|
|
|
|
fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2);
|
|
|
|
fogFactor = clamp(fogFactor, 0.0, 1.0);
|
|
|
|
|
2010-04-10 17:32:07 +00:00
|
|
|
vec4 noisevec = texture3D(NoiseTex, (rawpos.xyz)*0.01*scale);
|
|
|
|
vec4 nvL = texture3D(NoiseTex, (rawpos.xyz)*0.00066*scale);
|
|
|
|
|
|
|
|
float n=0.06;
|
|
|
|
n += nvL[0]*0.4;
|
|
|
|
n += nvL[1]*0.6;
|
|
|
|
n += nvL[2]*2.0;
|
|
|
|
n += nvL[3]*4.0;
|
|
|
|
n += noisevec[0]*0.1;
|
|
|
|
n += noisevec[1]*0.4;
|
|
|
|
|
|
|
|
n += noisevec[2]*0.8;
|
|
|
|
n += noisevec[3]*2.1;
|
|
|
|
n = mix(0.6, n, fogFactor);
|
|
|
|
|
|
|
|
vec4 finalColor = texture2D(BaseTex, uv);
|
|
|
|
finalColor = mix(finalColor, clamp(n+nvL[2]*4.1+vec4(0.1, 0.1, nvL[2]*2.2, 1.0), 0.7, 1.0),
|
2010-04-10 21:07:52 +00:00
|
|
|
step(0.8,Nz)*(1.0-emis)*smoothstep(snowlevel+300.0, snowlevel+360.0, (rawpos.z)+nvL[1]*3000.0));
|
2010-04-10 17:32:07 +00:00
|
|
|
finalColor *= ambient_light;
|
2010-09-25 21:28:56 +00:00
|
|
|
|
2010-03-22 22:00:44 +00:00
|
|
|
if (gl_Fog.density == 1.0)
|
2010-03-12 22:25:19 +00:00
|
|
|
fogFactor=1.0;
|
|
|
|
|
2010-03-22 22:00:44 +00:00
|
|
|
vec4 p = vec4( ecPos3 + tile_size * V * (d-1.0) * depth_factor / s.z, 1.0 );
|
|
|
|
vec4 iproj = gl_ProjectionMatrix * p;
|
|
|
|
iproj /= iproj.w;
|
2010-03-12 22:25:19 +00:00
|
|
|
gl_FragColor = mix(gl_Fog.color ,finalColor, fogFactor);
|
2010-03-22 22:00:44 +00:00
|
|
|
gl_FragDepth = (iproj.z+1.0)/2.0;
|
2010-03-12 22:25:19 +00:00
|
|
|
}
|