Beispiel #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");
        }
Beispiel #2
0
        public bool Scatter(Ray rayIn, ref HitRecord rec, out color attenuation, out Ray scattered)
        {
            attenuation = new color(1.0, 1.0, 1.0);
            double etai_over_etat = rec.isFrontFace ? 1.0 / ref_idx : ref_idx;

            var unit_direction = rayIn.Direction.unit_vector();
            var cos_theta      = Math.Min(Vec3.dot(-unit_direction, rec.normal), 1.0);
            var sin_theta      = Math.Sqrt(1 - cos_theta * cos_theta);

            if (etai_over_etat * sin_theta > 1)
            {
                var reflected = RayTracer.Reflect(unit_direction, rec.normal);
                scattered = new Ray(rec.p, reflected);
                return(true);
            }

            var reflect_prob = schlick(cos_theta, etai_over_etat);

            if (RayTracer.RandomDouble() < reflect_prob)
            {
                var reflected = RayTracer.Reflect(unit_direction, rec.normal);
                scattered = new Ray(rec.p, reflected);
                return(true);
            }

            var refracted = RayTracer.Refract(unit_direction, rec.normal, etai_over_etat);

            scattered = new Ray(rec.p, refracted);
            return(true);
        }