Пример #1
0
        private ColorF GetReflectionColor(
            Vector3 projectionDirection, List <IObject3D> scene, List <LightSource> lightSources, ImageF environmentMap,
            int depth, ref RayIntersectionResult rayIntersectionResult)
        {
            var reflectDirection =
                Vector3.Normalize(Vector3.Reflect(projectionDirection, rayIntersectionResult.NormalVector));

            var reflectOrigin = Vector3.Dot(reflectDirection, rayIntersectionResult.NormalVector) < 0
                ? rayIntersectionResult.HitPoint - rayIntersectionResult.NormalVector * 1e-3f
                : rayIntersectionResult.HitPoint + rayIntersectionResult.NormalVector * 1e-3f;

            return(CastRay(ref reflectOrigin, ref reflectDirection, scene, lightSources, environmentMap, depth + 1));
        }
Пример #2
0
        private void CalculateLightIntensity(
            Vector3 projectionDirection, List <IObject3D> scene, List <LightSource> lightSources,
            RayIntersectionResult rayIntersectionResult, out float diffuseLightIntensity,
            out float specularLightIntensity)
        {
            diffuseLightIntensity  = 0f;
            specularLightIntensity = 0f;

            foreach (var lightSource in lightSources)
            {
                var lightSourceDirection = Vector3.Normalize(lightSource.Position - rayIntersectionResult.HitPoint);
                var lightSourceDistance  = Vector3.Distance(lightSource.Position, rayIntersectionResult.HitPoint);

                var shadowOrigin = Vector3.Dot(lightSourceDirection, rayIntersectionResult.NormalVector) < 0
                    ? rayIntersectionResult.HitPoint - rayIntersectionResult.NormalVector * 1e-3f
                    : rayIntersectionResult.HitPoint + rayIntersectionResult.NormalVector * 1e-3f;

                RayIntersectionResult shadowIntersectionResult;
                if ((shadowIntersectionResult = SceneIntersect(ref shadowOrigin, ref lightSourceDirection, scene)).IsIntersect &&
                    Vector3.Distance(shadowIntersectionResult.HitPoint, shadowOrigin) < lightSourceDistance)
                {
                    continue;
                }

                diffuseLightIntensity += lightSource.Intensity * MathF.Max(
                    0f, Vector3.Dot(lightSourceDirection, rayIntersectionResult.NormalVector));

                specularLightIntensity +=
                    MathF.Pow(
                        MathF.Max(
                            0f,
                            -Vector3.Dot(
                                Vector3.Reflect(-lightSourceDirection, rayIntersectionResult.NormalVector),
                                projectionDirection)),
                        rayIntersectionResult.Material.SpecularExponent) * lightSource.Intensity;
            }
        }