Ejemplo n.º 1
0
        public void Run()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            int nx = 200;
            int ny = 100;
            // link to ppm viewer
            // http://www.cs.rhodes.edu/welshc/COMP141_F16/ppmReader.html

            Vector3 lowerLeftCorner = new Vector3(-2.0f, -1.0f, -1.0f);
            Vector3 horizontal      = new Vector3(4.0f, 0.0f, 0.0f);
            Vector3 vertical        = new Vector3(0.0f, 2.0f, 0.0f);
            Vector3 origin          = new Vector3(0.0f, 0.0f, 0.0f);

            Hitable[] objList = new Hitable[2];
            objList[0] = new Sphere(new Vector3(0, 0, -1), 0.5f);
            objList[1] = new Sphere(new Vector3(0, -100.5f, -1), 100);

            HitableList world = new HitableList(objList);

            string filePath = @"d:\DEV_stuff\DEV\Sanbox\RayTracingSanbox\RayTracingInWeek\Output";

            if (File.Exists(Path.Combine(filePath, "texture.ppm")))
            {
                File.Delete(Path.Combine(filePath, "texture.ppm"));
                Console.WriteLine("Deleted file: " + Path.Combine(filePath, "texture.ppm"));
            }

            using (StreamWriter outputFile = new StreamWriter(Path.Combine(filePath, "texture.ppm"), true))
            {
                outputFile.Write("P3\n" + nx + " " + ny + "\n255\n");
                //Console.Write("P3\n" + nx + " " + ny + "\n255\n");
                for (int j = ny - 1; j >= 0; j--)
                {
                    for (int i = 0; i < nx; i++)
                    {
                        float   u   = (float)i / (float)nx;
                        float   v   = (float)j / (float)ny;
                        Ray     r   = new Ray(origin, lowerLeftCorner + u * horizontal + v * vertical);
                        Vector3 p   = r.PointAtParameter(2);
                        Vector3 col = Color(r, world);

                        int ir = (int)(255.99 * col.X);
                        int ig = (int)(255.99 * col.Y);
                        int ib = (int)(255.99 * col.Z);
                        outputFile.Write(ir + " " + ig + " " + ib + "\n");
                        //Console.Write(ir + " " + ig + " " + ib + "\n");
                    }
                }
            }

            sw.Stop();

            Console.WriteLine("Time to output image with StreamWriter : " + sw.ElapsedMilliseconds.ToString() + " ms");
            Console.WriteLine("Press enter to close...");
            Console.ReadLine();
        }
Ejemplo n.º 2
0
 Vector3 Color(Ray r, HitableList world)
 {
     HitRecord rec = new HitRecord();
     if (world.hit(r, 0.001f, float.MaxValue, ref rec))
     {
         Vector3 target = rec.p + rec.normal + Sphere.random_in_unit_sphere();
         return 0.5f * Color(new Ray(rec.p, target - rec.p), world);
     }
     else
     {
         Vector3 unitDirection = Vector3.Normalize(r.Direction());
         float t = 0.5f * (unitDirection.Y + 1.0f);
         return (1.0f - t) * new Vector3(1.0f, 1.0f, 1.0f) + t * new Vector3(0.5f, 0.7f, 1.0f);
     }
 }
Ejemplo n.º 3
0
        Vector3 Color(Ray r, HitableList world)
        {
            HitRecord rec = new HitRecord();

            if (world.hit(r, 0.0f, float.MaxValue, ref rec))
            {
                return(0.5f * new Vector3(rec.normal.X + 1.0f, rec.normal.Y + 1.0f, rec.normal.Z + 1.0f));
            }
            else
            {
                Vector3 unitDirection = Vector3.Normalize(r.Direction());
                float   t             = 0.5f * (unitDirection.Y + 1.0f);
                return((1.0f - t) * new Vector3(1.0f, 1.0f, 1.0f) + t * new Vector3(0.5f, 0.7f, 1.0f));
            }
        }
Ejemplo n.º 4
0
        Vector3 Color(Ray r, HitableList world, int depth)
        {
            HitRecord rec = new HitRecord();

            if (world.hit(r, 0.001f, float.MaxValue, ref rec))
            {
                Ray     scattered   = null;
                Vector3 attenuation = new Vector3(0, 0, 0);
                if (depth < 50 && rec.mat.scatter(r, rec, ref attenuation, ref scattered))
                {
                    return(attenuation * Color(scattered, world, depth + 1));
                }
                else
                {
                    return(new Vector3(0, 0, 0));
                }
            }
            else
            {
                Vector3 unitDirection = Vector3.Normalize(r.Direction());
                float   t             = 0.5f * (unitDirection.Y + 1.0f);
                return((1.0f - t) * new Vector3(1.0f, 1.0f, 1.0f) + t * new Vector3(0.5f, 0.7f, 1.0f));
            }
        }
