Exemple #1
0
        public void SimpleCameraTest()
        {
            // Ray cast for screen coordinate 0,0
            {
                var camera      = new SimpleCamera(new Tuple4(0.0, 0.0, -4.0, TupleFlavour.Point), 6.0, 600, 800);
                var ray         = camera.GetRay(0, 0);
                var expectedRay = new Ray(
                    new Tuple4(0.0, 0.0, -4.0, TupleFlavour.Point),
                    Tuple4.Normalize(new Tuple4(-3.0, 3.0, 1.0, TupleFlavour.Vector)));
                Assert.Equal(expectedRay.origin, ray.origin);
                Assert.Equal(expectedRay.dir, ray.dir);

                var t = Tuple4.Add(ray.origin, Tuple4.Scale(ray.dir, 4.35890));
                Assert.Equal(new Tuple4(-3.0, 3.0, -3.0, TupleFlavour.Point), t);
            }

            // Ray cast for screen coordinate width,height
            {
                var camera      = new SimpleCamera(new Tuple4(0.0, 0.0, -4.0, TupleFlavour.Point), 6.0, 600, 800);
                var ray         = camera.GetRay(600, 800);
                var expectedRay = new Ray(
                    new Tuple4(0.0, 0.0, -4.0, TupleFlavour.Point),
                    Tuple4.Normalize(new Tuple4(3.0, -3.0, 1.0, TupleFlavour.Vector)));
                Assert.Equal(expectedRay.origin, ray.origin);
                Assert.Equal(expectedRay.dir, ray.dir);
            }

            {
                var camera = new SimpleCamera(new Tuple4(0, 0, -1.0, TupleFlavour.Point), 2.0, 600, 600);
                var ray    = camera.GetRay(0, 0);

                var t = Tuple4.Add(ray.origin, Tuple4.Scale(ray.dir, 1.73205));
                Assert.Equal(new Tuple4(-1.0, 1.0, 0.0, TupleFlavour.Point), t);
            }
        }
        public void Then_normalize(string a, double t1, double t2, double t3)
        {
            var c = Tuple4.Normalize(cache[a]);

            Assert.True(Constants.EpsilonCompare(t1, c.X));
            Assert.True(Constants.EpsilonCompare(t2, c.Y));
            Assert.True(Constants.EpsilonCompare(t3, c.Z));
        }
        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));
        }
        public TriangleFigure(IMatrix transformation, IMaterial material, Tuple4 p1, Tuple4 p2, Tuple4 p3)
        {
            this.Material       = material;
            this.Transformation = transformation;

            this.P1     = p1;
            this.P2     = p2;
            this.P3     = p3;
            this.E1     = Tuple4.Subtract(p2, p1);
            this.E2     = Tuple4.Subtract(p3, p1);
            this.Normal = Tuple4.Normalize(Tuple4.CrossProduct(E2, E1));
        }
        public Ray GetRay(double screenX, double screenY)
        {
            // When j changes from  [0, height - 1],
            // y should change from [-tan(fov/2), tan(fov/2)]
            var y = -(-tangy + 2 * (screenY + 0.5) * (tangy / (ScreenHeight)));
            // When i changes from  [0, width - 1],
            // x should change from [-tan(fov/2), tan(fov/2)]
            var x = -tangx + 2 * (screenX + 0.5) * (tangx / (ScreenWidth));

            var direction = Tuple4.Normalize(
                MatrixOperations.Geometry3D.Transform(Transformation, new Tuple4(x, y, 1.0, TupleFlavour.Vector)));

            return(new Ray(Origin, direction));
        }
        public Ray GetRay(double screenX, double screenY)
        {
            var xoffset = (screenX + 0.5) * PixleSize;
            var yoffset = (screenY + 0.5) * PixleSize;

            var worldX = tangx - xoffset;
            var worldY = tangy - yoffset;

            var pixel = MatrixOperations.Geometry3D.Transform(inverseTransform, new Tuple4(worldX, worldY, -1.0, TupleFlavour.Point));

            var direction = Tuple4.Normalize(Tuple4.Subtract(pixel, Origin));

            return(new Ray(Origin, direction));
        }
Exemple #7
0
        public Tuple4 TransformObjectNormalToWorldNormal(Tuple4 normal)
        {
            if (inverseTransposeTransformation != null)
            {
                normal = MatrixOperations.Geometry3D.Transform(inverseTransposeTransformation, normal);
                if (normal.W != 0.0)
                {
                    normal = Tuple4.Vector(normal.X, normal.Y, normal.Z);
                }
            }

            normal = Tuple4.Normalize(normal);

            if (Parent != null)
            {
                normal = Parent.TransformObjectNormalToWorldNormal(normal);
            }

            return(normal);
        }
 public DirectionLight(ColorModel colors, Tuple4 lightDirection, double intensity)
 {
     this.lightDirection = Tuple4.Normalize(lightDirection);
     this.intensity      = intensity;
     this.colors         = colors;
 }
 public void Then_n(string a, string b)
 {
     Assert.Equal(tuple[a], Tuple4.Normalize(tuple[b]));
 }
        public Tuple4 GetShadedColor(Tuple4 objectPoint, IMaterial material, Tuple4 eyeVector, Tuple4 pointOnSurface, Tuple4 surfaceNormal)
        {
            var lightDirection = Tuple4.Normalize(Tuple4.Subtract(location, pointOnSurface));

            return(DirectionLightCommon.GetShadedColor(objectPoint, material, colors.White, lightDirection, intensity, eyeVector, pointOnSurface, surfaceNormal));
        }
 public Tuple4 GetLightDirection(Tuple4 from)
 {
     return(Tuple4.Normalize(Tuple4.Subtract(location, from)));
 }
 public void When_normalize(string id, string v)
 {
     cache[id] = Tuple4.Normalize(cache[v]);
 }
 public void Normalize_vector(string id, double t1, double t2, double t3)
 {
     tuple.Add(id, Tuple4.Normalize(Tuple4.Vector(t1, t2, t3)));
 }
Exemple #14
0
 /// <summary>
 /// The only purpose of this method is to serve unit testing
 /// </summary>
 protected virtual Intersection[] GetBaseIntersectionsWithAnyDirection(Ray ray)
 {
     return(GetBaseIntersections(new Ray(ray.origin, Tuple4.Normalize(ray.dir))));
 }