   Ocean effect
  hi friendsI'm going to implement an ocean effect (from nvidia site) which seems somehow intricated, it's as follows:float4x4 worldMatrix : World &lt; string UIWidget = "none";&gt;;   // World or Model matrixfloat4x4 wvpMatrix : WorldViewProjection &lt; string UIWidget = "none";&gt;;// Model*View*Projectionfloat4x4 worldViewMatrix : WorldView &lt; string UIWidget = "none";&gt;;float4x4 viewInverseMatrix : ViewIT &lt; string UIWidget = "none";&gt;;float time : Time &lt; string UIWidget = "none"; &gt;;float ptime &lt; string UIWidget = "none"; &gt;;texture normalMap : Normal&lt;string Name = "waves2.dds";string TextureType = "2D";&gt;;texture cubeMap : Environment&lt;string Name = "CloudyHillsCubemap2.dds";string TextureType = "Cube";&gt;;sampler2D normalMapSampler = sampler_state{Texture = &lt;normalMap&gt;;#if 1// this is a trick from Halo - use point sampling for sparklesMagFilter = Linear;MinFilter = Point;MipFilter = None;#elseMagFilter = Linear;MinFilter = Linear;MipFilter = Linear;#endif    AddressU = Wrap;    AddressV = Wrap;};samplerCUBE envMapSampler = sampler_state{Texture = &lt;cubeMap&gt;;MinFilter = Linear;MagFilter = Linear;MipFilter = Linear;   AddressU = Wrap;   AddressV = Wrap;};float bumpHeight&lt;string UIWidget = "slider";float UIMin = 0.0; float UIMax = 2.0; float UIStep = 0.01;string UIName = "Bump Height";&gt; = 0.5;float2 textureScale&lt;    string UIName = "Texture scale";&gt; = { 8.0, 4.0 };float2 bumpSpeed&lt;    string UIName = "Bumpmap translation speed";&gt; = { -0.01, 0.0 };float fresnelBias&lt;    string UIName = "Fresnel bias";string UIWidget = "slider";float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.01;&gt; = 0.1;float fresnelPower&lt;    string UIName = "Fresnel exponent";string UIWidget = "slider";float UIMin = 1.0; float UIMax = 10.0; float UIStep = 0.01;&gt; = 4.0;float hdrMultiplier&lt;    string UIName = "HDR multiplier";string UIWidget = "slider";float UIMin = 0.0; float UIMax = 100.0; float UIStep = 0.01;&gt; = 4.0;float4 deepColor : Diffuse&lt;    string UIName = "Deep water color";&gt; = {0.0f, 0.1f, 0.2f, 1.0f};float4 shallowColor : Diffuse&lt;    string UIName = "Shallow water color";&gt; = {0.0f, 0.6f, 0.7f, 1.0f};float4 reflectionColor : Specular&lt;    string UIName = "Reflection color";&gt; = {0.9f, 1.0f, 0.45f, 1.0f};// these are redundant, but makes the ui easier:float reflectionAmount&lt;    string UIName = "Reflection amount";string UIWidget = "slider";    float UIMin = 0.0; float UIMax = 2.0; float UIStep = 0.01;    &gt; = 0.2f;float waterAmount&lt;    string UIName = "Water color amount";string UIWidget = "slider";    float UIMin = 0.0; float UIMax = 2.0; float UIStep = 0.01;    &gt; = 0.6f;float waveAmp&lt;    string UIName = "Wave amplitude";string UIWidget = "slider";float UIMin = 0.0; float UIMax = 10.0; float UIStep = 0.1;&gt; = 0.2;float waveFreq&lt;    string UIName = "Wave frequency";string UIWidget = "slider";float UIMin = 0.0; float UIMax = 1.0; float UIStep = 0.001;&gt; = 1.;struct a2v {float4 Position : POSITION;   // in object spacefloat2 TexCoord : TEXCOORD0;float3 Tangent  : TEXCOORD1;float3 Binormal : TEXCOORD2;float3 Normal   : NORMAL;};struct v2f {float4 Position  : POSITION;  // in clip spacefloat2 TexCoord  : TEXCOORD0;float3 TexCoord1 : TEXCOORD1; // first row of the 3x3 transform from tangent to cube spacefloat3 TexCoord2 : TEXCOORD2; // second row of the 3x3 transform from tangent to cube spacefloat3 TexCoord3 : TEXCOORD3; // third row of the 3x3 transform from tangent to cube spacefloat2 bumpCoord0 : TEXCOORD4;float2 bumpCoord1 : TEXCOORD5;float2 bumpCoord2 : TEXCOORD6;float3 eyeVector  : TEXCOORD7;};// wave functionsstruct Wave {  float freq;  // 2*PI / wavelength  float amp;   // amplitude  float phase; // speed * 2*PI / wavelength  float2 dir;};#define NWAVES 2Wave wave[NWAVES] = {{ 1.0, 1.0, 0.5, float2(-1, 0) },{ 2.0, 0.5, 1.3, float2(-0.7, 0.7) }};float evaluateWave(Wave w, float2 pos, float t){  return w.amp * sin( dot(w.dir, pos) * w.freq + t  * w.phase );}// derivative of wave functionfloat evaluateWaveDeriv(Wave w, float2 pos, float t){//  return w.amp * 1.0 ; //sin( /*dot(w.dir, pos) * w.freq +*/ t /* * w.phase */);  return w.freq * w.amp * cos( dot(w.dir, pos)*w.freq + t * w.phase );}// sharp wave functionsfloat evaluateWaveSharp(Wave w, float2 pos, float t, float k){  return w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k);}float evaluateWaveDerivSharp(Wave w, float2 pos, float t, float k){  return k*w.freq*w.amp * pow(sin( dot(w.dir, pos)*w.freq + t*w.phase)* 0.5 + 0.5 , k - 1) * cos( dot(w.dir, pos)*w.freq + t*w.phase);}v2f BumpReflectWaveVS(a2v IN,  uniform float4x4 WorldViewProj,  uniform float4x4 World,  uniform float4x4 ViewIT,  uniform float BumpScale,  uniform float2 textureScale,  uniform float2 bumpSpeed,  uniform float time,  uniform float waveFreq,  uniform float waveAmp   ){v2f OUT;// time = ptime;// time = time / 20;    wave[0].freq = waveFreq;    wave[0].amp = waveAmp;    wave[1].freq = waveFreq*2.0;    wave[1].amp = waveAmp*0.5;    float4 P = IN.Position;// sum wavesP.y = 0.0;float ddx = 0.0, ddy = 0.0;   for(int i=0; i&lt;NWAVES; i++) {    P.y += evaluateWave(wave, P.xz, time);//    float deriv = evaluateWaveDerivSharp(wave, P.xz, time, 0.01);    float deriv = evaluateWaveDeriv(wave, P.xz, time);    ddx += deriv * wave.dir.x;    ddy += deriv * wave.dir.y;    }// compute tangent basis   float3 B = float3(1, ddx, 0);   float3 T = float3(0, ddy, 1);   float3 N = float3(-ddx, 1, -ddy);OUT.Position = mul(P, WorldViewProj);// pass texture coordinates for fetching the normal mapOUT.TexCoord.xy = IN.TexCoord*textureScale;time = fmod(time, 100.0);OUT.bumpCoord0.xy = IN.TexCoord*textureScale + time*bumpSpeed;OUT.bumpCoord1.xy = IN.TexCoord*textureScale*1.0 + time*bumpSpeed*4.0;OUT.bumpCoord2.xy = IN.TexCoord*textureScale*2.0 + time*bumpSpeed*8.0;// compute the 3x3 tranform from tangent space to object spacefloat3x3 objToTangentSpace;// first rows are the tangent and binormal scaled by the bump scaleobjToTangentSpace[0] = BumpScale * normalize(T);objToTangentSpace[1] = BumpScale * normalize(B);objToTangentSpace[2] = normalize(N);OUT.TexCoord1.xyz = mul(objToTangentSpace, World[0].xyz);OUT.TexCoord2.xyz = mul(objToTangentSpace, World[1].xyz);OUT.TexCoord3.xyz = mul(objToTangentSpace, World[2].xyz);// compute the eye vector (going from shaded point to eye) in cube spacefloat4 worldPos = mul(P, World);OUT.eyeVector = ViewIT[3] - worldPos; // view inv. transpose contains eye position in world space in last rowreturn OUT;}// Pixel Shadersfloat4 BumpReflectPS20(v2f IN,   uniform sampler2D NormalMap,   uniform samplerCUBE EnvironmentMap) : COLOR{// fetch the bump normal from the normal mapfloat4 N = tex2D(NormalMap, IN.TexCoord.xy)*2.0 - 1.0;float3x3 m; // tangent to world matrixm[0] = IN.TexCoord1;m[1] = IN.TexCoord2;m[2] = IN.TexCoord3;float3 Nw = mul(m, N.xyz);//float3 E = float3(IN.TexCoord1.w, IN.TexCoord2.w, IN.TexCoord3.w);float3 E = IN.eyeVector;    float3 R = reflect(-E, Nw);return texCUBE(EnvironmentMap, R);}float4 OceanPS20(v2f IN, uniform sampler2D NormalMap, uniform samplerCUBE EnvironmentMap, uniform half4 deepColor, uniform half4 shallowColor, uniform half4 reflectionColor, uniform half4 reflectionAmount, uniform half4 waterAmount, uniform half fresnelPower, uniform half fresnelBias, uniform half hdrMultiplier ) : COLOR{// sum normal maps    half4 t0 = tex2D(NormalMap, IN.bumpCoord0.xy)*2.0-1.0;    half4 t1 = tex2D(NormalMap, IN.bumpCoord1.xy)*2.0-1.0;    half4 t2 = tex2D(NormalMap, IN.bumpCoord2.xy)*2.0-1.0;    half3 N = t0.xyz + t1.xyz + t2.xyz;//    half3 N = t1.xyz;    half3x3 m; // tangent to world matrix    m[0] = IN.TexCoord1;    m[1] = IN.TexCoord2;    m[2] = IN.TexCoord3;    half3 Nw = mul(m, N.xyz);    Nw = normalize(Nw);// reflection    float3 E = normalize(IN.eyeVector);    half3 R = reflect(-E, Nw);    half4 reflection = texCUBE(EnvironmentMap, R);    // hdr effect (multiplier in alpha channel)    reflection.rgb *= (1.0 + reflection.a*hdrMultiplier);// fresnel - could use 1D tex lookup for this    half facing = 1.0 - max(dot(E, Nw), 0);    half fresnel = fresnelBias + (1.0-fresnelBias)*pow(facing, fresnelPower);    half4 waterColor = lerp(deepColor, shallowColor, facing);    float4 newcol = waterColor*waterAmount + reflection*reflectionColor*reflectionAmount*fresnel;    newcol.a=0.9;    return newcol;}technique PS20{pass p0{VertexShader = compile vs_2_0 BumpReflectWaveVS(wvpMatrix, worldMatrix, viewInverseMatrix, bumpHeight, textureScale, bumpSpeed, time, waveFreq, waveAmp);Zenable = true;ZWriteEnable = true;CullMode = None;//PixelShader = compile ps_2_0 BumpReflectPS20(normalMapSampler, envMapSampler);PixelShader = compile ps_2_0 OceanPS20(normalMapSampler, envMapSampler,  deepColor, shallowColor, reflectionColor, reflectionAmount, waterAmount,  fresnelPower, fresnelBias, hdrMultiplier);}}what's UIWidget? what kind of data should I pass to this effect? can I work it with my d3d 9 app? what kind of Decleration should I use for it? how should I set textures, matrices and other values? I've seen it work great in a simple demo in DarkBASIC engine, and there wasn't any kind of parameter passed to it, does it work with default params? I've worked with effects and shader files, but this one is a bit strange, please guide me in the way of using this really nice effect.thanks in advanceEDIT: Please use [source] tags for large blocks of code...[Edited by - jollyjeffers on May 31, 2006 7:39:21 AM]
  You might want to look up "DirectX Standard Annotations and Semantics" in the SDK documentation. The various "UI.." parts seem to be a SAS-like (but not quite) set of annotations. Presumably these are compatable with some Nvidia framework (e.g. FXComposer).If you're not hooking this up to a GUI (e.g. your program provides all the settings, not the user) then you can probably ignore the majority of the annotations in that effect.By the looks of things it provides defaults for all the uniform variables, so you should be able to use it as-is straight from the technique.Try it and see! As long as you check for any/every error message and return code you should be able to isolate any problem spots. If you can't fix them then post back and ask us...Jack   ;  hithanks jack for your answer, I've tested this effect on a plane (as my ocean) but surprisingly there's nothing rendered on the screen, I got no errors during effect creation.how can I detect default values in the shader? what about the declaration? (POS, COORD0, COORD1, COORD2, NORM) is this correct, I should just create it and render my plane using this effect? It should be great, what may be the possible reasons for this problem (i.e. there's nothing rendered)?thanks again   ;  hi againI've another question, and it's about textures and setting them, is there any need to create and set textures in code? two textures (waves2.dds & CloudyHillsCubemap2.dds) have been called in this effect, should I create them in code? should I set them? what should I do to change the default values? effect->SetValue? what kind of values should I set.please help me, thanks   ;Quote:Original post by niazwarehi againI've another question, and it's about textures and setting them, is there any need to create and set textures in code? two textures (waves2.dds & CloudyHillsCubemap2.dds) have been called in this effect, should I create them in code? should I set them? what should I do to change the default values? effect->SetValue? what kind of values should I set.please help me, thanksYes. You should use the SetTexture method of the effect. The texture filenames in the sahder are just for the shader tools/to show you what they should be.I normally remove all the extra export 'junk' :).