Ejemplo n.º 1
0
        public static Color3D Color(Ray r, Hitable world, int depth)
        {
            HitRecord rec = new HitRecord();

            if (world.Hit(r, 0.001, double.MaxValue, ref rec))
            {
                Ray     scattered   = new Ray();
                Vector3 attenuation = new Vector3();
                if (depth < 50 && rec.Material.Scatter(r, rec, ref attenuation, ref scattered))
                {
                    Color3D colorTemp = Color(scattered, world, depth + 1);
                    colorTemp.R *= attenuation.X;
                    colorTemp.G *= attenuation.Y;
                    colorTemp.B *= attenuation.Z;
                    return(colorTemp);
                }
                else
                {
                    return(new Color3D(0, 0, 0));
                }

                //Vector3 target = rec.P + rec.Normal + RandomInUnitSphere();
                //Color3D colorTemp = Color(new Ray(new Point3D(rec.P.X, rec.P.Y, rec.P.Z), target - rec.P), world);
                //colorTemp.R *= 0.5;
                //colorTemp.G *= 0.5;
                //colorTemp.B *= 0.5;
                //return  colorTemp;
            }
            else
            {
                Vector3 unitDirection = Vector3.UnitVector(r.Direction);
                double  t             = 0.5 * (unitDirection.Y + 1.0);
                return(new Color3D((1.0 - t) + t * 0.5, (1.0 - t) + t * 0.7, (1.0 - t) + t * 1));
            }
        }
Ejemplo n.º 2
0
        private void button1_Click(object sender, EventArgs e)
        {
            int nx = 1080;
            int ny = 1080;
            int ns = 10;

            bmp = new Bitmap(nx, ny);

            Random rd = new Random();

            List <Hitable> list = new List <Hitable>();

            list.Add(new Sphere(new Point3D(0, 0, -1), 0.5, new Lambertian(new Vector3(0.8, 0.3, 0.3))));
            list.Add(new Sphere(new Point3D(0, -100.5, -1), 100, new Lambertian(new Vector3(0.8, 0.8, 0.0))));
            list.Add(new Sphere(new Point3D(1, 0, -1), 0.5, new Metal(new Vector3(0.8, 0.6, 0.2), 0.3)));
            list.Add(new Sphere(new Point3D(-1, 0, -1), 0.5, new Dielectrics(1.5)));
            HitableList world = new HitableList(list, 4);
            Camera      cam   = new Camera(new Point3D(-2, 2, 1), new Point3D(0, 0, -1), new Vector3(0, 1, 0), 90, (double)(nx) / (double)(ny));

            for (int i = 0; i < nx; i++)
            {
                for (int j = 0; j < ny; j++)
                {
                    Color3D col = new Color3D();
                    for (int s = 0; s < ns; s++)
                    {
                        double  u       = (double)(i + rd.NextDouble()) / (double)nx;
                        double  v       = (double)(j + rd.NextDouble()) / (double)ny;
                        Ray     r       = cam.GetRay(u, v);
                        Color3D colTemp = RTUtils.Color(r, world, 0);
                        col.R += colTemp.R;
                        col.G += colTemp.G;
                        col.B += colTemp.B;
                    }
                    col.R /= ns;
                    col.G /= ns;
                    col.B /= ns;

                    col = new Color3D(Math.Sqrt(col.R), Math.Sqrt(col.G), Math.Sqrt(col.B));

                    int ir = (int)(255.99 * col.R);
                    int ig = (int)(255.99 * col.G);
                    int ib = (int)(255.99 * col.B);
                    bmp.SetPixel(i, ny - j - 1, Color.FromArgb(ir, ig, ib));
                }
            }
            pictureBox1.BackgroundImage = bmp;
        }