Esempio n. 1
0
        public static Vektor ray_color(ray r, hittable world, int depth)
        {
            hit_record rec = new hit_record();

            if (depth <= 0)
            {
                return(new Vektor(0, 0, 0));
            }

            zwischenSpeicher zw = world.Hit(r, 0.0001, Mathe.infinity, rec);

            if (zw.IsTrue)
            {
                rec = zw.rec;
                ray              scattered   = new ray();
                Vektor           attenuation = new Vektor();
                zwischenSpeicher zw1         = rec.mat_ptr.scatter(r, rec, attenuation, scattered);

                if (zw1.IsTrue)
                {
                    attenuation = zw1.attenuation;
                    scattered   = zw1.scattered;

                    return(attenuation * ray_color(scattered, world, depth - 1));
                }
                return(new Vektor(0, 0, 0));
            }

            Vektor unit_direction = Vektor.unit_Vektor(r.Direction);
            var    t   = 0.5 * (unit_direction.Y + 1);
            Vektor col = (1 - t) * new Vektor(1, 1, 1) + t * new Vektor(0.5, 0.7, 1);

            return(col);
        }
Esempio n. 2
0
        public override zwischenSpeicher scatter(ray r_in, hit_record rec, Vektor attenuation, ray scattered)
        {
            zwischenSpeicher zw = new zwischenSpeicher();

            attenuation = new Vektor(1, 1, 1);
            double refraction_ratio = rec.front_face ? (1 / ir) : ir;

            Vektor unit_direction = Vektor.unit_Vektor(r_in.Direction);
            double cos_theta      = Math.Min(Vektor.dot(unit_direction * -1, rec.normal), 1);
            double sin_theta      = Math.Sqrt(1 - cos_theta * cos_theta);

            bool   cannot_refract = refraction_ratio * sin_theta > 1;
            Vektor direction;

            if (cannot_refract)
            {
                direction = Vektor.reflect(unit_direction, rec.normal);
            }
            else
            {
                direction = Vektor.refract(unit_direction, rec.normal, refraction_ratio);
            }

            scattered = new ray(rec.p, direction);

            zw.attenuation = attenuation;
            zw.scattered   = scattered;
            zw.IsTrue      = true;

            return(zw);
        }
Esempio n. 3
0
        public override zwischenSpeicher Hit(ray r, double t_min, double t_max, hit_record rec)
        {
            zwischenSpeicher zw = new zwischenSpeicher();
            Vektor           oc = r.Origin - center;
            var a      = r.Direction.length_squared();
            var half_b = Vektor.dot(oc, r.Direction);
            var c      = oc.length_squared() - radius * radius;

            var discriminant = half_b * half_b - a * c;

            if (discriminant < 0)
            {
                zw.IsTrue = false;
                return(zw);
            }
            var sqrtd = Math.Sqrt(discriminant);

            var root = (-half_b - sqrtd) / a;

            if (root < t_min || t_max < root)
            {
                root = (-half_b + sqrtd) / a;
                if (root < t_min || t_max < root)
                {
                    zw.IsTrue = false;
                    return(zw);
                }
            }

            rec.t = root;
            rec.p = r.at(rec.t);
            Vektor outward_normal = (rec.p - center) / radius;

            rec.set_face_normal(r, outward_normal);
            rec.mat_ptr = mat_ptr;

            zw.rec    = rec;
            zw.IsTrue = true;

            return(zw);
        }
Esempio n. 4
0
        public override zwischenSpeicher Hit(ray r, double t_min, double t_max, hit_record rec)
        {
            zwischenSpeicher zw           = new zwischenSpeicher();
            hit_record       temp_rec     = new hit_record();
            bool             hit_anything = false;
            var closest_so_far            = t_max;

            foreach (var Object in objects)
            {
                zwischenSpeicher zw1 = Object.Hit(r, t_min, closest_so_far, temp_rec);
                if (zw1.IsTrue)
                {
                    temp_rec       = zw1.rec;
                    hit_anything   = true;
                    closest_so_far = temp_rec.t;
                    rec            = temp_rec;
                }
            }

            zw.rec    = rec;
            zw.IsTrue = hit_anything;

            return(zw);
        }