Choose camera Distance to fit scene into WPF window
#2
There are multiple ways on how this can be done.

1)
The easiest is to call Camera1.FitIntoView method.
But in case the view area is wider than the object (view's aspect ratio = width / height is bigger than aspect ratio of the object), then this method will also fit the object vertically.

Also note that when you resize the window, then the rendered 3D objects will be also scaled and no additional space will be shown (as in usual 2D application when you increase the window size you see more and not scale the content).

2)
When using a perspective camera (by default CameraType is set to PerspectiveCamera), then the required distance depends on the width of the object and the FieldOfView - the angle at which the camera views the scene. With some trigonometry, you can calculate the required distance - here is the method that can do that:

Code:
private double GetCameraDistance(double width, double fieldOfView)
{
   double radian = fieldOfView * Math.PI / 180.0;
   return (width * 0.5) / Math.Tan(radian * 0.5);
}


3)
Instead of a perspective camera, you can also use an orthographic camera (there the objects do not scale down when they are farther away from the camera) - you can use that by setting CameraType to CameraType OrthographicCamera). In this case, the FieldOfView property does not have any effect. Instead, CameraWidth is used - this specifies the width of the visible area. So in your case, you could simply set CameraWidth to 800.

This mode is also recommended for showing 2D content. In this case you set the CameraWidth to the width of the visible area (Viewport3D or DXViewportView width). This way you get 1 : 1 ratio from the 3D to 2D space. When the view area is realized, then also the CameraWidth would be changed. This way you can show more area when increasing the size of the window. You can also use zooming - in this case you would divide the visible area width by the zoom factor.

In the following days I will prepare a sample that will show how to render many 2D lines with Ab3d.DXEngine  -it will use the OrthographicCamera and support zooming and panning. I will attach the sample to this post.
Andrej Benedik
  


Messages In This Thread
RE: Choose camera Distance to fit scene into WPF window - by abenedik - 10-26-2020, 10:17 AM

Forum Jump:


Users browsing this thread:
1 Guest(s)