From 2c825cbef2ea21950babf42418690620201dc8c2 Mon Sep 17 00:00:00 2001 From: vmmeazza Date: Sun, 28 Mar 2010 11:17:51 +0000 Subject: [PATCH] A simple reflection effect - needs this patch ftp://ftp.abbeytheatre2.org.uk/fgfs/Shader/Reflect/cube-map.patch which will be included in Simgear soon (hopefully). --- Effects/reflect.eff | 241 +++++++++++++++++++++++++++++++++++++++++++ Shaders/reflect.frag | 86 +++++++++++++++ Shaders/reflect.vert | 58 +++++++++++ 3 files changed, 385 insertions(+) create mode 100644 Effects/reflect.eff create mode 100644 Shaders/reflect.frag create mode 100644 Shaders/reflect.vert diff --git a/Effects/reflect.eff b/Effects/reflect.eff new file mode 100644 index 000000000..59d45e743 --- /dev/null +++ b/Effects/reflect.eff @@ -0,0 +1,241 @@ + + + Effects/reflect + Effects/model-default + + + + cubemap + + Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_px.png + Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_nx.png + Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_py.png + Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_ny.png + Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_pz.png + Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_nz.png + + + + Aircraft/Generic/Effects/Rainbow.png + linear-mipmap-linear + repeat + repeat + normalized + + + Aircraft/Generic/Effects/FresnelLookUp.png + linear-mipmap-linear + repeat + repeat + normalized + + transparent + smooth + + + + + + + /sim/rendering/shader-effects + + + 2.0 + + + + GL_ARB_shader_objects + GL_ARB_shading_language_100 + GL_ARB_vertex_shader + GL_ARB_fragment_shader + + + + + + + true + + + material/active + + + material/ambient + + + material/diffuse + + + material/specular + + + material/emissive + + + material/shininess + + + material/color-mode + + + + + blend/active + + + blend/source + + + blend/destination + + + + shade-model + + + cull-face + + + rendering-hint + + + + 0 + + texture[0]/image + + + texture[0]/filter + + + texture[0]/wrap-s + + + texture[0]/wrap-t + + + texture[0]/internal-format + + + + + 5 + + texture[5]/type + + + texture[5]/images + + + + + 6 + + texture[6]/type + + + texture[6]/image + + + texture[6]/filter + + + texture[6]/wrap-s + + + texture[6]/wrap-t + + + + + 7 + + texture[7]/type + + + texture[7]/image + + + texture[7]/filter + + + texture[7]/wrap-s + + + texture[7]/wrap-t + + + + + Shaders/reflect.vert + Shaders/reflect.frag + + tangent + 6 + + + binormal + 7 + + + normal + 15 + + + + + BaseTex + sampler-2d + 0 + + + + Environment + sampler-cube + 5 + + + + Rainbow + sampler-2d + 6 + + + + Fresnel + sampler-2d + 7 + + + + + + rainbowiness + float + 0.1 + + + + + fresneliness + float + 0.1 + + + + + transparency + float + 0.4 + + + + + correction + float + 0.2 + + + + diff --git a/Shaders/reflect.frag b/Shaders/reflect.frag new file mode 100644 index 000000000..a1f99a236 --- /dev/null +++ b/Shaders/reflect.frag @@ -0,0 +1,86 @@ +#version 120 + +varying vec4 ecPosition; +varying vec3 VNormal; +varying vec3 Normal; +varying vec4 constantColor; +varying vec3 vViewVec; +varying vec3 reflVec; + +varying vec3 Diffuse; +varying vec3 lightDir, halfVector; +varying float alpha, fogCoord; + +uniform samplerCube Environment; +uniform sampler2D Rainbow; +uniform sampler2D BaseTex; +uniform sampler2D Fresnel; + +uniform float transparency; +uniform float rainbowiness; +uniform float fresneliness; +uniform float correction; + + +void main (void) +{ + if (!gl_FrontFacing) discard; + + vec3 n, halfV; + float NdotL, NdotHV; + vec4 color = constantColor; + vec4 specular = vec4(0.0); + n = VNormal; + NdotL = max(0.0, dot(n, lightDir)); + + //calculate the specular light + if (NdotL > 0.0) { + color += vec4(Diffuse, 1.0) * NdotL; + halfV = normalize(halfVector); + NdotHV = max(dot(n, halfV), 0.0); + if (gl_FrontMaterial.shininess > 0.0) + specular.rgb = (gl_FrontMaterial.specular.rgb + * gl_LightSource[0].specular.rgb + * pow(NdotHV, gl_FrontMaterial.shininess)); + } + + color.a = alpha; + color = clamp(color, 0.0, 1.0); + vec4 texel = texture2D(BaseTex, gl_TexCoord[0].st); + vec4 texelcolor = color * texel + specular; + + // calculate the fog factor + float fogCoord = ecPosition.z; + const float LOG2 = 1.442695; + float fogFactor = exp2(-gl_Fog.density * gl_Fog.density * fogCoord * fogCoord * LOG2); + fogFactor = clamp(fogFactor, 0.0, 1.0); + + if(gl_Fog.density == 1.0) + fogFactor=1.0; + + vec3 normal = normalize(VNormal); + vec3 viewVec = normalize(vViewVec); + + // Map a rainbowish color + float v = dot(viewVec, normal); + vec4 rainbow = texture2D(Rainbow, vec2(v, 0.0)); + + // Map a fresnel effect + vec4 fresnel = texture2D(Fresnel, vec2(v, 0.0)); + + //map the refection of the environment + vec4 reflection = textureCube(Environment, reflVec); + + //add fringing fresnel and rainbow effects and modulate by transparency + vec4 reflcolor = mix(reflection, rainbow, rainbowiness * v); + vec4 reflfrescolor = mix(reflcolor, fresnel, fresneliness * v); + vec4 raincolor = vec4(reflfrescolor.rgb, 1.0) * transparency; + vec4 mixedcolor = mix(texel, raincolor, transparency); + + //the final reflection + vec4 ambient_correction = mix(gl_LightSource[0].ambient, vec4(1.0, 1.0, 0.9, 1.0), 0.5) * correction; + vec4 reflColor = color * mixedcolor + specular + ambient_correction ; + reflColor = clamp(reflColor, 0.0, 1.0); + + gl_FragColor = mix(gl_Fog.color, reflColor, fogFactor); +} diff --git a/Shaders/reflect.vert b/Shaders/reflect.vert new file mode 100644 index 000000000..6b46aa2a4 --- /dev/null +++ b/Shaders/reflect.vert @@ -0,0 +1,58 @@ +varying vec4 ecPosition; +varying vec3 VNormal; +varying vec3 Normal; +varying vec4 constantColor; +varying vec3 vViewVec; +varying vec3 reflVec; + +varying vec3 Diffuse; +varying vec3 normal, lightDir, halfVector; +varying float alpha, fogCoord; + +uniform mat4 osg_ViewMatrixInverse; + +//attribute vec3 tangent, binormal, normal; + +void main(void) +{ + ecPosition = gl_ModelViewMatrix * gl_Vertex; + vec3 ecPosition3 = vec3(gl_ModelViewMatrix * gl_Vertex) / ecPosition.w; + + vec3 t = normalize(cross(gl_Normal, vec3(1.0,0.0,0.0))); + vec3 b = normalize(cross(gl_Normal,t)); + vec3 n = normalize(gl_Normal); + + VNormal = normalize(gl_NormalMatrix * gl_Normal); + Normal = normalize(gl_Normal); + + lightDir = normalize(vec3(gl_LightSource[0].position)); + halfVector = normalize(gl_LightSource[0].halfVector.xyz); + Diffuse = gl_Color * gl_LightSource[0].diffuse; + //Diffuse= gl_Color.rgb * max(0.0, dot(normalize(VNormal), gl_LightSource[0].position.xyz)); + // Super hack: if diffuse material alpha is less than 1, assume a + // transparency animation is at work + if (gl_FrontMaterial.diffuse.a < 1.0) + alpha = gl_FrontMaterial.diffuse.a; + else + alpha = gl_Color.a; + + fogCoord = abs(ecPosition3.z); + + // Vertex in eye coordinates + vec3 vertVec = ecPosition.xyz; + + vViewVec.x = dot(t, vertVec); + vViewVec.y = dot(b, vertVec); + vViewVec.z = dot(n, vertVec); + + // calculate the reflection vector + vec4 reflect_eye = vec4(reflect(vertVec, VNormal), 0.0); + reflVec = normalize(gl_ModelViewMatrixInverse * reflect_eye); + + gl_FrontColor = gl_Color; + constantColor = gl_FrontMaterial.emission + + gl_Color * (gl_LightModel.ambient + gl_LightSource[0].ambient); + + gl_Position = ftransform(); + gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0; +} \ No newline at end of file