Пример #1
0
        public override Intersection Intersect(Ray ray)
        {
            double a = ray.Direction.Dot(Point);

            if (Math.Abs(a - 0d) < 0.01d)
            {
                return(new Intersection());
            }

            double t = Point.Dot(ray.Point.Add(-Point.Mult(Distance)));

            Vector point = ray.PointAt(t);

            return(new Intersection(t, point, this.point, material));
        }
Пример #2
0
        public override Intersection Intersect(Ray ray)
        {
            Vector diff        = this.point - ray.Point;
            double b           = diff.Dot(ray.Direction);
            double determinant = (b * b) - diff.Dot(diff) + radiusSquared;

            if (determinant < 0.0)
            {
                return(new Intersection());
            }
            else
            {
                double detSqrt = Math.Sqrt(determinant);
                double b1      = b - detSqrt;
                double b2      = b + detSqrt;
                double t       = (b1 > Vector.EPSILON) ? b1 :
                                 ((b2 > Vector.EPSILON) ? b2 : Intersection.MaxT);
                Vector point  = ray.PointAt(t);
                Vector normal = (point - this.point) * radiusRecip;
                return(new Intersection(t, point, normal, material));
            }
        }
        public static bool Hit(Sphere sphere, Ray ray, float tMin, float tMax, out RayHit hit)
        {
            Vector3 center       = sphere.Center;
            Vector3 oc           = ray.Origin - center;
            Vector3 rayDir       = ray.Direction;
            float   a            = Vector3.Dot(rayDir, rayDir);
            float   b            = Vector3.Dot(oc, rayDir);
            float   radius       = sphere.Radius;
            float   c            = Vector3.Dot(oc, oc) - radius * radius;
            float   discriminant = b * b - a * c;

            if (discriminant > 0)
            {
                float tmp = MathF.Sqrt(b * b - a * c);
                float t   = (-b - tmp) / a;
                if (t < tMax && t > tMin)
                {
                    Vector3 position = Ray.PointAt(ray, t);
                    Vector3 normal   = (position - center) / radius;
                    hit = RayHit.Create(Ray.PointAt(ray, t), t, normal);
                    return(true);
                }
                t = (-b + tmp) / a;
                if (t < tMax && t > tMin)
                {
                    Vector3 position = Ray.PointAt(ray, t);
                    Vector3 normal   = (position - center) / radius;
                    hit = RayHit.Create(position, t, normal);
                    return(true);
                }
            }

            hit.Position = new Vector3();
            hit.Normal   = new Vector3();
            hit.T        = 0;
            return(false);
        }
Пример #4
0
        public override Intersection Intersect(Ray ray)
        {
            double nMinX, nMinY, nMinZ, nMaxX, nMaxY, nMaxZ;
            double tMin, tMax;
            double tX1 = (-Width + this.point.X - ray.Point.X) / ray.Direction.X;
            double tX2 = (Width + this.point.X - ray.Point.X) / ray.Direction.X;

            if (tX1 < tX2)
            {
                tMin  = tX1;
                tMax  = tX2;
                nMinX = -Width;
                nMinY = 0.0;
                nMinZ = 0.0;
                nMaxX = Width;
                nMaxY = 0.0;
                nMaxZ = 0.0;
            }
            else
            {
                tMin  = tX2;
                tMax  = tX1;
                nMinX = Width;
                nMinY = 0.0;
                nMinZ = 0.0;
                nMaxX = -Width;
                nMaxY = 0.0;
                nMaxZ = 0.0;
            }

            if (tMin > tMax)
            {
                return(new Intersection());
            }

            double tY1 = (-Height + this.point.Y - ray.Point.Y) / ray.Direction.Y;
            double tY2 = (Height + this.point.Y - ray.Point.Y) / ray.Direction.Y;

            if (tY1 < tY2)
            {
                if (tY1 > tMin)
                {
                    tMin  = tY1;
                    nMinX = 0.0;
                    nMinY = -Height;
                    nMinZ = 0.0;
                }
                if (tY2 < tMax)
                {
                    tMax  = tY2;
                    nMaxX = 0.0;
                    nMaxY = Height;
                    nMaxZ = 0.0;
                }
            }
            else
            {
                if (tY2 > tMin)
                {
                    tMin  = tY2;
                    nMinX = 0.0;
                    nMinY = Height;
                    nMinZ = 0.0;
                }
                if (tY1 < tMax)
                {
                    tMax  = tY1;
                    nMaxX = 0.0;
                    nMaxY = -Height;
                    nMaxZ = 0.0;
                }
            }

            if (tMin > tMax)
            {
                return(new Intersection());
            }

            double tZ1 = (-Depth + this.point.Z - ray.Point.Z) / ray.Direction.Z;
            double tZ2 = (Depth + this.point.Z - ray.Point.Z) / ray.Direction.Z;

            if (tZ1 < tZ2)
            {
                if (tZ1 > tMin)
                {
                    tMin  = tZ1;
                    nMinX = 0.0;
                    nMinY = 0.0;
                    nMinZ = -Depth;
                }
                if (tZ2 < tMax)
                {
                    tMax  = tZ2;
                    nMaxX = 0.0;
                    nMaxY = 0.0;
                    nMaxZ = Depth;
                }
            }
            else
            {
                if (tZ2 > tMin)
                {
                    tMin  = tZ2;
                    nMinX = 0.0;
                    nMinY = 0.0;
                    nMinZ = Depth;
                }
                if (tZ1 < tMax)
                {
                    tMax  = tZ1;
                    nMaxX = 0.0;
                    nMaxY = 0.0;
                    nMaxZ = -Depth;
                }
            }

            if (tMin > tMax)
            {
                return(new Intersection());
            }

            if (tMin < 0.0)
            {
                tMin  = tMax;
                nMinX = nMaxX;
                nMinY = nMaxY;
                nMinZ = nMaxZ;
            }

            if (tMin >= 0.0)
            {
                double t      = tMin;
                Vector point  = ray.PointAt(t);
                Vector normal = new Vector(nMinX, nMinY, nMinZ).Normalized;
                return(new Intersection(t, point, normal, this.material));
            }
            else
            {
                return(new Intersection());
            }
        }