Exemplo n.º 1
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));
        }