Example #1
0
        public void StripedPattern()
        {
            Pattern pattern = new StripedPattern(white, black);

            Assert.AreEqual(pattern.A, white);
            Assert.AreEqual(pattern.B, black);
        }
Example #2
0
        public void TestCreate()
        {
            var pattern = new StripedPattern(_white, _black);

            Assert.AreEqual(_white, pattern.A);
            Assert.AreEqual(_black, pattern.B);
        }
Example #3
0
        public void Lighting_ShouldApplyColorsBasedOnTheMaterialPattern()
        {
            // arrange
            var light  = new PointLight(new(0f, 0f, -10f), Color.White);
            var shape  = new Sphere();
            var eye    = new Vector(0f, 0f, -1f);
            var normal = new Vector(0f, 0f, -1f);
            var sut    = Material.Default with
            {
                AmbientReflection = 1f,
                DiffuseReflection = 0f,
                Pattern           = new StripedPattern
                {
                    Patterns = ImmutableArray.Create <Pattern>(
                        new SolidPattern(Color.White),
                        new SolidPattern(Color.Black)
                        )
                },
                SpecularReflection = 0f
            };

            // act
            var color1 = sut.Lighting(light, shape, new Point(0.9f, 0f, 0f), eye, normal, true);
            var color2 = sut.Lighting(light, shape, new Point(1.1f, 0f, 0f), eye, normal, true);

            // assert
            using var _ = new AssertionScope();
            color1.Should().Be(Color.White);
            color2.Should().Be(Color.Black);
        }
Example #4
0
        public void AssigneTransformation()
        {
            StripedPattern pattern = new StripedPattern();

            pattern.Transform = Matrix4.TranslateMatrix(1, 2, 3);

            Assert.True(pattern.Transform == Matrix4.TranslateMatrix(1, 2, 3));
        }
Example #5
0
        public void TestConstantZ()
        {
            var pattern = new StripedPattern(_white, _black);

            Assert.AreEqual(_white, pattern.ColorAt(Tuple.Point(0, 0, 0)));
            Assert.AreEqual(_white, pattern.ColorAt(Tuple.Point(0, 0, 1)));
            Assert.AreEqual(_white, pattern.ColorAt(Tuple.Point(0, 0, 2)));
        }
Example #6
0
        public void StripedPatternZConst()
        {
            Pattern pattern = new StripedPattern(white, black);

            Assert.AreEqual(pattern.PatternAt(Tuple.Point(0, 0, 0)), white);
            Assert.AreEqual(pattern.PatternAt(Tuple.Point(0, 0, 1)), white);
            Assert.AreEqual(pattern.PatternAt(Tuple.Point(0, 0, 2)), white);
        }
Example #7
0
        public void CreateStripePattern()
        {
            Pattern black = new SolidPattern(Color.Black);
            Pattern white = new SolidPattern(Color.White);

            StripedPattern pattern = new StripedPattern(white, black);

            Assert.True(white == pattern.p1);
            Assert.True(black == pattern.p2);
        }
Example #8
0
        public void StripesWithPatternTransformation()
        {
            Sphere  s       = new Sphere();
            Pattern pattern = new StripedPattern(white, black);

            pattern.Transform = new Scale(2, 2, 2).GetTransform();
            Color c = pattern.PatternAtShape(s, Tuple.Point(1.5, 0, 0));

            Assert.AreEqual(c, white);
        }
Example #9
0
        public void TestObjectTransform()
        {
            var o = new Sphere
            {
                Transform = Matrix4.Scaling(2, 2, 2)
            };
            var pattern = new StripedPattern(_white, _black);

            Assert.AreEqual(_white, pattern.ColorAtObject(o.WorldToObject(Tuple.Point(1.5, 0, 0))));
        }
Example #10
0
        public void StripePatternConstantZ()
        {
            Pattern black = new SolidPattern(Color.Black);
            Pattern white = new SolidPattern(Color.White);

            StripedPattern pattern = new StripedPattern(white, black);

            Assert.True(Color.White == pattern.PatternAt(new Point(0, 0, 0)));
            Assert.True(Color.White == pattern.PatternAt(new Point(0, 0, 1)));
            Assert.True(Color.White == pattern.PatternAt(new Point(0, 0, 2)));
        }
