Exemple #1
0
        public void LightingUsesLightIntensityToAttenuateColor(
            double intensity,
            double r,
            double g,
            double b)
        {
            var w = new DefaultWorld();

            w.Lights = new ILight[]
            {
                new PointLight(Vector4.CreatePosition(0, 0, -10), Color.White),
            };

            var shape = w.Objects[0];

            shape.Material.Ambient  = 0.1;
            shape.Material.Diffuse  = 0.9;
            shape.Material.Specular = 0;
            shape.Material.Color    = new Color(1, 1, 1);
            var pt      = Vector4.CreatePosition(0, 0, -1);
            var eyev    = Vector4.CreateDirection(0, 0, -1);
            var normalv = Vector4.CreateDirection(0, 0, -1);
            var result  = shape.Material.Li(
                shape,
                w.Lights[0],
                pt,
                eyev,
                normalv,
                intensity);

            var expected = new Color(r, g, b);

            Assert.Equal(expected, result);
        }
Exemple #2
0
        public static Tuple <World, Camera> Example11(int width, int height)
        {
            var world = new World();

            var hex = Hexagon.Create();

            world.Objects.Add(hex);

            var light = new PointLight(
                Vector4.CreatePosition(-10, 10, -10),
                Color.White);

            world.Lights.Add(light);

            var camera = new Camera(width, height, Math.PI / 4.2)
            {
                Transform = Transform.View(
                    Vector4.CreatePosition(-4, 5, -3),
                    Vector4.CreatePosition(0, 0.5, 0),
                    Vector4.CreateDirection(0, 1, 0)),

                ProgressMonitorFactory =
                    (_rows, _cols) => new DefaultProgressMonitor(),
            };

            return(Tuple.Create(world, camera));
        }
Exemple #3
0
        public void ShadeIntersectionInShadow()
        {
            var w = new World();

            w.Lights.Add(
                new PointLight(
                    Vector4.CreatePosition(0, 0, -10),
                    Color.White));

            var s1 = new Sphere();
            var s2 = new Sphere()
            {
                Transform = Transform.Translate(0, 0, 10),
            };

            w.Objects.Add(s1);
            w.Objects.Add(s2);

            var r        = new Ray(Vector4.CreatePosition(0, 0, 5), Vector4.CreateDirection(0, 0, 1));
            var i        = new Intersection(4, s2);
            var comps    = i.Precompute(r);
            var c        = w.Render(comps, 5);
            var expected = new Color(0.1, 0.1, 0.1);

            Assert.Equal(expected, c);
        }
Exemple #4
0
        public void FindingN1AndN2AtVariousIntersections(int index, double n1, double n2)
        {
            var a = new GlassSphere()
            {
                Transform = Transform.Scale(2, 2, 2),
            };

            var b = new GlassSphere()
            {
                Transform = Transform.Translate(0, 0, -0.25),
            };

            var c = new GlassSphere()
            {
                Transform = Transform.Translate(0, 0, 25),
            };

            a.Material.RefractiveIndex = 1.5;
            b.Material.RefractiveIndex = 2.0;
            c.Material.RefractiveIndex = 2.5;

            var r = new Ray(Vector4.CreatePosition(0, 0, -4), Vector4.CreateDirection(0, 0, 1));
            var xs = IntersectionList.Create(
                new Intersection(2, a),
                new Intersection(2.75, b),
                new Intersection(3.25, c),
                new Intersection(4.75, b),
                new Intersection(5.25, c),
                new Intersection(6, a));

            var comps = xs[index].Precompute(r, xs);
            Assert.Equal(n1, comps.N1);
            Assert.Equal(n2, comps.N2);
        }
Exemple #5
0
        public void IntersectRayWithNonEmptyGroup()
        {
            var g = new Group();

            var s1 = new Sphere();

            var s2 = new Sphere()
            {
                Transform =
                    Transform.Translate(0, 0, -3),
            };

            var s3 = new Sphere()
            {
                Transform =
                    Transform.Translate(5, 0, 0),
            };

            g.Add(s1);
            g.Add(s2);
            g.Add(s3);

            var r  = new Ray(Vector4.CreatePosition(0, 0, -5), Vector4.CreateDirection(0, 0, 1));
            var xs = g.LocalIntersect(r);

            Assert.Equal(4, xs.Count);
            Assert.Equal(s2, xs[0].Object);
            Assert.Equal(s2, xs[1].Object);
            Assert.Equal(s1, xs[2].Object);
            Assert.Equal(s1, xs[3].Object);
        }
