Exemplo n.º 1
0
        public bool IsShadowed(RTObject obj, Computations comps, Point lightPosition, Point point)
        {
            bool isShadowed = false;

            var v         = new Vector(lightPosition - point);
            var distance  = v.Magnitude;
            var direction = v.Normalize;

            var r             = new Ray(point, direction);
            var intersections = Intersect(r);

            var h = intersections.Hit;

            if ((h != null) && (h.t < distance) && (h.t > MathHelper.Epsilon))
            {
                var dN = obj is Triangle?direction.Dot(comps.NormalVector) : -1;

                if (!obj.Equals(h.Object) && h.Object.HasShadow && (dN <= 0))
                {
                    isShadowed = true;
                }
            }

            return(isShadowed);
        }
Exemplo n.º 2
0
        public double IntensityAt(RTObject obj, Computations comps, World w)
        {
            if (w.IsShadowed(obj, comps, Position, comps.OverPoint))
            {
                return(0.0);
            }

            return(1.0);
        }
Exemplo n.º 3
0
        public Color ReflectedColor(Computations comps, int remaining = 5)
        {
            if (remaining <= 0)
            {
                return(Color.Black);
            }

            if (comps.Object.Material.Reflective.IsEqual(0.0))
            {
                return(Color.Black);
            }

            var reflectRay = new Ray(comps.OverPoint, comps.ReflectVector);
            var color      = ColorAt(reflectRay, remaining - 1);

            return(color * comps.Object.Material.Reflective);
        }
Exemplo n.º 4
0
        public double IntensityAt(RTObject obj, Computations comps, World w)
        {
            var total = 0.0;

            for (int v = 0; v < VSteps; v++)
            {
                for (int u = 0; u < USteps; u++)
                {
                    var lightPosition = PointOnLight(u, v);
                    if (!w.IsShadowed(obj, comps, lightPosition, comps.OverPoint))
                    {
                        total += 1.0;
                    }
                }
            }

            return(total / Samples);
        }
Exemplo n.º 5
0
        public Color ShadeHit(RTObject obj, Computations comps, int remaining = 5)
        {
            var    surface   = Color.Black;
            double intensity = 0.0;
            int    count     = 0;

            foreach (var light in Lights)
            {
                count++;
                intensity = light.IntensityAt(obj, comps, this);

                surface = surface +
                          comps.Object.Material.Lighting(
                    obj,
                    light,
                    comps.OverPoint,
                    comps.EyeVector,
                    comps.NormalVector,
                    intensity);
            }

            if (count > 0)
            {
                surface = surface / count;
            }

            var reflected = ReflectedColor(comps, remaining);
            var refracted = RefractedColor(comps, remaining);

            var material = comps.Object.Material;

            if ((material.Reflective > 0) && (material.Transparency > 0))
            {
                var reflectance = comps.Schlick;
                return(surface + reflected * reflectance + refracted * (1 - reflectance));
            }

            return(surface + reflected + refracted);
        }
Exemplo n.º 6
0
        public Color RefractedColor(Computations comps, int remaining)
        {
            if ((remaining <= 0) || comps.Object.Material.Transparency.IsEqual(0.0))
            {
                return(Color.Black);
            }

            var nRatio = comps.N1 / comps.N2;
            var cosI   = comps.EyeVector.Dot(comps.NormalVector);
            var sin2T  = nRatio * nRatio * (1 - cosI * cosI);

            if (sin2T > 1.0)
            {
                return(Color.Black);
            }

            var cosT       = Math.Sqrt(1.0 - sin2T);
            var direction  = new Vector(comps.NormalVector * (nRatio * cosI - cosT) - comps.EyeVector * nRatio);
            var refractRay = new Ray(comps.UnderPoint, direction);
            var color      = ColorAt(refractRay, remaining - 1) * comps.Object.Material.Transparency;

            return(color);
        }