Exemplo n.º 1
0
        private void IntersectCaps(Ray r, Intersections xs)
        {
            if (!this.Closed || Math.Abs(r.Direction.y) < EPSILON)
            {
                return;
            }

            foreach (var v in new double[] { Minimum, Maximum })
            {
                var t = (v - r.Origin.y) / r.Direction.y;
                if (CheckCap(r, t, Math.Abs(v)))
                {
                    xs.Add(new Intersection(t, this));
                }
            }
        }
        public Computation PrepareComputations(Ray ray, Intersections xs)
        {
            var comps = new Computation();

            comps.T     = T;
            comps.Shape = Shape;

            comps.Point = ray.Position(T);
            comps.EyeV  = -ray.Direction;

            comps.NormalV  = Shape.NormalAt(comps.Point, this);
            comps.ReflectV = ray.Direction.ReflectOn(comps.NormalV);

            if (Tuple.Dot(comps.NormalV, comps.EyeV) < 0)
            {
                comps.Inside  = true;
                comps.NormalV = -comps.NormalV;
            }
            comps.OverPoint  = (comps.Point + comps.NormalV * EPSILON).ToPoint();
            comps.UnderPoint = (comps.Point - comps.NormalV * EPSILON).ToPoint();

            List <Shape> containers = new List <Shape>();

            foreach (var i in xs)
            {
                if (i == this)
                {
                    if (containers.Count == 0)
                    {
                        comps.N1 = 1.0;
                    }
                    else
                    {
                        comps.N1 = containers.Last().Material.RefractiveIndex;
                    }
                }

                if (containers.Contains(i.Shape))
                {
                    containers.Remove(i.Shape);
                }
                else
                {
                    containers.Add(i.Shape);
                }

                if (i == this)
                {
                    if (containers.Count == 0)
                    {
                        comps.N2 = 1.0;
                    }
                    else
                    {
                        comps.N2 = containers.Last().Material.RefractiveIndex;
                    }
                    break;
                }
            }

            return(comps);
        }