Exemple #6
0
        public void TestDotProductOfTwoVector4s()
        {
            var a = Vector4.CreateDirection(1, 2, 3);
            var b = Vector4.CreateDirection(2, 3, 4);

            Assert.Equal(20, a.Dot(b));
        }
Exemple #7
0
        public void TestTranslationDoesNotAffectVectors()
        {
            var t = Transform.Translate(5, -3, 2);
            var v = Vector4.CreateDirection(-3, 4, 5);

            Assert.Equal(v, t * v);
        }
Exemple #8
0
        public void ComputingTheNormalVectorOnCone()
        {
            var shape = new Cone();
            var cases = new[]
            {
                new
                {
                    point  = Vector4.CreatePosition(0, 0, 0),
                    normal = Vector4.CreateDirection(0, 0, 0),
                },
                new
                {
                    point  = Vector4.CreatePosition(1, 1, 1),
                    normal = Vector4.CreateDirection(1, -Math.Sqrt(2), 1),
                },
                new
                {
                    point  = Vector4.CreatePosition(-1, -1, 0),
                    normal = Vector4.CreateDirection(-1, 1, 0),
                }
            };

            foreach (var c in cases)
            {
                var n = shape.GetLocalNormal(c.point);
                Assert.Equal(c.normal, n);
            }
        }
Exemple #9
0
        public void ConvertNormalFromObjectSpaceToWorldSpace()
        {
            var g1 = new Group()
            {
                Transform = Transform.RotateY(Math.PI / 2),
            };

            var g2 = new Group()
            {
                Transform = Transform.Scale(1, 2, 3),
            };

            g1.Add(g2);

            var s = new Sphere()
            {
                Transform = Transform.Translate(5, 0, 0),
            };

            g2.Add(s);

            var          v        = Vector4.CreateDirection(Math.Sqrt(3) / 3, Math.Sqrt(3) / 3, Math.Sqrt(3) / 3);
            var          n        = s.NormalToWorld(v);
            const double eps      = 0.0001;
            var          comparer = Vector4.GetEqualityComparer(eps);
            var          expected = Vector4.CreateDirection(0.2857, 0.4286, -0.8571);

            Assert.Equal(expected, n, comparer);
        }
Exemple #10
0
        public void RefractedColorWithRefractedRay()
        {
            var w = new DefaultWorld();

            var a = w.Objects[0];

            a.Material.Ambient = 1.0;
            a.Material.Pattern = new TestPattern();

            var b = w.Objects[1];

            b.Material.Transparency    = 1.0;
            b.Material.RefractiveIndex = 1.5;

            var r = new Ray(
                Vector4.CreatePosition(0, 0, 0.1),
                Vector4.CreateDirection(0, 1, 0));

            var xs = IntersectionList.Create(
                new Intersection(-0.9899, a),
                new Intersection(-0.4899, b),
                new Intersection(0.4899, b),
                new Intersection(0.9899, a));

            var comps    = xs[2].Precompute(r, xs);
            var c        = w.GetRefractedColor(comps, 5);
            var expected = new Color(0, 0.99888, 0.04725);

            const double eps      = 0.0001;
            var          comparer = Color.GetEqualityComparer(eps);

            Assert.Equal(expected, c, comparer);
        }
Exemple #11
0
        public void TestVectorFactoryMethod()
        {
            var v        = Vector4.CreateDirection(4, -4, 3);
            var expected = new Vector4(4, -4, 3, 0);

            Assert.Equal(expected, v);
        }
