Exemplo n.º 1
0
        public void LightOnSurfaceInShadow()
        {
            var s        = new Sphere();
            var m        = new Material();
            var position = Point.Zero;
            var eyeV     = new Vector(0, 0, -1f);
            var normal   = new Normal(0, 0, -1);
            var light    = new PointLight(new Point(0, 0, -10), new Color(1f, 1f, 1f));
            var result   = PhongShading.Lighting(m, s, light, position, eyeV, normal, 0);

            result.Should().BeEquivalentTo(new Color(0.1f, 0.1f, 0.1f));
        }
Exemplo n.º 2
0
        public void FullReflectionOfLightOffset45Deg()
        {
            var s        = new Sphere();
            var m        = new Material();
            var position = Point.Zero;
            var eyeV     = new Vector(0, -MathF.Sqrt(2) / 2, -MathF.Sqrt(2) / 2);
            var normal   = new Normal(0, 0, -1);
            var light    = new PointLight(new Point(0, 10, -10), new Color(1f, 1f, 1f));
            var result   = PhongShading.Lighting(m, s, light, position, eyeV, normal, 1);

            result.Should().BeEquivalentTo(new Color(1.6364f, 1.6364f, 1.6364f));
        }
Exemplo n.º 3
0
        public void LightOnSurfaceInPartialShadow()
        {
            var s = new Sphere();
            var m = new Material {
                Ambient = 0.1f, Diffuse = 0.9f, Specular = 0
            };
            var position = new Point(0, 0, -1);
            var eyeV     = new Vector(0, 0, -1f);
            var normal   = new Normal(0, 0, -1);
            var light    = new PointLight(new Point(0, 0, -10), new Color(1f, 1f, 1f));
            var result   = PhongShading.Lighting(m, s, light, position, eyeV, normal, 0.5f);

            result.Should().BeEquivalentTo(new Color(0.55f, 0.55f, 0.55f));
        }
Exemplo n.º 4
0
        public void LightingWithPatternApplied()
        {
            var s = new Sphere();
            var m = new Material
            {
                Texture  = new StripeTexture(Colors.White, Colors.Black),
                Ambient  = 1f,
                Diffuse  = 0f,
                Specular = 0f
            };
            var eye    = new Vector(0, 0, -1);
            var normal = new Normal(0, 0, -1);
            var light  = new PointLight(new Point(0, 0, -10), Colors.White);
            var c1     = PhongShading.Lighting(m, s, light, new Point(0.9f, 0, 0), eye, normal, 1);
            var c2     = PhongShading.Lighting(m, s, light, new Point(1.1f, 0, 0), eye, normal, 1);

            c1.Should().Be(Colors.White);
            c2.Should().Be(Colors.Black);
        }
Exemplo n.º 5
0
        public void RaycastTest()
        {
            const int canvasPixels = 100;
            var       canvas       = new Canvas(canvasPixels, canvasPixels);
            var       s            = new Sphere {
                Material = { Texture = new SolidColor(new Color(0.4f, 0.2f, 1)) }
            };
            var light = new PointLight(new Point(-10, 10, -10), new Color(1f, 1f, 1f));
            //var t = Transforms.Shear(0.1f, 0, 0, 0, 0, 0).Scale(0.9f, 1f, 1f);
            //s.SetTransform(t);
            var         rayOrigin = new Point(0f, 0f, -5f);
            const float wallZ     = 10f;
            const float wallSize  = 7.0f;
            const float pixelSize = wallSize / canvasPixels;
            const float half      = wallSize / 2;

            for (var y = 0; y < canvasPixels; y++)
            {
                var worldY = half - pixelSize * y;
                for (var x = 0; x < canvasPixels; x++)
                {
                    var worldX   = -half + pixelSize * x;
                    var position = new Point(worldX, worldY, wallZ);
                    var r        = new Ray(rayOrigin, (position - rayOrigin).Normalize());
                    var xs       = s.Intersects(r);
                    var hit      = xs.Hit();
                    if (!hit.HasValue)
                    {
                        continue;
                    }

                    var point  = r.Position(hit.Value.T);
                    var shape  = hit.Value.Geometry;
                    var normal = shape.NormalAt(point, hit.Value);
                    var eye    = -r.Direction;
                    var color  = PhongShading.Lighting(shape.Material, shape, light, point, eye, normal, 1);
                    canvas.WritePixel(color, x, y);
                }
            }

            PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "raycast");
        }
Exemplo n.º 6
0
        public void CalculateLightingWithAreaLight()
        {
            var corner = new Point(-0.5f, -0.5f, -5);
            var v1     = new Vector(1, 0, 0);
            var v2     = new Vector(0, 1, 0);
            var light  = new AreaLight(corner, v1, 2, v2, 2, Colors.White);
            var s      = new Sphere
            {
                Material = { Ambient = 0.1f, Diffuse = 0.9f, Specular = 0f, Texture = new SolidColor(Colors.White) }
            };
            var eye    = new Point(0, 0, -5);
            var pt     = new Point(0, 0, -1);
            var eyeV   = (eye - pt).Normalize();
            var normal = new Normal(pt.X, pt.Y, pt.Z);
            var r      = PhongShading.Lighting(s.Material, s, light, pt, eyeV, normal, 1.0f);

            r.Should().Be(new Color(0.9965f, 0.9965f, 0.9965f));

            pt     = new Point(0, 0.7071f, -0.7071f);
            eyeV   = (eye - pt).Normalize();
            normal = new Normal(pt.X, pt.Y, pt.Z);
            r      = PhongShading.Lighting(s.Material, s, light, pt, eyeV, normal, 1.0f);
            r.Should().Be(new Color(0.6232f, 0.6232f, 0.6232f));
        }