Пример #1
0
        public bool TryIntersect(Ray ray, double tmin, double tmax, ref ShadeRecord sr)
        {
            var t = (this.k - ray.Origin.X) / ray.Direction.X;

            if (t < tmin || t > tmax)
            {
                return(false);
            }

            var y = ray.Origin.Y + t * ray.Direction.Y;
            var z = ray.Origin.Z + t * ray.Direction.Z;

            if (y < y0 || y > y1 || z < z0 || z > z1)
            {
                return(false);
            }

            var u = (y - y0) / (y1 - y0);
            var v = (z - z0) / (z1 - z0);
            var n = new Normal3(1, 0, 0);

            sr = new ShadeRecord
            {
                U        = u,
                V        = v,
                T        = t,
                Material = this.material,
                Point    = ray.GetPosition(t),
            };

            sr.SetFaceNormal(ray, n);
            return(true);
        }
Пример #2
0
        public bool TryIntersect(Ray ray, double tmin, double tmax, ref ShadeRecord sr)
        {
            var t = (this.k - ray.Origin.Z) / ray.Direction.Z;

            if (t < tmin || t > tmax)
            {
                return(false);
            }

            var x = ray.Origin.X + t * ray.Direction.X;
            var y = ray.Origin.Y + t * ray.Direction.Y;

            if (x < x0 || x > x1 || y < y0 || y > y1)
            {
                return(false);
            }

            var u = (x - x0) / (x1 - x0);
            var v = (y - y0) / (y1 - y0);
            var n = new Normal3(0, 0, 1);

            sr = new ShadeRecord
            {
                U        = u,
                V        = v,
                T        = t,
                Material = this.material,
                Point    = ray.GetPosition(t),
            };

            sr.SetFaceNormal(ray, n);
            return(true);
        }
Пример #3
0
        public bool TryIntersect(
            Ray ray,
            double tmin,
            double tmax,
            ref ShadeRecord sr)
        {
            var oc    = ray.Origin - this.center;
            var a     = Vector3.MagnitudeSquared(ray.Direction);
            var halfB = Vector3.Dot(oc, ray.Direction);
            var c     = Vector3.MagnitudeSquared(oc) - this.radius * this.radius;

            var discriminant = halfB * halfB - a * c;

            if (discriminant < 0)
            {
                return(false);
            }

            var sqrtd = Math.Sqrt(discriminant);
            var root  = (-halfB - sqrtd) / a;

            if (root < tmin || tmax < root)
            {
                root = (-halfB + sqrtd) / a;
                if (root < tmin || tmax < root)
                {
                    return(false);
                }
            }

            ShadeRecord CreateShadeRecord(double t)
            {
                var p  = ray.GetPosition(t);
                var n  = (p - this.center) / this.radius;
                var sr = new ShadeRecord
                {
                    Point    = p,
                    T        = t,
                    Material = this.material,
                };

                sr.SetFaceNormal(ray, (Normal3)n);
                (sr.U, sr.V) = GetSphereUV((Point3)n);
                return(sr);
            }

            sr = CreateShadeRecord(root);
            return(true);
        }
Пример #4
0
        public bool TryIntersect(
            Ray ray,
            double tmin,
            double tmax,
            ref ShadeRecord sr)
        {
            if (!this.box.TryIntersect(ray, tmin, tmax))
            {
                // sr = null;
                return(false);
            }

            var hitLeft  = this.left.TryIntersect(ray, tmin, tmax, ref sr);
            var hitRight = this.right.TryIntersect(ray, tmin, hitLeft ? sr.T : tmax, ref sr);

            return(hitLeft || hitRight);
        }
Пример #5
0
        public bool TryIntersect(
            Ray ray,
            double tmin,
            double tmax,
            ref ShadeRecord sr)
        {
            var origin = new Point3(
                this.cosTheta * ray.Origin.X - this.sinTheta * ray.Origin.Z,
                ray.Origin.Y,
                this.sinTheta * ray.Origin.X + this.cosTheta * ray.Origin.Z);

            var direction = new Vector3(
                this.cosTheta * ray.Direction.X - this.sinTheta * ray.Direction.Z,
                ray.Direction.Y,
                this.sinTheta * ray.Direction.X + this.cosTheta * ray.Direction.Z);

            var rotatedRay = new Ray(origin, direction, ray.Time);

            if (!this.obj.TryIntersect(rotatedRay, tmin, tmax, ref sr))
            {
                return(false);
            }

            var p = new Point3(
                this.cosTheta * sr.Point.X + this.sinTheta * sr.Point.Z,
                sr.Point.Y,
                -this.sinTheta * sr.Point.X + this.cosTheta * sr.Point.Z);

            var n = new Normal3(
                this.cosTheta * sr.Normal.X + this.sinTheta * sr.Normal.Z,
                sr.Normal.Y,
                -this.sinTheta * sr.Normal.X + this.cosTheta * sr.Normal.Z);

            sr.Point = p;
            sr.SetFaceNormal(rotatedRay, n);

            return(true);
        }
Пример #6
0
        public bool TryIntersect(
            Ray ray,
            double tmin,
            double tmax,
            ref ShadeRecord sr)
        {
            var hitAnything  = false;
            var closestSoFar = tmax;

            ShadeRecord tmp = null;

            foreach (var obj in this.Objects)
            {
                if (obj.TryIntersect(ray, tmin, closestSoFar, ref tmp))
                {
                    hitAnything  = true;
                    closestSoFar = tmp.T;
                    sr           = tmp;
                }
            }

            return(hitAnything);
        }
Пример #7
0
        public bool TryIntersect(
            Ray ray,
            double tmin,
            double tmax,
            ref ShadeRecord sr)
        {
            var rng = new Random();

            ShadeRecord rec1 = null, rec2 = null;

            if (!this.boundary.TryIntersect(
                    ray,
                    double.NegativeInfinity,
                    double.PositiveInfinity,
                    ref rec1))
            {
                return(false);
            }

            if (!this.boundary.TryIntersect(
                    ray,
                    rec1.T + 0.0001,
                    double.PositiveInfinity,
                    ref rec2))
            {
                return(false);
            }

            if (rec1.T < tmin)
            {
                rec1.T = tmin;
            }

            if (rec2.T > tmax)
            {
                rec2.T = tmax;
            }

            if (rec1.T >= rec2.T)
            {
                return(false);
            }

            if (rec1.T < 0)
            {
                rec1.T = 0;
            }

            var rayLength              = ray.Direction.Magnitude();
            var distanceInsideBoundary = (rec2.T - rec1.T) * rayLength;
            var hitDistance            = this.negInvDensity * Math.Log(rng.RandomDouble());

            if (hitDistance > distanceInsideBoundary)
            {
                return(false);
            }

            sr       = new ShadeRecord();
            sr.T     = rec1.T + hitDistance / rayLength;
            sr.Point = ray.GetPosition(sr.T);
            var n = new Normal3(1, 0, 0);

            sr.SetFaceNormal(ray, n);
            sr.Material = this.phaseFunction;
            return(true);
        }