Beispiel #1
0
        public void TestIntersectionInShade()
        {
            var world = new World();

            world.Light = new PointLight(Tuple.Point(0, 0, -10), new Color(1, 1, 1));

            world.Shapes.Add(new Sphere());
            world.Shapes.Add(new Sphere
            {
                Transform = Matrix4.Translation(0, 0, 10)
            });

            var s2 = new Sphere
            {
                Transform = Matrix4.Translation(0, 0, 10)
            };

            world.Shapes.Add(s2);

            var ray = new Ray(Tuple.Point(0, 0, 5), Tuple.Vector(0, 0, 1));
            var i   = new Intersection(4, s2);

            var comp  = Computation.Prepare(i, ray);
            var color = LightUtil.ShadeHit(world, comp);

            Assert.AreEqual(new Color(0.1, 0.1, 0.1), color);
        }
Beispiel #2
0
        public void TestShadeIntersection()
        {
            var world  = WorldBuilder.DefaultWorld();
            var ray    = new Ray(Tuple.Point(0, 0, -5), Tuple.Vector(0, 0, 1));
            var sphere = world.Shapes[0];
            var i      = new Intersection(4, sphere);

            var comp  = Computation.Prepare(i, ray);
            var color = LightUtil.ShadeHit(world, comp);

            Assert.AreEqual(new Color(0.38066, 0.47583, 0.2855), color);
        }
Beispiel #3
0
        public Color ColorAt(Ray ray, int remaining = 5)
        {
            var intersections = Intersect(ray);
            var hit           = intersections.Hit();

            if (hit is null)
            {
                return(new Color(0, 0, 0));
            }

            var comps = Computation.Prepare(hit, ray, intersections);

            return(LightUtil.ShadeHit(this, comps, remaining));
        }
Beispiel #4
0
        public void TestShadeIntersectionInside()
        {
            var world = WorldBuilder.DefaultWorld();

            world.Light = new PointLight(Tuple.Point(0, 0.25, 0), new Color(1, 1, 1));
            var ray    = new Ray(Tuple.Point(0, 0, 0), Tuple.Vector(0, 0, 1));
            var sphere = world.Shapes[1];
            var i      = new Intersection(0.5, sphere);

            var comp  = Computation.Prepare(i, ray);
            var color = LightUtil.ShadeHit(world, comp);

            Assert.AreEqual(new Color(0.90498, 0.90498, 0.90498), color);
        }
Beispiel #5
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");
        }
Beispiel #6
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);
        }
Beispiel #7
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);
        }
Beispiel #8
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);
        }
Beispiel #9
0
        public static Vertex2D BuildVertex2D(Vertex vertex, Matrix4 transMat, Vector3 cameraPos, List <IPosLight> lights, int w, int h)
        {
            Vertex2D   result = default(Vertex2D);
            SoftDevice device = SoftDevice.Default;

            if (null == device)
            {
                return(result);
            }
            // 将3d坐标投影到2d空间;
            result = ProjectToScreen(vertex.position, transMat, (float)w, (float)h);
            // 顶点光照;
            LightUtil.ApplyVertexLightings(lights, cameraPos, vertex.worldPosition, vertex.worldNormal, ref result);
            // 填充顶点信息;
            result.FillRenderData(vertex.uv0, vertex.color, device.PerspectiveCorrection);
            return(result);
        }
Beispiel #10
0
        public void TestShadeHitOnReflectiveShape()
        {
            var world = WorldBuilder.DefaultWorld();

            var plane = new Plane
            {
                Transform = Matrix4.Translation(0, -1, 0)
            };

            plane.Material.Reflective = 0.5;
            world.Shapes.Add(plane);

            var ray = new Ray(Tuple.Point(0, 0, -3), Tuple.Vector(0, -0.5 * System.Math.Sqrt(2), 0.5 * System.Math.Sqrt(2)));
            var i   = new Intersection(System.Math.Sqrt(2), plane);

            var comp = Computation.Prepare(i, ray);
            var col  = LightUtil.ShadeHit(world, comp);

            Assert.AreEqual(new Color(0.876757, 0.924340, 0.829174), col);
        }
Beispiel #11
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));
        }
Beispiel #12
0
        public void TestRefractShadeHit()
        {
            var world = WorldBuilder.DefaultWorld();
            var floor = new Plane
            {
                Transform = Matrix4.Translation(0, -1, 0),
                Material  = new Material
                {
                    Reflective      = 0.5,
                    Transparency    = 0.5,
                    RefractiveIndex = 1.5
                }
            };
            var ball = new Sphere
            {
                Transform = Matrix4.Translation(0, -3.5, -0.5),
                Material  = new Material
                {
                    Color   = new Color(1, 0, 0),
                    Ambient = 0.5
                }
            };

            world.Shapes.Add(floor);
            world.Shapes.Add(ball);

            var ray = new Ray(Tuple.Point(0, 0, -3), Tuple.Vector(0, -0.5 * System.Math.Sqrt(2), 0.5 * System.Math.Sqrt(2)));

            var xs = new Intersections
            {
                new Intersection(System.Math.Sqrt(2), floor),
            };

            var comps = Computation.Prepare(xs[0], ray, xs);
            var color = LightUtil.ShadeHit(world, comps, 5);

            Assert.AreEqual(new Color(0.93391, 0.69643, 0.69243), color);
            //Assert.AreEqual(new Color(0.93642, 0.68642, 0.68642), color);
        }
Beispiel #13
0
 private void Start()
 {
     instance = this;
     FindLights();
 }