Esempio n. 1
0
    void PhongModel(DVector3 normal, DVector3 pos, ref int cr, ref int cg, ref int cb)
    {
        // ambient
        double AmbientComponent = Ka * Ia;

        double CameraZPosition = 220.0;

        // diffuse
        double distance = (new DVector3(0.0, 0.0, CameraZPosition) - pos).GetLength();

        DVector3 L = LightPosition - pos;

        L /= L.GetLength();

        normal /= normal.GetLength();

        double LNcos            = DVector3.DotProduct(L, normal);
        double DiffuseComponent = (Kd * Il) / (K + distance) * Clamp(LNcos, 0.0, 1.0);

        // specular
        DVector3 R = DVector3.Reflect(-L, normal);

        R /= R.GetLength();

        DVector3 S = new DVector3(0.0, 0.0, CameraZPosition) - pos;

        S /= S.GetLength();

        double SRcos             = DVector3.DotProduct(R, S);
        double SpecularComponent = Il * Ks * Math.Pow(Clamp(SRcos, 0.0, 1.0), Ip) / (K + distance);
        double result            = AmbientComponent + DiffuseComponent + SpecularComponent;

        //if(result >= 1.0)
        //{
        //    R /= R.GetLength();
        //}

        cr = Clamp((int)(255 * result * LightColor.X * ObjectColor.X), 0, 255);
        cg = Clamp((int)(255 * result * LightColor.Y * ObjectColor.Y), 0, 255);
        cb = Clamp((int)(255 * result * LightColor.Z * ObjectColor.Z), 0, 255);
    }