Exemplo n.º 1
0
        public static void TestRender(int spp, int height)
        {
            var w = BuildWorld();

            var aspectRatio = 3f / 2;
            var width       = (int)(aspectRatio * height);
            //  var height = 800;
            var canvas = new Canvas(width, height);

            var pps    = new PerPixelSampler(spp);
            var camera = new ApertureCamera(MathF.PI / 3f, 3f / 2, 0.05f,
                                            new Point(0, 1.25f, -4f),
                                            new Point(0, 1, 0), Vectors.Up, 3.5f);
            var cws = new ComposableWorldSampler(2,
                                                 16,
                                                 GGXNormalDistribution.Instance,
                                                 GGXSmithGeometricShadow.Instance,
                                                 SchlickFresnelFunction.Instance,
                                                 w);

            var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps));

            Console.WriteLine("Rendering at {0}x{1}...", width, height);
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            ctx.Render();
            PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "scene");
            stopwatch.Stop();
            Console.WriteLine("Done ({0})", stopwatch.Elapsed);
        }
Exemplo n.º 2
0
        public static void MappingTestRender()
        {
            Cube Cube(float rotY, float rotX, float tx, float ty)
            {
                var cube1 = new Cube
                {
                    Material = { Texture = CreateTestCubeMap(), Roughness = 1f, SpecularColor = new Color(0.3f, 0.3f, 0.3f) }
                };

                cube1.SetTransform(Transform.RotateY(rotY).RotateX(rotX).Translate(tx, ty, 0));
                return(cube1);
            }

            var g = new Group();

            g.AddChild(Cube(0.7854f, 0.7854f, -6, 2));
            g.AddChild(Cube(2.3562f, 0.7854f, -2, 2));
            g.AddChild(Cube(3.927f, 0.7854f, 2, 2));
            g.AddChild(Cube(5.4978f, 0.7854f, 6, 2));
            g.AddChild(Cube(0.7854f, -0.7854f, -6, -2));
            g.AddChild(Cube(2.3562f, -0.7854f, -2, -2));
            g.AddChild(Cube(3.927f, -0.7854f, 2, -2));
            g.AddChild(Cube(5.4978f, -0.7854f, 6, -2));

            g.Divide(1);

            var w = new World();

            w.SetLights(new PointLight(new Point(0, 2, -100), Colors.White));
            w.SetObjects(g);

            var width  = 800;
            var height = 400;
            var from   = new Point(0, 0, -20f);
            var to     = new Point(0, 0, 0);

            var canvas      = new Canvas(width, height);
            var pps         = new PerPixelSampler(16);
            var fov         = 0.8f;
            var aspectRatio = (float)width / height;
            var transform   = Transform.LookAt(from, to, new Vector(0, 1, 0));
            var camera      = new PinholeCamera(transform, fov, aspectRatio);
            var cws         = new PhongWorldShading(1, w);
            var ctx         = new RenderContext(canvas, new RenderPipeline(cws, camera, pps));

            Console.WriteLine("Rendering at {0}x{1}...", width, height);
            var stopwatch = new Stopwatch();

            stopwatch.Start();

            ctx.Render();
            PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "mapping");
            stopwatch.Stop();
            Console.WriteLine("Done ({0})", stopwatch.Elapsed);
        }
Exemplo n.º 3
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.º 4
0
        public void ProjectileTest()
        {
            var start = new Point(0, 1, 0);
            var vel   = new Vector(1f, 1.8f, 0f).Normalize() * 11.25f;
            var p     = new Projectile(start, vel);
            var w     = new SimWorld(new Vector(0f, -0.1f, 0f), new Vector(-0.01f, 0, 0));
            var c     = new Canvas(900, 550);

            var(x, y) = p.ToXY();
            y         = 550 - y;
            var red = new Color(1f, 0f, 0f);

            while (c.IsInBounds(x, y))
            {
                c.WritePixel(red, x, y);
                p      = p.Tick(w);
                (x, y) = p.ToXY();
                y      = 550 - y;
            }

            PPM.ToFile(c, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "projectile");
        }
