Search code examples
c#wpfviewport3drescale

Viewport3D disable rescaling / resizing


How can I disable the following strange resize / rescaling behaviour of a Viewport3d in WPF? Notice that I do not change the window height:

https://i.sstatic.net/jCgPe.png

https://i.sstatic.net/Q0V6X.png

How can I disable this "feature" of WPF (or why does it do this?)


Solution

  • To anyone stumbling upon this question, here's the answer by Mike Danes: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/642253a6-e7a5-4ce2-bc08-e91f2634605b/

    This auto scaling is a consequence of the way perspective works.

    The default field of view is 45 and the default near plane distance is 0.125. This means that a point (x, 0, 0.125) will be visible at the right side of the viewport when its x coordinate is tan(45/2) * 0.125 = 0.051776. Note how the viewport width doesn't play a part in this computation, if you make it wider the point (0.051776, 0, 0.125) will still be visible at the right side of the viewport, that's why the teapot appears larger.

    You could compensate for a width increase by changing the field of view and near plane distance, something along these lines:

    double newWidth = grid.ActualWidth;
    double originalWidth = 509.0;
    double originalNearPlaneDistance = 0.125;
    double originalFieldOfView = 45.0;
    double scale = newWidth / originalWidth;
    
    double fov = Math.Atan(Math.Tan(originalFieldOfView / 2.0 / 180.0 * Math.PI) * scale) * 2.0;
    camera.FieldOfView = fov / Math.PI * 180.0;
    camera.NearPlaneDistance = originalNearPlaneDistance * scale;