public RTF.Canvas DrawRedCircle(RTF.Shapes.Sphere s)
        {
            var    rayOrigin   = RTF.PointType.Point(0, 0, -5);
            double wallZ       = 10;
            double wallSize    = 7;
            var    canvasPixel = 100;
            double pixelSize   = (double)wallSize / (double)canvasPixel;
            double half        = wallSize / 2;

            var canvas = new RTF.Canvas(canvasPixel, canvasPixel, new RTF.Color(0, 0, 0));
            var color  = new RTF.Color(255, 0, 0);
            var shape  = s;

            for (int y = 0; y < canvasPixel; y++)
            {
                var worldY = half - pixelSize * y;
                for (int x = 0; x < canvasPixel; x++)
                {
                    var worldX   = -half + pixelSize * x;
                    var position = RTF.PointType.Point(worldX, worldY, wallZ);

                    var r  = new RTF.Ray(rayOrigin, (position - rayOrigin).Normalize());
                    var xs = shape.Intersect(r);

                    var hit = RTF.Intersection.Hit(xs);

                    if (hit != null)
                    {
                        canvas.WritePixel(x, y, color);
                    }
                }
            }

            return(canvas);
        }
示例#2
0
        public void ShadeHitIsGivenIntersectionInShadow()
        {
            var w = new RTF.World
            {
                Lights = new List <RTF.Light>()
                {
                    new RTF.Light(p.Point(0, 0, -10), RTF.Color.White)
                }
            };

            var s1 = new Sphere();

            w.Objects.Add(s1);

            var s2 = new Sphere(t.Translation(0, 0, 10));

            w.Objects.Add(s2);

            var r = new RTF.Ray(p.Point(0, 0, 5), p.Vector(0, 0, 1));
            var i = new RTF.Intersection(4, s2);

            var comps = RTF.Computation.PrepareComputations(i, r);
            var c     = w.ShadeHit(comps);

            var expected = new RTF.Color(0.1, 0.1, 0.1);

            Assert.Equal(expected, c);
        }
        public void TransformationSequence()
        {
            var origin = RTF.PointType.Point(1, 2, 3);
            var direction = RTF.PointType.Vector(4, 5, 6);
            var r = new RTF.Ray(origin, direction);

            Assert.Equal(origin, r.Origin);
            Assert.Equal(direction, r.Direction);
        }
示例#4
0
        public void IntersectWithCoplanarRay()
        {
            var p = new shapes.Plane();
            var r = new RTF.Ray(
                pt.Point(0, 0, 0), pt.Vector(0, 0, 1));
            var xs = p.LIntersect(r);

            Assert.Empty(xs);
        }
        public void IntersectTranslatedSphereWithRay()
        {
            var s = new Sphere();
            var r = new RTF.Ray(RTF.PointType.Point(0, 0, -5),
                                RTF.PointType.Vector(0, 0, 1));
            s.Transform = RTH.Transformations.Translation(5, 0, 0);
            var xs = s.Intersect(r);

            Assert.Empty(xs);
        }
示例#6
0
        public void IntersectingTranslatedShapeWithRay()
        {
            var r = new RTF.Ray(pt.Point(0, 0, -5), pt.Vector(0, 0, 1));
            var s = new shapes.TestShape(tf.Translation(5, 0, 0));

            _ = s.Intersect(r);

            Assert.Equal(pt.Point(-5, 0, -5), s.SavedRay.Origin);
            Assert.Equal(pt.Vector(0, 0, 1), s.SavedRay.Direction);
        }
示例#7
0
        public void IntersectingScaledShapeWithRay()
        {
            var r = new RTF.Ray(pt.Point(0, 0, -5), pt.Vector(0, 0, 1));
            var s = new shapes.TestShape(tf.Scaling(2, 2, 2));

            _ = s.Intersect(r);

            Assert.Equal(pt.Point(0, 0, -2.5), s.SavedRay.Origin);
            Assert.Equal(pt.Vector(0, 0, 0.5), s.SavedRay.Direction);
        }
