Пример #1
0
        public Computation(Intersection i, Ray r)
        {
            T        = i.T;
            Object   = i.Object;
            Material = Object.Material;
            Shape parent = Object.Parent;

            while (parent != null)
            {
                Material = parent.Material;
                parent   = parent.Parent;
            }

            Point   = r.Position(T);
            Eyev    = -r.Direction;
            Normalv = Object.NormalAt(Point, i);

            if (Eyev.Dot(Normalv) < 0)
            {
                Inside  = true;
                Normalv = -Normalv;
            }
            else
            {
                Inside = false;
            }

            OverPoint  = Point + Normalv * Constants.overPointEps;
            UnderPoint = Point - Normalv * Constants.floatEps;
            Reflectv   = r.Direction.Reflect(Normalv);
        }
Пример #2
0
        public override Color Sample(int x, int y, World world)
        {
            Color color = Color.Black;

            Ray   ray        = this.RayForPixel(x, y);
            Point focalPoint = ray.Position(this.focalLength);

            for (int i = 0; i < this.numSamples; i++)
            {
                Vector apertureOffset = RandomInUnitDisk() * this.aperture;

                Point  newOrigin    = ray.Origin + apertureOffset;
                Vector direction    = (focalPoint - newOrigin).Normalize();
                Ray    secondaryRay = new Ray(newOrigin, direction, RayType.Primary);

                Interlocked.Increment(ref Stats.PrimaryRays);
                color += world.ColorAt(secondaryRay);
            }

            return(color / (double)numSamples);
        }
        public Computation PrepareComputations(Ray ray, List <Intersection> xs = null)
        {
            if (xs == null)
            {
                xs = new List <Intersection> {
                    this
                };
            }

            var comp = new Computation
            {
                T      = this.T,
                Object = this.Object
            };

            comp.Point   = ray.Position(comp.T);
            comp.EyeV    = (-ray.Direction).ToVector();
            comp.NormalV = comp.Object.NormalAt(comp.Point);

            if (comp.NormalV.Dot(comp.EyeV) < 0)
            {
                comp.Inside  = true;
                comp.NormalV = comp.NormalV.Negate().ToVector();
            }
            else
            {
                comp.Inside = false;
            }

            comp.ReflectV   = ray.Direction.Reflect(comp.NormalV);
            comp.OverPoint  = (comp.Point + comp.NormalV * (DoubleUtils.Epsilon)).ToPoint();
            comp.UnderPoint = (comp.Point - comp.NormalV * (DoubleUtils.Epsilon)).ToPoint();


            var containers = new List <ISceneObject>();

            foreach (var intersection in xs)
            {
                if (intersection.Equals(this))
                {
                    if (!containers.Any())
                    {
                        comp.N1 = 1.0;
                    }
                    else
                    {
                        var last = containers.Last();
                        comp.N1 = last.Material.RefractiveIndex;
                    }
                }

                if (containers.Contains(intersection.Object))
                {
                    containers.Remove(intersection.Object);
                }
                else
                {
                    containers.Add(intersection.Object);
                }

                if (intersection == this)
                {
                    if (!containers.Any())
                    {
                        comp.N2 = 1.0;
                    }
                    else
                    {
                        var last = containers.Last();
                        comp.N2 = last.Material.RefractiveIndex;
                    }

                    break;
                }
            }

            return(comp);
        }
Пример #4
0
        protected override bool CheckCap(Ray r, float t)
        {
            Tuple pos = r.Position(t);

            return((pos.X * pos.X - pos.Y * pos.Y + pos.Z * pos.Z) < Constants.floatEps);
        }
Пример #5
0
        public Comps PrepareComputations(Ray ray, List <Intersection> xs)
        {
            Comps comps;

            comps.n1     = 0.0;
            comps.n2     = 0.0;
            comps.Time   = this.Time;
            comps.Object = this.Object;
            comps.Point  = ray.Position(comps.Time);
            comps.Eye    = -ray.Direction;
            if (!xs.Any())
            {
                comps.Normal = comps.Object.NormalAt(comps.Point);
            }
            else
            {
                comps.Normal = comps.Object.NormalAt(comps.Point, xs[0]);
            }

            if (comps.Normal.Dot(comps.Eye) < 0)
            {
                comps.Inside = true;
                comps.Normal = -comps.Normal;
            }
            else
            {
                comps.Inside = false;
            }

            comps.Reflect = ray.Direction.Reflect(comps.Normal);

            var 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.Object))
                {
                    containers.Remove(i.Object);
                }
                else
                {
                    containers.Add(i.Object);
                }

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

            comps.OverPoint  = comps.Point + comps.Normal * EPSILON;
            comps.UnderPoint = comps.Point - comps.Normal * EPSILON;

            return(comps);
        }
Пример #6
0
        protected virtual bool CheckCap(Ray r, float t)
        {
            Tuple pos = r.Position(t);

            return((pos.X * pos.X + pos.Z * pos.Z) < (1 + Constants.floatEps));
        }