Ejemplo n.º 1
0
        public void T14_FindRefractedColor()
        {
            if (Scene.current == null)
            {
                new Scene();
            }

            Scene.current.Default();

            RayObject a = Scene.current.root.GetChildren()[0];

            a.material.Ambient = 1.0;
            a.material.pattern = new Patterns.TestPattern();

            RayObject b = Scene.current.root.GetChildren()[1];

            b.material.Transparency = 1.0;
            b.material.RefracIndex  = 1.5;

            Ray ray = new Ray(new Point(0, 0, 0.1), new Vector(0, 1, 0));
            List <Intersection> xs = new List <Intersection>();

            xs.Add(new Intersection(a, -0.9899));
            xs.Add(new Intersection(b, -0.4899));
            xs.Add(new Intersection(b, 0.4899));
            xs.Add(new Intersection(a, 0.9899));

            Computations c = Computations.Prepare(xs[2], ray, xs);

            Color color = Scene.current.RefractedColor(c, 5);

            Assert.AreEqual(new Color(0, 0.99888, 0.04725), color);
        }
Ejemplo n.º 2
0
        public void T06_ShadeHitIntersectionInShadow()
        {
            if (Scene.current == null)
            {
                new Scene();
            }

            Scene.current.Clear();

            Light  light = new Light(new Point(0, 0, -10), new Color(1, 1, 1));
            Sphere s1    = new Sphere();
            Sphere s2    = new Sphere();

            s2.SetMatrix(Mat4.TranslateMatrix(0, 0, 10));

            Ray ray = new Ray(new Point(0, 0, 5), new Vector(0, 0, 1));

            Intersection i = new Intersection(s2, 4);

            Computations c = Computations.Prepare(i, ray, null);

            Color color = Scene.current.ShadeHit(c);

            Assert.AreEqual(new Color(0.1f, 0.1f, 0.1f), color);
        }
        public void ShouldFindN1AndN2AtIntersection(int index, float n1, float n2)
        {
            var a = Sphere.CreateGlass();

            a.Transform = CreateScale(2, 2, 2);
            a.Material.RefractiveIndex = 1.5f;

            var b = Sphere.CreateGlass();

            b.Transform = CreateTranslation(0, 0, -0.25f);
            b.Material.RefractiveIndex = 2f;

            var c = Sphere.CreateGlass();

            c.Transform = CreateTranslation(0, 0, 0.25f);
            c.Material.RefractiveIndex = 2.5f;

            var r  = new Ray(CreatePoint(0, 0, -4f), CreateVector(0, 0, 1));
            var xs = new List <Intersection>
            {
                new Intersection(2, a),
                new Intersection(2.75f, b),
                new Intersection(3.25f, c),
                new Intersection(4.75f, b),
                new Intersection(5.25f, c),
                new Intersection(6, a),
            };

            var comps = Computations.Prepare(xs[index], r, xs);

            comps.N1.Should().BeApproximately(n1, Tolerance);
            comps.N2.Should().BeApproximately(n2, Tolerance);
        }
Ejemplo n.º 4
0
        public void T19_CombiningReflectionAndRefraction()
        {
            if (Scene.current == null)
            {
                new Scene();
            }

            Scene.current.Default();

            Plane plane = new Plane();

            plane.SetMatrix(Mat4.TranslateMatrix(0, -1, 0));
            plane.material.Reflective   = 0.5;
            plane.material.Transparency = 0.5;
            plane.material.RefracIndex  = 1.5;

            Sphere ball = new Sphere();

            ball.material.color   = new Color(1, 0, 0);
            ball.material.Ambient = 0.5;
            ball.SetMatrix(Mat4.TranslateMatrix(0, -3.5, -0.5));

            Ray r = new Ray(new Point(0, 0, -3), new Vector(0, Math.Sqrt(2) / -2,
                                                            Math.Sqrt(2) / 2));
            List <Intersection> xs = new List <Intersection>();

            xs.Add(new Intersection(plane, Math.Sqrt(2)));

            Computations c = Computations.Prepare(xs[0], r, xs);

            Color color = Scene.current.ShadeHit(c, 5);

            Assert.AreEqual(new Color(0.93391, 0.69643, 0.69243), color);
        }