Exemple #12
0
        public void FindNormalOnChildObject()
        {
            var g1 = new Group()
            {
                Transform = Transform.RotateY(Math.PI / 2),
            };

            var g2 = new Group()
            {
                Transform = Transform.Scale(1, 2, 3),
            };

            g1.Add(g2);

            var s = new Sphere()
            {
                Transform = Transform.Translate(5, 0, 0),
            };

            g2.Add(s);

            var          p        = Vector4.CreatePosition(1.7321, 1.1547, -5.5774);
            var          n        = s.GetNormal(p);
            const double eps      = 0.0001;
            var          comparer = Vector4.GetEqualityComparer(eps);
            var          expected = Vector4.CreateDirection(0.2857, 0.4286, -0.8571);

            Assert.Equal(expected, n, comparer);
        }
Exemple #13
0
        public void AreaLightWithJitteredSamples(
            double px,
            double py,
            double pz,
            double result)
        {
            var w      = new DefaultWorld();
            var corner = Vector4.CreatePosition(-0.5, -0.5, -5);
            var v1     = Vector4.CreateDirection(1, 0, 0);
            var v2     = Vector4.CreateDirection(0, 1, 0);
            var light  = new AreaLight(
                corner,
                v1,
                2,
                v2,
                2,
                Color.White)
            {
                Jitter = new Sequence(0.7, 0.3, 0.9, 0.1, 0.5),
            };

            var pt        = Vector4.CreatePosition(px, py, pz);
            var intensity = light.GetIntensity(pt, w);

            Assert.Equal(result, intensity);
        }
Exemple #14
0
        public void FindSinglePointOnJitteredAreaLight(
            double u,
            double v,
            double rx,
            double ry,
            double rz)
        {
            var corner = Vector4.CreatePosition(0, 0, 0);
            var v1     = Vector4.CreateDirection(2, 0, 0);
            var v2     = Vector4.CreateDirection(0, 0, 1);
            var light  = new AreaLight(
                corner,
                v1,
                4,
                v2,
                2,
                Color.White)
            {
                Jitter = new Sequence(0.3, 0.7),
            };

            var pt       = light.GetPoint(u, v);
            var expected = Vector4.CreatePosition(rx, ry, rz);

            Assert.Equal(expected, pt);
        }
Exemple #15
0
        public void TestMagnitudeOfNormalizedVector()
        {
            var v    = Vector4.CreateDirection(1, 2, 3);
            var norm = v.Normalize();

            Assert.Equal(1, norm.Magnitude(), 6);
        }
Exemple #16
0
        public void ShadeWithReflectiveTransparentMaterial()
        {
            var w = new DefaultWorld();
            var r = new Ray(
                Vector4.CreatePosition(0, 0, -3),
                Vector4.CreateDirection(0, -Math.Sqrt(2) / 2, Math.Sqrt(2) / 2));

            var floor = new Plane();

            floor.Transform                = Transform.Translate(0, -1, 0);
            floor.Material.Reflective      = 0.5;
            floor.Material.Transparency    = 0.5;
            floor.Material.RefractiveIndex = 1.5;

            var ball = new Sphere();

            ball.Transform        = Transform.Translate(0, -3.5, -0.5);
            ball.Material.Color   = new Color(1, 0, 0);
            ball.Material.Ambient = 0.5;

            w.Objects.Add(floor);
            w.Objects.Add(ball);

            var xs = IntersectionList.Create(
                new Intersection(Math.Sqrt(2), floor));

            var          comps    = xs[0].Precompute(r, xs);
            var          c        = w.Render(comps, 5);
            var          expected = new Color(0.93391, 0.69643, 0.69243);
            const double eps      = 0.00001;
            var          comparer = Color.GetEqualityComparer(eps);

            Assert.Equal(expected, c, comparer);
        }
Exemple #17
0
        public void TestMultiplyByTheInverseOfScalingMatrix()
        {
            var t        = Transform.Scale(2, 3, 4).Inverse();
            var v        = Vector4.CreateDirection(-4, 6, 8);
            var expected = Vector4.CreateDirection(-2, 2, 2);

            Assert.Equal(expected, t * v);
        }
Exemple #18
0
        public void TestScalingMatrixAppliedToVector()
        {
            var t        = Transform.Scale(2, 3, 4);
            var p        = Vector4.CreateDirection(-4, 6, 8);
            var expected = Vector4.CreateDirection(-8, 18, 32);

            Assert.Equal(expected, t * p);
        }
