예제 #1
0
        static void Main(string[] args)
        {
            const int width  = 100;
            const int height = 100;
            var       canvas = new Canvas(width, height);

            var sphere = new Sphere();

            sphere.Material.Color = new Color(1, 0.2, 1);

            var light = new PointLight(Tuple.Point(-10, 10, -10), new Color(1, 1, 1));

            var origin = Tuple.Point(0, 0, -5);
            var red    = new Color(255, 0, 0);

            var scale = 0.01;

            for (int x = 0; x < width; x++)
            {
                System.Console.Write(".");
                for (int y = 0; y < height; y++)
                {
                    var ray = new Ray(origin,
                                      Tuple.Vector(scale * (x - 0.5 * width),
                                                   scale * (y - 0.5 * height), 1));
                    ray.Direction.Normalize();

                    var intersections = sphere.Intersect(ray);

                    if (intersections.Count > 0)
                    {
                        var hit       = intersections.Hit();
                        var hitObject = hit.Shape;
                        var point     = ray.Position(hit.Distance);
                        var normal    = hitObject.Normal(point);
                        var eye       = -ray.Direction;

                        var color = LightUtil.Lighting(hitObject.Material, hitObject, light, point, eye, normal, false);

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

            PPMWriter.WriteToFile(canvas, "RayCast2.ppm");
        }
예제 #2
0
        public void TestLightingStraightReflect()
        {
            var material = new Material();
            var s        = new Sphere
            {
                Material = material
            };
            var position = Tuple.Point(0, 0, 0);

            var eye    = Tuple.Vector(0, 0, -1);
            var normal = Tuple.Vector(0, 0, -1);
            var light  = new PointLight(Tuple.Point(0, 0, -10), new Color(1, 1, 1));

            var res = LightUtil.Lighting(material, s, light, position, eye, normal, false);

            Assert.AreEqual(new Color(1.9, 1.9, 1.9), res);
        }
예제 #3
0
        public void TestLightingAngleReflectHighlight()
        {
            var material = new Material();
            var s        = new Sphere
            {
                Material = material
            };
            var position = Tuple.Point(0, 0, 0);

            var eye    = Tuple.Vector(0, -0.5 * System.Math.Sqrt(2), -0.5 * System.Math.Sqrt(2));
            var normal = Tuple.Vector(0, 0, -1);
            var light  = new PointLight(Tuple.Point(0, 10, -10), new Color(1, 1, 1));

            var res = LightUtil.Lighting(material, s, light, position, eye, normal, false);

            Assert.AreEqual(new Color(1.6364, 1.6364, 1.6364), res);
        }
예제 #4
0
        public void TestLightingInShadow()
        {
            var material = new Material();
            var s        = new Sphere
            {
                Material = material
            };
            var position = Tuple.Point(0, 0, 0);

            var eye    = Tuple.Vector(0, 0, -1);
            var normal = Tuple.Vector(0, 0, -1);
            var light  = new PointLight(Tuple.Point(0, 0, -10), new Color(1, 1, 1));

            var res = LightUtil.Lighting(material, s, light, position, eye, normal, true);

            Assert.AreEqual(new Color(0.1, 0.1, 0.1), res);
        }
예제 #5
0
        public void TestLighting()
        {
            var m = new Material
            {
                Ambient  = 1,
                Diffuse  = 0,
                Specular = 0,
                Pattern  = new StripedPattern(_white, _black)
            };
            var s = new Sphere
            {
                Material = m
            };

            var eye    = Tuple.Vector(0, 0, -1);
            var normal = Tuple.Vector(0, 0, -1);
            var light  = new PointLight(Tuple.Point(0, 0, -10), new Color(1, 1, 1));

            Assert.AreEqual(_white, LightUtil.Lighting(m, s, light, Tuple.Point(0.9, 0, 0), eye, normal, false));
            Assert.AreEqual(_black, LightUtil.Lighting(m, s, light, Tuple.Point(1.1, 0, 0), eye, normal, false));
        }