示例#1
0
文件: ToyTracer.cs 项目: Tilps/Stash
 private double lambert2(Vector light, Vector normal, double brilliance)
 {
     return Math.Pow(Math.Abs(light.Dot(normal) / light.Length), brilliance);
 }
示例#2
0
文件: ToyTracer.cs 项目: Tilps/Stash
        private void CalculateLightContrib(Vector normal, ColorIntensity lightColor, Material surf, ColorIntensity pigment, ref double rI, ref double gI, ref double bI, Vector lightdir, Vector eye)
        {
            double diffusivity = 1.0-surf.Specularity;
            if (surf.Phong > 0.0)
            {
                Vector reflection = eye;
                reflection.Add(normal.Scale(-2.0 * eye.Dot(normal)));
                double phong = -lightdir.Scale(1.0 / lightdir.Length).Dot(reflection);
                if (phong > 0.0)
                {
                    double phongpowd = Math.Pow(phong, surf.Exponent);
                    rI += surf.Phong * phongpowd * lightColor.R*diffusivity;
                    gI += surf.Phong * phongpowd * lightColor.G * diffusivity;
                    bI += surf.Phong * phongpowd * lightColor.B * diffusivity;
                }
            }
            if (surf.Specular > 0.0)
            {
                Vector half = lightdir.Scale(1.0 / lightdir.Length);
                half.Add(eye);
                half.ScaleSelf(0.5);
                double spec = half.Dot(normal) / half.Length;

                if (spec > 0.0)
                {
                    double specpowd = Math.Pow(spec, 1.0 / surf.Roughness);
                    rI += surf.Specular * specpowd * lightColor.R * diffusivity;
                    gI += surf.Specular * specpowd * lightColor.G * diffusivity;
                    bI += surf.Specular * specpowd * lightColor.B * diffusivity;
                }
            }
            if (surf.Iridescence > 0.0)
            {
                double cosaoi = lightdir.Dot(normal)/lightdir.Length;
                double interference = 4.0 * Math.PI * surf.FilmThickness * cosaoi;

                double intensity = cosaoi * surf.Iridescence;
                double rwl = 0.25;
                double gwl = 0.18;
                double bwl = 0.14;
                /* Modify color by phase offset for each wavelength. */

                rI += surf.Iridescence * (intensity * (1.0 - 0.5 * Math.Cos(interference / rwl))) * diffusivity;
                gI += surf.Iridescence * (intensity * (1.0 - 0.5 * Math.Cos(interference / gwl))) * diffusivity;
                bI += surf.Iridescence * (intensity * (1.0 - 0.5 * Math.Cos(interference / bwl))) * diffusivity;

            }
            double lam = lambert2(lightdir, normal, surf.Brilliance);
            rI += surf.Diffuse * pigment.R * lam * lightColor.R * diffusivity;
            gI += surf.Diffuse * pigment.G * lam * lightColor.G * diffusivity;
            bI += surf.Diffuse * pigment.B * lam * lightColor.B * diffusivity;
        }