示例#8
0
        public void RayIntersectionPlaneFromBelow()
        {
            var p = new shapes.Plane();
            var r = new RTF.Ray(
                pt.Point(0, -1, 0), pt.Vector(0, 1, 0));
            var xs = p.LIntersect(r);

            Assert.NotEmpty(xs);
            Assert.Equal(1, xs[0].T);
            Assert.Equal(p, xs[0].Object);
        }
示例#9
0
        public void HitWhenIntersectionOccursOutside()
        {
            var r = new RTF.Ray(
                RTF.PointType.Point(0, 0, -5),
                RTF.PointType.Vector(0, 0, 1));
            var shape = new Sphere();
            var i     = new RTF.Intersection(4, shape);
            var comps = RTF.Computation.PrepareComputations(i, r);

            Assert.False(comps.Inside);
        }
        public void SetObjectIntersection()
        {
            var r = new RTF.Ray(RTF.PointType.Point(0, 0, -5),
                RTF.PointType.Vector(0, 0, 1));
            var s = new Sphere();
            var xs = s.Intersect(r);

            Assert.Equal(2, xs.Length);
            Assert.Equal(s, xs[0].Object);
            Assert.Equal(s, xs[1].Object);
        }
        public void TranslatingRay()
        {
            var r = new RTF.Ray(RTF.PointType.Point(1, 2, 3),
                                RTF.PointType.Vector(0, 1, 0));
            var m = RTH.Transformations.Translation(3, 4, 5);

            RTF.Ray r2 = RTF.Ray.Transform(r, m);

            Assert.Equal(RTF.PointType.Point(4, 6, 8), r2.Origin);
            Assert.Equal(RTF.PointType.Vector(0, 1, 0), r2.Direction);
        }
        public void ScalingRay()
        {
            var r = new RTF.Ray(RTF.PointType.Point(1, 2, 3),
                                RTF.PointType.Vector(0, 1, 0));
            var m = RTH.Transformations.Scaling(2, 3, 4);

            RTF.Ray r2 = RTF.Ray.Transform(r, m);

            Assert.Equal(RTF.PointType.Point(2, 6, 12), r2.Origin);
            Assert.Equal(RTF.PointType.Vector(0, 3, 0), r2.Direction);
        }
示例#13
0
        public void ColorRayHits()
        {
            var w = RTF.World.Default();
            var r = new RTF.Ray(
                RTF.PointType.Point(0, 0, -5),
                RTF.PointType.Vector(0, 0, 1));
            var c   = w.ColorAt(r);
            var exp = new RTF.Color(0.38066, 0.47583, 0.2855);

            CustomAssert.Equal(exp, c, 5);
        }
示例#14
0
        public void ColorRayMissed()
        {
            var w = RTF.World.Default();
            var r = new RTF.Ray(
                RTF.PointType.Point(0, 0, -5),
                RTF.PointType.Vector(0, 1, 0));
            var c   = w.ColorAt(r);
            var exp = RTF.Color.Black;

            CustomAssert.Equal(exp, c, 5);
        }
        public void RaySphereMiss()
        {
            var origin = RTF.PointType.Point(0, 2, -5);
            var direction = RTF.PointType.Vector(0, 0, 1);
            var r = new RTF.Ray(origin, direction);
            var s = new Sphere();

            var xs = RTH.Transformations.Intersect(s, r);

            Assert.Empty(xs);
        }
示例#16
0
        public void TheHitShouldOffsetThePoint()
        {
            var r = new RTF.Ray(p.Point(0, 0, -5), p.Vector(0, 0, 1));

            var shape = new Sphere(t.Translation(0, 0, 1));
            var i     = new RTF.Intersection(5, shape);

            var comps = RTF.Computation.PrepareComputations(i, r);

            Assert.True(comps.OverPoint.Z < -EPSILON / 2);
            Assert.True(comps.Point.Z > comps.OverPoint.Z);
        }