Exemplo n.º 5
0
        public static void TeapotTest()
        {
            Console.WriteLine("Loading file...");
            var filePath = Path.Combine(GetExecutionPath(), "indoor_env.ppm");

            //var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm");
            Console.WriteLine("Parsing file...");
            var textureCanvas = PPM.ParseFile(filePath);
            var image         = new UVImage(textureCanvas);
            var map           = new TextureMap(image, UVMapping.Spherical);

            var skySphere = new Sphere
            {
                Material =
                {
                    Texture       = map,            Ambient = 2.5f, CastsShadows = false, Transparency = 0f, Roughness = 1f,
                    SpecularColor = new Color(0.0f, 0.0f, 0.0f)
                }
            };

            //skySphere.SetTransform(Transforms.RotateY(3.4f).Scale(1000f));
            skySphere.SetTransform(Transform.RotateY(3.3f).Scale(10000f));

            var path = Path.Combine(GetExecutionPath(), "teapot.obj");

            Console.WriteLine("Loading file {0}...", path);

            var data = ObjFile.ParseFile(path);

            Console.WriteLine("File parsed...");

            var triangulated = data.Groups[0];

            triangulated.SetTransform(Transform.Scale(0.10f).RotateX(-MathF.PI / 2f).RotateY(MathF.PI / 8f));

            var glass = new Material
            {
                Texture         = new SolidColor(new Color(0.8f, 1f, 0.9f)),
                Reflective      = 0.78f,
                Roughness       = 0.35f,
                Metallic        = 0.65f,
                SpecularColor   = new Color(0.7f, 0.9f, 0.8f),
                RefractiveIndex = 1.52f,
                Transparency    = 0.80f,
                Ambient         = 0.0f,
                Diffuse         = 0.2f,
                Shininess       = 200f,
                Specular        = 0.9f
            };

            ApplyMaterialToChildren(triangulated, glass);

            var checkerboard = new Material
            {
                Texture       = new CheckerTexture(new Color(0.95f, 0.95f, 0.95f), new Color(0.8f, 0.05f, 0.05f)),
                Metallic      = 0.4f,
                SpecularColor = new Color(0.3f, 0.3f, 0.3f),
                Reflective    = 0.5f,
                Roughness     = 0.1f,
                Ambient       = 0.0f,
                Diffuse       = 0.3f
            };

            checkerboard.Texture.SetTransform(Transform.Scale(0.125f));

            var group = new Group();
            var floor = new Cube();

            floor.SetMaterial(checkerboard);
            floor.SetTransform(Transform.TranslateY(-1).Scale(5f));
            group.AddChild(floor);
            group.AddChild(triangulated);
            group.AddChild(skySphere);
            group.Divide(1);

            var w = new World();

            w.SetLights(new AreaLight(new Point(-80f, 80, -60), new Vector(100f, 0, 0), 6, new Vector(0, 0f, -10f), 3,
                                      new Color(1.8f, 1.8f, 1.8f), new Sequence(0.7f, 0.3f, 0.9f, 0.1f, 0.5f)));
            //w.SetLights(new PointLight(new Point(-30, 30, -30), new Color(1.4f, 1.4f, 1.4f)));
            w.SetObjects(group);

            //var width = 600;
            //var height = 400;
            //var transform = Transforms.View(new Point(0, 3.0f, -5f), new Point(0, 1, 0), new Vector(0, 1, 0));
            //var c = new PinholeCamera(transform, MathF.PI / 3f, width, height);
            ////var ws = new ComposableWorldShading(3, GGXNormalDistribution.Instance, SchlickBeckmanGeometricShadow.Instance, SchlickFresnelFunction.Instance, w);
            //var ws = new PhongWorldShading(5, w);
            //var scene = new Scene(c, ws);
            //var aaa = new AdaptiveRenderer(3, 0.05f, scene);
            //var canvas = new Canvas(width, height);

            var width  = 600;
            var height = 400;
            var from   = new Point(0, 2.5f, -2.5f);
            var to     = new Point(0.25f, 0.5f, 0);

            var canvas      = new Canvas(width, height);
            var pps         = new PerPixelSampler(10);
            var fov         = MathF.PI / 3f;
            var aspectRatio = (float)width / height;
            var transform   = Transform.LookAt(from, to, new Vector(0, 1, 0));
            //var camera = new PinholeCamera(transform, fov, aspectRatio);
            var camera = new ApertureCamera(fov, aspectRatio, 0.08f, from, to, Vectors.Up);
            //  var cws = new PhongWorldShading(5, w);
            var cws = new ComposableWorldSampler(2,
                                                 16,
                                                 GGXNormalDistribution.Instance,
                                                 GGXSmithGeometricShadow.Instance,
                                                 SchlickFresnelFunction.Instance,
                                                 w);
            var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps));

            Console.WriteLine("Rendering at {0}x{1}...", width, height);
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            //RenderContext.Render(canvas, aaa);
            ctx.Render();
            PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "teapot");
            stopwatch.Stop();
            Console.WriteLine("Done ({0})", stopwatch.Elapsed);
        }
