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: heavily modified by Jeff Doyle (nfz) for Ogre |
---|
19 | |
---|
20 | ******************************************************************************/ |
---|
21 | |
---|
22 | struct a2v { |
---|
23 | float4 Position : POSITION; // in object space |
---|
24 | float2 TexCoord : TEXCOORD0; |
---|
25 | }; |
---|
26 | |
---|
27 | struct v2f { |
---|
28 | float4 Position : POSITION; // in clip space |
---|
29 | float3 rotMatrix1 : TEXCOORD0; // first row of the 3x3 transform from tangent to obj space |
---|
30 | float3 rotMatrix2 : TEXCOORD1; // second row of the 3x3 transform from tangent to obj space |
---|
31 | float3 rotMatrix3 : TEXCOORD2; // third row of the 3x3 transform from tangent to obj space |
---|
32 | |
---|
33 | float2 bumpCoord0 : TEXCOORD3; |
---|
34 | float2 bumpCoord1 : TEXCOORD4; |
---|
35 | float2 bumpCoord2 : TEXCOORD5; |
---|
36 | |
---|
37 | float3 eyeVector : TEXCOORD6; |
---|
38 | }; |
---|
39 | |
---|
40 | // wave functions |
---|
41 | |
---|
42 | struct Wave { |
---|
43 | float freq; // 2*PI / wavelength |
---|
44 | float amp; // amplitude |
---|
45 | float phase; // speed * 2*PI / wavelength |
---|
46 | float2 dir; |
---|
47 | }; |
---|
48 | |
---|
49 | v2f main(a2v IN, |
---|
50 | uniform float4x4 WorldViewProj, |
---|
51 | uniform float3 eyePosition, |
---|
52 | uniform float BumpScale, |
---|
53 | uniform float2 textureScale, |
---|
54 | uniform float2 bumpSpeed, |
---|
55 | uniform float time, |
---|
56 | uniform float waveFreq, |
---|
57 | uniform float waveAmp |
---|
58 | ) |
---|
59 | { |
---|
60 | v2f OUT; |
---|
61 | |
---|
62 | #define NWAVES 2 |
---|
63 | Wave wave[NWAVES] = { |
---|
64 | { 1.0, 1.0, 0.5, float2(-1, 0) }, |
---|
65 | { 2.0, 0.5, 1.7, float2(-0.7, 0.7) } |
---|
66 | }; |
---|
67 | |
---|
68 | wave[0].freq = waveFreq; |
---|
69 | wave[0].amp = waveAmp; |
---|
70 | |
---|
71 | wave[1].freq = waveFreq * 3.0; |
---|
72 | wave[1].amp = waveAmp * 0.33; |
---|
73 | |
---|
74 | float4 P = IN.Position; |
---|
75 | |
---|
76 | // sum waves |
---|
77 | float ddx = 0.0, ddy = 0.0; |
---|
78 | float deriv; |
---|
79 | float angle; |
---|
80 | |
---|
81 | // wave synthesis using two sine waves at different frequencies and phase shift |
---|
82 | for(int i = 0; i<NWAVES; ++i) |
---|
83 | { |
---|
84 | angle = dot(wave[i].dir, P.xz) * wave[i].freq + time * wave[i].phase; |
---|
85 | P.y += wave[i].amp * sin( angle ); |
---|
86 | // calculate derivate of wave function |
---|
87 | deriv = wave[i].freq * wave[i].amp * cos(angle); |
---|
88 | ddx += deriv * wave[i].dir.x; |
---|
89 | ddy += deriv * wave[i].dir.y; |
---|
90 | } |
---|
91 | |
---|
92 | OUT.Position = mul(WorldViewProj, P); |
---|
93 | |
---|
94 | // calculate texture coordinates for normal map lookup |
---|
95 | OUT.bumpCoord0.xy = IN.TexCoord*textureScale + time * bumpSpeed; |
---|
96 | OUT.bumpCoord1.xy = IN.TexCoord*textureScale * 2.0 + time * bumpSpeed * 4.0; |
---|
97 | OUT.bumpCoord2.xy = IN.TexCoord*textureScale * 4.0 + time * bumpSpeed * 8.0; |
---|
98 | |
---|
99 | // compute the 3x3 tranform from tangent space to object space |
---|
100 | // first rows are the tangent and binormal scaled by the bump scale |
---|
101 | |
---|
102 | OUT.rotMatrix1.xyz = BumpScale * normalize(float3(1, ddx, 0)); // Binormal |
---|
103 | OUT.rotMatrix2.xyz = BumpScale * normalize(float3(0, ddy, 1)); // Tangent |
---|
104 | OUT.rotMatrix3.xyz = normalize(float3(-ddx, 1, -ddy)); // Normal |
---|
105 | |
---|
106 | OUT.eyeVector = P.xyz - eyePosition; // eye position in vertex space |
---|
107 | return OUT; |
---|
108 | } |
---|