public void TupleNormalizeTest()
        {
            var t1  = new Tuple4(1, 2, 3, TupleFlavour.Vector);
            var len = t1.Length();

            Assert.Equal(Math.Sqrt(14), len);

            var expected = new Tuple4(1.0 / len, 2.0 / len, 3.0 / len, TupleFlavour.Vector);

            Assert.Equal(expected, Tuple4.Normalize(t1));
        }
예제 #2
0
        private static Lighting GetIntensity(IMaterial material, Tuple4 lightDirection, double intensity, Tuple4 eyeVector, Tuple4 pointOnSurface, Tuple4 surfaceNormal)
        {
            var diffuse  = 0.0;
            var specular = 0.0;

            var cosine = Tuple4.DotProduct(lightDirection, surfaceNormal);

            if (cosine >= 0)
            {
                diffuse = cosine * material.Diffuse;

                if (material.IsShining())
                {
                    // var reflected = Tuple4.Subtract(Tuple4.Scale(surfaceNormal, 2 * Tuple4.DotProduct(surfaceNormal, lightDirection)), lightDirection);
                    var reflected       = Tuple4.Reflect(Tuple4.Negate(lightDirection), surfaceNormal);
                    var reflectedDotDir = Tuple4.DotProduct(reflected, eyeVector);
                    if (reflectedDotDir > 0)
                    {
                        specular = material.Specular * Math.Pow(reflectedDotDir / (reflected.Length() * eyeVector.Length()), material.Shininess);
                    }
                }
            }

            return(diffuse, specular, intensity);
        }