Exemplo n.º 6
0
        public static void LowPolyTeapotTest()
        {
            var path         = Path.Combine(GetExecutionPath(), "teapot-low.obj");
            var data         = ObjFile.ParseFile(path);
            var triangulated = data.Groups[0];

            triangulated.SetTransform(Transform.Scale(0.1f).RotateX(-MathF.PI / 2f));

            var material = new Material
            {
                Texture    = new SolidColor(new Color(0.3f, 0.3f, 1f)),
                Reflective = 0.4f,
                Ambient    = 0.2f,
                Diffuse    = 0.3f
            };

            var floor = new Cube();

            floor.SetMaterial(material);
            var fg = new Group();

            fg.AddChild(floor);
            fg.SetTransform(Transform.TranslateY(-1).Scale(1f));

            var g = new Group(fg, triangulated);

            g.Divide(1);

            var w = new World();

            w.SetLights(new PointLight(new Point(-10, 10, -10), Colors.White));
            w.SetObjects(g);

            //var width = 300;
            //var height = 200;
            //var transform = Transforms.View(new Point(0, 1.5f, -5f), new Point(0, 1, 0), new Vector(0, 1, 0));
            //var c = new PinholeCamera(transform, MathF.PI / 3f, width, height);
            //var scene = new Scene(c, new PhongWorldShading(1, w));
            //var canvas = new Canvas(width, height);

            var width  = 300;
            var height = 200;
            var from   = new Point(0, 1.5f, -5f);
            var to     = new Point(0, 1, 0);

            var canvas      = new Canvas(width, height);
            var pps         = new PerPixelSampler(3);
            var fov         = MathF.PI / 3f;
            var aspectRatio = (float)width / height;
            var transform   = Transform.LookAt(from, to, new Vector(0, 1, 0));
            var camera      = new PinholeCamera(transform, fov, aspectRatio);
            var cws         = new PhongWorldShading(1, w);
            var ctx         = new RenderContext(canvas, new RenderPipeline(cws, camera, pps));

            Console.WriteLine("Rendering at {0}x{1}...", width, height);
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            //RenderContext.Render(canvas, scene);
            ctx.Render();
            PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "teapot");
            stopwatch.Stop();
            Console.WriteLine("Done ({0})", stopwatch.Elapsed);
        }