Example #11
0
        public void StripedPatternXChanges()
        {
            Pattern pattern = new StripedPattern(white, black);

            Assert.AreEqual(pattern.PatternAt(Tuple.Point(0, 0, 0)), white);
            Assert.AreEqual(pattern.PatternAt(Tuple.Point(0.9, 0, 0)), white);
            Assert.AreEqual(pattern.PatternAt(Tuple.Point(1, 0, 0)), black);
            Assert.AreEqual(pattern.PatternAt(Tuple.Point(-0.1, 0, 0)), black);
            Assert.AreEqual(pattern.PatternAt(Tuple.Point(-1, 0, 0)), black);
            Assert.AreEqual(pattern.PatternAt(Tuple.Point(-1.1, 0, 0)), white);
        }
Example #12
0
        public void TestVariableX()
        {
            var pattern = new StripedPattern(_white, _black);

            Assert.AreEqual(_white, pattern.ColorAt(Tuple.Point(0, 0, 0)));
            Assert.AreEqual(_white, pattern.ColorAt(Tuple.Point(0.9, 0, 0)));
            Assert.AreEqual(_black, pattern.ColorAt(Tuple.Point(1, 0, 0)));

            Assert.AreEqual(_black, pattern.ColorAt(Tuple.Point(-0.1, 0, 0)));
            Assert.AreEqual(_black, pattern.ColorAt(Tuple.Point(-1, 0, 0)));
            Assert.AreEqual(_white, pattern.ColorAt(Tuple.Point(-1.1, 0, 0)));
        }
Example #13
0
        public void StripePatternAlternatesX()
        {
            Pattern black = new SolidPattern(Color.Black);
            Pattern white = new SolidPattern(Color.White);

            StripedPattern pattern = new StripedPattern(white, black);

            Assert.True(Color.White == pattern.PatternAt(new Point(0, 0, 0)));
            Assert.True(Color.White == pattern.PatternAt(new Point(0.9f, 0, 0)));
            Assert.True(Color.Black == pattern.PatternAt(new Point(1, 0, 0)));
            Assert.True(Color.Black == pattern.PatternAt(new Point(-0.1f, 0, 0)));
            Assert.True(Color.Black == pattern.PatternAt(new Point(-1, 0, 0)));
            Assert.True(Color.White == pattern.PatternAt(new Point(-1.1f, 0, 0)));
        }
Example #14
0
        public void StripesObjectTransform()
        {
            Pattern black = new SolidPattern(Color.Black);
            Pattern white = new SolidPattern(Color.White);

            Sphere s = new Sphere();

            s.Transform = Matrix4.ScaleMatrix(2, 2, 2);

            StripedPattern pattern = new StripedPattern(white, black);

            Color result = pattern.PatternAtObject(s, new Point(1.5f, 0, 0));

            Assert.True(Color.White == result);
        }
Example #15
0
        public void LightingWithPattern()
        {
            Pattern black = new SolidPattern(Color.Black);
            Pattern white = new SolidPattern(Color.White);

            Sphere s = new Sphere();

            StripedPattern pattern  = new StripedPattern(white, black);
            Material       material = new Material(pattern: pattern, ambient: 1.0f, diffuse: 0.0f, specular: 0.0f);

            Vector3 eyeV    = new Vector3(0, 0, -1);
            Vector3 normalV = new Vector3(0, 0, -1);

            Light light = new Light(Color.White, new Point(0, 0, -10));

            Color c1 = material.Lighting(material, s, light, new Point(0.9f, 0, 0), eyeV, normalV, false);
            Color c2 = material.Lighting(material, s, light, new Point(1.9f, 0, 0), eyeV, normalV, false);

            Assert.True(new Color(1, 1, 1) == c1);
            Assert.True(new Color(0, 0, 0) == c2);
        }
Example #16
0
        public void DefaultPatternTransform()
        {
            StripedPattern pattern = new StripedPattern();

            Assert.True(pattern.Transform == new Matrix4());
        }
