[12115] | 1 | /*********************************************************************NVMH3**** |
---|
| 2 | Copyright NVIDIA Corporation 2003 |
---|
| 3 | TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THIS SOFTWARE IS PROVIDED |
---|
| 4 | *AS IS* AND NVIDIA AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES, EITHER EXPRESS |
---|
| 5 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY |
---|
| 6 | AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL NVIDIA OR ITS SUPPLIERS |
---|
| 7 | BE LIABLE FOR ANY SPECIAL, INCIDENTAL, INDIRECT, OR CONSEQUENTIAL DAMAGES |
---|
| 8 | WHATSOEVER (INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, |
---|
| 9 | BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, OR ANY OTHER PECUNIARY LOSS) |
---|
| 10 | ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF NVIDIA HAS |
---|
| 11 | BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | Comments: |
---|
| 15 | Simple ocean shader with animated bump map and geometric waves |
---|
| 16 | Based partly on "Effective Water Simulation From Physical Models", GPU Gems |
---|
| 17 | |
---|
| 18 | 11 Aug 05: converted from HLSL to GLSL by Jeff Doyle (nfz) to work in Ogre |
---|
| 19 | |
---|
| 20 | ******************************************************************************/ |
---|
| 21 | |
---|
| 22 | uniform vec3 eyePosition; |
---|
| 23 | uniform float BumpScale; |
---|
| 24 | uniform vec2 textureScale; |
---|
| 25 | uniform vec2 bumpSpeed; |
---|
| 26 | uniform float time; |
---|
| 27 | uniform float waveFreq; |
---|
| 28 | uniform float waveAmp; |
---|
| 29 | uniform mat4 worldViewProj; |
---|
| 30 | |
---|
| 31 | attribute vec4 vertex; |
---|
| 32 | attribute vec4 uv0; |
---|
| 33 | |
---|
| 34 | varying mat3 rotMatrix; // transform from tangent to obj space |
---|
| 35 | varying vec2 bumpCoord0; |
---|
| 36 | varying vec2 bumpCoord1; |
---|
| 37 | varying vec2 bumpCoord2; |
---|
| 38 | varying vec3 eyeVector; |
---|
| 39 | |
---|
| 40 | // wave functions |
---|
| 41 | struct Wave { |
---|
| 42 | float freq; // 2*PI / wavelength |
---|
| 43 | float amp; // amplitude |
---|
| 44 | float phase; // speed * 2*PI / wavelength |
---|
| 45 | vec2 dir; |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | void main(void) |
---|
| 50 | { |
---|
| 51 | #define NWAVES 2 |
---|
| 52 | |
---|
| 53 | Wave wave[NWAVES]; |
---|
| 54 | |
---|
| 55 | wave[0] = Wave( waveFreq, waveAmp, 0.5, vec2(-1, 0) ); |
---|
| 56 | wave[1] = Wave( 3.0 * waveFreq, 0.33 * waveAmp, 1.7, vec2(-0.7, 0.7) ); |
---|
| 57 | |
---|
| 58 | vec4 P = vertex; |
---|
| 59 | |
---|
| 60 | // sum waves |
---|
| 61 | float ddx = 0.0, ddy = 0.0; |
---|
| 62 | float deriv; |
---|
| 63 | float angle; |
---|
| 64 | |
---|
| 65 | // wave synthesis using two sine waves at different frequencies and phase shift |
---|
| 66 | for(int i = 0; i<NWAVES; ++i) |
---|
| 67 | { |
---|
| 68 | angle = dot(wave[i].dir, P.xz) * wave[i].freq + time * wave[i].phase; |
---|
| 69 | P.y += wave[i].amp * sin( angle ); |
---|
| 70 | // calculate derivate of wave function |
---|
| 71 | deriv = wave[i].freq * wave[i].amp * cos(angle); |
---|
| 72 | ddx -= deriv * wave[i].dir.x; |
---|
| 73 | ddy -= deriv * wave[i].dir.y; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | // compute the 3x3 transform from tangent space to object space |
---|
| 77 | // compute tangent basis |
---|
| 78 | vec3 T = normalize(vec3(1.0, ddy, 0.0)) * BumpScale; |
---|
| 79 | vec3 B = normalize(vec3(0.0, ddx, 1.0)) * BumpScale; |
---|
| 80 | vec3 N = normalize(vec3(ddx, 1.0, ddy)); |
---|
| 81 | |
---|
| 82 | rotMatrix = mat3(T, B, N); |
---|
| 83 | |
---|
| 84 | gl_Position = worldViewProj * P; |
---|
| 85 | |
---|
| 86 | // calculate texture coordinates for normal map lookup |
---|
| 87 | bumpCoord0.xy = uv0.xy * textureScale + time * bumpSpeed; |
---|
| 88 | bumpCoord1.xy = uv0.xy * textureScale * 2.0 + time * bumpSpeed * 4.0; |
---|
| 89 | bumpCoord2.xy = uv0.xy * textureScale * 4.0 + time * bumpSpeed * 8.0; |
---|
| 90 | |
---|
| 91 | eyeVector = P.xyz - eyePosition; // eye position in vertex space |
---|
| 92 | } |
---|