1/* 2GLSL conversion of Michael Horsch water demo 3http://www.bonzaisoftware.com/wfs.html 4Converted by Mars_999 58/20/2005 6*/ 7 8uniform sampler2D m_water_normalmap; 9uniform sampler2D m_water_reflection; 10uniform sampler2D m_water_refraction; 11uniform sampler2D m_water_dudvmap; 12uniform sampler2D m_water_depthmap; 13uniform vec4 m_waterColor; 14uniform float m_waterDepth; 15uniform vec4 m_distortionScale; 16uniform vec4 m_distortionMix; 17uniform vec4 m_texScale; 18uniform vec2 m_FrustumNearFar; 19uniform float m_waterTransparency; 20 21 22 23varying vec4 lightDir; //lightpos 24varying vec4 waterTex1; //moving texcoords 25varying vec4 waterTex2; //moving texcoords 26varying vec4 position; //for projection 27varying vec4 viewDir; //viewts 28varying vec4 viewLightDir; 29varying vec4 viewCamDir; 30 31//unit 0 = m_water_reflection 32//unit 1 = m_water_refraction 33//unit 2 = m_water_normalmap 34//unit 3 = m_water_dudvmap 35//unit 4 = m_water_depthmap 36 37 const vec4 two = vec4(2.0, 2.0, 2.0, 1.0); 38 const vec4 mone = vec4(-1.0, -1.0, -1.0, 1.0); 39 40 const vec4 ofive = vec4(0.5,0.5,0.5,1.0); 41 42 const float exponent = 64.0; 43 44float tangDot(in vec3 v1, in vec3 v2){ 45 float d = dot(v1,v2); 46 #ifdef V_TANGENT 47 d = 1.0 - d*d; 48 return step(0.0, d) * sqrt(d); 49 #else 50 return d; 51 #endif 52} 53 54vec4 readDepth(vec2 uv){ 55 float depth= (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - texture2D(m_water_depthmap, uv).r* (m_FrustumNearFar.y-m_FrustumNearFar.x)); 56 return vec4( depth); 57} 58 59void main(void) 60{ 61 62 63 vec4 lightTS = normalize(lightDir); 64 vec4 viewt = normalize(viewDir); 65 vec4 disdis = texture2D(m_water_dudvmap, vec2(waterTex2 * m_texScale)); 66 vec4 fdist = texture2D(m_water_dudvmap, vec2(waterTex1 + disdis*m_distortionMix)); 67 fdist =normalize( fdist * 2.0 - 1.0)* m_distortionScale; 68 69 70 //load normalmap 71 vec4 nmap = texture2D(m_water_normalmap, vec2(waterTex1 + disdis*m_distortionMix)); 72 nmap = (nmap-ofive) * two; 73 // nmap = nmap*2.0-1.0; 74 vec4 vNorm = normalize(nmap); 75 76 77 vec4 projCoord = position / position.w; 78 projCoord =(projCoord+1.0)*0.5 + fdist; 79 projCoord = clamp(projCoord, 0.001, 0.999); 80 81 //load reflection,refraction and depth texture 82 vec4 refl = texture2D(m_water_reflection, vec2(projCoord.x,1.0-projCoord.y)); 83 vec4 refr = texture2D(m_water_refraction, vec2(projCoord)); 84 vec4 wdepth =readDepth(vec2(projCoord)); 85 86 wdepth = vec4(pow(wdepth.x, m_waterDepth)); 87 vec4 invdepth = 1.0 - wdepth; 88 89 90 // Blinn - Phong 91 // vec4 H = (viewt - lightTS); 92 // vec4 specular =vec4(pow(max(dot(H, vNorm), 0.0), exponent)); 93 94// Standard Phong 95 96 // vec4 R =reflect(-L, vNorm); 97 // vec4 specular =vec4( pow(max(dot(R, E), 0.0),exponent)); 98 99 100 //calculate specular highlight 101 vec4 L=normalize(viewLightDir); 102 vec4 E=normalize(viewCamDir); 103 vec4 vRef = normalize(reflect(-L,vNorm)); 104 float stemp =max(0.0, dot( vRef,E) ); 105 //initializing to 0 to avoid artifacts on old intel cards 106 vec4 specular = vec4(0.0,0.0,0.0,0.0); 107 if(stemp>0.0){ 108 stemp = pow(stemp, exponent); 109 specular = vec4(stemp); 110 } 111 112 113 114 vec4 fresnelTerm = vec4(0.02+0.97*pow((1.0-dot(normalize(viewt), vNorm)),5.0)); 115 116 117 118 fresnelTerm=fresnelTerm*invdepth*m_waterTransparency; 119 fresnelTerm=clamp(fresnelTerm,0.0,1.0); 120 121 refr*=(fresnelTerm); 122 refr *= invdepth; 123 refr= refr+ m_waterColor*wdepth*fresnelTerm; 124 125 gl_FragColor =(refr+ refl*(1.0-fresnelTerm))+specular; 126} 127