[1] | 1 | // oceanGLSL.frag |
---|
| 2 | // fragment program for Ocean water simulation |
---|
| 3 | // 05 Aug 2005 |
---|
| 4 | // adapted for Ogre by nfz |
---|
| 5 | // converted from HLSL to GLSL |
---|
| 6 | // original shader source from Render Monkey 1.6 Reflections Refractions.rfx |
---|
| 7 | |
---|
| 8 | // 06 Aug 2005: moved uvw calculation from fragment program into vertex program |
---|
| 9 | |
---|
| 10 | uniform float fadeBias; |
---|
| 11 | uniform float fadeExp; |
---|
| 12 | uniform vec4 waterColor; |
---|
| 13 | uniform sampler3D Noise; |
---|
| 14 | uniform samplerCube skyBox; |
---|
| 15 | |
---|
| 16 | varying vec3 uvw; |
---|
| 17 | varying vec3 normal; |
---|
| 18 | varying vec3 vVec; |
---|
| 19 | |
---|
| 20 | void main(void) |
---|
| 21 | { |
---|
| 22 | vec3 noisy = texture3D(Noise, uvw).xyz; |
---|
| 23 | |
---|
| 24 | // convert to signed noise |
---|
| 25 | vec3 bump = 2.0 * noisy - 1.0; |
---|
| 26 | bump.xz *= 0.15; |
---|
| 27 | // Make sure the normal always points upwards |
---|
| 28 | // note that Ogres y axis is vertical (RM Z axis is vertical) |
---|
| 29 | bump.y = 0.8 * abs(bump.y) + 0.2; |
---|
| 30 | // Offset the surface normal with the bump |
---|
| 31 | bump = normalize(normal + bump); |
---|
| 32 | |
---|
| 33 | // Find the reflection vector |
---|
| 34 | vec3 normView = normalize(vVec); |
---|
| 35 | vec3 reflVec = reflect(normView, bump); |
---|
| 36 | // Ogre has z flipped for cubemaps |
---|
| 37 | reflVec.z = -reflVec.z; |
---|
| 38 | vec4 refl = textureCube(skyBox, reflVec); |
---|
| 39 | |
---|
| 40 | // set up for fresnel calc |
---|
| 41 | float lrp = 1.0 - dot(-normView, bump); |
---|
| 42 | |
---|
| 43 | // Interpolate between the water color and reflection for fresnel effect |
---|
| 44 | gl_FragColor = mix(waterColor, refl, clamp(fadeBias + pow(lrp, fadeExp), 0.0, 1.0) ); |
---|
| 45 | } |
---|