home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Multimedia / MPC-HC / MPC-HC_Portable.exe / MPC-HC_Portable / Shaders / Sphere.hlsl < prev    next >
Text File  |  2014-10-05  |  2KB  |  74 lines

  1. /*
  2.  * (C) 2003-2006 Gabest
  3.  * (C) 2006-2013 see Authors.txt
  4.  *
  5.  * This file is part of MPC-HC.
  6.  *
  7.  * MPC-HC is free software; you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License as published by
  9.  * the Free Software Foundation; either version 3 of the License, or
  10.  * (at your option) any later version.
  11.  *
  12.  * MPC-HC is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  * GNU General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  *
  20.  */
  21.  
  22. sampler s0 : register(s0);
  23. float4 p0 :  register(c0);
  24.  
  25. #define clock (p0[3])
  26. #define PI acos(-1)
  27.  
  28. float4 main(float2 tex : TEXCOORD0) : COLOR
  29. {
  30.     // - this is a very simple raytracer, one sphere only
  31.     // - no reflection or refraction, yet (my ati 9800 has a 64 + 32 instruction limit...)
  32.  
  33.     float3 pl = float3(3, -3, -4); // light pos
  34.     float4 cl = 0.4; // light color
  35.  
  36.     float3 pc = float3(0, 0, -1);  // cam pos
  37.     float3 ps = float3(0, 0, 0.5); // sphere pos
  38.     float r = 0.65; // sphere radius
  39.  
  40.     float3 pd = normalize(float3(tex.x - 0.5, tex.y - 0.5, 0) - pc);
  41.  
  42.     float A = 1;
  43.     float B = 2 * dot(pd, pc - ps);
  44.     float C = dot(pc - ps, pc - ps) - r * r;
  45.     float D = B * B - 4 * A * C;
  46.  
  47.     float4 c0 = 0;
  48.  
  49.     if (D >= 0) {
  50.         // t2 is the smaller, obviously...
  51.         // float t1 = (-B + sqrt(D)) / (2 * A);
  52.         // float t2 = (-B - sqrt(D)) / (2 * A);
  53.         // float t = min(t1, t2);
  54.  
  55.         float t = (-B - sqrt(D)) / (2 * A);
  56.  
  57.         // intersection data
  58.         float3 p = pc + pd * t;
  59.         float3 n = normalize(p  - ps);
  60.         float3 l = normalize(pl - p);
  61.  
  62.         // mapping the image onto the sphere
  63.         tex = acos(-n) / PI;
  64.  
  65.         // rotate it
  66.         tex.x = frac(tex.x + frac(clock / 10));
  67.  
  68.         // diffuse + specular
  69.         c0 = tex2D(s0, tex) * dot(n, l) + cl * pow(max(dot(l, reflect(pd, n)), 0), 50);
  70.     }
  71.  
  72.     return c0;
  73. }
  74.