Exemple #19
0
        public void TestColorWhenRayMisses()
        {
            var w = new DefaultWorld();
            var r = new Ray(Vector4.CreatePosition(0, 0, -5), Vector4.CreateDirection(0, 1, 0));
            var c = w.Trace(r, 5);

            Assert.Equal(Color.Black, c);
        }
Exemple #20
0
        public void TestRayMissesSphere()
        {
            var r  = new Ray(Vector4.CreatePosition(0, 2, -5), Vector4.CreateDirection(0, 0, 1));
            var s  = new Sphere();
            var xs = s.Intersect(r);

            Assert.Empty(xs);
        }
Exemple #21
0
        public void IntersectRayWithEmptyGroup()
        {
            var g  = new Group();
            var r  = new Ray(Vector4.CreatePosition(0, 0, 0), Vector4.CreateDirection(0, 0, 1));
            var xs = g.LocalIntersect(r);

            Assert.Empty(xs);
        }
Exemple #22
0
        public void TestNormalOnSphereAtPointOnXAxis()
        {
            var s        = new Sphere();
            var n        = s.GetNormal(Vector4.CreatePosition(1, 0, 0));
            var expected = Vector4.CreateDirection(1, 0, 0);

            Assert.Equal(expected, n);
        }
Exemple #23
0
 public void HitWhenIntersectionOnOutside()
 {
     var r = new Ray(Vector4.CreatePosition(0, 0, -5), Vector4.CreateDirection(0, 0, 1));
     var shape = new Sphere();
     var i = new Intersection(4, shape);
     var comps = i.Precompute(r);
     Assert.False(comps.Inside);
 }
Exemple #24
0
        public void RayMissesCsgObject()
        {
            var c  = new Csg(Operation.Union, new Sphere(), new Cube());
            var r  = new Ray(Vector4.CreatePosition(0, 2, -5), Vector4.CreateDirection(0, 0, 1));
            var xs = c.LocalIntersect(r);

            Assert.Empty(xs);
        }
Exemple #25
0
        public void TestSubtractVectorFromZeroVector()
        {
            var zero     = Vector4.CreateDirection(0, 0, 0);
            var v        = Vector4.CreateDirection(1, -2, 3);
            var expected = Vector4.CreateDirection(-1, 2, -3);

            Assert.Equal(expected, zero - v);
        }
Exemple #26
0
        public void IntersectWithCoplanarRay()
        {
            var p  = new Plane();
            var r  = new Ray(Vector4.CreatePosition(0, 0, 0), Vector4.CreateDirection(0, 0, 1));
            var xs = p.LocalIntersect(r);

            Assert.Empty(xs);
        }
Exemple #27
0
        public void TestSubtractTwoVectors()
        {
            var v1       = Vector4.CreateDirection(3, 2, 1);
            var v2       = Vector4.CreateDirection(5, 6, 7);
            var expected = Vector4.CreateDirection(-2, -4, -6);

            Assert.Equal(expected, v1 - v2);
        }
Exemple #28
0
        public void TestSubtractTwoPoints()
        {
            var p1       = Vector4.CreatePosition(3, 2, 1);
            var p2       = Vector4.CreatePosition(5, 6, 7);
            var expected = Vector4.CreateDirection(-2, -4, -6);

            Assert.Equal(expected, p1 - p2);
        }
Exemple #29
0
        public void TestSubtractVectorFromPoint()
        {
            var p        = Vector4.CreatePosition(3, 2, 1);
            var v        = Vector4.CreateDirection(5, 6, 7);
            var expected = Vector4.CreatePosition(-2, -4, -6);

            Assert.Equal(expected, p - v);
        }
Exemple #30
0
        public void TestReflectVectorApproachingAt45Degrees()
        {
            var v        = Vector4.CreateDirection(1, -1, 0);
            var n        = Vector4.CreateDirection(0, 1, 0);
            var r        = v.Reflect(n);
            var expected = Vector4.CreateDirection(1, 1, 0);

            Assert.Equal(expected, r);
        }