Example #1
0
        public override bool Hit(ref Ray ray, double t_min, double t_max, ref Hit_record rec)
        {
            Vector3 oc   = ray.Origin - this.Position;
            Vector3 rdir = ray.Destination;
            double  a    = Vector3.Dot(ref rdir, ref rdir);
            double  b    = Vector3.Dot(ref oc, ref rdir);
            double  c    = Vector3.Dot(ref oc, ref oc) - this.Radius * this.Radius;
            double  disc = b * b - a * c;

            if (disc > 0)
            {
                double tmp = (-b - Math.Sqrt(disc)) / a;
                if (tmp < t_max && tmp > t_min)
                {
                    rec.mat_ref = this._material;
                    rec.t       = tmp;
                    rec.p       = ray.Point_to_parameter(ref tmp);
                    rec.normal  = (rec.p - this.Position) / this.Radius;
                    return(true);
                }
                tmp = (-b + Math.Sqrt(disc)) / a;
                if (tmp < t_max && tmp > t_min)
                {
                    rec.mat_ref = this._material;
                    rec.t       = tmp;
                    rec.p       = ray.Point_to_parameter(ref tmp);
                    rec.normal  = (rec.p - this.Position) / this.Radius;
                    return(true);
                }
            }
            return(false);
        }
Example #2
0
        public override bool Scatter(ref Ray ray_in, ref Hit_record rec, ref Vector3 attenuation, ref Ray scatered)
        {
            Vector3 target = rec.normal + Rand_in_unit_spthere();

            scatered    = new Ray(rec.p, target, 10000.0);
            attenuation = _albedo;
            return(true);
        }
Example #3
0
        public override bool Scatter(ref Ray ray_in, ref Hit_record rec, ref Vector3 attenuation, ref Ray scatered)
        {
            Vector3 tmp = Reflect(ray_in.Destination.Normalized(), rec.normal);

            scatered    = new Ray(rec.p, tmp + _fuzz * Rand_in_unit_spthere(), 10000.0);
            tmp         = scatered.Destination;
            attenuation = _albedo;
            return(Vector3.Dot(ref tmp, ref rec.normal) > 0);
        }
Example #4
0
        public override bool Hit(ref Ray ray, double t_min, double t_max, ref Hit_record rec)
        {
            Hit_record tmp         = new Hit_record();
            bool       did_hit     = false;
            double     closest_hit = t_max;

            foreach (var element in _list)
            {
                if (element.Hit(ref ray, t_min, closest_hit, ref tmp))
                {
                    did_hit     = true;
                    closest_hit = tmp.t;
                    rec         = tmp;
                }
            }
            return(did_hit);
        }
Example #5
0
        public override bool Scatter(ref Ray ray_in, ref Hit_record rec, ref Vector3 attenuation, ref Ray scatered)
        {
            Vector3 direction = ray_in.Destination, out_norm;

            Vector3 reflected = reflect(ref direction, ref rec.normal);
            double  ni_over_nt;

            attenuation = new Vector3(1.0, 1.0, 1.0);
            Vector3 refracted = new Vector3(1.0, 1.0, 1.0);
            double  cosine, ref_prob;

            if (Vector3.Dot(ref direction, ref rec.normal) > 0)
            {
                out_norm   = -rec.normal;
                ni_over_nt = this._ref_idx;
                cosine     = Vector3.Dot(ref direction, ref rec.normal) / ray_in.Destination.Length();
                cosine     = Math.Sqrt(1 - this._ref_idx * this._ref_idx * (1 - cosine * cosine));
            }
            else
            {
                out_norm   = rec.normal;
                ni_over_nt = 1.0 / this._ref_idx;
                cosine     = -Vector3.Dot(ref direction, ref rec.normal) / ray_in.Destination.Length();
            }
            if (refract(ref direction, ref out_norm, ref ni_over_nt, ref refracted))
            {
                ref_prob = schlick(cosine, this._ref_idx);
            }
            else
            {
                ref_prob = 1.0;
            }
            if (this._rand.NextDouble() < ref_prob)
            {
                scatered = new Ray(rec.p, reflected, 10000000.0);
            }
            else
            {
                scatered = new Ray(rec.p, refracted, 10000000.0);
            }
            return(true);
        }
Example #6
0
        private Vector3 Pixel_color(ref Ray ray, ref Hitable_list hlist, int depth)
        {
            Hit_record hrec = new Hit_record();
            Vector3    v;

            if (hlist.Hit(ref ray, 0.001, Double.MaxValue, ref hrec))
            {
                Ray     scatered    = new Ray();
                Vector3 attenuation = new Vector3(1.0, 1.0, 1.0);
                if (depth < this._rebond && hrec.mat_ref.Scatter(ref ray, ref hrec, ref attenuation, ref scatered))
                {
                    return(attenuation * Pixel_color(ref scatered, ref hlist, depth + 1));
                }
                return(new Vector3(0, 0, 0));
            }
            else
            {
                v = ray.Destination.Normalized();
                double t = 0.5 * (v[1] + 1.0);
                v = (1.0 - t) * new Vector3(1.0, 1.0, 1.0) + t * new Vector3(0.5, 0.7, 1.0);
                return(v);
            }
        }
Example #7
0
 public abstract bool Scatter(ref Ray ray_in, ref Hit_record rec, ref Vector3 attenuation, ref Ray scatered);
Example #8
0
 public abstract bool Hit(ref Ray ray, double f_min, double f_max, ref Hit_record rec);