Example #17
0
        // Patterns
        public static void Chapter10()
        {
            Plane floor = new Plane();

            floor.material          = new Material();
            floor.material.mColor   = new Color(1, 0.9f, 0.9f);
            floor.material.Pattern  = new CheckerPattern(SolidPattern.White, SolidPattern.White * .25f);
            floor.material.Specular = 0;

            Plane backWall = new Plane();

            backWall.Transform = Matrix4.TranslateMatrix(0, 0, 10) * Matrix4.RotateMatrix_X(Math.PI / 2);
            StripedPattern s01 = new StripedPattern(SolidPattern.White, SolidPattern.Black);
            StripedPattern s02 = new StripedPattern(SolidPattern.Red, SolidPattern.Green);

            s02.Transform              = Matrix4.RotateMatrix_Y((float)Math.PI / 4);
            backWall.material.Pattern  = new BlendPattern(s01, s02);
            backWall.material.Specular = 0;

            Plane wallRight = new Plane();

            wallRight.Transform       = Matrix4.TranslateMatrix(4, 0, 0) * Matrix4.RotateMatrix(0, 0, Math.PI / 2);
            wallRight.material        = new Material();
            wallRight.material.mColor = Color.Blue;
            floor.material.Specular   = 0;

            Plane wallLeft = new Plane();

            wallLeft.Transform        = Matrix4.TranslateMatrix(-4, 0, 0) * Matrix4.RotateMatrix(0, 0, Math.PI / 2);
            wallLeft.material         = new Material();
            wallLeft.material.mColor  = Color.Red;
            wallLeft.material.Pattern = new CheckerPattern(new StripedPattern(SolidPattern.Yellow, SolidPattern.Green), new RingPattern(SolidPattern.White, SolidPattern.Black));
            floor.material.Specular   = 0;

            Sphere middle = new Sphere();

            middle.Transform                  = Matrix4.TranslateMatrix(-1.5f, 1, 2.0f);
            middle.material                   = new Material();
            middle.material.mColor            = new Color(0.1f, 1, 0.5f);
            middle.material.Pattern           = new TestPattern();
            middle.material.Pattern.Transform = Matrix4.ScaleMatrix(.25f, .25f, .25f);
            middle.material.Diffuse           = 0.7f;
            middle.material.Specular          = 0.3f;

            Sphere middle02 = new Sphere();

            middle02.Transform                  = Matrix4.TranslateMatrix(1.5f, 1, 2.0f);
            middle02.material                   = new Material();
            middle02.material.mColor            = new Color(0.1f, 1, 0.5f);
            middle02.material.Pattern           = new RadialGradientPattern(SolidPattern.Purple, SolidPattern.Orange);
            middle02.material.Pattern.Transform = Matrix4.RotateMatrix_Z((float)Math.PI / 2) * Matrix4.ScaleMatrix(0.25f, 0.25f, 0.25f);
            middle02.material.Diffuse           = 0.7f;
            middle02.material.Specular          = 0.3f;

            Sphere middle03 = new Sphere();

            middle03.Transform                  = Matrix4.TranslateMatrix(0, .5f, -1.5f) * Matrix4.ScaleMatrix(.5f, .5f, .5f);
            middle03.material                   = new Material();
            middle03.material.mColor            = new Color(0.1f, 1, 0.5f);
            middle03.material.Pattern           = new CheckerPattern(SolidPattern.Yellow, SolidPattern.Green);
            middle03.material.Pattern.Transform = Matrix4.ScaleMatrix(0.5f, 0.5f, 0.5f);
            middle03.material.Diffuse           = 0.7f;
            middle03.material.Specular          = 0.3f;

            Sphere right = new Sphere();

            right.Transform                  = Matrix4.TranslateMatrix(2.0f, 1.0f, -0.5f) * Matrix4.ScaleMatrix(0.85f, 0.25f, 0.85f);
            right.material                   = new Material();
            right.material.mColor            = new Color(0.5f, 1, 0.1f);
            right.material.Pattern           = new StripedPattern(SolidPattern.White, SolidPattern.White * .25f);
            right.material.Pattern.Transform = Matrix4.ScaleMatrix(.15f, 1, 1);
            right.material.Diffuse           = 0.4f;
            right.material.Specular          = 0.6f;

            Sphere left = new Sphere();

            left.Transform         = Matrix4.TranslateMatrix(-2.5f, 0.33f, -0.75f) * Matrix4.ScaleMatrix(0.33f, 0.33f, 0.33f);
            left.material          = new Material();
            left.material.mColor   = new Color(1.0f, 0.8f, 0.1f);
            left.material.Pattern  = new GradientPattern(SolidPattern.Yellow, SolidPattern.Blue);
            left.material.Diffuse  = 0.7f;
            left.material.Specular = 0.1f;

            // Create Scene
            Scene        scene  = new Scene();
            Light        l1     = new Light(Color.White * .8f, new Point(-3, 10, -10));
            Light        l2     = new Light(new Color(0.5f, 0.5f, 0.65f), new Point(3, 10, -5));
            List <Light> lights = new List <Light>()
            {
                l1, l2
            };

            scene.Lights = lights;


            List <RayObject> sceneObjects = new List <RayObject>()
            {
                floor, backWall, wallRight, wallLeft, middle, middle02, middle03, right, left
            };

            scene.Objects = sceneObjects;

            // Create Camera
            Camera cam = new Camera(1920 / 4, 1080 / 4, (float)Math.PI / 4);

            cam.Transform = cam.ViewTransform(new Point(0, 2f, -8),
                                              new Point(0, 1f, 0),
                                              new Vector3(0, 1, 0));
            Canvas image = cam.Render(scene); // Outputs image



            // Save Canvas to ppm
            Console.WriteLine("\nSaving PPM file");
            string filePath = "C:\\Dev\\C#\\PracticePrograms\\RayTracerChallenge\\__renders";
            string fileName = "Chapter10Challenge_07";
            string fileDirectoryComplete = filePath + "\\" + fileName + ".ppm";

            Save.PPM(fileDirectoryComplete, image);

            Console.WriteLine("Done: Program complete.");
            Console.ReadKey();
        }
