Ejemplo n.º 1
0
        [Test] public void GetWorldPos()
        {
            using (DrawDevice device = new DrawDevice())
            {
                Vector2 targetSize     = new Vector2(800, 600);
                Vector2 viewportCenter = targetSize * 0.5f;

                // We'll check twice the focus distance to make sure orthographic
                // scaling is working as expected.
                device.FocusDist    = DrawDevice.DefaultFocusDist * 2.0f;
                device.NearZ        = 100;
                device.FarZ         = 10000;
                device.TargetSize   = targetSize;
                device.ViewportRect = new Rect(targetSize);
                device.ViewerPos    = new Vector3(0, 0, -device.FocusDist);

                // Screen space rendering
                device.Projection = ProjectionMode.Screen;

                // 1:1 world coordinate output in all cases
                AssertRoughlyEqual(new Vector3(0.0f, 0.0f, 0.0f), device.GetWorldPos(new Vector3(0.0f, 0.0f, 0.0f)));
                AssertRoughlyEqual(new Vector3(400.0f, 300.0f, 0.0f), device.GetWorldPos(new Vector3(400.0f, 300.0f, 0.0f)));
                AssertRoughlyEqual(new Vector3(800.0f, 600.0f, 0.0f), device.GetWorldPos(new Vector3(800.0f, 600.0f, 0.0f)));
                AssertRoughlyEqual(new Vector3(0.0f, 0.0f, 1000.0f), device.GetWorldPos(new Vector3(0.0f, 0.0f, 1000.0f)));
                AssertRoughlyEqual(new Vector3(400.0f, 0.0f, 1000.0f), device.GetWorldPos(new Vector3(400.0f, 0.0f, 1000.0f)));
                AssertRoughlyEqual(new Vector3(800.0f, 0.0f, 1000.0f), device.GetWorldPos(new Vector3(800.0f, 0.0f, 1000.0f)));
                AssertRoughlyEqual(new Vector3(0.0f, 300.0f, 1000.0f), device.GetWorldPos(new Vector3(0.0f, 300.0f, 1000.0f)));
                AssertRoughlyEqual(new Vector3(0.0f, 600.0f, 1000.0f), device.GetWorldPos(new Vector3(0.0f, 600.0f, 1000.0f)));

                // World space rendering with orthographic projection
                device.Projection = ProjectionMode.Orthographic;

                // Scaled up 2:1 due to focus distance scaling factor
                AssertRoughlyEqual(new Vector3(0.0f, 0.0f, 0.0f), device.GetWorldPos(new Vector3(viewportCenter, 0.0f)));
                AssertRoughlyEqual(new Vector3(-200.0f, -150.0f, 0.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(-400.0f, -300.0f), 0.0f)));
                AssertRoughlyEqual(new Vector3(200.0f, 150.0f, 0.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(400.0f, 300.0f), 0.0f)));

                // No scale changes at other distances
                AssertRoughlyEqual(new Vector3(0.0f, 0.0f, 1000.0f), device.GetWorldPos(new Vector3(viewportCenter, 1000.0f)));
                AssertRoughlyEqual(new Vector3(-200.0f, 0.0f, 1000.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(-400.0f, 0.0f), 1000.0f)));
                AssertRoughlyEqual(new Vector3(200.0f, 0.0f, 1000.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(400.0f, 0.0f), 1000.0f)));
                AssertRoughlyEqual(new Vector3(0.0f, -150.0f, 1000.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(0.0f, -300.0f), 1000.0f)));
                AssertRoughlyEqual(new Vector3(0.0f, 150.0f, 1000.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(0.0f, 300.0f), 1000.0f)));

                // World space rendering with perspective projection
                device.Projection = ProjectionMode.Perspective;

                // 1:1 scaling at focus distance
                AssertRoughlyEqual(new Vector3(0.0f, 0.0f, 0.0f), device.GetWorldPos(new Vector3(viewportCenter, 0.0f)));
                AssertRoughlyEqual(new Vector3(-400.0f, -300.0f, 0.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(-400.0f, -300.0f), 0.0f)));
                AssertRoughlyEqual(new Vector3(400.0f, 300.0f, 0.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(400.0f, 300.0f), 0.0f)));

                // Scaled down 1:2 at double the focus distance
                AssertRoughlyEqual(new Vector3(0.0f, 0.0f, 1000.0f), device.GetWorldPos(new Vector3(viewportCenter, 1000.0f)));
                AssertRoughlyEqual(new Vector3(-400.0f, 0.0f, 1000.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(-200.0f, 0.0f), 1000.0f)));
                AssertRoughlyEqual(new Vector3(400.0f, 0.0f, 1000.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(200.0f, 0.0f), 1000.0f)));
                AssertRoughlyEqual(new Vector3(0.0f, -600.0f, 1000.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(0.0f, -300.0f), 1000.0f)));
                AssertRoughlyEqual(new Vector3(0.0f, 600.0f, 1000.0f), device.GetWorldPos(new Vector3(viewportCenter + new Vector2(0.0f, 300.0f), 1000.0f)));
            }
        }