diff --git a/Effects/water.eff b/Effects/water.eff index 171ec5c3c..248950cb6 100644 --- a/Effects/water.eff +++ b/Effects/water.eff @@ -2,6 +2,150 @@ Effects/water Effects/terrain-default + + + Textures/Water/water-reflection.png + linear-mipmap-linear + repeat + repeat + normalized + + + Textures/Water/water-refraction.png + linear-mipmap-linear + repeat + repeat + normalized + + + Textures/Water/water-normalmap.png + linear-mipmap-linear + repeat + repeat + normalized + + + Textures/Water/water-dudv.png + linear-mipmap-linear + repeat + repeat + normalized + + + /rendering/scene/saturation + + + + + + /sim/rendering/water-shader + /sim/rendering/shader-effects + + 2.0 + /sim/rendering/quality-level + + + + 2.0 + + + + GL_ARB_shader_objects + GL_ARB_shading_language_100 + GL_ARB_vertex_shader + GL_ARB_fragment_shader + + + + + + true + + material/ambient + material/diffuse + material/specular + ambient-and-diffuse + + transparent + transparent + smooth + back + + render-bin/bin-number + render-bin/bin-name + + + 0 + texture[0]/image + texture[0]/filter + texture[0]/wrap-s + texture[0]/wrap-t + + texture[0]/internal-format + + + + 1 + texture[1]/image + texture[1]/filter + texture[1]/wrap-s + texture[1]/wrap-t + + texture[1]/internal-format + + + + 2 + texture[2]/image + texture[2]/filter + texture[2]/wrap-s + texture[2]/wrap-t + + texture[2]/internal-format + + + + 3 + texture[3]/image + texture[3]/filter + texture[3]/wrap-s + texture[3]/wrap-t + + texture[3]/internal-format + + + + Shaders/water.vert + Shaders/water.frag + + + water_reflection + sampler-2d + 0 + + + water_refraction + sampler-2d + 1 + + + water_normalmap + sampler-2d + 2 + + + water_dudvmap + sampler-2d + 3 + + + saturation + float + 0.4 + + + + @@ -42,8 +186,8 @@ noise - Shaders/water.vert - Shaders/water.frag + Shaders/water-orig.vert + Shaders/water-orig.frag texture diff --git a/Shaders/reflect-bump-spec.frag b/Shaders/reflect-bump-spec.frag index f8f1a3b6d..fcc6d3850 100644 --- a/Shaders/reflect-bump-spec.frag +++ b/Shaders/reflect-bump-spec.frag @@ -97,7 +97,7 @@ void main (void) reflFactor = (gl_FrontMaterial.shininess / 128) + transparency_offset; } - reflFactor = clamp(reflFactor * ns.a, 0.0, 1.0); + reflFactor = clamp(reflFactor, 0.0, 1.0); // set ambient adjustment to remove bluiness with user input float ambient_offset = clamp(ambient_correction, -1.0, 1.0); diff --git a/Shaders/water-orig.frag b/Shaders/water-orig.frag new file mode 100644 index 000000000..50ae098f8 --- /dev/null +++ b/Shaders/water-orig.frag @@ -0,0 +1,86 @@ +#version 120 + +varying vec4 rawpos; +varying vec4 ecPosition; +varying vec3 VNormal; +varying vec3 Normal; +varying vec3 lightVec; + +uniform sampler3D NoiseTex; +uniform float osg_SimulationTime; + +//const float scale = 1.0; + +void main (void) +{ + vec4 noisevecS = texture3D(NoiseTex, (rawpos.xyz)*0.0126); + vec4 nvLS = texture3D(NoiseTex, (rawpos.xyz)*-0.0003323417); + + vec4 noisevec = texture3D(NoiseTex, (rawpos.xyz)*0.00423+vec3(0.0,0.0,osg_SimulationTime*0.035217)); + vec4 nvL = texture3D(NoiseTex, (rawpos.xyz)*0.001223417+(0.0,0.0,osg_SimulationTime*-0.0212)); + + float fogFactor; + if (gl_Fog.density == 1.0) + { + fogFactor=1.0; + } + else + { + float fogCoord = ecPosition.z; + const float LOG2 = 1.442695; + fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2); + fogFactor = clamp(fogFactor, 0.0, 1.0); + } + + float a=1.0; + float n=0.00; + n += nvLS[0]*a; + a/=2.0; + n += nvLS[1]*a; + a/=2.0; + n += nvLS[2]*a; + a/=2.0; + n += nvLS[3]*a; + + a=4.0; + float na=n; + na += nvL[0]*1.1; + a*=1.2; + na += nvL[1]*a; + a*=1.2; + na += nvL[2]*a; + a*=1.2; + na += nvL[3]*a; + a=2.0; + na += noisevec[0]*a*0.2; + a*=1.2; + na += noisevec[1]*a; + a*=1.2; + na += noisevec[2]*a; + a*=1.2; + na += noisevec[3]*a; + + vec4 c1; + c1 = asin(vec4(smoothstep(0.0, 2.2, n), smoothstep(-0.1, 2.10, n), smoothstep(-0.2, 2.0, n), 1.0)); + + vec3 Eye = normalize(-ecPosition.xyz); + vec3 Reflected = normalize(reflect(-normalize(lightVec), normalize(VNormal+vec3(0.0,0.0,na*0.10-0.24)))); + + vec3 bump = normalize(VNormal+vec3(0.0, 0.0, na)-0.9); + vec3 bumped = max(normalize(refract(lightVec, normalize(bump), 0.16)), 0.0); + + vec4 ambientColor = gl_LightSource[0].ambient; + vec4 light = ambientColor; + c1 *= light; + + float bumpFact = (bumped.r+bumped.g+bumped.b); + float ReflectedEye = max(dot(Reflected, Eye), 0.0); + float eyeFact = pow(ReflectedEye, 20.0); + c1 += 0.3 * gl_LightSource[0].diffuse * (1.0-eyeFact) * bumpFact*bumpFact; + c1 += 0.4 * gl_LightSource[0].diffuse * eyeFact * 3*bumpFact; + eyeFact = pow(eyeFact, 20.0); + c1 += gl_LightSource[0].specular * eyeFact * 4*bumpFact; + + vec4 finalColor = c1; + gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor); +} diff --git a/Shaders/water-orig.vert b/Shaders/water-orig.vert new file mode 100644 index 000000000..ca61aa33c --- /dev/null +++ b/Shaders/water-orig.vert @@ -0,0 +1,15 @@ +varying vec4 rawpos; +varying vec4 ecPosition; +varying vec3 VNormal; +varying vec3 Normal; +varying vec3 lightVec; + +void main(void) +{ + rawpos = gl_Vertex; + ecPosition = gl_ModelViewMatrix * gl_Vertex; + VNormal = normalize(gl_NormalMatrix * gl_Normal); + Normal = normalize(gl_Normal); + lightVec = normalize(gl_LightSource[0].position.xyz/* - ecPosition*/); + gl_Position = ftransform(); +} diff --git a/Shaders/water.frag b/Shaders/water.frag index 50ae098f8..d40c77b1e 100644 --- a/Shaders/water.frag +++ b/Shaders/water.frag @@ -1,86 +1,80 @@ -#version 120 +uniform sampler2D water_normalmap; +uniform sampler2D water_reflection; +uniform sampler2D water_dudvmap; +uniform float saturation; -varying vec4 rawpos; +varying vec4 waterTex1; //moving texcoords +varying vec4 waterTex2; //moving texcoords +varying vec4 waterTex4; //viewts varying vec4 ecPosition; -varying vec3 VNormal; -varying vec3 Normal; -varying vec3 lightVec; +varying vec3 viewerdir; +varying vec3 lightdir; +varying vec3 normal; -uniform sampler3D NoiseTex; -uniform float osg_SimulationTime; - -//const float scale = 1.0; - -void main (void) +void main(void) { - vec4 noisevecS = texture3D(NoiseTex, (rawpos.xyz)*0.0126); - vec4 nvLS = texture3D(NoiseTex, (rawpos.xyz)*-0.0003323417); +const vec4 sca = vec4(0.005, 0.005, 0.005, 0.005); +const vec4 sca2 = vec4(0.02, 0.02, 0.02, 0.02); +const vec4 tscale = vec4(0.25, 0.25, 0.25, 0.25); - vec4 noisevec = texture3D(NoiseTex, (rawpos.xyz)*0.00423+vec3(0.0,0.0,osg_SimulationTime*0.035217)); - vec4 nvL = texture3D(NoiseTex, (rawpos.xyz)*0.001223417+(0.0,0.0,osg_SimulationTime*-0.0212)); +// compute direction to viewer +vec3 E = normalize(viewerdir); - float fogFactor; - if (gl_Fog.density == 1.0) - { - fogFactor=1.0; - } - else - { - float fogCoord = ecPosition.z; - const float LOG2 = 1.442695; - fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2); - fogFactor = clamp(fogFactor, 0.0, 1.0); - } +// compute direction to light source +vec3 L = normalize(lightdir); - float a=1.0; - float n=0.00; - n += nvLS[0]*a; - a/=2.0; - n += nvLS[1]*a; - a/=2.0; - n += nvLS[2]*a; - a/=2.0; - n += nvLS[3]*a; +// half vector +vec3 H = normalize(L + E); + +const float water_shininess = 240.0; - a=4.0; - float na=n; - na += nvL[0]*1.1; - a*=1.2; - na += nvL[1]*a; - a*=1.2; - na += nvL[2]*a; - a*=1.2; - na += nvL[3]*a; - a=2.0; - na += noisevec[0]*a*0.2; - a*=1.2; - na += noisevec[1]*a; - a*=1.2; - na += noisevec[2]*a; - a*=1.2; - na += noisevec[3]*a; +vec4 viewt = normalize(waterTex4); - vec4 c1; - c1 = asin(vec4(smoothstep(0.0, 2.2, n), smoothstep(-0.1, 2.10, n), smoothstep(-0.2, 2.0, n), 1.0)); +vec4 disdis = texture2D(water_dudvmap, vec2(waterTex2 * tscale)) * 2.0 - 1.0; +vec4 dist = texture2D(water_dudvmap, vec2(waterTex1 + disdis*sca2)) * 2.0 - 1.0; +vec4 fdist = normalize(dist); +fdist *= sca; - vec3 Eye = normalize(-ecPosition.xyz); - vec3 Reflected = normalize(reflect(-normalize(lightVec), normalize(VNormal+vec3(0.0,0.0,na*0.10-0.24)))); +//normalmap +vec4 nmap0 = texture2D(water_normalmap, vec2(waterTex1+ disdis*sca2)) * 2.0 - 1.0; +vec4 nmap2 = texture2D(water_normalmap, vec2(waterTex2 * tscale)) * 2.0 - 1.0; +vec4 vNorm = normalize(nmap0 + nmap2); - vec3 bump = normalize(VNormal+vec3(0.0, 0.0, na)-0.9); - vec3 bumped = max(normalize(refract(lightVec, normalize(bump), 0.16)), 0.0); +//load reflection +vec4 tmp = vec4(lightdir, 0.0); +vec4 refTex = texture2D(water_reflection, vec2(tmp)); +vec4 refl = normalize(refTex); - vec4 ambientColor = gl_LightSource[0].ambient; - vec4 light = ambientColor; - c1 *= light; +vec3 N0 = vec3(texture2D(water_normalmap, vec2(waterTex1+ disdis*sca2)) * 2.0 - 1.0); +vec3 N1 = vec3(texture2D(water_normalmap, vec2(waterTex2 * tscale)) * 2.0 - 1.0); +vec3 N = normalize(normal+N0+N1); - float bumpFact = (bumped.r+bumped.g+bumped.b); - float ReflectedEye = max(dot(Reflected, Eye), 0.0); - float eyeFact = pow(ReflectedEye, 20.0); - c1 += 0.3 * gl_LightSource[0].diffuse * (1.0-eyeFact) * bumpFact*bumpFact; - c1 += 0.4 * gl_LightSource[0].diffuse * eyeFact * 3*bumpFact; - eyeFact = pow(eyeFact, 20.0); - c1 += gl_LightSource[0].specular * eyeFact * 4*bumpFact; +vec3 specular_color = vec3(gl_LightSource[0].diffuse) + * pow(max(0.0, dot(N, H)), water_shininess) * 6.0; - vec4 finalColor = c1; - gl_FragColor = mix(gl_Fog.color, finalColor, fogFactor); +vec4 specular = vec4(specular_color,0.5); + +//calculate fresnel +vec4 invfres = vec4( dot(vNorm, viewt) ); +vec4 fres = vec4(1.0) + invfres; +refl *= fres; + + +specular = specular * saturation; + +vec4 finalColor = refl + specular; + +float fogFactor; +float fogCoord = ecPosition.z; +const float LOG2 = 1.442695; +fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2); + +vec4 ambient_light = gl_LightSource[0].diffuse; + + finalColor *= ambient_light; + + if(gl_Fog.density == 1.0) + fogFactor=1.0; + +gl_FragColor = mix(gl_Fog.color,finalColor, fogFactor); } diff --git a/Shaders/water.vert b/Shaders/water.vert index ca61aa33c..1f83a168f 100644 --- a/Shaders/water.vert +++ b/Shaders/water.vert @@ -1,15 +1,29 @@ -varying vec4 rawpos; +varying vec4 waterTex1; +varying vec4 waterTex2; +varying vec4 waterTex4; varying vec4 ecPosition; -varying vec3 VNormal; -varying vec3 Normal; -varying vec3 lightVec; +uniform float osg_SimulationTime; +varying vec3 viewerdir; +varying vec3 lightdir; +varying vec3 normal; void main(void) { - rawpos = gl_Vertex; - ecPosition = gl_ModelViewMatrix * gl_Vertex; - VNormal = normalize(gl_NormalMatrix * gl_Normal); - Normal = normalize(gl_Normal); - lightVec = normalize(gl_LightSource[0].position.xyz/* - ecPosition*/); - gl_Position = ftransform(); -} +vec3 N = normalize(gl_Normal); +normal = N; + +ecPosition = gl_ModelViewMatrix * gl_Vertex; + +viewerdir = vec3(gl_ModelViewMatrixInverse[3]) - vec3(gl_Vertex); +lightdir = normalize(vec3(gl_ModelViewMatrixInverse * gl_LightSource[0].position)); + +waterTex4 = vec4( ecPosition.xzy, 0.0 ); + +vec4 t1 = vec4(0.0, osg_SimulationTime*0.005217, 0.0,0.0); +vec4 t2 = vec4(0.0, osg_SimulationTime*-0.0012, 0.0,0.0); + +waterTex1 = gl_MultiTexCoord0 + t1; +waterTex2 = gl_MultiTexCoord0 + t2; + +gl_Position = ftransform(); +} \ No newline at end of file diff --git a/Textures/Water/water-dudv.png b/Textures/Water/water-dudv.png new file mode 100644 index 000000000..2a3778822 Binary files /dev/null and b/Textures/Water/water-dudv.png differ diff --git a/Textures/Water/water-normalmap.png b/Textures/Water/water-normalmap.png new file mode 100644 index 000000000..62e82158f Binary files /dev/null and b/Textures/Water/water-normalmap.png differ diff --git a/Textures/Water/water-reflection.png b/Textures/Water/water-reflection.png new file mode 100644 index 000000000..719668aa7 Binary files /dev/null and b/Textures/Water/water-reflection.png differ