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
|
2011-12-03 08:07:57 +00:00
|
|
|
// Adapted from the paper and sources by M. Drobot in GPU Pro : Quadtree Displacement Mapping with Height Blending
|
2010-03-22 22:00:44 +00:00
|
|
|
|
2010-03-12 22:25:19 +00:00
|
|
|
#version 120
|
|
|
|
|
2011-12-02 15:21:27 +00:00
|
|
|
#extension GL_ATI_shader_texture_lod : enable
|
2011-12-08 07:58:36 +00:00
|
|
|
#extension GL_ARB_shader_texture_lod : enable
|
2011-12-02 15:21:27 +00:00
|
|
|
|
2010-11-02 21:31:39 +00:00
|
|
|
#define TEXTURE_MIP_LEVELS 10
|
|
|
|
#define TEXTURE_PIX_COUNT 1024 //pow(2,TEXTURE_MIP_LEVELS)
|
|
|
|
#define BINARY_SEARCH_COUNT 10
|
|
|
|
#define BILINEAR_SMOOTH_FACTOR 2.0
|
|
|
|
|
2010-03-12 22:25:19 +00:00
|
|
|
varying vec4 rawpos;
|
|
|
|
varying vec4 ecPosition;
|
|
|
|
varying vec3 VNormal;
|
|
|
|
varying vec3 VTangent;
|
2012-08-06 11:15:43 +00:00
|
|
|
//varying vec3 VBinormal;
|
2010-03-12 22:25:19 +00:00
|
|
|
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;
|
2010-11-02 21:31:39 +00:00
|
|
|
uniform sampler2D QDMTex;
|
2010-03-12 22:25:19 +00:00
|
|
|
uniform float depth_factor;
|
2010-03-22 22:00:44 +00:00
|
|
|
uniform float tile_size;
|
2011-12-17 16:21:35 +00:00
|
|
|
uniform float quality_level;
|
|
|
|
uniform float snowlevel;
|
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-11-02 20:31:31 +00:00
|
|
|
int linear_search_steps = 10;
|
2010-11-02 21:31:39 +00:00
|
|
|
int GlobalIterationCount = 0;
|
|
|
|
int gIterationCap = 64;
|
|
|
|
|
2011-11-28 15:02:20 +00:00
|
|
|
////fog "include" /////
|
|
|
|
uniform int fogType;
|
|
|
|
|
|
|
|
vec3 fog_Func(vec3 color, int type);
|
|
|
|
//////////////////////
|
|
|
|
|
2010-11-02 21:31:39 +00:00
|
|
|
void QDM(inout vec3 p, inout vec3 v)
|
|
|
|
{
|
2011-12-01 21:04:27 +00:00
|
|
|
const int MAX_LEVEL = TEXTURE_MIP_LEVELS;
|
|
|
|
const float NODE_COUNT = TEXTURE_PIX_COUNT;
|
|
|
|
const float TEXEL_SPAN_HALF = 1.0 / NODE_COUNT / 2.0;
|
|
|
|
|
|
|
|
float fDeltaNC = TEXEL_SPAN_HALF * depth_factor;
|
|
|
|
|
|
|
|
vec3 p2 = p;
|
|
|
|
float level = MAX_LEVEL;
|
|
|
|
vec2 dirSign = (sign(v.xy) + 1.0) * 0.5;
|
|
|
|
GlobalIterationCount = 0;
|
|
|
|
float d = 0.0;
|
|
|
|
|
|
|
|
while (level >= 0.0 && GlobalIterationCount < gIterationCap)
|
|
|
|
{
|
|
|
|
vec4 uv = vec4(p2.xyz, level);
|
|
|
|
d = texture2DLod(QDMTex, uv.xy, uv.w).w;
|
|
|
|
|
|
|
|
if (d > p2.z)
|
|
|
|
{
|
|
|
|
//predictive point of ray traversal
|
|
|
|
vec3 tmpP2 = p + v * d;
|
|
|
|
|
|
|
|
//current node count
|
|
|
|
float nodeCount = pow(2.0, (MAX_LEVEL - level));
|
|
|
|
//current and predictive node ID
|
|
|
|
vec4 nodeID = floor(vec4(p2.xy, tmpP2.xy)*nodeCount);
|
|
|
|
|
|
|
|
//check if we are crossing the current cell
|
|
|
|
if (nodeID.x != nodeID.z || nodeID.y != nodeID.w)
|
|
|
|
{
|
|
|
|
//calculate distance to nearest bound
|
|
|
|
vec2 a = p2.xy - p.xy;
|
|
|
|
vec2 p3 = (nodeID.xy + dirSign) / nodeCount;
|
|
|
|
vec2 b = p3.xy - p.xy;
|
|
|
|
|
|
|
|
vec2 dNC = (b.xy * p2.z) / a.xy;
|
|
|
|
//take the nearest cell
|
|
|
|
d = min(d,min(dNC.x, dNC.y))+fDeltaNC;
|
|
|
|
|
|
|
|
level++;
|
|
|
|
|
|
|
|
//use additional convergence speed-up
|
|
|
|
#ifdef USE_QDM_ASCEND_INTERVAL
|
|
|
|
if(frac(level*0.5) > EPSILON)
|
2013-06-08 11:05:37 +00:00
|
|
|
level++;
|
|
|
|
#elif USE_QDM_ASCEND_CONST
|
|
|
|
level++;
|
2011-12-01 21:04:27 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
p2 = p + v * d;
|
|
|
|
}
|
|
|
|
level--;
|
|
|
|
GlobalIterationCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Manual Bilinear filtering
|
|
|
|
//
|
|
|
|
float rayLength = length(p2.xy - p.xy) + fDeltaNC;
|
|
|
|
|
|
|
|
float dA = p2.z * (rayLength - BILINEAR_SMOOTH_FACTOR * TEXEL_SPAN_HALF) / rayLength;
|
|
|
|
float dB = p2.z * (rayLength + BILINEAR_SMOOTH_FACTOR * TEXEL_SPAN_HALF) / rayLength;
|
|
|
|
|
|
|
|
vec4 p2a = vec4(p + v * dA, 0.0);
|
|
|
|
vec4 p2b = vec4(p + v * dB, 0.0);
|
|
|
|
dA = texture2DLod(NormalTex, p2a.xy, p2a.w).w;
|
|
|
|
dB = texture2DLod(NormalTex, p2b.xy, p2b.w).w;
|
|
|
|
|
|
|
|
dA = abs(p2a.z - dA);
|
|
|
|
dB = abs(p2b.z - dB);
|
|
|
|
|
|
|
|
p2 = mix(p2a.xyz, p2b.xyz, dA / (dA + dB));
|
|
|
|
|
|
|
|
p = p2;
|
2010-11-02 21:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float ray_intersect_QDM(vec2 dp, vec2 ds)
|
|
|
|
{
|
2011-12-01 21:04:27 +00:00
|
|
|
vec3 p = vec3( dp, 0.0 );
|
|
|
|
vec3 v = vec3( ds, 1.0 );
|
|
|
|
QDM( p, v );
|
|
|
|
return p.z;
|
2010-11-02 21:31:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float ray_intersect_relief(vec2 dp, vec2 ds)
|
2010-03-12 22:25:19 +00:00
|
|
|
{
|
2011-12-01 21:04:27 +00:00
|
|
|
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;
|
|
|
|
float t = step(0.95, texture2D(NormalTex, dp + ds * depth).a);
|
|
|
|
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;
|
|
|
|
float t = step(0.95, texture2D(NormalTex, dp + ds * depth).a);
|
|
|
|
if(depth >= t)
|
|
|
|
{
|
|
|
|
best_depth = depth;
|
|
|
|
depth -= 2.0 * size;
|
|
|
|
}
|
|
|
|
depth += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(best_depth);
|
2010-03-12 22:25:19 +00:00
|
|
|
}
|
|
|
|
|
2010-11-02 21:31:39 +00:00
|
|
|
float ray_intersect(vec2 dp, vec2 ds)
|
|
|
|
{
|
2011-12-01 21:04:27 +00:00
|
|
|
if ( quality_level >= 4.0 )
|
|
|
|
return ray_intersect_QDM( dp, ds );
|
|
|
|
else
|
|
|
|
return ray_intersect_relief( dp, ds );
|
2010-11-02 21:31:39 +00:00
|
|
|
}
|
|
|
|
|
2010-03-12 22:25:19 +00:00
|
|
|
void main (void)
|
|
|
|
{
|
2011-12-17 16:21:35 +00:00
|
|
|
if ( quality_level >= 3.0 ) {
|
2011-12-01 21:04:27 +00:00
|
|
|
linear_search_steps = 20;
|
|
|
|
}
|
2012-02-05 23:35:52 +00:00
|
|
|
vec3 normal = normalize(VNormal);
|
|
|
|
vec3 tangent = normalize(VTangent);
|
2012-08-06 11:15:43 +00:00
|
|
|
//vec3 binormal = normalize(VBinormal);
|
|
|
|
vec3 binormal = normalize(cross(normal, tangent));
|
2011-12-01 21:04:27 +00:00
|
|
|
vec3 ecPos3 = ecPosition.xyz / ecPosition.w;
|
|
|
|
vec3 V = normalize(ecPos3);
|
2012-02-05 23:35:52 +00:00
|
|
|
vec3 s = vec3(dot(V, tangent), dot(V, binormal), dot(normal, -V));
|
2011-12-01 21:04:27 +00:00
|
|
|
vec2 ds = s.xy * depth_factor / s.z;
|
|
|
|
vec2 dp = gl_TexCoord[0].st - ds;
|
|
|
|
float d = ray_intersect(dp, ds);
|
|
|
|
|
|
|
|
vec2 uv = dp + ds * d;
|
|
|
|
vec3 N = texture2D(NormalTex, uv).xyz * 2.0 - 1.0;
|
|
|
|
|
|
|
|
|
|
|
|
float emis = N.z;
|
|
|
|
N.z = sqrt(1.0 - min(1.0,dot(N.xy, N.xy)));
|
|
|
|
float Nz = N.z;
|
2012-02-05 23:35:52 +00:00
|
|
|
N = normalize(N.x * tangent + N.y * binormal + N.z * normal);
|
2011-12-01 21:04:27 +00:00
|
|
|
|
|
|
|
vec3 l = gl_LightSource[0].position.xyz;
|
|
|
|
vec3 diffuse = gl_Color.rgb * max(0.0, dot(N, l));
|
|
|
|
float shadow_factor = 1.0;
|
|
|
|
|
|
|
|
// Shadow
|
2011-12-17 16:21:35 +00:00
|
|
|
if ( quality_level >= 2.0 ) {
|
2011-12-01 21:04:27 +00:00
|
|
|
dp += ds * d;
|
2012-02-05 23:35:52 +00:00
|
|
|
vec3 sl = normalize( vec3( dot( l, tangent ), dot( l, binormal ), dot( -l, normal ) ) );
|
2011-12-01 21:04:27 +00:00
|
|
|
ds = sl.xy * depth_factor / sl.z;
|
|
|
|
dp -= ds * d;
|
|
|
|
float dl = ray_intersect(dp, ds);
|
|
|
|
if ( dl < d - 0.05 )
|
|
|
|
shadow_factor = dot( constantColor.xyz, vec3( 1.0, 1.0, 1.0 ) ) * 0.25;
|
|
|
|
}
|
|
|
|
// end shadow
|
|
|
|
|
|
|
|
vec4 ambient_light = constantColor + gl_LightSource[0].diffuse * vec4(diffuse, 1.0);
|
|
|
|
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);
|
|
|
|
float emission_factor = (1.0 - smoothstep(0.15, 0.25, reflectance)) * emis;
|
|
|
|
vec4 tc = texture2D(BaseTex, uv);
|
|
|
|
emission_factor *= 0.5*pow(tc.r+0.8*tc.g+0.2*tc.b, 2.0) -0.2;
|
|
|
|
ambient_light += (emission_factor * vec4(night_color, 0.0));
|
|
|
|
|
|
|
|
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, length(ecPosition.xyz) );
|
|
|
|
|
|
|
|
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),
|
|
|
|
step(0.8,Nz)*(1.0-emis)*smoothstep(snowlevel+300.0, snowlevel+360.0, (rawpos.z)+nvL[1]*3000.0));
|
|
|
|
finalColor *= ambient_light;
|
|
|
|
|
|
|
|
vec4 p = vec4( ecPos3 + tile_size * V * (d-1.0) * depth_factor / s.z, 1.0 );
|
|
|
|
|
|
|
|
finalColor.rgb = fog_Func(finalColor.rgb, fogType);
|
|
|
|
gl_FragColor = finalColor;
|
|
|
|
|
2012-02-11 09:47:39 +00:00
|
|
|
if (dot(normal,-V) > 0.1) {
|
2012-02-05 23:35:52 +00:00
|
|
|
vec4 iproj = gl_ProjectionMatrix * p;
|
|
|
|
iproj /= iproj.w;
|
|
|
|
gl_FragDepth = (iproj.z+1.0)/2.0;
|
|
|
|
} else {
|
|
|
|
gl_FragDepth = gl_FragCoord.z;
|
|
|
|
}
|
2010-03-12 22:25:19 +00:00
|
|
|
}
|