This is the initial intersection with the scene. We send out a ray from our origin point with a direction, and in return we get a t (ray length) and sphere (defined by a vec4) as well as an identifier for which sphere was hit. If nothing is hit, we’ll get -1.0 for this identifier.

If we determine that we’ve hit something, we’ll figure out exactly where we hit it and calculate Lambertian reflectance for that point.

//check to see if this point is in shadowvec2 shadowT;vec4 shadowHit;//check for intersect between the sphere and the lightfloat shadowId = intersect(pos,normalize(light-pos), shadowT, shadowHit);//if we have a non-negative id, we've hit something other than the lightif(shadowId >=0.0){
col =vec4(0.0);}}

Then, we try to intersect the scene from our original intersection point with a ray in the direction toward the light. If we hit something, our point is in shadow, so we’ll replace the color with black, which will give us a hard shadow.

//If we hit the lightelseif(id ==-2.0){
col =vec4(1.0);}

Finally, we’ll add the light to the scene as its own special case, making it totally white. And that’s it!