Exemplo n.º 1
0
        private IEnumerable <ISect> Intersections(Ray ray, Scene scene)
        {
            List <ISect> ret = new List <ISect>();

            // loop over every thing in the scene
            foreach (SceneObject t in scene.Things)
            {
                // see if the thing intersects with the ray
                ISect intersect = t.Intersect(ray);
                // if it does...
                if (intersect != null)
                {
                    // ... add it to our list (ordered by distance, ascending)
                    bool added = false;
                    for (int i = 0; i < ret.Count; i++)
                    {
                        if (ret[i].Dist > intersect.Dist)
                        {
                            ret.Insert(i, intersect);
                            added = true;
                            break;
                        }
                    }
                    if (!added)
                    {
                        // add as last element in the list
                        ret.Add(intersect);
                    }
                }
            }
            return(ret);
        }
Exemplo n.º 2
0
        private Color TraceRay(Ray ray, Scene scene, int depth)
        {
            IEnumerable <ISect> isects = Intersections(ray, scene);
            ISect isect = isects.FirstOrDefault();

            if (isect == null)
            {
                return(Color.Background);
            }
            return(Shade(isect, scene, depth));
        }
Exemplo n.º 3
0
        private double TestRay(Ray ray, Scene scene)
        {
            IEnumerable <ISect> isects = Intersections(ray, scene);
            ISect isect = isects.FirstOrDefault();

            if (isect == null)
            {
                return(0);
            }
            return(isect.Dist);
        }
Exemplo n.º 4
0
        private Color Shade(ISect isect, Scene scene, int depth)
        {
            Vector d          = isect.Ray.Dir;
            Vector pos        = Vector.Plus(Vector.Times(isect.Dist, isect.Ray.Dir), isect.Ray.Start);
            Vector normal     = isect.Thing.Normal(pos);
            Vector reflectDir = Vector.Minus(d, Vector.Times(2 * Vector.Dot(normal, d), normal));
            Color  ret        = Color.DefaultColor;

            ret = Color.Plus(ret, GetNaturalColor(isect.Thing, pos, normal, reflectDir, scene));
            if (depth >= MaxDepth)
            {
                return(Color.Plus(ret, Color.Make(.5, .5, .5)));
            }
            return(Color.Plus(ret, GetReflectionColor(isect.Thing, Vector.Plus(pos, Vector.Times(.001, reflectDir)), normal, reflectDir, scene, depth)));
        }