Example #18
0
        public static void GenerateImage()
        {
            var boxMaterial = Material.Default with
            {
                Pattern = new SolidPattern(new Color(1f, 0.9f, 0.9f)), SpecularReflection = 0f
            };

            var floor = new InfinitePlane {
                Material = boxMaterial, Normal = Vector.UnitY
            };

            var leftWall = new InfinitePlane
            {
                Material  = boxMaterial,
                Normal    = Vector.UnitX,
                Transform = Matrix4.Translation(3f, 0f, 0f)
            };

            var rightWall = new InfinitePlane
            {
                Material  = boxMaterial,
                Normal    = Vector.UnitZ,
                Transform = Matrix4.Translation(0f, 0f, 3f)
            };

            var sphere = new Sphere
            {
                Material = Material.Default with
                {
                    DiffuseReflection = 0.7f,
                    Pattern           = new StripedPattern
                    {
                        Patterns = ImmutableArray.Create <Pattern>(
                            new StripedPattern
                        {
                            Patterns = ImmutableArray.Create <Pattern>(
                                new StripedPattern
                            {
                                Patterns = ImmutableArray.Create <Pattern>(
                                    new SolidPattern(Color.Red),
                                    new SolidPattern(Color.White)
                                    ),
                                Transform = Matrix4.Identity.RotateZ(MathF.PI / 4f)
                                            .Scale(0.25f, 0.25f, 0.25f)
                            },
                                new StripedPattern
                            {
                                Patterns = ImmutableArray.Create <Pattern>(
                                    new SolidPattern(Color.Green),
                                    new SolidPattern(Color.White)
                                    ),
                                Transform = Matrix4.Identity.RotateZ(-MathF.PI / 4f)
                                            .Scale(0.25f, 0.25f, 0.25f)
                            }
                                ),
                            Transform = Matrix4.Identity.RotateZ(MathF.PI / 4f)
                                        .Scale(0.25f, 0.25f, 0.25f)
                        },
                            new StripedPattern
                        {
                            Patterns = ImmutableArray.Create <Pattern>(
                                new StripedPattern
                            {
                                Patterns = ImmutableArray.Create <Pattern>(
                                    new SolidPattern(Color.Blue),
                                    new SolidPattern(Color.White)
                                    ),
                                Transform = Matrix4.Identity.RotateZ(MathF.PI / 4f)
                                            .Scale(0.25f, 0.25f, 0.25f)
                            },
                                new StripedPattern
                            {
                                Patterns = ImmutableArray.Create <Pattern>(
                                    new SolidPattern(new Color(1f, 0.99f, 0.23f)),
                                    new SolidPattern(Color.White)
                                    ),
                                Transform = Matrix4.Identity.RotateZ(-MathF.PI / 4f)
                                            .Scale(0.25f, 0.25f, 0.25f)
                            }
                                ),
                            Transform = Matrix4.Identity.RotateZ(-MathF.PI / 4f)
                                        .Scale(0.25f, 0.25f, 0.25f)
                        }
                            ),
                        Transform = Matrix4.Identity.RotateZ(MathF.PI / 2f)
                                    .Scale(0.25f, 0.25f, 0.25f)
                    },
                    SpecularReflection = 0.5f
                },