Exemplo n.º 1
0
        private Color GetSpecularColor(Point3d point, Vector3d reflectedRay, Scene scene, float p)
        {
            Color         lightColor = new Color(0, 0, 0);
            LightSource3d light;
            Vector3d      vLight;
            float         cosLight;
            Color         colorLight;

            for (var i = 0; i < scene.LightSources.Count; i++)
            {
                light = scene.LightSources[i];
                if (IsViewable(light.Location, point, scene))
                {
                    vLight   = new Vector3d(point, light.Location);
                    cosLight = Utils.CosVectors(reflectedRay, vLight);
                    if (cosLight > Consts.EPSILON)
                    {
                        colorLight = Color.MulColor(light.Color, Math.Pow(cosLight, p));
                        lightColor = Color.AddColors(lightColor, colorLight);
                    }
                }
            }

            return(lightColor);
        }
Exemplo n.º 2
0
        public void MakeRendering(Canvas canvas, Action <int> progress)
        {
            int w = canvas.Width;
            int h = canvas.Height;

            float dx    = w / 2f;
            float dy    = h / 2f;
            float focus = Camera.ProjPlaneDist;

            int count = 0;

            var result = Parallel.For(0, w, i =>
            {
                progress.Invoke(count++);
                Parallel.For(0, h, j =>
                {
                    float x   = i - dx;
                    float y   = j - dy;
                    var ray   = new Vector3d(x, y, focus);
                    var color = tracing.Trace(Scene, Camera, ray);
                    canvas.SetPixel(i, j, color);
                });
            });

            var grayCanvas = canvas.DetectEdgesCanvas();


            Parallel.For(1, w, i =>
            {
                Parallel.For(1, h, j =>
                {
                    var gray = grayCanvas.GetPixel(i, j).B;
                    if (gray > 10)
                    {
                        float x = i - dx;
                        float y = j - dy;

                        var c        = canvas.GetPixel(i, j);
                        float weight = 1f / 4;
                        c            = Color.MulColor(c, weight);
                        c            = Color.AddColors(c, Color.MulColor(tracing.Trace(Scene, Camera, new Vector3d(x + 0.5f, y, focus)), weight));
                        c            = Color.AddColors(c, Color.MulColor(tracing.Trace(Scene, Camera, new Vector3d(x, y + 0.5f, focus)), weight));
                        c            = Color.AddColors(c, Color.MulColor(tracing.Trace(Scene, Camera, new Vector3d(x + 0.5f, y + 0.5f, focus)), weight));
                        //c = Color.AddColors(c, Color.MulColor(tracing.Trace(Scene, Camera, new Vector3d(x - 0.5f, y, focus)), weight));
                        //c = Color.AddColors(c, Color.MulColor(tracing.Trace(Scene, Camera, new Vector3d(x, y - 0.5f, focus)), weight));
                        //c = Color.AddColors(c, Color.MulColor(tracing.Trace(Scene, Camera, new Vector3d(x - 0.5f, y - 0.5f, focus)), weight));

                        canvas.SetPixel(i, j, c);
                    }
                });
            });
        }
Exemplo n.º 3
0
        private Color GetLightingColor(Point3d point, Vector3d norm, Scene scene)
        {
            Color lightColor = new Color(0, 0, 0);

            for (var i = 0; i < scene.LightSources.Count; i++)
            {
                var light = scene.LightSources[i];
                if (IsViewable(light.Location, point, scene))
                {
                    var vLight     = new Vector3d(point, light.Location);
                    var cosLight   = Math.Abs(Utils.CosVectors(norm, vLight));
                    var colorLight = Color.MulColor(light.Color, cosLight);
                    lightColor = Color.AddColors(lightColor, colorLight);
                }
            }

            return(lightColor);
        }
Exemplo n.º 4
0
        private Color CalcColor(Scene scene, Point3d vectorStart, Vector3d vector, IObject3d obj,
                                Point3d point, float dist, float intensity, int recursionLevel)
        {
            Material material = obj.GetMaterial;
            Vector3d norm     = obj.GetNormalVector(point);

            Color objColor       = obj.GetColor;
            Color ambientColor   = new Color();
            Color diffuseColor   = new Color();
            Color reflectedColor = new Color();
            Color specularColor  = new Color();

            float fogDensity = (scene.FogDestiny?.Invoke(dist)) ?? 0;

            Vector3d reflectedRay = new Vector3d();

            if (material.Ks != 0f || material.Kr != 0f)
            {
                reflectedRay = ReflectRay(vector, norm);
            }

            // Ambient
            if (material.Ka != 0)
            {
                ambientColor = Color.MixColors(scene.BackgroundColor, objColor);
            }

            // Diffuse
            if (material.Kd != 0)
            {
                diffuseColor = objColor;
                if (scene.LightSources.Count > 0)
                {
                    Color light_color = GetLightingColor(point, norm, scene);
                    diffuseColor = Color.MixColors(diffuseColor, light_color);
                }
            }

            // Specular
            if (material.Ks != 0)
            {
                specularColor = scene.BackgroundColor;
                if (scene.LightSources.Count > 0)
                {
                    specularColor = GetSpecularColor(point, reflectedRay, scene, material.P);
                }
            }

            // Reflect
            if (material.Kr != 0)
            {
                if (intensity > Consts.INTENSITY && recursionLevel < Consts.RESUCRIONLEVEL)
                {
                    reflectedColor = Recursively(scene, point, reflectedRay, intensity * material.Kr * (1 - fogDensity),
                                                 recursionLevel + 1);
                }
                else
                {
                    reflectedColor = scene.BackgroundColor;
                }
            }

            // Result
            Color resultColor = new Color(0, 0, 0);

            if (material.Ka != 0)
            {
                resultColor = Color.AddColors(resultColor, Color.MulColor(ambientColor, material.Ka));
            }

            if (material.Kd != 0)
            {
                resultColor = Color.AddColors(resultColor, Color.MulColor(diffuseColor, material.Kd));
            }

            if (material.Ks != 0)
            {
                resultColor = Color.AddColors(resultColor, Color.MulColor(specularColor, material.Ks));
            }

            if (material.Kr != 0)
            {
                resultColor = Color.AddColors(resultColor, Color.MulColor(reflectedColor, material.Kr));
            }

            if (scene.FogDestiny != null)
            {
                resultColor = Color.AddColors(Color.MulColor(scene.BackgroundColor, fogDensity),
                                              Color.MulColor(resultColor, 1 - fogDensity));
            }

            return(resultColor);
        }