Пример #1
0
        private RealColor?GetColorForRay(Ray viewRay, int iteration = 1)
        {
            var materialColor = new RealColor();

            var intersection = Scene.Objects
                               .Select(o => new { Obj = o, Dist = o.Intersect(viewRay) })
                               .Where(o => o.Dist != null)
                               .OrderBy(f => f.Dist)
                               .FirstOrDefault();

            if (intersection != null)
            {
                var start  = viewRay.Start + (intersection.Dist.Value * viewRay.Direction);
                var normal = start - intersection.Obj.Position;
                normal.Normalize();

                foreach (var light in Scene.Lights)
                {
                    var lightRay = Ray.FromStartAndEndPoints(start, light.Position);

                    if (Scene.Objects.Any(o => o.Intersect(lightRay) != null))
                    {
                        continue;
                    }

                    if (Vector3D.DotProduct(normal, lightRay.Direction) < 0)
                    {
                        continue;
                    }

                    var lambert = Vector3D.DotProduct(lightRay.Direction, normal);

                    materialColor += lambert * light.Color * intersection.Obj.Material.Diffuse;
                }

                if (iteration > 10 || intersection.Obj.Material.ReflectionCoefficient <= 0)
                {
                    return(materialColor);
                }

                var reflectionAngle = 2.0f * (Vector3D.DotProduct(viewRay.Direction, normal));
                var reflectionRay   = new Ray(start, viewRay.Direction - reflectionAngle * normal);

                var reflectedColor = GetColorForRay(reflectionRay, iteration + 1);

                if (reflectedColor == null)
                {
                    return(materialColor);
                }

                return(materialColor +
                       reflectedColor * intersection.Obj.Material.ReflectionCoefficient);
            }

            //return null;
            return(new RealColor(Background));
        }
Пример #2
0
        private RealColor? GetColorForRay(Ray viewRay, int iteration = 1)
        {
            var materialColor = new RealColor();

            var intersection = Scene.Objects
                .Select(o => new {Obj = o, Dist = o.Intersect(viewRay)})
                .Where(o => o.Dist != null)
                .OrderBy(f => f.Dist)
                .FirstOrDefault();

            if (intersection != null)
            {
                var start = viewRay.Start + (intersection.Dist.Value*viewRay.Direction);
                var normal = start - intersection.Obj.Position;
                normal.Normalize();

                foreach (var light in Scene.Lights)
                {
                    var lightRay = Ray.FromStartAndEndPoints(start, light.Position);

                    if (Scene.Objects.Any(o => o.Intersect(lightRay) != null))
                        continue;

                    if (Vector3D.DotProduct(normal, lightRay.Direction) < 0)
                        continue;

                    var lambert = Vector3D.DotProduct(lightRay.Direction, normal);

                    materialColor += lambert * light.Color * intersection.Obj.Material.Diffuse;

                }

                if (iteration > 10 || intersection.Obj.Material.ReflectionCoefficient <= 0)
                    return materialColor;

                var reflectionAngle = 2.0f * (Vector3D.DotProduct(viewRay.Direction, normal));
                var reflectionRay = new Ray(start, viewRay.Direction - reflectionAngle * normal);

                var reflectedColor = GetColorForRay(reflectionRay, iteration + 1);

                if (reflectedColor == null)
                    return materialColor;

                return materialColor +
                       reflectedColor*intersection.Obj.Material.ReflectionCoefficient;
            }

            //return null;
            return new RealColor(Background);
        }
Пример #3
0
 public PointLight(RealColor color, float x, float y, float z)
 {
     Color = color;
     Position = new Vector3D(x, y, z);
 }
Пример #4
0
 public PointLight(RealColor color, float x, float y, float z)
 {
     Color    = color;
     Position = new Vector3D(x, y, z);
 }