Ejemplo n.º 1
0
        public void ReadingPpmFile_ShouldRespectTheScaleSetting()
        {
            var file =
                @"P3
2 2
100
100 100 100  50 50 50
75 50 25  0 0 0";

            var reader = new StringReader(file);
            var canvas = PpmReader.ReadCanvasFromPpm(reader);

            Assert.Equal(new Color(0.75, 0.5, 0.25), canvas.GetPixel(0, 1));
        }
Ejemplo n.º 2
0
        public void ReadingPpmFileWithTheWrongMagicNumber_ShouldFail()
        {
            var file =
                @"P32
1 1
255
0 0 0";

            var reader = new StringReader(file);
            var canvas = PpmReader.ReadCanvasFromPpm(reader);

            Assert.Equal(0, canvas.Width);
            Assert.Equal(0, canvas.Height);
        }
Ejemplo n.º 3
0
        public void ReadingPixelDataFromPpmFile_ShouldCreateCanvas(int x, int y, Color expected)
        {
            var file =
                @"P3
4 3
255
255 127 0  0 127 255  127 255 0  255 255 255
0 0 0  255 0 0  0 255 0  0 0 255
255 255 0  0 255 255  255 0 255  127 127 127";

            var reader = new StringReader(file);
            var canvas = PpmReader.ReadCanvasFromPpm(reader);

            Assert.Equal(expected, canvas.GetPixel(x, y), ColorComparer);
        }
Ejemplo n.º 4
0
        public void ReadingPpmFile_ShouldAllowAnRgbTripleToSpanLines()
        {
            var file =
                @"P3
1 1
255
51
153

204";

            var reader = new StringReader(file);
            var canvas = PpmReader.ReadCanvasFromPpm(reader);

            Assert.Equal(new Color(0.2, 0.6, 0.8), canvas.GetPixel(0, 0));
        }
Ejemplo n.º 5
0
        public void ReadingPpmFile_ShouldReturnCanvasWithCorrectDimensions()
        {
            var file =
                @"P3
10 2
255
0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
0 0 0  0 0 0  0 0 0  0 0 0  0 0 0
0 0 0  0 0 0  0 0 0  0 0 0  0 0 0";

            var reader = new StringReader(file);
            var canvas = PpmReader.ReadCanvasFromPpm(reader);

            Assert.Equal(10, canvas.Width);
            Assert.Equal(2, canvas.Height);
        }
Ejemplo n.º 6
0
        public void ReadingPpmFile_ShouldIgnoreCommentLines()
        {
            var file =
                @"P3
# this is a comment
2 1
# this, too
255
# another comment
255 255 255
# oh, no, comments in the pixel data!
255 0 255";

            var reader = new StringReader(file);
            var canvas = PpmReader.ReadCanvasFromPpm(reader);

            Assert.Equal(Color.White, canvas.GetPixel(0, 0));
            Assert.Equal(new Color(1, 0, 1), canvas.GetPixel(1, 0));
        }
Ejemplo n.º 7
0
        public void ReadingCheckerPatternFromPpmFile_ShouldReturnCanvas(double u, double v, Color expected)
        {
            var file =
                @"P3
10 10
10
0 0 0  1 1 1  2 2 2  3 3 3  4 4 4  5 5 5  6 6 6  7 7 7  8 8 8  9 9 9
1 1 1  2 2 2  3 3 3  4 4 4  5 5 5  6 6 6  7 7 7  8 8 8  9 9 9  0 0 0
2 2 2  3 3 3  4 4 4  5 5 5  6 6 6  7 7 7  8 8 8  9 9 9  0 0 0  1 1 1
3 3 3  4 4 4  5 5 5  6 6 6  7 7 7  8 8 8  9 9 9  0 0 0  1 1 1  2 2 2
4 4 4  5 5 5  6 6 6  7 7 7  8 8 8  9 9 9  0 0 0  1 1 1  2 2 2  3 3 3
5 5 5  6 6 6  7 7 7  8 8 8  9 9 9  0 0 0  1 1 1  2 2 2  3 3 3  4 4 4
6 6 6  7 7 7  8 8 8  9 9 9  0 0 0  1 1 1  2 2 2  3 3 3  4 4 4  5 5 5
7 7 7  8 8 8  9 9 9  0 0 0  1 1 1  2 2 2  3 3 3  4 4 4  5 5 5  6 6 6
8 8 8  9 9 9  0 0 0  1 1 1  2 2 2  3 3 3  4 4 4  5 5 5  6 6 6  7 7 7
9 9 9  0 0 0  1 1 1  2 2 2  3 3 3  4 4 4  5 5 5  6 6 6  7 7 7  8 8 8";

            var reader  = new StringReader(file);
            var canvas  = PpmReader.ReadCanvasFromPpm(reader);
            var pattern = new UvImage(canvas);
            var color   = pattern.UvPatternAt(u, v);

            Assert.Equal(expected, color);
        }
