예제 #1
0
        private static HitRecord GetHitPoint(Ray ray, HitRecord hit)
        {
            var r = ray.At(hit.T);

            hit.Point = new Point(r.X, r.Y, r.Z);
            return(hit);
        }
예제 #2
0
        public override bool Hit(Ray r, double tMin, double tMax, out Object3D rec)
        {
            var oc           = r.Origin - Center;
            var a            = r.Direction.LengthSquared;
            var halfB        = Vector3.DotProduct(oc, r.Direction);
            var c            = oc.LengthSquared - Radius * Radius;
            var discriminant = halfB * halfB - a * c;

            if (discriminant > 0)
            {
                var root = Math.Sqrt(discriminant);
                var temp = (-halfB - root) / a;
                if (temp < tMax && temp > tMin)
                {
                    rec   = new Sphere(Center, Radius, Material);
                    rec.T = temp;
                    rec.P = r.At(rec.T);
                    var outwardNormal = (rec.P - Center) / Radius;
                    rec.SetFaceNormal(r, outwardNormal);
                    return(true);
                }

                temp = (-halfB + root) / a;
                if (temp < tMax && temp > tMin)
                {
                    rec   = new Sphere(Center, Radius, Material);
                    rec.T = temp;
                    rec.P = r.At(rec.T);
                    var outwardNormal = (rec.P - Center) / Radius;
                    rec.SetFaceNormal(r, outwardNormal);
                    return(true);
                }
            }

            rec = null;
            return(false);
        }