Example #1
0
        public static Tuple ColourAt(World world, Ray ray, int remaining)
        {
            Intersection[] worldIntersections = IntersectWorld(world, ray);
            Intersection   hit = Intersect.Hit(worldIntersections);

            if (hit != null)
            {
                Precomputation comps = Intersect.PrepareComputations(hit, ray);
                return(ShadeHit(world, comps, remaining));
            }
            else
            {
                return(new Tuple(0, 0, 0, 0));
            }
        }
        public override Intersection[] LocalIntersect(Ray localRay)
        {
            LocalRay = localRay;

            if (MathF.Abs(LocalRay.Direction.y) < Arithmetic.EPSILON)
            {
                return(new Intersection[0]);
            }
            else
            {
                float        t  = -localRay.Origin.y / localRay.Direction.y;
                Intersection i1 = new Intersection(t, this);
                return(Intersect.Intersections(i1));
            }
        }
Example #3
0
        public static Intersection[] IntersectWorld(World world, Ray r)
        {
            //Create list that we will turn into array, it is done this way for now but refactoring other parts of the code to a list later will help
            int objCount = world.SceneObjects.Length;
            List <Intersection> intersectionList = new List <Intersection>();

            for (int i = 0; i < objCount; i++)
            {
                Intersection[] intersection = Intersect.IntersectShape(world.SceneObjects[i], r);
                //practicing foreach loops
                foreach (Intersection elem in intersection)
                {
                    intersectionList.Add(elem);
                }
            }

            Intersection[] intersections = Intersect.SortIntersections(intersectionList.ToArray());
            return(intersections);
        }
Example #4
0
        public static bool IsShadowed(World world, Tuple point)
        {
            Tuple lightv    = world.SceneLight.Position - point;
            float distance  = lightv.Magnitude();
            Tuple direction = lightv.Normalise();
            Ray   shadowRay = new Ray(point, direction);

            Intersection[] worldIntersections = IntersectWorld(world, shadowRay);
            Intersection   hit = Intersect.Hit(worldIntersections);

            if (hit != null)
            {
                if (hit.T_Value < distance)
                {
                    return(true);
                }
            }
            return(false);
        }
        public override Intersection[] LocalIntersect(Ray localRay)
        {
            Tuple sphereToRay = localRay.Origin - new Tuple(0, 0, 0, 1);
            float a           = localRay.Direction.Dot(localRay.Direction);
            float b           = 2 * localRay.Direction.Dot(sphereToRay);
            float c           = sphereToRay.Dot(sphereToRay) - 1;

            float discriminant = (b * b) - 4 * a * c;

            if (discriminant < 0)
            {
                Intersection[] empty = new Intersection[0];
                return(empty);
            }



            Intersection i1 = new Intersection((-b - MathF.Sqrt(discriminant)) / (2 * a), this);
            Intersection i2 = new Intersection((-b + MathF.Sqrt(discriminant)) / (2 * a), this);

            return(Intersect.Intersections(i1, i2));
        }