Ejemplo n.º 8
0
        public (World, Camera) Setup(int width, int height, double fov)
        {
            // ======================================================
            // the camera
            // ======================================================

            var camera = new Camera(width, height, fov)
            {
                Transform = Transformation.ViewTransform(
                    new Point(1, 2, -5),           // view from
                    new Point(0, 2, 0),            // view to
                    new Vector(0, 1, 0)),          // vector up

                ProgressMonitor = new ParallelConsoleProgressMonitor(height),
            };

            // ======================================================
            // light sources
            // ======================================================

            var light = new PointLight(
                new Point(-20, 20, -20),
                new Color(1, 1, 1)
                );
            var light2 = new PointLight(
                new Point(-20, 10, -5),
                new Color(1, 1, 1)
                );
            var areaLight = new AreaLight(
                new Point(-100, 100, -100),
                new Vector(4, 0, 0),
                6,
                new Vector(0, 4, 0),
                6,
                new Color(1.0, 1.0, 1.0)
                );


            var floor = new Plane()
            {
                Material = new Material()
                {
                    Color      = Color.White,
                    Ambient    = 0.1,
                    Diffuse    = 0.1,
                    Specular   = 0.0,
                    Reflective = 0.4,
                },
            };


            FileStream   stream       = File.OpenRead("/Users/rhagan/VSCode Projects/RayTracer/RayTracer.Program/Scenes/Textures/brickwall.ppm");
            StreamReader reader       = new StreamReader(stream);
            var          wallTexture  = PpmReader.ReadCanvasFromPpm(reader);
            var          textureLeft  = new UvImage(wallTexture);
            var          textureFront = textureLeft;
            var          textureRight = textureLeft;
            var          textureBack  = textureLeft;
            var          textureUp    = textureLeft;
            var          textureDown  = textureLeft;

            stream = File.OpenRead("/Users/rhagan/VSCode Projects/RayTracer/RayTracer.Program/Scenes/Textures/brickwall_normal.ppm");
            reader = new StreamReader(stream);
            var wallNormalMap = PpmReader.ReadCanvasFromPpm(reader);
            var normalLeft    = new UvImage(wallNormalMap);
            var normalFront   = normalLeft;
            var normalRight   = normalLeft;
            var normalBack    = normalLeft;
            var normalUp      = normalLeft;
            var normalDown    = normalLeft;

            var mapCubeMaterial = new Material()
            {
                Pattern   = new CubeMap(textureLeft, textureFront, textureRight, textureBack, textureUp, textureDown),
                NormalMap = new CubeMap(normalLeft, normalFront, normalRight, normalBack, normalUp, normalDown),
                Ambient   = 0.7,
                Specular  = 0.0,
                Diffuse   = 0.8,
            };

            var box = new Cube()
            {
                Transform =
                    Transformation.Translation(0, 2, 0) *
                    Transformation.Rotation_y(1.9),
                Material = mapCubeMaterial,
            };

            World world = new World();

            world.Shapes = new List <Shape> {
                floor, box
            };
            world.Lights = new List <ILight> {
                light
            };

            return(world, camera);
        }
