Exemplo n.º 1
0
        public Image <Rgba32> Render(Camera camera, IHitable world)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var image = new Image <Rgba32>(this.imageWidth, this.imageHeight);

            Console.WriteLine("Rendering scene... ");
            using var progress = new ProgressBar();
            {
                int completeLines = 0;
                Parallel.For(0, this.imageHeight, j =>
                {
                    Span <Rgba32> rowSpan = image.GetPixelRowSpan(this.imageHeight - j - 1);
                    for (int i = 0; i < this.imageWidth; i++)
                    {
                        Vector3 colour = Vector3.Zero;
                        for (int s = 0; s < this.samplesPerPixel; s++)
                        {
                            float u = (float)(i + StaticRandom.NextDouble()) / this.imageWidth;
                            float v = (float)(j + StaticRandom.NextDouble()) / this.imageHeight;
                            Ray ray = camera.GetRay(u, v);
                            colour += Colour(in ray, world, 0);
                        }

                        colour /= (float)this.samplesPerPixel;
                        colour  = new Vector3((float)Math.Sqrt(colour.X), (float)Math.Sqrt(colour.Y),
                                              (float)Math.Sqrt(colour.Z));
                        rowSpan[i] = new Rgba32(colour);
                    }
Exemplo n.º 2
0
        private static IHitable CreateRandomScene()
        {
            var hitables = new List <IHitable>(500);

            hitables.Add(new Sphere(new Vector3(0f, -1000f, 0f), 1000f, new Lambertian(new Vector3(0.5f, 0.5f, 0.5f))));
            for (int a = -11; a < 11; a++)
            {
                for (int b = -11; b < 11; b++)
                {
                    float randomMat = (float)StaticRandom.NextDouble();
                    var   center    = new Vector3(a + 0.9f * (float)StaticRandom.NextDouble(), 0.2f, b + 0.9f * (float)StaticRandom.NextDouble());
                    if ((center - new Vector3(4f, 0.2f, 0f)).Length() > 0.9f)
                    {
                        if (randomMat < 0.8f) //diffuse
                        {
                            hitables.Add(new Sphere(center, 0.2f, new Lambertian(new Vector3((float)StaticRandom.NextDouble() * (float)StaticRandom.NextDouble(), (float)StaticRandom.NextDouble() * (float)StaticRandom.NextDouble(), (float)StaticRandom.NextDouble() * (float)StaticRandom.NextDouble()))));
                        }
                        else if (randomMat < 0.95f) //metal
                        {
                            hitables.Add(new Sphere(center, 0.2f, new Metal(new Vector3(0.5f * (1f + (float)StaticRandom.NextDouble()), 0.5f * (1 + (float)StaticRandom.NextDouble()), 0.5f * (float)StaticRandom.NextDouble()), 0.2f)));
                        }
                        else //glass
                        {
                            hitables.Add(new Sphere(center, 0.2f, new Dielectric(1.5f)));
                        }
                    }
                }
            }

            hitables.Add(new Sphere(new Vector3(0f, 1f, 0f), 1.0f, new Dielectric(1.5f)));
            hitables.Add(new Sphere(new Vector3(-4f, 1f, 0f), 1.0f, new Lambertian(new Vector3(0.4f, 0.2f, 0.1f))));
            hitables.Add(new Sphere(new Vector3(4f, 1f, 0f), 1.0f, new Metal(new Vector3(0.7f, 0.6f, 0.5f), 0f)));

            return(new HitableCollection(hitables));
        }
Exemplo n.º 3
0
        private Vector3 GetRandomInUnitDisk()
        {
            Vector3 point;

            do
            {
                point = 2f * new Vector3((float)StaticRandom.NextDouble(), (float)StaticRandom.NextDouble(), 0) -
                        new Vector3(1f, 1f, 0f);
            } while (Vector3.Dot(point, point) >= 1f);

            return(point);
        }