Exemplo n.º 7
0
        public static void SolidTestRender(int spp)
        {
            Console.WriteLine("Loading file...");
            var filePath = Path.Combine(GetExecutionPath(), "neon_env.ppm");

            //var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm");
            Console.WriteLine("Parsing file...");
            var textureCanvas = PPM.ParseFile(filePath);
            var image         = new UVImage(textureCanvas);
            var map           = new TextureMap(image, UVMapping.Spherical);

            var skySphere = new Sphere
            {
                Material =
                {
                    Texture       = map,            Ambient = 1.0f, CastsShadows = false, Transparency = 0f, Roughness = 1f,
                    SpecularColor = new Color(0.0f, 0.0f, 0.0f)
                }
            };

            skySphere.SetTransform(Transform.RotateY(3.3f).Scale(1000f));

            var radius = 0.25f;
            var red    = new Material {
                Texture = new SolidColor(new Color(1, 0, 0)), Reflective = 0.3f, Roughness = 0.3f, Ambient = 0.0f, Metallic = 0.3f, SpecularColor = new Color(0.3f, 0.3f, 0.3f)
            };
            var blue = new Material {
                Texture = new SolidColor(new Color(0, 0, 1)), Reflective = 0.3f, Roughness = 0.3f, Ambient = 0.0f, Metallic = 0.3f, SpecularColor = new Color(0.3f, 0.3f, 0.3f)
            };
            var yellow = new Material {
                Texture = new SolidColor(new Color(1, 1, 0)), Reflective = 0.3f, Roughness = 0.3f, Ambient = 0.0f, Metallic = 0.3f, SpecularColor = new Color(0.3f, 0.3f, 0.3f)
            };
            var white = new Material {
                Texture = new SolidColor(new Color(1, 1, 1)), Reflective = 0.3f, Roughness = 0.3f, Ambient = 0.0f, Metallic = 0.3f, SpecularColor = new Color(0.3f, 0.3f, 0.3f)
            };
            var blackPip = new Material {
                Texture = new SolidColor(new Color(0.1f, 0.1f, 0.1f)), Ambient = 0.0f, Roughness = 0.8f, Metallic = 0f, SpecularColor = new Color(0.2f, 0.2f, 0.2f)
            };
            var whitePip = new Material {
                Texture = new SolidColor(new Color(0.95f, 0.95f, 0.95f)), Ambient = 0.0f, Roughness = 0.8f, Metallic = 0f, SpecularColor = new Color(0.2f, 0.2f, 0.2f)
            };

            var d1 = CutPips(RoundedCube(radius, blue), whitePip);
            var d2 = CutPips(RoundedCube(radius, red), whitePip);
            var d3 = CutPips(RoundedCube(radius, white), blackPip);
            var d4 = CutPips(RoundedCube(radius, yellow), blackPip);

            d1.SetTransform(Transform.RotateY(-2.2f).TranslateY(1f).Scale(0.5f));
            d2.SetTransform(Transform.RotateZ(MathF.PI / 2f).TranslateY(1f).TranslateX(2f).TranslateZ(1f).Scale(0.5f));
            d3.SetTransform(Transform.RotateY(0.5f).TranslateY(1f).TranslateX(-4f).TranslateZ(1f).Scale(0.5f));
            d4.SetTransform(Transform.RotateY(-0.2f).TranslateY(3f).TranslateX(0.2f).TranslateZ(1.25f).Scale(0.5f));

            var lightGray = new Color(0.48f, 0.48f, 0.48f);
            var darkGray  = new Color(0.15f, 0.15f, 0.15f);
            //var s1 = new StripeTexture(lightGray, darkGray);
            //var s2 = new StripeTexture(lightGray, darkGray);
            //s2.SetTransform(Transforms.RotateY(MathF.PI / 2));
            //var pattern = new BlendedCompositeTexture(s1, s2);
            //pattern.SetTransform(Transforms.Scale(1f / 20f));

            var pattern = new CheckerTexture(lightGray, darkGray);

            pattern.SetTransform(Transform.Scale(1f / 20f));

            var floor = new Cube
            {
                Material =
                {
                    Texture       = pattern,
                    Roughness     =           0.5f,
                    Specular      =           0.1f,
                    Diffuse       =           0.3f,
                    Reflective    =          0.15f,
                    SpecularColor = new Color(0.3f, 0.3f, 0.3f),
                    Ambient       = 0f
                }
            };

            floor.SetTransform(Transform.TranslateY(-1f).Scale(20f));
            var g = new Group(d1, d2, d3, d4, floor, skySphere);

            g.Divide(1);

            var w = new World();

            w.SetLights(new AreaLight(new Point(-8, 10, -10), new Vector(12f, 0, 0), 6, new Vector(0, 0f, -10f), 3,
                                      new Color(1.1f, 1.1f, 1.1f), new Sequence(0.7f, 0.3f, 0.9f, 0.1f, 0.5f)));
            //w.SetLights(new PointLight(new Point(-8, 10, -10), new Color(0.9f, 0.9f, 0.9f)));
            //w.SetLights(new AreaLight(new Point(-3, 6, -4), new Vector(1f, 0, 0), 3, new Vector(0, 1f, 0), 3,
            //                          new Color(1.4f, 1.4f, 1.4f), new Sequence(0.7f, 0.3f, 0.9f, 0.1f, 0.5f)));
            //w.SetLights(new AreaLight(new Point(-10, 10, -10), new Vector(1,0,0), 4, new Vector(0,1,0), 3, Colors.White));
            w.SetObjects(g);

            //var width = 600;
            //var height = 400;
            //var transform = Transforms.View(new Point(0, 1.5f, -5f), new Point(0, 1, 0), new Vector(0, 1, 0));
            //var c = new PinholeCamera(transform, MathF.PI / 3f, width, height);
            //var ws = new ComposableWorldShading(2, GGXNormalDistribution.Instance, SchlickBeckmanGeometricShadow.Instance, SchlickFresnelFunction.Instance, w);
            ////var ws = new PhongWorldShading(2, w);
            //var scene = new Scene(c, ws);
            ////var aaa = new AdaptiveRenderer(2, 0.01f, scene);
            //var aaa = new SamplesPerPixelRenderer(24, scene);
            //var canvas = new Canvas(width, height);

            var width  = 600;
            var height = 400;
            var from   = new Point(0, 1.5f, -5f);
            var to     = new Point(0, 1, -0.5f);

            var canvas = new Canvas(width, height);

            var pps         = new PerPixelSampler(spp);
            var fov         = MathF.PI / 3f;
            var aspectRatio = (float)width / height;
            var camera      = new ApertureCamera(fov, aspectRatio, 0.15f, from, to, Vectors.Up);
            //var transform = Transforms.View(from, to, new Vector(0, 1, 0));
            //var camera = new PinholeCamera2(transform, fov, aspectRatio);
            //var cws = new PhongWorldShading2(4, w);
            var cws = new ComposableWorldSampler(2,
                                                 4,
                                                 GGXNormalDistribution.Instance,
                                                 SchlickBeckmanGeometricShadow.Instance,
                                                 SchlickFresnelFunction.Instance,
                                                 w);

            var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps));

            Console.WriteLine("Rendering at {0}x{1}...", width, height);
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            ctx.Render();
            // RenderContext.Render(canvas, aaa);
            PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "solid");
            stopwatch.Stop();
            Console.WriteLine("Done ({0})", stopwatch.Elapsed);
        }
