From 8e645e61f53b03cb2d1dd58d5dad21c00ac9346b Mon Sep 17 00:00:00 2001 From: "gral@who.net" Date: Tue, 24 Aug 2010 14:53:42 +0200 Subject: [PATCH] Added relief code to new effects and shaders --- Effects/forest.eff | 48 +++++++++++++++++++++- Effects/glacier.eff | 48 +++++++++++++++++++++- Effects/herbtundra.eff | 48 +++++++++++++++++++++- Shaders/forest.frag | 92 ++++++++++++++++++++++++++++++++++++++++-- Shaders/forest.vert | 31 +++++++++----- 5 files changed, 250 insertions(+), 17 deletions(-) diff --git a/Effects/forest.eff b/Effects/forest.eff index eba84aca1..0e28433db 100644 --- a/Effects/forest.eff +++ b/Effects/forest.eff @@ -23,8 +23,22 @@ repeat normalized + + Textures.high/Terrain/forest-relief.png + linear-mipmap-linear + repeat + repeat + normalized + /sim/rendering/snow-level-m + 0.01 + 15.0 + /sim/rendering/quality-level + + 6 + 7 + @@ -97,10 +111,27 @@ texture[4]/internal-format - + + 5 + texture[5]/image + texture[5]/filter + texture[5]/wrap-s + texture[5]/wrap-t + + texture[5]/internal-format + + Shaders/forest.vert Shaders/forest.frag + + tangent + 6 + + + binormal + 7 + NoiseTex @@ -122,11 +153,26 @@ sampler-1d 3 + + NormalTex + sampler-2d + 5 + + + depth_factor + float + depth-factor + snowlevel float snow-level + + quality_level + float + quality-level + diff --git a/Effects/glacier.eff b/Effects/glacier.eff index a161886a3..dd477f7b8 100644 --- a/Effects/glacier.eff +++ b/Effects/glacier.eff @@ -23,8 +23,22 @@ repeat normalized + + Textures.high/Terrain/forest-relief.png + linear-mipmap-linear + repeat + repeat + normalized + /sim/rendering/snow-level-m + 0.01 + 15.0 + /sim/rendering/quality-level + + 6 + 7 + @@ -97,10 +111,27 @@ texture[4]/internal-format - + + 5 + texture[5]/image + texture[5]/filter + texture[5]/wrap-s + texture[5]/wrap-t + + texture[5]/internal-format + + Shaders/forest.vert Shaders/forest.frag + + tangent + 6 + + + binormal + 7 + NoiseTex @@ -122,11 +153,26 @@ sampler-1d 3 + + NormalTex + sampler-2d + 5 + + + depth_factor + float + depth-factor + snowlevel float snow-level + + quality_level + float + quality-level + diff --git a/Effects/herbtundra.eff b/Effects/herbtundra.eff index 87e6babdf..5a180b4a7 100644 --- a/Effects/herbtundra.eff +++ b/Effects/herbtundra.eff @@ -23,8 +23,22 @@ repeat normalized + + Textures.high/Terrain/forest-relief.png + linear-mipmap-linear + repeat + repeat + normalized + /sim/rendering/snow-level-m + 0.01 + 15.0 + /sim/rendering/quality-level + + 6 + 7 + @@ -97,10 +111,27 @@ texture[4]/internal-format - + + 5 + texture[5]/image + texture[5]/filter + texture[5]/wrap-s + texture[5]/wrap-t + + texture[5]/internal-format + + Shaders/forest.vert Shaders/forest.frag + + tangent + 6 + + + binormal + 7 + NoiseTex @@ -122,11 +153,26 @@ sampler-1d 3 + + NormalTex + sampler-2d + 5 + + + depth_factor + float + depth-factor + snowlevel float snow-level + + quality_level + float + quality-level + diff --git a/Shaders/forest.frag b/Shaders/forest.frag index 7739a6677..6fc1bf12d 100644 --- a/Shaders/forest.frag +++ b/Shaders/forest.frag @@ -1,23 +1,90 @@ #version 120 -varying vec4 rawpos; +varying vec4 rawpos; varying vec4 ecPosition; +varying vec3 VTangent; +varying vec3 VBinormal; varying vec3 VNormal; varying vec3 Normal; +varying float bump; uniform sampler3D NoiseTex; uniform sampler2D SampleTex; uniform sampler1D ColorsTex; uniform sampler2D SampleTex2; +uniform sampler2D NormalTex; +uniform float depth_factor; +uniform float quality_level; // From /sim/rendering/quality-level uniform float snowlevel; // From /sim/rendering/snow-level-m const float scale = 1.0; +int linear_search_steps = 10; + +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; + float t = texture2D(reliefMap, 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 = texture2D(reliefMap, dp + ds * depth).a; + if(depth >= t) + { + best_depth = depth; + depth -= 2.0 * size; + } + depth += size; + } + + return(best_depth); +} void main (void) { + if ( quality_level >= 3.5 ) { + linear_search_steps = 20; + } + vec2 uv, dp, ds; + vec3 N; + float d; + if ( bump > 0.9 && quality_level >= 2.0 ) + { + vec3 V = normalize(ecPosition.xyz); + float a = dot(VNormal, -V); + vec2 s = vec2(dot(V, VTangent), dot(V, VBinormal)); + s *= depth_factor / a; + ds = s; + dp = gl_TexCoord[0].st; + d = ray_intersect(NormalTex, dp, ds); + + uv = dp + ds * d; + N = texture2D(NormalTex, uv).xyz * 2.0 - 1.0; + } + else + { + uv = gl_TexCoord[0].st; + N = vec3(0.0, 0.0, 1.0); + } + + vec4 basecolor = texture2D(SampleTex, rawpos.xy*0.000144); vec4 basecolor2 = texture2D(SampleTex2, rawpos.xy*0.000144); @@ -89,13 +156,32 @@ void main (void) c1 = mix(c1, c5, clamp(0.65, -n, 0.6)); } + 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)); + float shadow_factor = 1.0; + + vec4 constantColor = vec4(1.0, 1.0, 1.0, 1.0); + + // Shadow + 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; + } + // end shadow + //old snow //c1 = mix(c1, clamp(n+nvL[2]*4.1+vec4(0.1, 0.1, nvL[2]*2.2, 1.0), 0.7, 1.0), smoothstep(snowlevel+300.0, snowlevel+360.0, (rawpos.z)+nvL[1]*3000.0)); - vec3 diffuse = gl_Color.rgb * max(0.4, dot(VNormal, gl_LightSource[0].position.xyz)); - vec4 ambient_light = gl_LightSource[0].diffuse * vec4(diffuse, 1.0); + //diffuse = gl_Color.rgb * max(0.4, dot(VNormal, gl_LightSource[0].position.xyz)); + vec4 ambient_light = gl_LightSource[0].diffuse * shadow_factor * vec4(diffuse, 1.0); c1 *= ambient_light; vec4 finalColor = c1; diff --git a/Shaders/forest.vert b/Shaders/forest.vert index 631133d09..de27c5ac3 100644 --- a/Shaders/forest.vert +++ b/Shaders/forest.vert @@ -1,19 +1,28 @@ -#version 120 +varying vec4 rawpos; +varying vec4 ecPosition; +varying vec3 VNormal; +varying vec3 VTangent; +varying vec3 VBinormal; +varying vec3 Normal; +varying vec4 constantColor; +varying float bump; -varying vec4 rawpos; -varying vec4 ecPosition; -varying vec3 VNormal; -varying vec3 Normal; +attribute vec3 tangent; +attribute vec3 binormal; void main(void) { - gl_TexCoord[0] = gl_MultiTexCoord0; - rawpos = gl_Vertex; - ecPosition = gl_ModelViewMatrix * gl_Vertex; - VNormal = normalize(gl_NormalMatrix * gl_Normal); + ecPosition = gl_ModelViewMatrix * rawpos; Normal = normalize(gl_Normal); - - gl_FrontColor = gl_Color; + VNormal = gl_NormalMatrix * gl_Normal; + VTangent = gl_NormalMatrix * tangent; + VBinormal = gl_NormalMatrix * binormal; + bump = 1.0; + + gl_FrontColor = gl_Color; + constantColor = gl_FrontMaterial.emission + + gl_FrontColor * (gl_LightModel.ambient + gl_LightSource[0].ambient); gl_Position = ftransform(); + gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; }