1
0
Fork 0

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).

This commit is contained in:
vmmeazza 2010-03-28 11:17:51 +00:00
parent b38d470782
commit 2c825cbef2
3 changed files with 385 additions and 0 deletions

241
Effects/reflect.eff Normal file
View file

@ -0,0 +1,241 @@
<?xml version="1.0" encoding="utf-8"?>
<PropertyList>
<name>Effects/reflect</name>
<inherits-from>Effects/model-default</inherits-from>
<parameters>
<texture n="5">
<type>cubemap</type>
<images>
<positive-x>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_px.png</positive-x>
<negative-x>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_nx.png</negative-x>
<positive-y>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_py.png</positive-y>
<negative-y>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_ny.png</negative-y>
<positive-z>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_pz.png</positive-z>
<negative-z>Aircraft/Generic/Effects/CubeMaps/fair-sky/fair-sky_nz.png</negative-z>
</images>
</texture>
<texture n="6">
<image>Aircraft/Generic/Effects/Rainbow.png</image>
<filter>linear-mipmap-linear</filter>
<wrap-s>repeat</wrap-s>
<wrap-t>repeat</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<texture n="7">
<image>Aircraft/Generic/Effects/FresnelLookUp.png</image>
<filter>linear-mipmap-linear</filter>
<wrap-s>repeat</wrap-s>
<wrap-t>repeat</wrap-t>
<internal-format>normalized</internal-format>
</texture>
<rendering-hint>transparent</rendering-hint>
<shade-model>smooth</shade-model>
</parameters>
<technique n="9">
<predicate>
<and>
<property>/sim/rendering/shader-effects</property>
<or>
<less-equal>
<value type="float">2.0</value>
<glversion/>
</less-equal>
<and>
<extension-supported>GL_ARB_shader_objects</extension-supported>
<extension-supported>GL_ARB_shading_language_100</extension-supported>
<extension-supported>GL_ARB_vertex_shader</extension-supported>
<extension-supported>GL_ARB_fragment_shader</extension-supported>
</and>
</or>
</and>
</predicate>
<pass>
<lighting>true</lighting>
<material>
<active>
<use>material/active</use>
</active>
<ambient>
<use>material/ambient</use>
</ambient>
<diffuse>
<use>material/diffuse</use>
</diffuse>
<specular>
<use>material/specular</use>
</specular>
<emissive>
<use>material/emissive</use>
</emissive>
<shininess>
<use>material/shininess</use>
</shininess>
<color-mode>
<use>material/color-mode</use>
</color-mode>
</material>
<blend>
<active>
<use>blend/active</use>
</active>
<source>
<use>blend/source</use>
</source>
<destination>
<use>blend/destination</use>
</destination>
</blend>
<shade-model>
<use>shade-model</use>
</shade-model>
<cull-face>
<use>cull-face</use>
</cull-face>
<rendering-hint>
<use>rendering-hint</use>
</rendering-hint>
<texture-unit>
<unit>0</unit>
<image>
<use>texture[0]/image</use>
</image>
<filter>
<use>texture[0]/filter</use>
</filter>
<wrap-s>
<use>texture[0]/wrap-s</use>
</wrap-s>
<wrap-t>
<use>texture[0]/wrap-t</use>
</wrap-t>
<internal-format>
<use>texture[0]/internal-format</use>
</internal-format>
</texture-unit>
<texture-unit>
<unit>5</unit>
<type>
<use>texture[5]/type</use>
</type>
<images>
<use>texture[5]/images</use>
</images>
</texture-unit>
<texture-unit>
<unit>6</unit>
<type>
<use>texture[6]/type</use>
</type>
<image>
<use>texture[6]/image</use>
</image>
<filter>
<use>texture[6]/filter</use>
</filter>
<wrap-s>
<use>texture[6]/wrap-s</use>
</wrap-s>
<wrap-t>
<use>texture[6]/wrap-t</use>
</wrap-t>
</texture-unit>
<texture-unit>
<unit>7</unit>
<type>
<use>texture[7]/type</use>
</type>
<image>
<use>texture[7]/image</use>
</image>
<filter>
<use>texture[7]/filter</use>
</filter>
<wrap-s>
<use>texture[7]/wrap-s</use>
</wrap-s>
<wrap-t>
<use>texture[7]/wrap-t</use>
</wrap-t>
</texture-unit>
<program>
<vertex-shader>Shaders/reflect.vert</vertex-shader>
<fragment-shader>Shaders/reflect.frag</fragment-shader>
<attribute>
<name>tangent</name>
<index>6</index>
</attribute>
<attribute>
<name>binormal</name>
<index>7</index>
</attribute>
<attribute>
<name>normal</name>
<index>15</index>
</attribute>
</program>
<uniform>
<name>BaseTex</name>
<type>sampler-2d</type>
<value type="int">0</value>
</uniform>
<uniform>
<name>Environment</name>
<type>sampler-cube</type>
<value type="int">5</value>
</uniform>
<uniform>
<name>Rainbow</name>
<type>sampler-2d</type>
<value type="int">6</value>
</uniform>
<uniform>
<name>Fresnel</name>
<type>sampler-2d</type>
<value type="int">7</value>
</uniform>
<!-- set the amount of fringing colour 0.0 - 1.0 -->
<uniform>
<name>rainbowiness</name>
<type>float</type>
<value type="float">0.1</value>
</uniform>
<!-- set the amount of fresnel effect colour 0.0 - 1.0 -->
<uniform>
<name>fresneliness</name>
<type>float</type>
<value type="float">0.1</value>
</uniform>
<!-- set the amount of tranparency 0.0 (fully tranaparent)
- 1.0 (fully opaque) -->
<uniform>
<name>transparency</name>
<type>float</type>
<value type="float">0.4</value>
</uniform>
<!-- set the amount of ambient light correction 0.0 -1.0 -->
<uniform>
<name>correction</name>
<type>float</type>
<value type="float">0.2</value>
</uniform>
</pass>
</technique>
</PropertyList>

86
Shaders/reflect.frag Normal file
View file

@ -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);
}

58
Shaders/reflect.vert Normal file
View file

@ -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;
}