home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2006 March / Gamestar_82_2006-03_dvd.iso / Dema / ankh_demo_en.exe / media / shared / programs / water.cg.c < prev   
C/C++ Source or Header  |  2005-08-09  |  2KB  |  107 lines

  1. // Expand a range-compressed vector
  2. float3 expand(float3 v)
  3. {
  4.   return (v - 0.5) * 2;
  5. }
  6.  
  7. // Expand a range-compressed vector
  8. float2 expand2(float2 v)
  9. {
  10.   return (v - 0.5) * 2;
  11. }
  12.  
  13. // define inputs from application
  14. struct appin
  15. {
  16.   float4 vertexPos     : POSITION;
  17.   float4 normal        : NORMAL;
  18.   float4 diffuseCoords : TEXCOORD0;
  19. };
  20.  
  21. // define outputs from vertex shader
  22. struct vertout
  23. {
  24.   float4 HPosition         : POSITION;
  25.   float4 diffuseMapCoords  : TEXCOORD0;
  26.   float4 reflCoords        : TEXCOORD1;
  27.  
  28.   float4 vertexColor       : COLOR;
  29. };
  30.  
  31. vertout vp_main(appin IN,
  32.         uniform float4x4 MVPMatrix,
  33.         uniform float tiling,
  34.         uniform float4 color
  35.         )
  36. {
  37.   vertout OUT;
  38.  
  39.   // transform vertex position into homogenous clip-space
  40.   OUT.HPosition = mul(MVPMatrix, IN.vertexPos);
  41.   OUT.diffuseMapCoords = IN.diffuseCoords*tiling;
  42.  
  43.   OUT.vertexColor = color;
  44.  
  45.   // Projective texture coordinates, adjust for mapping
  46.   float4x4 scalemat = float4x4(0.5,   0,   0, 0.5, 
  47.                                0,-0.5,   0, 0.5,
  48.                                0,   0, 0.5, 0.5,
  49.                                0,   0,   0,   1);
  50.  
  51.   OUT.reflCoords = mul(scalemat, OUT.HPosition);
  52.  
  53.   return OUT;
  54. }
  55.  
  56. //////////////////
  57.  
  58. // define inputs from vertex program
  59. struct in_fragment
  60. {
  61.   float4 color     : COLOR0;
  62.   float4 texCoord0 : TEXCOORD0;
  63.   float4 texCoord1 : TEXCOORD1;
  64. };
  65.  
  66. // define outputs from fragment program
  67. struct out_fragment
  68. {
  69.   float4 color : COLOR0;
  70. };
  71.  
  72. // fragment program
  73. out_fragment fp_main
  74. (
  75.  // varying parameters
  76.  in_fragment f_in,
  77.  
  78.  // uniform samplers (textures)
  79.  uniform sampler2D reflMap : TEXUNIT0,
  80.  uniform sampler2D bumpMap : TEXUNIT1,
  81.  
  82.  // modulate value for the final color output
  83.  uniform float uTime,
  84.  uniform float distortFactor
  85.  )
  86. {
  87.   out_fragment f_out;
  88.   
  89.   float2 final = f_in.texCoord1.xy / f_in.texCoord1.w;
  90.  
  91.   // apply distortion/ripple
  92.   //f_in.texCoord0.x += uTime;// * 0.05;
  93.   float3 normalTex  = tex2D(bumpMap, f_in.texCoord0.xy).xyz;
  94.   float3 normal     = expand(normalTex);
  95.  
  96.   final.xy += (normal.xy*distortFactor);
  97.  
  98.   float4 reflColor = tex2D(reflMap, final);
  99.  
  100.   f_out.color = f_in.color;
  101.  
  102.   f_out.color   += reflColor*0.25;
  103.   f_out.color.a  = f_in.color.a;
  104.  
  105.   return f_out;
  106. }
  107.