示例#1
0
        public Vec GetRayColor(Ray r, ref HitableList world, int depth)
        {
            HitRecord rec = new HitRecord();

            if (depth <= 0)
            {
                return(new Vec(0, 0, 0));            // no more color contribution
            }
            if (world.Hit(r, 0.001, Double.MaxValue, ref rec))
            {
                Ray scattered   = new Ray();
                Vec attenuation = new Vec();

                if (rec.Material.Scatter(r, ref rec, ref attenuation, ref scattered))
                {
                    return(attenuation * GetRayColor(scattered, ref world, depth - 1));
                }
                return(new Vec(0, 0, 0));
            }

            Vec    dir = r.Direction.Normalize();
            double t   = 0.5 * (dir.Y + 1.0);

            return((1.0 - t) * new Vec(1.0, 1.0, 1.0) + t * new Vec(0.5, 0.7, 1.0));
        }
示例#2
0
        public Bitmap Render()
        {
            int nbSamples = 100;
            int maxDepth  = 10;

            HitableList world = new HitableList();

            var groundMaterial = new Lambertian(new Vec(0.8, 0.8, 0.8));
            var pinkMaterial   = new Lambertian(new Vec(1, 0, 0.57));
            var redMaterial    = new Metal(new Vec(0.8, 0.3, 0.3));
            var mirrorMaterial = new Metal(new Vec(0.8, 0.8, 0.8));
            var blueMaterial   = new Metal(new Vec(0.3, 0.7, 1));
            var greenMaterial  = new Metal(new Vec(0.3, 0.7, 0.3));

            world.Add(new Sphere(new Vec(0.0, -100.5, -1.0), 100.0, groundMaterial)); // ground
            world.Add(new Sphere(new Vec(0.0, -0.0, -1.5), 0.5, mirrorMaterial));     // center
            world.Add(new Sphere(new Vec(-1.0, -0.0, -1.0), 0.5, redMaterial));       // left
            world.Add(new Sphere(new Vec(1.0, -0.2, -1.0), 0.3, blueMaterial));       // rsight
            world.Add(new Sphere(new Vec(0.5, -0.0, 10.5), 10, greenMaterial));       // back
            world.Add(new Sphere(new Vec(0.3, -0.4, -1.0), 0.1, pinkMaterial));       // back
            world.Add(new Sphere(new Vec(0.4, -0.4, -0.2), 0.1, pinkMaterial));       // back

            Camera cam = new Camera(90.0, _aspectRatio);

            for (int j = _imageHeight - 1; j >= 0; j--)
            {
                Console.WriteLine($"Computing line {_imageHeight - j} of {_imageHeight}");
                for (int i = 0; i < _imageWidth; i++)
                {
                    Vec col = new Vec(0, 0, 0);
                    for (int sample = 0; sample < nbSamples; sample++)
                    {
                        double u = (double)(i + RNG.val.NextDouble()) / (double)_imageWidth;
                        double v = (double)(j + RNG.val.NextDouble()) / (double)_imageHeight;

                        Ray r = cam.GetRay(u, v);
                        col += GetRayColor(r, ref world, maxDepth);
                    }
                    _map.SetPixel(i, _imageHeight - j - 1, GetRGBColor(col, nbSamples));
                }
            }
            return(_map);
        }