Beispiel #1
0
        public void shade(SceneObject ClosestObj, Ray TheRay, Vector3D Normal, Vector3D Point, Color col)
        {
            // If no intersection just return. A small optimisation.
            if (ClosestObj == null)
            {
                return;
            }

            double K, Ambient, Diffuse, Specular, DistanceT;
            Ray    LightRay = new Ray();
            Ray    ReflectedRay = new Ray();
            double Red, Green, Blue;    // Working RGB values.. need to be onverted to a CColour later.

            int NumLights;              //

            LightSource Light;

            K  = Vector3D.DotProduct(TheRay.direction, Normal);                      /// Dot product of the ray and the objects normal.
            K *= 2.0;

            // Reflect ray (obviousily) starts at the intersection point.
            ReflectedRay.origin = Point;

            ReflectedRay.direction = new Vector3D(K * Normal.X + TheRay.direction.X,
                                                  K * Normal.Y + TheRay.direction.Y,
                                                  K * Normal.Z + TheRay.direction.Z
                                                  );
            Ambient = ClosestObj.properties.Ambient;
            Red     = ClosestObj.properties.Colour.R * Ambient;
            Green   = ClosestObj.properties.Colour.G * Ambient;
            Blue    = ClosestObj.properties.Colour.B * Ambient;

            // Iterate thru a Light list.
            // DEBUG for now use a bogus one.

            Light = new LightSource(new Vector3D(65, -50, -100));
            Light.SetColour(Color.FromArgb(255, 255, 255));

            NumLights = lightSources.Count();


            if (Light != null)
            {
                Color LightColour;
                DistanceT = Light.MakeLightRay(Point, LightRay);

                if (RecurseLevel == 0)
                {
                    LightColour = Light.getColour(ClosestObj, LightRay, DistanceT, sceneObjects);
                }
                else
                {
                    LightColour = Light.properties.Colour;
                }

                if ((LightColour.R == 0.0) || (LightColour.G == 0.0) || (LightColour.B == 0.0))
                {
                    col = Color.FromArgb(0, 0, 0);
                    return;
                }
                else
                {
                    Diffuse = Vector3D.DotProduct(Normal, LightRay.direction);                                                  // Dot Product

                    if ((Diffuse > 0.0) && (ClosestObj.properties.Diffuse > 0.0))
                    {
                        Diffuse = Math.Pow(Diffuse, ClosestObj.properties.Brilliance) * ClosestObj.properties.Diffuse;
                        Red    += LightColour.R * ClosestObj.properties.Colour.R * Diffuse;
                        Green  += LightColour.G * ClosestObj.properties.Colour.G * Diffuse;
                        Blue   += LightColour.B * ClosestObj.properties.Colour.B * Diffuse;
                    }

                    ReflectedRay.direction.Normalize();                                         //
                    Specular = Vector3D.DotProduct(ReflectedRay.direction, LightRay.direction); // Dot product..


                    if ((Specular > 0.0) && (ClosestObj.properties.Specular > 0.0))
                    {
                        Specular = Math.Pow(Specular, ClosestObj.properties.Roughness) * ClosestObj.properties.Roughness;
                        Red     += LightColour.R * Specular;
                        Green   += LightColour.G * Specular;
                        Blue    += LightColour.B * Specular;
                    }

                    // Reflection ..
                    K = ClosestObj.properties.Reflection;
                    if (K > 0.0)
                    {
                        Color NewColour = new Color();
                        RecurseLevel++;

                        trace(ReflectedRay, NewColour);         // Recursive..
                        Red   += NewColour.R * K;
                        Green += NewColour.G * K;
                        Blue  += NewColour.B * K;

                        RecurseLevel--;
                    }
                }
            }
            // Convert 1.0 to 255
            col = Color.FromArgb(Convert.ToByte(Red * 255), Convert.ToByte(Green * 255), Convert.ToByte(Blue * 255));

            Console.WriteLine("col = " + col.ToString());
        }