title: What's wrong with my soft-shadow code?
body: <p>I'm trying to write a simple raytracer as a hobby project and it's all working fine now, except I can't get soft-shadows to work at all. My idea of soft-shadows is that the lightsource is considered to have a location and a radius. To do a shadow test on this light I take the point where the primary ray hit an object in the scene and cast an n-amount of rays towards the lightsource where each new ray has a random component to every axis, where the random component varies between -radius and radius. </p>
  
  <p>If such a ray hits an object in the scene, I increment a hitcounter (if a ray hits multiple objects, it still only increments with one). If it makes it to the lightsource without collisions, I add the distance of the primary ray's intersect point to the lightsource's center to a variable.</p>
  
  <p>When n samples have been taken, I calculate the ratio of rays that have collided and multiply the color of the light by this ratio (so a light with color 1000,1000,1000 will become 500,500,500 with a ratio of 0.5, where half the rays have collided). Then I calculate the average distance to the lightsource by dividing the distance variable of earlier by the amount of non-colliding rays. I return that variable and the function exits.</p>
  
  <p>The problem is: it doesn't work. Not quite at least. What it looks like can be seen <a href="http://img16.imageshack.us/img16/5376/719sv8.png" rel="nofollow noreferrer">here</a>. You can see it sort of resembles soft-shadows, if you squint real hard. </p>
  
  <p>I don't get it, am I making some sort of fundamental flaw here, or is it something tiny? I'm fairly sure the problem is in this method, because when I count the number of partially lit pixels produced directly by this method, there are only about 250, when there should be a lot more. And when you look closely at the picture, you can see there's some partially lit pixels, suggesting the rest of the code processes the partially lit pixels just fine. </p>
  
  <p>Here's the actual light for soft-shadows class:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  namespace MyFirstRayTracer
  {
    public class AreaLight : ILight
    {
      private const int _radius = 5;
      private const int _samples = 16;
      public Color Color { get; set; }
      public Vector Location { get; set; }
      #region ILight Members
  
      public float GetLightingInformation(Vector point, ISceneObject[] scene, out Color color)
      {
        int intersectCount = 0;
        float distance = -1;
        for(int i = 0; i &lt; _samples; i++)
        {
      bool intersects = false;
      float rand = 0;
      rand = _radius - (float)(new Random().NextDouble()*(2*_radius));
      foreach (ISceneObject obj in scene)
      {
        Vector iPoint;
  
        Vector loc = new Vector(Location.X + rand, Location.Y + rand, Location.Z + rand);
  
        if (!obj.Intersect(new Ray(point, loc), out iPoint))
        {
          distance += (Location - point).SqLength;
  
        }
        else
        {
          intersects = true;
          distance -= (Location - point).SqLength;
        }
      }
      if (intersects)
        intersectCount++;
        }
        float factor = 1-((float)intersectCount/_samples);
  
        color = new Color(factor*Color.R, factor*Color.G, factor*Color.B);
  
        return (float)Math.Sqrt(distance / (_samples - intersectCount));
      }
  
  
      #endregion
    }
  }
  </code></pre>