Exemplo n.º 8
0
        public static void RowTestByDelegate(int spp, string fileSuffix, Func <int, Material> materialFunc)
        {
            Console.WriteLine("Loading file...");
            var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm");

            Console.WriteLine("Parsing file...");
            var textureCanvas = PPM.ParseFile(filePath);
            var image         = new UVImage(textureCanvas);
            var map           = new TextureMap(image, UVMapping.Spherical);

            var skySphere = new Sphere
            {
                Material = { Texture = map, Ambient = 1.5f, CastsShadows = false, Transparency = 1f }
            };

            skySphere.SetTransform(Transform.RotateY(3.4f).Scale(1000f));

            var g  = new Group();
            var dx = 2.75f;
            var dz = 3.5f;
            var y  = 1f;
            var nX = 10;
            var nZ = 1;
            int n  = 0;

            for (var z = 0; z < nZ; z++)
            {
                for (var x = 0; x < nX; x++)
                {
                    var s = new Sphere();
                    s.SetTransform(Transform.TranslateY(1f).Scale(1.2f).Translate(x * dx, 0, z * dz));
                    s.SetMaterial(materialFunc(x + 1));
                    g.AddChild(s);
                    n++;
                }
            }

            var lightGray = new Color(0.9f, 0.9f, 0.9f);
            var darkGray  = new Color(0.1f, 0.9f, 0.1f);
            var s1        = new StripeTexture(lightGray, darkGray);
            var s2        = new StripeTexture(lightGray, darkGray);

            s2.SetTransform(Transform.RotateY(MathF.PI / 2));
            var pattern = new BlendedCompositeTexture(s1, s2);

            pattern.SetTransform(Transform.Scale(1f / 30f));

            var text = new CheckerTexture(new Color(0.3f, 0.7f, 0.3f), new Color(0.13f, 0.13f, 0.13f));

            text.SetTransform(Transform.Scale(1f / 16f));

            var floor = new Cube
            {
                Material =
                {
                    Texture       = text,
                    SpecularColor = new Color(0.3f, 0.3f, 0.3f),
                    Metallic      =             0f,
                    Roughness     =          0.45f,
                    Ambient       = 0.15f
                }
            };

            floor.SetTransform(Transform.TranslateY(-1f).Scale(40f));

            var min = g.LocalBounds().Min;
            var max = g.LocalBounds().Max;
            var dir = max - min;
            var mid = min + (dir * 0.5f);

            var g2 = new Group(g, floor, skySphere);

            g2.Divide(1);

            var w = new World();

            w.SetLights(new PointLight(new Point(mid.X, 500, -500), new Color(1.7f, 1.7f, 1.7f)));
            w.SetObjects(g2);

            var width  = 1200;
            var height = 140;
            var from   = new Point(mid.X, 6f, -32f);
            var to     = mid;

            var canvas      = new Canvas(width, height);
            var pps         = new PerPixelSampler(spp);
            var fov         = MathF.PI / 4f;
            var aspectRatio = (float)width / height;
            var camera      = new ApertureCamera(fov, aspectRatio, 0.2F, from, to, Vectors.Up);
            var cws         = new ComposableWorldSampler(2,
                                                         16,
                                                         GGXNormalDistribution.Instance,
                                                         GGXSmithGeometricShadow.Instance,
                                                         SchlickFresnelFunction.Instance,
                                                         w);
            var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps));

            Console.WriteLine("Rendering at {0}x{1}...", width, height);
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            ctx.Render();
            PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "col_row_" + fileSuffix);
            stopwatch.Stop();
            Console.WriteLine("Done ({0})", stopwatch.Elapsed);
        }
