コード例 #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);
        }
コード例 #2
0
        private bool ChildContains(RTObject child, RTObject obj)
        {
            if (child.Equals(obj))
            {
                return(true);
            }

            var group = child as Group;

            if (group != null)
            {
                if (group.Contains(obj))
                {
                    return(true);
                }
            }

            var csg = child as CSG;

            if (csg != null)
            {
                return(ChildContains(csg.Left, obj) || ChildContains(csg.Right, obj));
            }

            return(false);
        }
コード例 #3
0
        public Color Lighting(RTObject obj, ILight light, Point position, Vector eyev, Vector normalv, double intensity)
        {
            Color color;

            if (Pattern != null)
            {
                color = Pattern.PatternAtObject(obj, position);
            }
            else if (obj.Parent != null)
            {
                color = obj.Parent.Material.Lighting(obj.Parent, light, position, eyev, normalv, intensity);
            }
            else
            {
                color = Color;
            }

            var effectiveColor = color * light.Intensity;
            var ambient        = effectiveColor * Ambient;
            var samples        = light.SamplePoints();

            Color sum = Color.Black;

            foreach (var sample in samples)
            {
                Color diffuse;
                Color specular;

                var lightV         = (new Vector(sample - position)).Normalize;
                var lightDotNormal = lightV.Dot(normalv);

                if ((lightDotNormal < 0) || intensity.IsEqual(0.0))
                {
                    diffuse  = Color.Black; // inShadow ? Color.Green : Color.Black;
                    specular = Color.Black; // inShadow ? Color.Green : Color.Black;
                }
                else
                {
                    diffuse = effectiveColor * Diffuse * lightDotNormal;
                    var reflectV      = (-lightV).Reflect(normalv);
                    var reflectDotEye = reflectV.Dot(eyev);

                    if (reflectDotEye <= 0)
                    {
                        specular = Color.Black;
                    }
                    else
                    {
                        var factor = Math.Pow(reflectDotEye, Shininess);
                        specular = light.Intensity * Specular * factor;
                    }
                }

                sum += diffuse;
                sum += specular;
            }

            return(ambient + ((sum / light.Samples) * intensity));
        }
コード例 #4
0
        public double IntensityAt(RTObject obj, Computations comps, World w)
        {
            if (w.IsShadowed(obj, comps, Position, comps.OverPoint))
            {
                return(0.0);
            }

            return(1.0);
        }
コード例 #5
0
        public CSG(CsgOperation op, RTObject left, RTObject right)
        {
            Operation = op;
            Left      = left;
            Right     = right;

            Left.Parent  = this;
            Right.Parent = this;
            _bounds      = null;
        }
コード例 #6
0
        public Computations(double time, RTObject obj, Point p, Vector ev, Vector nv, Vector rv, double n1, double n2, bool inside)
        {
            t             = time;
            Object        = obj;
            Point         = p;
            EyeVector     = ev;
            NormalVector  = nv;
            ReflectVector = rv;
            N1            = n1;
            N2            = n2;
            Inside        = inside;

            OverPoint  = new Point(Point + NormalVector * MathHelper.Epsilon);
            UnderPoint = new Point(Point - NormalVector * MathHelper.Epsilon);
        }
コード例 #7
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);
        }
コード例 #8
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);
        }
コード例 #9
0
        protected Color PatternAtShape(RTObject obj, Point localPoint)
        {
            var patternPoint = transformInverse * localPoint;

            return(PatternAt(patternPoint));
        }
コード例 #10
0
        public Color PatternAtObject(RTObject obj, Point worldPoint)
        {
            var localPoint = obj.WorldToObject(worldPoint);

            return(PatternAtShape(obj, localPoint));
        }
コード例 #11
0
 public Intersection(double time, RTObject obj)
 {
     t      = time;
     Object = obj;
 }
コード例 #12
0
 public Intersection(double time, RTObject obj, double U, double V) : this(time, obj)
 {
     u = U;
     v = V;
 }