Beispiel #1
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);
        }
        public void TupleNegateTest()
        {
            var t1 = new Tuple4(1, 7, 3, TupleFlavour.Vector);

            var expected = new Tuple4(-1, -7, -3, TupleFlavour.Vector);

            Assert.Equal(expected, Tuple4.Negate(t1));
        }
        public void Then_scale_scalar(string a, double t1, double t2, double t3, double t4)
        {
            var c = Tuple4.Negate(cache[a]);

            Assert.True(Constants.EpsilonCompare(t1, c.X));
            Assert.True(Constants.EpsilonCompare(t2, c.Y));
            Assert.True(Constants.EpsilonCompare(t3, c.Z));
            Assert.True(Constants.EpsilonCompare(t4, c.W));
        }
Beispiel #4
0
        public virtual HitResult[] AllHits(Tuple4 origin, Tuple4 dir)
        {
            var intersections = GetIntersections(new Ray(origin, dir));

            if (intersections == null)
            {
                return(new HitResult[] { HitResult.NoHit });
            }

            var result = new HitResult[intersections.Length];

            for (int i = 0; i < intersections.Length; ++i)
            {
                var figure         = intersections[i].figure;
                var distance       = intersections[i].t;
                var pointOnSurface = Tuple4.Geometry3D.MovePoint(origin, dir, distance); // orig + dir*dist
                // TODO: Remove this cast to BaseFigure
                (var surfaceNormal, var objectPoint) = ((BaseFigure)figure).GetTransformedNormal(figure, pointOnSurface, intersections[i].u, intersections[i].v);
                var eyeVector = Tuple4.Negate(dir);
                var isInside  = false;
                if (Tuple4.DotProduct(surfaceNormal, eyeVector) < 0)
                {
                    isInside      = true;
                    surfaceNormal = Tuple4.Negate(surfaceNormal);
                }
                var pointOverSurface  = Tuple4.Add(pointOnSurface, Tuple4.Scale(surfaceNormal, Constants.Epsilon));
                var pointUnderSurface = Tuple4.Subtract(pointOnSurface, Tuple4.Scale(surfaceNormal, Constants.Epsilon));
                var reflectionVector  = Tuple4.Reflect(dir, surfaceNormal);

                result[i] = new HitResult(true,
                                          figure,
                                          distance,
                                          intersections[i].u,
                                          intersections[i].v,
                                          objectPoint,
                                          pointOnSurface,
                                          pointOverSurface,
                                          pointUnderSurface,
                                          surfaceNormal,
                                          eyeVector,
                                          reflectionVector,
                                          isInside);
            }

            return(result);
        }