home *** CD-ROM | disk | FTP | other *** search
/ Chip 2011 November / CHIP_2011_11.iso / Programy / Narzedzia / Aplikacje_64-bitowe / Daum_PotPlayer / PotPlayer1.5.29332-x64.EXE / PxShader / Sphere.txt < prev    next >
Text File  |  2010-05-20  |  1KB  |  58 lines

  1. // sphere=ps_2_0
  2. // Code from MPC
  3.  
  4. sampler s0 : register(s0);
  5. float4 p0 : register(c0);
  6.  
  7. #define clock (p0[3])
  8.  
  9. #define PI acos(-1)
  10.  
  11. float4 main(float2 tex : TEXCOORD0) : COLOR
  12. {
  13.     // - this is a very simple raytracer, one sphere only
  14.     // - no reflection or refraction, yet (my ati 9800 has a 64 + 32 instruction limit...)
  15.     
  16.     float3 pl = float3(3,-3,-4); // light pos
  17.     float4 cl = 0.4; // light color
  18.     
  19.     float3 pc = float3(0,0,-1); // cam pos
  20.     float3 ps = float3(0,0,0.5); // sphere pos
  21.     float r = 0.65; // sphere radius
  22.     
  23.     float3 pd = normalize(float3(tex.x-0.5, tex.y-0.5, 0) - pc);
  24.     
  25.     float A = 1;
  26.     float B = 2*dot(pd, pc - ps);
  27.     float C = dot(pc - ps, pc - ps) - r*r;
  28.     float D = B*B - 4*A*C;
  29.     
  30.     float4 c0 = 0;
  31.     
  32.     if(D >= 0)
  33.     {
  34.         // t2 is the smaller, obviously...
  35.         // float t1 = (-B + sqrt(D)) / (2*A);
  36.         // float t2 = (-B - sqrt(D)) / (2*A);
  37.         // float t = min(t1, t2); 
  38.         
  39.         float t = (-B - sqrt(D)) / (2*A);
  40.         
  41.         // intersection data
  42.         float3 p = pc + pd*t;
  43.         float3 n = normalize(p - ps);
  44.         float3 l = normalize(pl - p);
  45.         
  46.         // mapping the image onto the sphere
  47.         tex = acos(-n)/PI; 
  48.         
  49.         // rotate it
  50.         tex.x = frac(tex.x + frac(clock/10));
  51.         
  52.         // diffuse + specular
  53.         c0 = tex2D(s0, tex) * dot(n, l) + cl * pow(max(dot(l, reflect(pd, n)), 0), 50);
  54.     }
  55.     
  56.     return c0;
  57. }
  58.