Esempio n. 1
0
        public override bool hit(Ray r, double t_min, double t_max, ref hit_record rec)
        {
            Vec3 oc     = r.origin - center;
            var  a      = r.direction.length_squared();
            var  half_b = Vec3.dot(oc, r.direction);
            var  c      = oc.length_squared() - radius * radius;

            var discriminant = half_b * half_b - a * c;

            if (discriminant < 0)
            {
                return(false);
            }
            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)
                {
                    return(false);
                }
            }

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

            rec.set_face_normal(r, outward_normal);
            rec.mat_prt = this.mat;

            return(true);
        }
Esempio n. 2
0
        public override bool scatter(Ray r_in, hit_record rec, ref Color attenuation, ref Ray scattered)
        {
            Vec3 reflected = Vec3.reflect(Vec3.unit_vector(r_in.direction), rec.normal);

            scattered   = new Ray(rec.p, reflected + fuzz * Vec3.random_in_unit_sphere());
            attenuation = this.albedo;
            return(Vec3.dot(scattered.direction, rec.normal) > 0);
        }
Esempio n. 3
0
        public override bool scatter(Ray r_in, hit_record rec, ref Color attenuation, ref Ray scattered)
        {
            var scatter_direction = rec.normal + Vec3.random_unit_vector();

            if (scatter_direction.near_zero())
            {
                scatter_direction = rec.normal;
            }
            scattered   = new Ray(rec.p, scatter_direction);
            attenuation = albedo;
            return(true);
        }
Esempio n. 4
0
        public virtual bool hit(Ray r, double t_min, double t_max, ref hit_record rec)
        {
            hit_record temp_rec       = new hit_record();
            bool       hit_anything   = false;
            var        closest_so_far = t_max;

            foreach (Hittable hittable in hittables)
            {
                if (hittable.hit(r, t_min, closest_so_far, ref temp_rec))
                {
                    hit_anything   = true;
                    closest_so_far = temp_rec.t;
                    rec            = temp_rec;
                }
            }
            return(hit_anything);
        }
Esempio n. 5
0
        public override bool scatter(Ray r_in, hit_record rec, ref Color attenuation, ref Ray scattered)
        {
            attenuation = new Color(1.0, 1.0, 1.0);
            double refraction_ration = rec.front_face ? (1.0 / ir) : ir;

            Vec3   unit_direction = Vec3.unit_vector(r_in.direction);
            double cos_theta      = Math.Min(Vec3.dot(-unit_direction, rec.normal), 1.0);
            double sin_theta      = Math.Sqrt(1.0 - cos_theta * cos_theta);

            bool cannot_refract = refraction_ration * sin_theta > 1.0;
            Vec3 direction      = new Vec3();

            if (cannot_refract || reflectance(cos_theta, refraction_ration) > Utilities.random_double())
            {
                direction = Vec3.reflect(unit_direction, rec.normal);
            }
            else
            {
                direction = Vec3.refract(unit_direction, rec.normal, refraction_ration);
            }
            scattered = new Ray(rec.p, direction);
            return(true);
        }
Esempio n. 6
0
 public virtual bool scatter(Ray r_in, hit_record rec, ref Color attenuation, ref Ray scattered) => false;
Esempio n. 7
0
 public virtual bool hit(Ray r, double t_min, double t_max, ref hit_record rec) => false;