Ejemplo n.º 9
0
        public (World, Camera) Setup(int width, int height, double fov)
        {
            // ======================================================
            // the camera
            // ======================================================

            var camera = new Camera(width, height, fov)
            {
                Transform = Transformation.ViewTransform(
                    new Point(1, 2, -7),           // view from
                    new Point(0, 1, 0),            // view to
                    new Vector(0, 1, 0)),          // vector up

                ProgressMonitor = new ParallelConsoleProgressMonitor(height),
            };

            // ======================================================
            // light sources
            // ======================================================

            var light = new PointLight(
                new Point(-100, 100, -100),
                new Color(1, 1, 1)
                );
            var areaLight = new AreaLight(
                new Point(-100, 100, -100),
                new Vector(4, 0, 0),
                6,
                new Vector(0, 4, 0),
                6,
                new Color(1.0, 1.0, 1.0)
                );


            var floor = new Plane()
            {
                Material = new Material()
                {
                    Color      = Color.White,
                    Ambient    = 0.1,
                    Diffuse    = 0.1,
                    Specular   = 0.0,
                    Reflective = 0.4,
                },
            };

            var pedestal = new Cylinder()
            {
                Minimum  = 0.0,
                Maximum  = 0.1,
                IsClosed = true,
                Material = new Material()
                {
                    Color      = Color.White,
                    Ambient    = 0.1,
                    Diffuse    = 0.2,
                    Specular   = 0.0,
                    Reflective = 0.1,
                },
            };

            FileStream   stream       = File.OpenRead("/Users/rhagan/VSCode Projects/RayTracer/RayTracer.Program/Scenes/Textures/earthmap1k.ppm");
            StreamReader reader       = new StreamReader(stream);
            var          earthTexture = PpmReader.ReadCanvasFromPpm(reader);

            stream = File.OpenRead("/Users/rhagan/VSCode Projects/RayTracer/RayTracer.Program/Scenes/Textures/earthspec1k.ppm");
            reader = new StreamReader(stream);
            var earthSpecMap = PpmReader.ReadCanvasFromPpm(reader);

            stream = File.OpenRead("/Users/rhagan/VSCode Projects/RayTracer/RayTracer.Program/Scenes/Textures/earthbump1k.ppm");
            reader = new StreamReader(stream);
            var earthBumpMap = PpmReader.ReadCanvasFromPpm(reader);
            var earth        = new Sphere()
            {
                Transform =
                    Transformation.Translation(0, 1.1, 0) *
                    Transformation.Rotation_y(1.9),
                Material = new Material()
                {
                    Pattern = new TextureMap(
                        new UvImage(earthTexture),
                        TextureMapper.SphericalMap
                        ),
                    SpecularMap = new TextureMap(
                        new UvImage(earthSpecMap),
                        TextureMapper.SphericalMap
                        ), /*
                            * BumpMap = new TextureMap(
                            * new UvImage(earthBumpMap),
                            * TextureMapper.SphericalMap
                            * ), */
                    Ambient    = 0.3,
                    Diffuse    = 0.9,
                    Specular   = 0.0,
                    Shininess  = 300,
                    Reflective = 0.7,
                },
            };

            stream = File.OpenRead("/Users/rhagan/VSCode Projects/RayTracer/RayTracer.Program/Scenes/Textures/earthcloudmap.ppm");
            reader = new StreamReader(stream);
            var cloudTexture = PpmReader.ReadCanvasFromPpm(reader);
            var clouds       = new Sphere()
            {
                Transform =
                    Transformation.Translation(0, 1.1, 0) *
                    Transformation.Scaling(1.1, 1.1, 1.1),
                Material = new Material()
                {
                    Pattern = new TextureMap(
                        new UvImage(cloudTexture),
                        TextureMapper.SphericalMap
                        ),
                    Diffuse      = 0.1,
                    Specular     = 0.0,
                    Shininess    = 100,
                    Ambient      = 0.1,
                    Transparency = 1.0,
                },
                HitBy = RayType.NoShadows,
            };


            World world = new World();

            world.Shapes = new List <Shape> {
                floor, pedestal, earth
            };
            world.Lights = new List <ILight> {
                light
            };

            return(world, camera);
        }