Exemple #1
0
        public static void Main(string[] args)
        {
            RayTracer    rt    = new RayTracer(800, 600, 1.0);
            var          bmp   = new Bitmap(rt.Camera.OutputImageWidth, rt.Camera.OutputImageHeight);
            HittableList world = new HittableList();

            world.Add(new Sphere(new Point3(0, 0, -1), 0.5, new Lambertian(new color(0.1, 0.2, 0.5))));
            world.Add(new Sphere(new Point3(0, -100.5, -1), 100, new Lambertian(new color(0.8, 0.8, 0))));
            world.Add(new Sphere(new Point3(1, 0, -1), 0.5, new Metal(new color(0.8, 0.6, 0.2), 0)));
            //world.Add(new Sphere(new Point3(-1, 0, -1), 0.5, new Metal(new color(0.8, 0.8, 0.8), 0.6)));
            world.Add(new Sphere(new Point3(-1, 0, -1), 0.5, new Dielectric(1.5)));

            for (int y = 0; y < rt.Camera.OutputImageHeight; ++y)
            {
                Console.WriteLine("Line: " + y.ToString() + "...");
                for (int x = 0; x < rt.Camera.OutputImageWidth; ++x)
                {
                    color pixelColor = color.Zero;
                    for (int s = 0; s < rt.SamplePerPixel; ++s)
                    {
                        var u   = (double)(x + RayTracer.RandomDouble()) / (rt.Camera.OutputImageWidth - 1);
                        var v   = (double)(y + RayTracer.RandomDouble()) / (rt.Camera.OutputImageHeight - 1);
                        Ray ray = rt.Camera.GetRay(u, v);
                        pixelColor += rt.ComputeRayColor(ray, world, RayTracer.MaxRecursiveDepth);
                    }
                    rt.WriteOutputColorAt(x, y, pixelColor, bmp);
                }
            }
            Console.WriteLine("Save out final image.");
            bmp.Save("/Users/jhq/Desktop/RT.bmp");
        }
Exemple #2
0
        private static void AntiAlias()
        {
            var width   = 400;
            var height  = 200;
            var samples = 100;

            var sb = new StringBuilder();

            sb.AppendLine("P3");
            sb.AppendLine(width + " " + height);
            sb.AppendLine("255");

            var world = new HittableList();

            world.Add(new Sphere(new Vector3(0, 0, -1), 0.5f));
            world.Add(new Sphere(new Vector3(0, -100.5f, -1), 100));
            var camera = new Camera();
            var col    = new Func <Ray, IHitable, Vector3>((ray, w) => {
                HitRecord rec;
                if (w.Hit(ray, 0, Single.MaxValue, out rec))
                {
                    return(new Vector3(rec.Normal.X + 1, rec.Normal.Y + 1, rec.Normal.Z + 1) * 0.5f);
                }
                var unit = ray.Direction.Normalize();
                var t    = 0.5f * (unit.Y + 1);
                return(new Vector3(1, 1, 1) * (1.0f - t) + new Vector3(0.5f, 0.7f, 1.0f) * t);
            });
            var rand = new Random();

            for (var y = height - 1; y >= 0; y--)
            {
                for (var x = 0; x < width; x++)
                {
                    Vector3 color = new Vector3();

                    for (int i = 0; i < samples; i++)
                    {
                        var u = (float)(x + rand.NextDouble()) / width;
                        var v = (float)(y + rand.NextDouble()) / height;
                        var r = camera.GetRay(u, v);
                        color += col(r, world);
                    }
                    color /= (float)samples;
                    var ir = (int)(255.99 * color.R);
                    var ig = (int)(255.99 * color.G);
                    var ib = (int)(255.99 * color.B);
                    sb.AppendLine(ir + " " + ig + " " + ib);
                }
            }
            File.WriteAllText("antialias.ppm", sb.ToString());
            Process.Start("antialias.ppm");
        }
Exemple #3
0
        private static void TwoSpheres()
        {
            var width  = 400;
            var height = 200;

            var sb = new StringBuilder();

            sb.AppendLine("P3");
            sb.AppendLine(width + " " + height);
            sb.AppendLine("255");

            var lowerLeftCorner = new Vector3(-2, -1, -1);
            var horizontal      = new Vector3(4, 0, 0);
            var vertical        = new Vector3(0, 2, 0);
            var origin          = Vector3.Zero;

            var world = new HittableList();

            world.Add(new Sphere(new Vector3(0, 0, -1), 0.5f));
            world.Add(new Sphere(new Vector3(0, -100.5f, -1), 100));

            var col = new Func <Ray, IHitable, Vector3>((ray, w) => {
                HitRecord rec;
                if (w.Hit(ray, 0, Single.MaxValue, out rec))
                {
                    return(new Vector3(rec.Normal.X + 1, rec.Normal.Y + 1, rec.Normal.Z + 1) * 0.5f);
                }
                var unit = ray.Direction.Normalize();
                var t    = 0.5f * (unit.Y + 1);
                return(new Vector3(1, 1, 1) * (1.0f - t) + new Vector3(0.5f, 0.7f, 1.0f) * t);
            });

            for (var y = height - 1; y >= 0; y--)
            {
                for (var x = 0; x < width; x++)
                {
                    var u = (float)x / width;
                    var v = (float)y / height;
                    var r = new Ray(origin, lowerLeftCorner + horizontal * u + vertical * v);

                    var color = col(r, world);
                    var ir    = (int)(255.99 * color.R);
                    var ig    = (int)(255.99 * color.G);
                    var ib    = (int)(255.99 * color.B);
                    sb.AppendLine(ir + " " + ig + " " + ib);
                }
            }
            File.WriteAllText("TwoSpheres.ppm", sb.ToString());
            Process.Start("TwoSpheres.ppm");
        }