Ejemplo n.º 5
0
        public void Run()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            int nx = 200;
            int ny = 100;


            // link to ppm viewer
            // http://www.cs.rhodes.edu/welshc/COMP141_F16/ppmReader.html

            Hitable[] objList = new Hitable[5];
            objList[0] = new Sphere(new Vector3(0, 0, -1), 0.5f, new Lambertian(new Vector3(.8f, .3f, .3f)));
            objList[1] = new Sphere(new Vector3(0, -100.5f, -1), 100, new Lambertian(new Vector3(.8f, .8f, 0)));
            objList[2] = new Sphere(new Vector3(1, 0, -1), 0.5f, new Metal(new Vector3(.8f, .6f, .2f)));
            objList[3] = new Sphere(new Vector3(-1, 0, -1), 0.5f, new Dielectric(1.5f));
            objList[4] = new Sphere(new Vector3(-1, 0, -1), -0.45f, new Dielectric(1.5f));
            HitableList world = new HitableList(objList);

            Camera cam         = new Camera(new Vector3(-2, 1, 0), new Vector3(0, 0, -1), new Vector3(0, 1, 0), 90, (float)nx / (float)ny);
            int    sampleCount = 100;
            Random rdm         = new Random();

            string filePath = @"d:\DEV_stuff\DEV\Sanbox\RayTracingSanbox\RayTracingInWeek\Output";

            if (File.Exists(Path.Combine(filePath, "texture.ppm")))
            {
                File.Delete(Path.Combine(filePath, "texture.ppm"));
                Console.WriteLine("Deleted file: " + Path.Combine(filePath, "texture.ppm"));
            }

            using (StreamWriter outputFile = new StreamWriter(Path.Combine(filePath, "texture.ppm"), true))
            {
                outputFile.Write("P3\n" + nx + " " + ny + "\n255\n");
                //Console.Write("P3\n" + nx + " " + ny + "\n255\n");
                for (int j = ny - 1; j >= 0; j--)
                {
                    for (int i = 0; i < nx; i++)
                    {
                        Vector3 col = new Vector3(0, 0, 0);
                        for (int c = 0; c < sampleCount; c++)
                        {
                            float   u = ((float)i + (float)rdm.NextDouble()) / (float)nx;
                            float   v = ((float)j + (float)rdm.NextDouble()) / (float)ny;
                            Ray     r = cam.getRay(u, v);
                            Vector3 p = r.PointAtParameter(2);
                            col += Color(r, world, 10);
                        }


                        col /= (float)sampleCount;
                        col  = new Vector3((float)(Math.Sqrt(col.X)), (float)Math.Sqrt(col.Y),
                                           (float)Math.Sqrt(col.Z));
                        int ir = (int)(255.99 * col.X);
                        int ig = (int)(255.99 * col.Y);
                        int ib = (int)(255.99 * col.Z);
                        outputFile.Write(ir + " " + ig + " " + ib + "\n");
                        //Console.Write(ir + " " + ig + " " + ib + "\n");
                    }
                }
            }

            sw.Stop();

            Console.WriteLine("Time to output image with StreamWriter : " + sw.ElapsedMilliseconds.ToString() + " ms");
            Console.WriteLine("Press enter to close...");
            Console.ReadLine();
        }
Ejemplo n.º 6
0
        public void Run()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            int nx = 1280;
            int ny = 760;


            // link to ppm viewer
            // http://www.cs.rhodes.edu/welshc/COMP141_F16/ppmReader.html

            HitableList world = random_scene();

            Vector3 lookfrom      = new Vector3(13f, 2f, 3f);
            Vector3 lookat        = new Vector3(0f, 0f, 0f);
            float   dist_to_focus = (lookfrom - lookat).Length();
            float   aperture      = 0f;
            Camera  cam           = new Camera(lookfrom, lookat, new Vector3(0f, 1f, 0f), 30f, (float)nx / (float)ny, aperture, 0.7f * dist_to_focus);

            int    sampleCount = 10;
            Random rdm         = new Random();

            string filePath = @"d:\DEV_stuff\DEV\Sanbox\RayTracingSanbox\RayTracingInWeek\Output";

            if (File.Exists(Path.Combine(filePath, "texture.ppm")))
            {
                File.Delete(Path.Combine(filePath, "texture.ppm"));
                Console.WriteLine("Deleted file: " + Path.Combine(filePath, "texture.ppm"));
            }

            using (StreamWriter outputFile = new StreamWriter(Path.Combine(filePath, "texture.ppm"), true))
            {
                outputFile.Write("P3\n" + nx + " " + ny + "\n255\n");
                //Console.Write("P3\n" + nx + " " + ny + "\n255\n");
                for (int j = ny - 1; j >= 0; j--)
                {
                    for (int i = 0; i < nx; i++)
                    {
                        Vector3 col = new Vector3(0, 0, 0);
                        for (int c = 0; c < sampleCount; c++)
                        {
                            float   u = ((float)i + (float)rdm.NextDouble()) / (float)nx;
                            float   v = ((float)j + (float)rdm.NextDouble()) / (float)ny;
                            Ray     r = cam.getRayDOF(u, v);
                            Vector3 p = r.PointAtParameter(2);
                            col += Color(r, world, 10);
                        }


                        col /= (float)sampleCount;
                        col  = new Vector3((float)(Math.Sqrt(col.X)), (float)Math.Sqrt(col.Y),
                                           (float)Math.Sqrt(col.Z));
                        int ir = (int)(255.99 * col.X);
                        int ig = (int)(255.99 * col.Y);
                        int ib = (int)(255.99 * col.Z);
                        outputFile.Write(ir + " " + ig + " " + ib + "\n");
                        //Console.Write(ir + " " + ig + " " + ib + "\n");
                    }
                }
            }

            sw.Stop();

            Console.WriteLine("Time to output image with StreamWriter : " + sw.ElapsedMilliseconds.ToString() + " ms");
            Console.WriteLine("Press enter to close...");
            Console.ReadLine();
        }