Ejemplo n.º 5
0
        public void T16_ReflectanceUnderTotalInteralReflection()
        {
            if (Scene.current == null)
            {
                new Scene();
            }

            Scene.current.Clear();

            Sphere sphere = new Sphere();

            sphere.material.Glassy();

            Ray ray = new Ray(new Point(0, 0, Math.Sqrt(2) / 2),
                              new Vector(0, 1, 0));

            List <Intersection> xs = new List <Intersection>();

            xs.Add(new Intersection(sphere, Math.Sqrt(2) / -2));
            xs.Add(new Intersection(sphere, Math.Sqrt(2) / 2));

            Computations c = Computations.Prepare(xs[1], ray, xs);

            double reflectance = Scene.current.Schlick(c);

            Assert.IsTrue(Utility.FE(1.0, reflectance));
        }
Ejemplo n.º 6
0
        public void T17_ReflectanceOfPerpRay()
        {
            if (Scene.current == null)
            {
                new Scene();
            }

            Scene.current.Clear();

            Sphere sphere = new Sphere();

            sphere.material.Glassy();

            Ray ray = new Ray(new Point(0, 0, 0),
                              new Vector(0, 1, 0));

            List <Intersection> xs = new List <Intersection>();

            xs.Add(new Intersection(sphere, -1));
            xs.Add(new Intersection(sphere, 1));

            Computations c = Computations.Prepare(xs[1], ray, xs);

            double reflectance = Scene.current.Schlick(c);

            Assert.IsTrue(Utility.FE(0.04, reflectance));
        }
Ejemplo n.º 7
0
        public void T18_ReflectanceN2GreaterN1()
        {
            if (Scene.current == null)
            {
                new Scene();
            }

            Scene.current.Clear();

            Sphere sphere = new Sphere();

            sphere.material.Glassy();

            Ray ray = new Ray(new Point(0, 0.99, -2),
                              new Vector(0, 0, 1));

            List <Intersection> xs = new List <Intersection>();

            xs.Add(new Intersection(sphere, 1.8589));

            Computations c = Computations.Prepare(xs[0], ray, xs);

            double reflectance = Scene.current.Schlick(c);

            Assert.IsTrue(Utility.FE(0.48873, reflectance));
        }
Ejemplo n.º 8
0
        public void T12_RefractedColorAtMaxRecusionDepth()
        {
            if (Scene.current == null)
            {
                new Scene();
            }

            Scene.current.Default();

            RayObject ro1 = Scene.current.root.GetChildren()[0];

            ro1.material.Transparency = 1;
            ro1.material.RefracIndex  = 1.5;

            Ray r = new Ray(new Point(0, 0, -5), new Vector(0, 0, 1));

            List <Intersection> xs = new List <Intersection>();

            xs.Add(new Intersection(ro1, 4));
            xs.Add(new Intersection(ro1, 6));

            Computations c = Computations.Prepare(xs[0], r, xs);

            Color color = Scene.current.RefractedColor(c, 0);

            Assert.AreEqual(Color.black, color);
        }
Ejemplo n.º 9
0
        public void T13_TotalInternalReflection()
        {
            if (Scene.current == null)
            {
                new Scene();
            }

            Scene.current.Default();

            RayObject ro1 = Scene.current.root.GetChildren()[0];

            ro1.material.Transparency = 1.0;
            ro1.material.RefracIndex  = 1.5;

            Ray r = new Ray(new Point(0, 0, Math.Sqrt(2) / 2),
                            new Vector(0, 1, 0));
            List <Intersection> xs = new List <Intersection>();

            xs.Add(new Intersection(ro1, Math.Sqrt(2) / -2));
            xs.Add(new Intersection(ro1, Math.Sqrt(2) / 2));
            Computations c     = Computations.Prepare(xs[1], r, xs);
            Color        color = Scene.current.RefractedColor(c, 5);

            Assert.AreEqual(Color.black, color);
        }
