Exemplo n.º 1
0
 public void RenderWeek1Parallel(int width, int height, int samples)
 {
     var img = RayTrace1.RenderParallelFor(width, height, samples);
 }
Exemplo n.º 2
0
        private static void Run(Options options)
        {
            //var extOutput =

            Func <RayImage, Stream, bool> encoder = null;
            string filename = null;

            // if output not set, use default output filename.
            if (string.IsNullOrWhiteSpace(options.Output))
            {
                filename = "image.bmp";
            }
            else
            {
                filename = options.Output;
            }

            // TODO: implement encoders as a proper interface with factory methods instead of this switch.
            //var ext = string.IsNullOrWhiteSpace(options.Output) ? string.Empty:
            var ext = Path.GetExtension(filename).ToUpperInvariant();

            switch (ext)
            {
            case ".TGA":
                encoder = (r, s) => r.WriteTga(s);
                break;

            case ".PPM":
                encoder = (r, s) => r.WritePpm(s);
                break;

            case ".BMP":
                encoder = (r, s) => r.WriteBmp(s);
                break;

            default:
                encoder   = (r, s) => r.WriteBmp(s);
                filename += ".bmp";
                break;
            }

            // code.
            var watch = new Stopwatch();

            watch.Start();

            // render.
            RayImage img;

            //var img = RayTrace1.Render(options.Width, options.Height, options.Samples);
            if (options.Parallel)
            {
                img = RayTrace1.RenderParallelFor(options.Width, options.Height, options.Samples);
            }
            else
            {
                img = RayTrace1.Render(options.Width, options.Height, options.Samples);
            }

            watch.Stop();

            // remember if the file exists before opening it.
            var flagExists = File.Exists(filename);

            // save.
            using (var f = File.OpenWrite(filename))
            {
                encoder(img, f);

                // in case new output is smaller than the last rendered image, trunc the rest.
                if (flagExists)
                {
                    f.SetLength(f.Position);
                }
            }

            // done message.
            Console.WriteLine($"Image '{filename}' rendered in {watch.ElapsedMilliseconds}ms.");
        }