Exemplo n.º 9
0
        public static void ColRowTestRender()
        {
            Console.WriteLine("Loading file...");
            //var filePath = Path.Combine(GetExecutionPath(), "indoor_env.ppm");
            var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm");

            Console.WriteLine("Parsing file...");
            var textureCanvas = PPM.ParseFile(filePath);
            var image         = new UVImage(textureCanvas);
            var map           = new TextureMap(image, UVMapping.Spherical);

            var skySphere = new Sphere()
            {
                Material = { Texture = map, Ambient = 1.5f, CastsShadows = false, Transparency = 1f }
            };

            skySphere.SetTransform(Transform.RotateY(3.4f).Scale(1000f));

            var  g        = new Group();
            var  dx       = 2.75f;
            var  dz       = 3.5f;
            var  y        = 1f;
            var  nX       = 10;
            var  nZ       = 1;
            var  delta    = 1f / (nX * nZ - 1);
            int  n        = 0;
            bool metallic = false;

            for (var z = 0; z < nZ; z++)
            {
                for (var x = 0; x < nX; x++)
                {
                    var s = new Sphere();
                    s.SetTransform(Transform.TranslateY(1f).Scale(1.2f).Translate(x * dx, 0, z * dz));
                    // var color = x % 2 == 0 ? new Color(1f, 1f, 1f) : new Color(1f, 0.3f, 0.3f);
                    var color = new Color(1f, 0.3f, 0.3f);
                    s.Material.Texture       = SolidColor.Create(color);
                    s.Material.SpecularColor = metallic ? color : new Color(0.2f, 0.2f, 0.2f);
                    s.Material.Roughness     = MathFunction.Saturate(n * delta + 0.01f);
                    s.Material.Metallic      = metallic ? 1f : 0f;
                    s.Material.Ambient       = 0f;
                    s.Material.Reflective    = 0.9f;
                    g.AddChild(s);
                    n++;
                }
            }

            var lightGray = new Color(0.9f, 0.9f, 0.9f);
            var darkGray  = new Color(0.1f, 0.9f, 0.1f);
            var s1        = new StripeTexture(lightGray, darkGray);
            var s2        = new StripeTexture(lightGray, darkGray);

            s2.SetTransform(Transform.RotateY(MathF.PI / 2));
            var pattern = new BlendedCompositeTexture(s1, s2);

            pattern.SetTransform(Transform.Scale(1f / 30f));

            var text = new CheckerTexture(new Color(0.3f, 0.7f, 0.3f), new Color(0.13f, 0.13f, 0.13f));

            text.SetTransform(Transform.Scale(1f / 16f));

            var floor = new Cube
            {
                Material =
                {
                    Texture       = text,
                    SpecularColor = new Color(0.3f, 0.3f, 0.3f),
                    Metallic      =             0f,
                    Roughness     =          0.45f,
                    Ambient       = 0.15f
                }
            };

            floor.SetTransform(Transform.TranslateY(-1f).Scale(40f));

            var min = g.LocalBounds().Min;
            var max = g.LocalBounds().Max;
            var dir = max - min;
            var mid = min + (dir * 0.5f);

            var g2 = new Group(g, floor, skySphere);

            //var g2 = new Group(g);
            g2.Divide(1);

            var w = new World();

            w.SetLights(new PointLight(new Point(mid.X, 500, -500), new Color(1.7f, 1.7f, 1.7f)));
            w.SetObjects(g2);

            //var width = 1200;
            //var height = 140;
            //var transform = Transforms.View(new Point(mid.X, 6f, -32f), mid, new Vector(0, 1, 0));
            //var c = new PinholeCamera(transform, MathF.PI / 4f, width, height);
            ////var c = new ApertureCamera(MathF.PI / 4f, width, height, 0.04f, new Point(mid.X, 6f, -32f), mid);
            //var ws = new ComposableWorldShading(3, GGXNormalDistribution.Instance, GGXSmithGeometricShadow.Instance, SchlickFresnelFunction.Instance, w);
            ////var ws = new PhongWorldShading(3, w);
            //var scene = new Scene(c, ws);
            //var aaa = new AdaptiveRenderer(4, 0.00001f, scene);
            //var canvas = new Canvas(width, height);


            var width  = 1200;
            var height = 140;
            var from   = new Point(mid.X, 6f, -32f);
            var to     = mid;

            var canvas      = new Canvas(width, height);
            var pps         = new PerPixelSampler(400);
            var fov         = MathF.PI / 4f;
            var aspectRatio = (float)width / height;
            var camera      = new ApertureCamera(fov, aspectRatio, 0.2F, from, to, Vectors.Up);
            var cws         = new ComposableWorldSampler(2,
                                                         16,
                                                         GGXNormalDistribution.Instance,
                                                         GGXSmithGeometricShadow.Instance,
                                                         SchlickFresnelFunction.Instance,
                                                         w);
            var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps));

            Console.WriteLine("Rendering at {0}x{1}...", width, height);
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            ctx.Render();
            //RenderContext.Render(canvas, aaa);
            PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "col_row");
            stopwatch.Stop();
            Console.WriteLine("Done ({0})", stopwatch.Elapsed);
        }