Ejemplo n.º 10
0
        public void ShouldShadeHitWithReflectiveTransparentMaterial()
        {
            var w          = World.CreateDefault();
            var floorPlane = new XZPlane
            {
                Transform = CreateTranslation(0, -1, 0),
                Material  =
                {
                    Reflective      = 0.5f,
                    Transparency    = 0.5f,
                    RefractiveIndex = 1.5f,
                }
            };

            w.Objects.Add(floorPlane);
            var ball = new Sphere
            {
                Material =
                {
                    Color   = VColor.LinearRGB(1, 0, 0),
                    Ambient =               0.5f,
                },
                Transform = CreateTranslation(0, -3.5f, -0.5f),
            };

            w.Objects.Add(ball);

            var r     = new Ray(CreatePoint(0, 0, -3), CreateVector(0, -Sqrt(2) / 2, Sqrt(2) / 2));
            var xs    = Intersection.CreateList((Sqrt(2), floorPlane));
            var comps = Computations.Prepare(xs[0], r, xs);
            var color = w.ShadeHit(comps, 5);

            AssertActualEqualToExpected(color, VColor.LinearRGB(0.93391f, 0.69643f, 0.69243f));
        }
Ejemplo n.º 11
0
        public void T11_RefractedColorOfOpaque()
        {
            if (Scene.current == null)
            {
                new Scene();
            }

            Scene.current.Clear();

            Scene.current.Default();

            RayObject ro1 = Scene.current.root.GetChildren()[0];

            Ray ray = new Ray(new Point(0, 0, -5), new Vector(0, 0, 1));

            List <Intersection> xs = new List <Intersection>();

            xs.Add(new Intersection(ro1, 4));
            xs.Add(new Intersection(ro1, 6));

            Computations c     = Computations.Prepare(xs[0], ray, xs);
            Color        color = Scene.current.RefractedColor(c, 5);

            Assert.AreEqual(Color.black, color);
        }
        public void ShouldPrecomputeReflectionVector()
        {
            var shape = new XZPlane();
            var r     = CreateRay(CreatePoint(0, 1, -1), CreateVector(0, -Sqrt(2) / 2, Sqrt(2) / 2));
            var i     = new Intersection(Sqrt(2), shape);
            var comps = Computations.Prepare(i, r);

            AssertActualEqualToExpected(comps.ReflectV, CreateVector(0, Sqrt(2) / 2, Sqrt(2) / 2));
        }
        public void ShouldComputeSchlickApproximationUnderTotalInternalReflection()
        {
            var shape       = Sphere.CreateGlass();
            var r           = new Ray(CreatePoint(0, 0, Sqrt(2) / 2), CreateVector(0, 1, 0));
            var xs          = Intersection.CreateList((-Sqrt(2) / 2, shape), (Sqrt(2) / 2, shape));
            var comps       = Computations.Prepare(xs[1], r, xs);
            var reflectance = comps.GetSchlickReflectance();

            reflectance.Should().BeApproximately(1, Tolerance);
        }
        public void ShouldComputeSchlickApproximationWithPerpendicularViewingAngle()
        {
            var shape       = Sphere.CreateGlass();
            var r           = new Ray(CreatePoint(0, 0, 0), CreateVector(0, 1, 0));
            var xs          = Intersection.CreateList((-1, shape), (1, shape));
            var comps       = Computations.Prepare(xs[1], r, xs);
            var reflectance = comps.GetSchlickReflectance();

            reflectance.Should().BeApproximately(0.04f, Tolerance);
        }
        public void ShouldComputeSchlickApproximationWithSmallAngleAndN2GreaterThanN1()
        {
            var shape       = Sphere.CreateGlass();
            var r           = new Ray(CreatePoint(0, .99f, -2), CreateVector(0, 0, 1));
            var xs          = Intersection.CreateList((1.8589f, shape));
            var comps       = Computations.Prepare(xs[0], r, xs);
            var reflectance = comps.GetSchlickReflectance();

            reflectance.Should().BeApproximately(0.48873f, Tolerance);
        }
        public void ShouldIndicateWhenIntersectionOccursOnTheOutside()
        {
            var r     = CreateRay(CreatePoint(0, 0, -5), CreateVector(0, 0, 1));
            var shape = new Sphere();
            var i     = new Intersection(4, shape);

            var comps = Computations.Prepare(i, r);

            comps.Inside.Should().Be(false);
        }
Ejemplo n.º 17
0
        public void ShouldShadeIntersection()
        {
            var w     = World.CreateDefault();
            var r     = CreateRay(CreatePoint(0, 0, -5), CreateVector(0, 0, 1));
            var shape = w.Objects.First();
            var i     = new Intersection(4, shape);
            var comps = Computations.Prepare(i, r);
            var c     = w.ShadeHit(comps);

            AssertActualEqualToExpected(c, VColor.LinearRGB(0.38066f, 0.47583f, 0.2855f));
        }
