Example #1
0
        public unsafe Color ShadeHit(IntersectionData intersectionData, int remaining = 5)
        {
            double *x = stackalloc double[ILight.MAX_SAMPLE];
            double *y = stackalloc double[ILight.MAX_SAMPLE];
            double *z = stackalloc double[ILight.MAX_SAMPLE];

            var overPoint  = intersectionData.OverPoint;
            var eyeVector  = intersectionData.EyeVector;
            var normal     = intersectionData.Normal;
            var material   = intersectionData.Object.Material;
            var shapeColor = material.Pattern.GetColorAtShape(intersectionData.Object, ref overPoint);

            var surface = Color.Black;

            for (var i = 0; i < Lights.Count; i++)
            {
                var light = Lights[i];

                int    nbSamples      = light.GetPositions(x, y, z);
                double lightIntensity = 0;
                var    lightColor     = Color.Black;
                for (int j = 0; j < nbSamples; j++)
                {
                    var sampleLightColor = light.GetIntensityAt(x[j], y[j], z[j], ref overPoint);
                    lightColor += sampleLightColor;
                    if (lightColor.Equals(Color.Black))
                    {
                        continue;
                    }
                    bool isShadowed = IsShadowed(overPoint, x[j], y[j], z[j]);
                    lightIntensity += isShadowed ? 0 : 1;
                }

                lightIntensity /= nbSamples;
                lightColor     /= nbSamples;
                surface        += material.Lighting(nbSamples, x, y, z, ref overPoint, ref eyeVector, ref normal, lightIntensity, shapeColor, lightColor);
            }

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

            if (material.Reflective > 0 && material.Transparency > 0)
            {
                var reflectance = intersectionData.Schlick();
                var color       = surface + reflected * reflectance + refracted * (1 - reflectance);
                return(color);
            }
            else
            {
                var color = surface + reflected + refracted;
                return(color);
            }
        }
Example #2
0
        public Color ReflectedColor(IntersectionData intersectionData, int remaining = 5)
        {
            var materialReflective = intersectionData.Object.Material.Reflective;

            if (remaining == 0 || materialReflective < double.Epsilon)
            {
                return(Color.Black);
            }

            var reflectRay = Helper.Ray(intersectionData.OverPoint, intersectionData.ReflectionVector);
            var color      = ColorAt(reflectRay, remaining - 1);

            return(color * materialReflective);
        }