Exemplo n.º 10
0
        public static void InsideSphere()
        {
            Console.WriteLine("Loading file...");
            var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm");

            Console.WriteLine("Parsing file...");
            var textureCanvas = PPM.ParseFile(filePath);
            var image         = new UVImage(textureCanvas);
            var map           = new TextureMap(image, UVMapping.Spherical);

            var skySphere = new Sphere()
            {
                Material = { Texture = map, Ambient = 1.2f, CastsShadows = false, Transparency = 1f }
            };

            skySphere.SetTransform(Transform.RotateY(2.1f).Scale(1000f));

            var s = new Sphere
            {
                Material =
                {
                    Roughness     =                           0.1f,
                    Texture       = SolidColor.Create(new Color(1f, 0.0f, 0.0f)),
                    SpecularColor = new Color(0.2f,                 0.2f,  0.2f),
                    Metallic      =                             0f,
                    Ambient       = 0f
                }
            };

            var g = new Group();

            g.AddChild(skySphere);
            g.AddChild(s);

            g.Divide(1);

            var w = new World();

            w.SetLights(new PointLight(new Point(100, 100, -100), new Color(1f, 1f, 1f)));
            w.SetObjects(g);

            var width  = 400;
            var height = 400;
            var from   = new Point(0, -0.8f, -4f);
            var to     = new Point(0, 0, 0);

            var canvas      = new Canvas(width, height);
            var pps         = new PerPixelSampler(1000);
            var fov         = 0.8f;
            var aspectRatio = (float)width / height;
            var camera      = new ApertureCamera(fov, aspectRatio, 0.05F, from, to, Vectors.Up);
            var cws         = new ComposableWorldSampler(1,
                                                         2,
                                                         GGXNormalDistribution.Instance,
                                                         SchlickBeckmanGeometricShadow.Instance,
                                                         SchlickFresnelFunction.Instance,
                                                         w);
            var ctx = new RenderContext(canvas, new RenderPipeline(cws, camera, pps));

            Console.WriteLine("Rendering at {0}x{1}...", width, height);
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            ctx.Render();
            PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "sky_sphere");
            stopwatch.Stop();
            Console.WriteLine("Done ({0})", stopwatch.Elapsed);
        }