Ejemplo n.º 18
0
        public void ShouldDetermineReflectedColorForNonReflectiveMaterial()
        {
            var w     = World.CreateDefault();
            var r     = CreateRay(CreatePoint(0, 0, 0), CreateVector(0, 0, 1));
            var shape = w.Objects[1];

            shape.Material.Ambient = 1;
            var i     = new Intersection(1, shape);
            var comps = Computations.Prepare(i, r);
            var color = w.ComputeReflectedColor(comps);

            AssertActualEqualToExpected(color, VColor.Black);
        }
        public void ShouldOffsetPointInHit()
        {
            var r     = CreateRay(CreatePoint(0, 0, -5), CreateVector(0, 0, 1));
            var shape = new Sphere {
                Transform = CreateTranslation(0, 0, 1)
            };
            var i = new Intersection(5, shape);

            var comps = Computations.Prepare(i, r);

            comps.OverPoint.Z.Should().BeLessThan(-Tolerance / 2);
            comps.Point.Z.Should().BeGreaterThan(comps.OverPoint.Z);
        }
Ejemplo n.º 20
0
        public void ShouldComputeRefractedColorWithOpaqueSurface()
        {
            var w     = World.CreateDefault();
            var shape = w.Objects.First();
            var r     = new Ray(CreatePoint(0, 0, -5), CreateVector(0, 0, 1));
            var xs    = new List <Intersection> {
                new Intersection(4, shape), new Intersection(6, shape)
            };
            var comps = Computations.Prepare(xs[0], r, xs);
            var c     = w.ComputeRefractedColor(comps, 5);

            AssertActualEqualToExpected(c, VColor.Black);
        }
        public void ShouldIndicateWhenIntersectionOccursOnTheInside()
        {
            var r     = CreateRay(CreatePoint(0, 0, 0), CreateVector(0, 0, 1));
            var shape = new Sphere();
            var i     = new Intersection(1, shape);

            var comps = Computations.Prepare(i, r);

            AssertActualEqualToExpected(comps.Point, CreatePoint(0, 0, 1));
            AssertActualEqualToExpected(comps.EyeV, CreateVector(0, 0, -1));
            comps.Inside.Should().Be(true);
            AssertActualEqualToExpected(comps.NormalV, CreateVector(0, 0, -1));
        }
        public void ShouldPrecomputeStateOfIntersection()
        {
            var r     = CreateRay(CreatePoint(0, 0, -5), CreateVector(0, 0, 1));
            var shape = new Sphere();
            var i     = new Intersection(4, shape);

            var comps = Computations.Prepare(i, r);

            comps.Object.Should().Be(shape);
            AssertActualEqualToExpected(comps.Point, CreatePoint(0, 0, -1));
            AssertActualEqualToExpected(comps.EyeV, CreateVector(0, 0, -1));
            AssertActualEqualToExpected(comps.NormalV, CreateVector(0, 0, -1));
        }
Ejemplo n.º 23
0
        public void ShouldShadeIntersectionFromTheInside()
        {
            var w = World.CreateDefault();

            w.Lights[0] = new PointLight(CreatePoint(0, 0.25f, 0), VColor.White);
            var r     = CreateRay(CreatePoint(0, 0, 0), CreateVector(0, 0, 1));
            var shape = w.Objects[1];
            var i     = new Intersection(0.5f, shape);
            var comps = Computations.Prepare(i, r);
            var c     = w.ShadeHit(comps);

            AssertActualEqualToExpected(c, VColor.LinearRGB(0.90498f, 0.90498f, 0.90498f));
        }
        public void ShouldComputeUnderPointBelowTheSurface()
        {
            var r     = new Ray(CreatePoint(0, 0, -5f), CreateVector(0, 0, 1));
            var shape = Sphere.CreateGlass();

            shape.Transform = CreateTranslation(0, 0, 1);
            var i  = new Intersection(5, shape);
            var xs = new List <Intersection> {
                i
            };
            var comps = Computations.Prepare(i, r, xs);

            comps.UnderPoint.Z.Should().BeGreaterThan(Tolerance / 2);
            comps.Point.Z.Should().BeLessThan(comps.UnderPoint.Z);
        }
