import myContext
VERTEX_SHADER = ("""
void main() {
gl_Position = ftransform();
}
""")
FRAGMENT_SHADER = ("""
uniform sampler2D webcam;
uniform sampler2D tex0;
uniform vec2 resolution;
uniform vec2 mouse_pos;
uniform float time;
const float maxDist = 10.0;
const float incrFract = 0.01;
const vec3 cameraPos = vec3( 0,1,-10 );
const vec3 sphereCenter = vec3( 0,0,2 );
const float sphereRadius = 1.0;
const vec3 lightSource = vec3( -10,10,-10 );
float fog = 0.98;
vec3 rayOrigin;
vec3 rayDirection;
vec3 rayIncident;
vec3 incidentNormal;
vec3 reflectDirection;
float lightDiffuse;
float lightSpecular;
float lightAmbient;
bool sphereCoincident( vec3 pos )
{
vec3 disp = pos-sphereCenter;
float dist = length(disp);
if(dist<=sphereRadius)
{
//Solve for intersection by quadratic formula
vec3 sphereCenterRel = sphereCenter-rayOrigin;
float b = dot(rayDirection,sphereCenterRel);
float rayDist = b-sqrt(length(b)-length(sphereCenterRel)+pow(sphereRadius,2));
rayIncident = rayDist*rayDirection+rayOrigin;
incidentNormal = normalize( rayIncident - sphereCenter );
reflectDirection = reflect(rayDirection,incidentNormal);
return true;
}
else {
return false;
}
}
void lighting()
{
vec3 disp = lightSource-rayIncident;
vec3 dir = normalize(disp);
float dist = length(disp);
float attenuation = pow( fog, dist );
float brightness = attenuation*dot(dir,incidentNormal);
brightness = max(brightness,0.0);
float reflection = dot(dir,reflectDirection);
reflection = max(reflection,0.0);
lightAmbient = attenuation;
lightDiffuse = brightness;
lightSpecular = brightness*reflection;
}
bool castRay()
{
float delt = maxDist*incrFract*10;
for( float t = 0.01; t < maxDist; t += delt )
{
vec3 pos = rayOrigin + rayDirection*t;
if( sphereCoincident( pos ) )
{
// interpolate the intersection distance
return true;
}
// allow the error to be proportional to the distance
delt = incrFract*t;
}
return false;
}
void main(void)
{
vec2 uv = gl_FragCoord.xy/resolution;
rayOrigin = vec3( 2*(uv.x-0.5)*0.8/0.7, 2*(uv.y-0.5)*0.6/0.7+1, -5 );
rayDirection = normalize(rayOrigin - cameraPos);
if( castRay() )
{
lighting();
float red = 0.8*lightAmbient+0.4*lightDiffuse+1.0*lightSpecular;
float green = 0.3*lightDiffuse+0.4*lightSpecular;
float blue = 0.3*lightDiffuse+0.4*lightSpecular;
gl_FragColor = vec4(red,green,blue,1);
} else {
gl_FragColor = vec4(texture2D(webcam,uv).xyz,1);
}
}
""")
TEXTURES = []
myContext.RunShaders( VERTEX_SHADER, FRAGMENT_SHADER, TEXTURES )