Example #1
0
        public void AddCamera()
        {
            const string yaml = @"
- add: camera
  width: 100
  height: 100
  field-of-view: 0.785
  from: [ -6, 6, -10 ]
  to: [ 6, 0, 6 ]
  up: [ -0.45, 1, 0 ]";

            var parser = new YamlParser();

            var(_, camera) = parser.LoadYaml(yaml);

            var transform = ViewTransform.Create(
                new Point(-6, 6, -10),
                new Point(6, 0, 6),
                new Vector(-0.45f, 1, 0));

            Assert.Equal(100, camera.Width);
            Assert.Equal(100, camera.Height);
            Assert.Equal(0.785f, camera.FieldOfView);
            Assert.Equal(transform, camera.Transform);
        }
Example #2
0
        public void CreateDefaultOrientation()
        {
            var from = new Point(0, 0, 0);
            var to   = new Point(0, 0, -1);
            var up   = new Vector(0, 1, 0);

            Assert.Equal(Matrix4x4.Identity(), ViewTransform.Create(from, to, up));
        }
Example #3
0
        public void CreateTranslation()
        {
            var from = new Point(0, 0, 8);
            var to   = new Point(0, 0, 0);
            var up   = new Vector(0, 1, 0);

            var expected = Matrix4x4.Translation(0, 0, -8);
            var actual   = ViewTransform.Create(from, to, up);

            Assert.Equal(expected, actual);
        }
Example #4
0
        public void CreateScaling()
        {
            var from = new Point(0, 0, 0);
            var to   = new Point(0, 0, 1);
            var up   = new Vector(0, 1, 0);

            var expected = Matrix4x4.Scaling(-1, 1, -1);
            var actual   = ViewTransform.Create(from, to, up);

            Assert.Equal(expected, actual);
        }
Example #5
0
        public void RenderDefaultWorld()
        {
            var from      = new Point(0, 0, -5);
            var to        = new Point(0, 0, 0);
            var up        = new Vector(0, 1, 0);
            var transform = ViewTransform.Create(from, to, up);
            var camera    = new Camera(11, 11, MathF.PI / 2, transform);

            var world  = new DefaultWorld();
            var canvas = world.Render(camera);

            Assert.Equal(new Color(0.38066f, 0.47583f, 0.2855f), canvas[5, 5]);
        }
Example #6
0
        private Camera ParseCamera(YamlNode node)
        {
            var   width       = 0;
            var   height      = 0;
            float fieldOfView = 0;

            var from = Point.Zero;
            var to   = Point.Zero;
            var up   = Vector.UnitY;

            foreach (var child in (YamlMappingNode)node)
            {
                var key = ((YamlScalarNode)child.Key).Value.ToUpperInvariant();

                switch (key)
                {
                case "WIDTH":
                    width = ParseInt(child.Value);
                    break;

                case "HEIGHT":
                    height = ParseInt(child.Value);
                    break;

                case "FIELD-OF-VIEW":
                    fieldOfView = ParseFloat(child.Value);
                    break;

                case "FROM":
                    from = ParsePoint(child.Value);
                    break;

                case "TO":
                    to = ParsePoint(child.Value);
                    break;

                case "UP":
                    up = ParseVector(child.Value);
                    break;

                case "ADD": break;

                default:
                    throw new Exception($"Camera attribute {key} not supported (line {child.Key.Start.Line})");
                }
            }

            var cameraTransform = ViewTransform.Create(from, to, up);

            return(new Camera(width, height, fieldOfView, cameraTransform));
        }
Example #7
0
        public void CreateArbitrary()
        {
            var from = new Point(1, 3, 2);
            var to   = new Point(4, -2, 8);
            var up   = new Vector(1, 1, 0);

            var expected = new Matrix4x4(
                -0.50709f, 0.50709f, 0.67612f, -2.36643f,
                0.76772f, 0.60609f, 0.12122f, -2.82843f,
                -0.35857f, 0.59761f, -0.71714f, 0,
                0, 0, 0, 1);
            var actual = ViewTransform.Create(from, to, up);

            Assert.Equal(expected, actual);
        }