示例#17
0
        public RTF.Canvas CreateCircle(RTF.Shapes.Sphere s)
        {
            var lightPos = RTF.PointType.Point(-10, 10, -10);
            var light    = new RTF.Light(lightPos, RTF.Color.White);

            var    rayOrigin   = RTF.PointType.Point(0, 0, -5);
            double wallZ       = 10;
            double wallSize    = 7;
            var    canvasPixel = 100;
            double pixelSize   = wallSize / canvasPixel;
            double half        = wallSize / 2;

            var canvas = new RTF.Canvas(canvasPixel, canvasPixel, RTF.Color.Black);

            var shape = s;

            shape.Material.Color = RTF.Color.Magenta;

            for (int y = 0; y < canvasPixel; y++)
            {
                var worldY = half - pixelSize * y;
                for (int x = 0; x < canvasPixel; x++)
                {
                    var worldX   = -half + pixelSize * x;
                    var position = RTF.PointType.Point(worldX, worldY, wallZ);

                    var r  = new RTF.Ray(rayOrigin, (position - rayOrigin).Normalize());
                    var xs = shape.Intersect(r);

                    var hit = RTF.Intersection.Hit(xs);

                    if (hit != null)
                    {
                        var point  = RTH.Transformations.Position(r, hit.T);
                        var normal = hit.Object.NormalAt(point);
                        var eye    = -r.Direction;

                        var color = RTF.Light.Lighting(
                            (hit.Object as RTF.Shapes.Sphere).Material,
                            hit.Object,
                            light,
                            point,
                            eye,
                            normal,
                            false);

                        canvas.WritePixel(x, y, color);
                    }
                }
            }

            return(canvas);
        }
        public void IntersectingScaledSphereWithRay()
        {
            var s = new Sphere();
            var r = new RTF.Ray(RTF.PointType.Point(0, 0, -5),
                                RTF.PointType.Vector(0, 0, 1));

            s.Transform = RTH.Transformations.Scaling(2, 2, 2);
            var xs = s.Intersect(r);

            Assert.Equal(2, xs.Length);
            Assert.Equal(3, xs[0].T);
            Assert.Equal(7, xs[1].T);
        }
        public void RaySphereBehind()
        {
            var origin = RTF.PointType.Point(0, 0, 5);
            var direction = RTF.PointType.Vector(0, 0, 1);
            var r = new RTF.Ray(origin, direction);
            var s = new Sphere();

            var xs = RTH.Transformations.Intersect(s, r);

            Assert.Equal(2, xs.Length);
            Assert.Equal(-6, xs[0].T);
            Assert.Equal(-4, xs[1].T);
        }
        public void RaySphereTwoPoints()
        {
            var origin = RTF.PointType.Point(0, 0, -5);
            var direction = RTF.PointType.Vector(0, 0, 1);
            var r = new RTF.Ray(origin, direction);
            var s = new Sphere();

            var xs = s.Intersect(r);

            Assert.Equal(2, xs.Length);
            Assert.Equal(4, xs[0].T);
            Assert.Equal(6, xs[1].T);
        }
示例#21
0
        public void HitWhenIntersectionOccursInside()
        {
            var r = new RTF.Ray(
                RTF.PointType.Point(0, 0, 0),
                RTF.PointType.Vector(0, 0, 1));
            var shape = new Sphere();
            var i     = new RTF.Intersection(1, shape);
            var comps = RTF.Computation.PrepareComputations(i, r);


            Assert.Equal(RTF.PointType.Point(0, 0, 1), comps.Point);
            Assert.Equal(RTF.PointType.Vector(0, 0, -1), comps.EyeV);
            Assert.True(comps.Inside);
            Assert.Equal(RTF.PointType.Vector(0, 0, -1), comps.NormalV);
        }
