Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: Python  |  size: 2.83 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. import myContext
  2.  
  3. VERTEX_SHADER = ("""
  4.  
  5. void main() {
  6.    gl_Position = ftransform();
  7. }
  8. """)
  9.  
  10.  
  11.  
  12. FRAGMENT_SHADER = ("""
  13.  
  14. uniform sampler2D webcam;
  15. uniform sampler2D tex0;
  16. uniform vec2 resolution;
  17. uniform vec2 mouse_pos;
  18. uniform float time;
  19.  
  20. const float maxDist = 10.0;
  21. const float incrFract = 0.01;
  22.  
  23. const vec3 cameraPos = vec3( 0,1,-10 );
  24.  
  25. const vec3 sphereCenter = vec3( 0,0,2 );
  26. const float sphereRadius = 1.0;
  27.  
  28. const vec3 lightSource = vec3( -10,10,-10 );
  29. float fog = 0.98;
  30.  
  31. vec3 rayOrigin;
  32. vec3 rayDirection;
  33. vec3 rayIncident;
  34. vec3 incidentNormal;
  35. vec3 reflectDirection;
  36.  
  37. float lightDiffuse;
  38. float lightSpecular;
  39. float lightAmbient;
  40.  
  41.  
  42. bool sphereCoincident( vec3 pos )
  43. {
  44.   vec3 disp = pos-sphereCenter;
  45.   float dist = length(disp);
  46.   if(dist<=sphereRadius)
  47.   {
  48.      //Solve for intersection by quadratic formula
  49.      vec3 sphereCenterRel = sphereCenter-rayOrigin;
  50.      float b = dot(rayDirection,sphereCenterRel);
  51.      float rayDist = b-sqrt(length(b)-length(sphereCenterRel)+pow(sphereRadius,2));
  52.      
  53.      rayIncident = rayDist*rayDirection+rayOrigin;
  54.      incidentNormal = normalize( rayIncident - sphereCenter );
  55.      reflectDirection = reflect(rayDirection,incidentNormal);
  56.      return true;
  57.   }
  58.   else {
  59.      return false;
  60.   }
  61. }
  62.  
  63.  
  64. void lighting()
  65. {
  66.   vec3 disp = lightSource-rayIncident;
  67.   vec3 dir = normalize(disp);
  68.   float dist = length(disp);
  69.   float attenuation = pow( fog, dist );
  70.   float brightness = attenuation*dot(dir,incidentNormal);
  71.   brightness = max(brightness,0.0);
  72.   float reflection = dot(dir,reflectDirection);
  73.   reflection = max(reflection,0.0);
  74.  
  75.   lightAmbient = attenuation;
  76.   lightDiffuse = brightness;
  77.   lightSpecular = brightness*reflection;
  78. }
  79.  
  80.  
  81.    
  82.  
  83. bool castRay()
  84. {
  85.    float delt = maxDist*incrFract*10;
  86.    for( float t = 0.01; t < maxDist; t += delt )
  87.    {
  88.        vec3  pos = rayOrigin + rayDirection*t;
  89.        if( sphereCoincident( pos ) )
  90.        {
  91.            // interpolate the intersection distance
  92.            return true;
  93.        }
  94.        // allow the error to be proportional to the distance
  95.        delt = incrFract*t;
  96.    }
  97.    return false;
  98. }
  99.  
  100.  
  101. void main(void)
  102. {
  103.    vec2 uv = gl_FragCoord.xy/resolution;
  104.    
  105.    rayOrigin = vec3( 2*(uv.x-0.5)*0.8/0.7, 2*(uv.y-0.5)*0.6/0.7+1, -5 );
  106.    rayDirection = normalize(rayOrigin - cameraPos);
  107.  
  108.    if( castRay() )
  109.    {
  110.       lighting();
  111.       float red = 0.8*lightAmbient+0.4*lightDiffuse+1.0*lightSpecular;
  112.       float green = 0.3*lightDiffuse+0.4*lightSpecular;
  113.       float blue = 0.3*lightDiffuse+0.4*lightSpecular;
  114.       gl_FragColor = vec4(red,green,blue,1);
  115.    } else {
  116.       gl_FragColor = vec4(texture2D(webcam,uv).xyz,1);
  117.    }
  118. }
  119. """)
  120.  
  121. TEXTURES = []
  122.  
  123. myContext.RunShaders( VERTEX_SHADER, FRAGMENT_SHADER, TEXTURES )