Example #1
0
        public IPixelColor Trace(IRay ray, int depth)
        {
            IRayHit hit = this.FindHit(ray);

            if (hit == null)
            {
                return(this.BackGroundColor);
            }

            IPixelColor color = new PixelColor
            {
                Ambient  = new RGBColor(0.0f, 0.0f, 0.0f),
                Diffuse  = new RGBColor(0.0f, 0.0f, 0.0f),
                Specular = new RGBColor(0.0f, 0.0f, 0.0f),
                Color    = new RGBColor(0.0f, 0.0f, 0.0f)
            };

            this.World.Lights.ForEach(delegate(ILight light)
            {
                IPixelColor lightColor = this.Shading.GetColor(hit, light);

                if (light.Type == LightType.PointLight)
                {
                    IVector lightRayVec = new Vector(hit.Intersection, light.Position);
                    IRay lightRay       = new Ray(hit.Intersection, lightRayVec, lightRayVec.Length());
                    IRayHit obstruction = this.FindHit(lightRay);

                    if (obstruction != null)
                    {
                        if (obstruction.IntersectShape is Sphere && obstruction.IntersectShape != hit.IntersectShape)
                        {
                            IVector vectorIntersectionToObstruction = new Vector(hit.Intersection, obstruction.Intersection);
                            float distanceToObstruction             = vectorIntersectionToObstruction.Length();
                            float distanceToLight = lightRayVec.Length();
                            float distanceFactor  = distanceToObstruction / distanceToLight;

                            lightColor.Color = lightColor.Color.Intensify(distanceFactor);
                        }
                    }
                }

                color = color.Blend(lightColor);
            });

            if (depth < this.MaxRecursionLevel)
            {
                if (hit.IntersectShape.Material.IsReflective())
                {
                    color = color.Blend(this.Trace(hit.GetReflectionRay(), depth + 1).Intensify(hit.IntersectShape.Material.Reflection));
                }

                if (hit.IntersectShape.Material.IsRefractive())
                {
                    color = color.Blend(this.Trace(hit.GetRefractionRay(), depth + 1).Intensify(hit.IntersectShape.Material.Refraction));
                }
            }

            return(color);
        }
        private PixelColor[,] DrawNoiseMap(IAlgorithmTarget target, PixelColor[,] source, NoiseMap noiseMap, PixelColor baseColor, PixelColor shapeColor, double cutoffValue)
        {
            var   random         = new Random();
            float random1        = (float)random.NextDouble() * random.Next(-1, 1);
            float random2        = (float)random.NextDouble() * random.Next(-1, 1);
            float upperBounds    = Math.Max(random1, random2);
            float lowerBounds    = Math.Min(random1, random2);
            float boundsDistance = upperBounds - lowerBounds;

            target.AlgorithmPixels.ForEach
            (
                pixel =>
            {
                var position = pixel.Position;
                int x        = (int)position.X, y = (int)position.Y;
                var value    = noiseMap.GetValue(x, y);
                value        = value.Clamp(lowerBounds, upperBounds);
                var distanceFromLowerBounds = value - lowerBounds;
                var offsetValue             = (boundsDistance == 0.0f) ? 0.0f : distanceFromLowerBounds / boundsDistance;
                //source[y, x] = ((value * 100) >= cutoffValue) ? shapeColor.Blend(baseColor, value) : baseColor;
                source[y, x] = shapeColor.Blend(baseColor, offsetValue);
            }
            );
            return(source);
        }