示例#22
0
        public void IntersectWorldWithRay()
        {
            var w = RTF.World.Default();
            var r = new RTF.Ray(
                RTF.PointType.Point(0, 0, -5),
                RTF.PointType.Vector(0, 0, 1));

            RTF.Intersection[] xs = w.Intersect(r);

            Assert.Equal(4, xs.Length);
            Assert.Equal(4, xs[0].T);
            Assert.Equal(4.5, xs[1].T);
            Assert.Equal(5.5, xs[2].T);
            Assert.Equal(6, xs[3].T);
        }
示例#23
0
        public void PrecomputingStateIntersection()
        {
            var r = new RTF.Ray(
                RTF.PointType.Point(0, 0, -5),
                RTF.PointType.Vector(0, 0, 1));
            var shape = new Sphere();
            var i     = new RTF.Intersection(4, shape);
            var comps = RTF.Computation.PrepareComputations(i, r);

            Assert.Equal(i.T, comps.T);
            Assert.Equal(i.Object, comps.Object);
            Assert.Equal(RTF.PointType.Point(0, 0, -1), comps.Point);
            Assert.Equal(RTF.PointType.Vector(0, 0, -1), comps.EyeV);
            Assert.Equal(RTF.PointType.Vector(0, 0, -1), comps.NormalV);
        }
        public void ComputePointFromDistance()
        {
            var origin = RTF.PointType.Point(2, 3, 4);
            var direction = RTF.PointType.Vector(1, 0, 0);
            var r = new RTF.Ray(origin, direction);

            var e1 = RTF.PointType.Point(2, 3, 4);
            var e2 = RTF.PointType.Point(3, 3, 4);
            var e3 = RTF.PointType.Point(1, 3, 4);
            var e4 = RTF.PointType.Point(4.5, 3, 4);

            Assert.Equal(e1, RTH.Transformations.Position(r, 0));
            Assert.Equal(e2, RTH.Transformations.Position(r, 1));
            Assert.Equal(e3, RTH.Transformations.Position(r, -1));
            Assert.Equal(e4, RTH.Transformations.Position(r, 2.5));
        }
示例#25
0
        public void ColorWithIntersecBehindRay()
        {
            var    w     = RTF.World.Default();
            Sphere outer = (Sphere)w.Objects[0];

            outer.Material.Ambient = 1;
            Sphere inner = (Sphere)w.Objects[1];

            inner.Material.Ambient = 1;
            var r = new RTF.Ray(
                RTF.PointType.Point(0, 0, 0.75),
                RTF.PointType.Vector(0, 0, -1));
            var c = w.ColorAt(r);

            Assert.Equal(inner.Material.Color, c);
        }
示例#26
0
        public void ShadingIntersection()
        {
            var w = RTF.World.Default();
            var r = new RTF.Ray(
                RTF.PointType.Point(0, 0, -5),
                RTF.PointType.Vector(0, 0, 1));
            var shape = w.Objects[0];
            var i     = new RTF.Intersection(4, shape);

            var comps = RTF.Computation.PrepareComputations(i, r);
            var c     = w.ShadeHit(comps);
            var exp   = new RTF.Color(0.38066, 0.47583, 0.2855);

            //var exp = new RTF.Color(0.50066, 0.57583, 0.42550);

            CustomAssert.Equal(exp, c, 5);
        }
示例#27
0
        public void ShadingIntersectionFromInside()
        {
            var w = RTF.World.Default();

            w.Lights = new System.Collections.Generic.List <RTF.Light>()
            {
                new RTF.Light(
                    RTF.PointType.Point(0, 0.25, 0),
                    RTF.Color.White)
            };
            var r = new RTF.Ray(
                RTF.PointType.Point(0, 0, 0),
                RTF.PointType.Vector(0, 0, 1));
            var shape = w.Objects[1];

            var i     = new RTF.Intersection(0.5, shape);
            var comps = RTF.Computation.PrepareComputations(i, r);
            var c     = w.ShadeHit(comps);
            var exp   = new RTF.Color(0.90498, 0.90498, 0.90498);

            CustomAssert.Equal(exp, c, 5);
        }