Exemplo n.º 11
0
        public static void SphereMappingTestRender()
        {
            Console.WriteLine("Loading file...");
            var filePath = Path.Combine(GetExecutionPath(), "winter_river_1k.ppm");

            Console.WriteLine("Parsing file...");
            var textureCanvas = PPM.ParseFile(filePath);
            var image         = new UVImage(textureCanvas);
            var map           = new TextureMap(image, UVMapping.Spherical);


            Sphere Sphere(float rotY, float rotX, float tx, float ty)
            {
                var s = new Sphere()
                {
                    Material = { Texture = map }
                };

                s.SetTransform(Transform.RotateY(rotY).RotateX(rotX).Translate(tx, ty, 0));
                return(s);
            }

            var g = new Group();

            g.AddChild(Sphere(0.7854f, 0.7854f, -6, 2));
            g.AddChild(Sphere(2.3562f, 0.7854f, -2, 2));
            g.AddChild(Sphere(3.927f, 0.7854f, 2, 2));
            g.AddChild(Sphere(5.4978f, 0.7854f, 6, 2));
            g.AddChild(Sphere(0.7854f, -0.7854f, -6, -2));
            g.AddChild(Sphere(2.3562f, -0.7854f, -2, -2));
            g.AddChild(Sphere(3.927f, -0.7854f, 2, -2));
            g.AddChild(Sphere(5.4978f, -0.7854f, 6, -2));

            g.Divide(1);

            var w = new World();

            w.SetLights(new PointLight(new Point(0, 2, -100), Colors.White));
            w.SetObjects(g);

            var width  = 800;
            var height = 400;
            var from   = new Point(0, 0, -20f);
            var to     = new Point(0, 0, 0);

            var canvas      = new Canvas(width, height);
            var pps         = new PerPixelSampler(16);
            var fov         = 0.8f;
            var aspectRatio = (float)width / height;
            var transform   = Transform.LookAt(from, to, new Vector(0, 1, 0));
            var camera      = new PinholeCamera(transform, fov, aspectRatio);
            var cws         = new PhongWorldShading(1, w);
            var ctx         = new RenderContext(canvas, new RenderPipeline(cws, camera, pps));

            Console.WriteLine("Rendering at {0}x{1}...", width, height);
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            ctx.Render();
            PPM.ToFile(canvas, Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "mapping_spheres");
            stopwatch.Stop();
            Console.WriteLine("Done ({0})", stopwatch.Elapsed);
        }