1 | // oceanHLSL_Cg.frag |
---|
2 | // fragment program for Ocean water simulation |
---|
3 | // 04 Aug 2005 |
---|
4 | // adapted for Ogre by nfz |
---|
5 | // original shader source from Render Monkey 1.6 Reflections Refractions.rfx |
---|
6 | // can be used in both Cg and HLSL compilers |
---|
7 | |
---|
8 | // 06 Aug 2005: moved uvw calculation from fragment program into vertex program |
---|
9 | |
---|
10 | float4 main(float3 uvw: TEXCOORD0, float3 normal: TEXCOORD1, float3 vVec: TEXCOORD2, |
---|
11 | uniform float fadeBias, |
---|
12 | uniform float fadeExp, |
---|
13 | uniform float4 waterColor, |
---|
14 | uniform sampler Noise, |
---|
15 | uniform sampler skyBox |
---|
16 | |
---|
17 | ) : COLOR |
---|
18 | { |
---|
19 | |
---|
20 | // in OpenGL Ogre uses Devil 1.6.7 for loading dds textures. |
---|
21 | // But Devil buggers up the green and blue channels of the 3D |
---|
22 | // texture by setting them to zero so only the red channel has good data |
---|
23 | // Dx9 loads the texture properly but if we want things to look the same |
---|
24 | // between Dx9 and GL we only use the red channel for now until Devil dds issues are fixed |
---|
25 | float3 noisy = tex3D(Noise, uvw).xxx; |
---|
26 | |
---|
27 | // convert to Signed noise |
---|
28 | float3 bump = 2 * noisy - 1; |
---|
29 | bump.xz *= 0.15; |
---|
30 | // Make sure the normal always points upwards |
---|
31 | // note that Ogres y axis is vertical (RM Z axis is vertical) |
---|
32 | bump.y = 0.8 * abs(bump.y) + 0.2; |
---|
33 | // Offset the surface normal with the bump |
---|
34 | bump = normalize(normal + bump); |
---|
35 | |
---|
36 | // Find the reflection vector |
---|
37 | float3 normView = normalize(vVec); |
---|
38 | float3 reflVec = reflect(normView, bump); |
---|
39 | // Ogre has z flipped for cubemaps |
---|
40 | reflVec.z = -reflVec.z; |
---|
41 | float4 refl = texCUBE(skyBox, reflVec); |
---|
42 | |
---|
43 | // set up for fresnel calc |
---|
44 | float lrp = 1 - dot(-normView, bump); |
---|
45 | |
---|
46 | // Interpolate between the water color and reflection for fresnel effect |
---|
47 | return lerp(waterColor, refl, saturate(fadeBias + pow(lrp, fadeExp))); |
---|
48 | |
---|
49 | } |
---|