Ejemplo n.º 25
0
        public void ShouldComputeRefractedColorAtMaxRecursiveDepth()
        {
            var w     = World.CreateDefault();
            var shape = w.Objects.First();

            shape.Material.Transparency    = 1;
            shape.Material.RefractiveIndex = 1.5f;
            var r  = new Ray(CreatePoint(0, 0, -5), CreateVector(0, 0, 1));
            var xs = new List <Intersection> {
                new Intersection(4, shape), new Intersection(6, shape)
            };
            var comps = Computations.Prepare(xs[0], r, xs);
            var c     = w.ComputeRefractedColor(comps, 0);

            AssertActualEqualToExpected(c, VColor.Black);
        }
Ejemplo n.º 26
0
        public void ShouldComputeRefractedColorUnderTotalInternalReflection()
        {
            var w     = World.CreateDefault();
            var shape = w.Objects.First();

            shape.Material.Transparency    = 1;
            shape.Material.RefractiveIndex = 1.5f;
            var r  = new Ray(CreatePoint(0, 0, Sqrt(2) / 2), CreateVector(0, 1, 0));
            var xs = new List <Intersection> {
                new Intersection(-Sqrt(2) / 2, shape), new Intersection(Sqrt(2) / 2, shape)
            };
            var comps = Computations.Prepare(xs[1], r, xs);
            var c     = w.ComputeRefractedColor(comps, 5);

            AssertActualEqualToExpected(c, VColor.Black);
        }
Ejemplo n.º 27
0
        public void ShouldComputeColorForReflectiveMaterial()
        {
            var w     = World.CreateDefault();
            var shape = new XZPlane
            {
                Material  = { Reflective = 0.5f },
                Transform = CreateTranslation(0, -1, 0),
            };

            w.Objects.Add(shape);
            var r     = CreateRay(CreatePoint(0, 0, -3), CreateVector(0, -Sqrt(2) / 2, Sqrt(2) / 2));
            var i     = new Intersection(Sqrt(2), shape);
            var comps = Computations.Prepare(i, r);
            var color = w.ShadeHit(comps, 4);

            AssertActualEqualToExpected(color, VColor.LinearRGB(0.87677f, 0.92436f, 0.82918f));
        }
Ejemplo n.º 28
0
        public void ShouldComputeColorForReflectiveMaterialAtMaximumRecursiveDepth()
        {
            var w     = World.CreateDefault();
            var shape = new XZPlane
            {
                Material  = { Reflective = 0.5f },
                Transform = CreateTranslation(0, -1, 0),
            };

            w.Objects.Add(shape);
            var r     = CreateRay(CreatePoint(0, 0, -3), CreateVector(0, -Sqrt(2) / 2, Sqrt(2) / 2));
            var i     = new Intersection(Sqrt(2), shape);
            var comps = Computations.Prepare(i, r);
            var color = w.ComputeReflectedColor(comps, 0);

            AssertActualEqualToExpected(color, VColor.Black);
        }
Ejemplo n.º 29
0
        public void T06_ShadingIntersectionsOutside()
        {
            //From the Outside
            if (Scene.current == null)
            {
                Scene scene = new Scene();
            }
            Scene.current.Default();

            Ray r = new Ray(new Point(0, 0, -5), new Vector(0, 0, 1));
            List <Intersection> hits = Scene.current.Intersections(r);
            Intersection        hit  = Scene.current.Hit(hits);
            Computations        c    = Computations.Prepare(hit, r, null);
            Color finalColor         = Scene.current.ShadeHit(c);

            Assert.AreEqual(new Color(0.38066, 0.47583, 0.2855), finalColor);
        }
Ejemplo n.º 30
0
        public void T02_ReflectiveVector()
        {
            if (Scene.current == null)
            {
                new Scene();
            }

            Scene.current.Clear();

            Plane        plane = new Plane();
            Ray          ray   = new Ray(new Point(0, 1, -1), new Vector(0, Math.Sqrt(2) / -2, Math.Sqrt(2) / 2));
            Intersection i     = new Intersection(plane, Math.Sqrt(2));

            Computations c = Computations.Prepare(i, ray, null);

            Assert.AreEqual(new Vector(0, Math.Sqrt(2) / 2, Math.Sqrt(2) / 2),
                            c.reflectVector);
        }