コード例 #1
0
 public static Color ColorInfo2Color(ColorInfo color)
 {
     return(Color.FromArgb((int)(color.R * 255), (int)(color.G * 255), (int)(color.B * 255)));
 }
コード例 #2
0
        public static Color PhongeIllumination(Vector <double> position, Vector <double> normal,
                                               ColorInfo colorInfo, SurfaceInfo surface, List <Light> lights)
        {
            ColorInfo outcome = new ColorInfo {
                R = 0, G = 0, B = 0
            };


            //ambient
            outcome.R += colorInfo.R * surface.Ka;
            outcome.G += colorInfo.G * surface.Ka;
            outcome.B += colorInfo.B * surface.Ka;

            foreach (var light in lights)
            {
                if (!light.IsOn)
                {
                    continue;
                }
                double spotlightFactor = 1;

                var l = light.CalculateLVector(position);

                if (light.IsSpotLight)
                {
                    var cos = (-light.ProcessedDirection).DotProduct(l);
                    if (cos > 0)
                    {
                        spotlightFactor = Math.Pow(cos, light.P);
                    }
                    else
                    {
                        spotlightFactor = 0;
                    }
                }
                //diffuse

                var lightNormalAngle = normal.DotProduct(l);
                var diffuseR         = surface.Kd * lightNormalAngle * spotlightFactor;
                if (lightNormalAngle < 0)
                {
                    continue;
                }

                outcome.R += colorInfo.R * diffuseR;
                outcome.G += colorInfo.G * diffuseR;
                outcome.B += colorInfo.B * diffuseR;


                //sepcular
                var v = (-position).Normalize(2);

                var r = 2 * lightNormalAngle * normal - l;
                r = r.Normalize(2);

                var cameraRAngle = r.DotProduct(v);
                if (cameraRAngle < 0)
                {
                    continue;
                }

                var specularR = surface.Ks * Math.Pow(cameraRAngle, surface.N_shiny) * spotlightFactor;

                outcome.R += specularR;
                outcome.G += specularR;
                outcome.B += specularR;
            }

            if (outcome.R > 1)
            {
                outcome.R = 1;
            }
            if (outcome.G > 1)
            {
                outcome.G = 1;
            }
            if (outcome.B > 1)
            {
                outcome.B = 1;
            }

            return(ColorInfo2Color(outcome));
        }