Exemple #1
0
        public static void Main(string[] args)
        {
            // output to a ppm file
            Console.Write("P3\n" + nx + " " + ny + " " + "\n255\n");

            // create the world
            List <Hitable> world = new List <Hitable>();

            world.Add(new Moving_Sphere(new Vector3(0.0f, 0.0f, 0.0f),
                                        new Vector3(0.0f, 0.0f, -2.0f),
                                        0.0f,
                                        10.0f,
                                        0.5f,
                                        new Lambertian(new Const_Texture(new Vector3(0.137f, 0.467f, 1.0f)))));
            world.Add(new Sphere(new Vector3(1.0f, 0.0f, -1.0f),
                                 0.5f,
                                 new Dielectric(1.5f)));
            world.Add(new Sphere(new Vector3(1.0f, 0.0f, -1.0f),
                                 -0.3f,
                                 new Dielectric(1.5f)));
            world.Add(new Sphere(new Vector3(-1.0f, 0.0f, -1.0f),
                                 0.5f,
                                 new Metal(new Noise(10.0f))));
            world.Add(new Sphere(new Vector3(0.0f, -100.5f, -1.0f),
                                 100.0f,
                                 new Lambertian(new Marble(new Vector3(0.0f, 0.8f, 0.0f), 5.0f))));

            BVH_Node tree = new BVH_Node(world, 0.0f, 10.0f);

            // init the camera
            Vector3 lookfrom      = new Vector3(3.0f, 2.0f, 2.0f);
            Vector3 lookat        = new Vector3(0.0f, 0.0f, -1.0f);
            float   dist_to_focus = (lookfrom - lookat).length();
            float   apeture       = 0.5f;
            Camera  cam           = new Camera(lookfrom,
                                               lookat,
                                               new Vector3(0.0f, 1.0f, 0.0f),
                                               40.0f,
                                               (float)nx / (float)ny,
                                               apeture,
                                               dist_to_focus,
                                               0.0f,
                                               10.0f);

            for (int i = ny - 1; i >= 0; i--)
            {
                for (int j = 0; j < nx; j++)
                {
                    // supersampling for antialiasing
                    Vector3 col = new Vector3();
                    for (int s = 0; s < ns; s++)
                    {
                        float u = (float)(j + Utils.rnd.NextDouble()) / (float)nx;
                        float v = (float)(i + Utils.rnd.NextDouble()) / (float)ny;

                        Ray r = cam.getRay(u, v);
                        col += color(r, tree, 0);
                    }

                    col /= (float)ns;
                    // gamma correction
                    col = new Vector3((float)Math.Sqrt(col.x()),
                                      (float)Math.Sqrt(col.y()),
                                      (float)Math.Sqrt(col.z()));
                    // values written to file
                    int ir = (int)(255.99f * col.r());
                    int ig = (int)(255.99f * col.g());
                    int ib = (int)(255.99f * col.b());
                    Console.Write(ir + " " + ig + " " + ib + "\n");
                }
            }
        }