移动相机,以适应3D场景场景、相机、以适应

2023-09-07 17:21:08 作者:未眠

我在寻找一种算法,以适应视口内边框(在我的情况下,支持DirectX场景)。我知道用于在正交相机定心一个包围球算法,但需要相同的边界框的立体照相机。我不能只改变FOV,因为这个程序有FOV作为一个用户可编辑的变量,因此它必须移动相机。

I'm looking for an algorithm to fit a bounding box inside a viewport (in my case a DirectX scene). I know about algorithms for centering a bounding sphere in a orthographic camera but would need the same for a bounding box and a perspective camera. I can not just change the FOV because this app has FOV as a user editable variable, so it must move the camera.

我的大部分数据:

我有向上矢量相机 我有边框的中心点 我也有看,在向量(方向和距离),从相机指向盒中心 我已投影在平面上的点垂直于照相机和检索描述最大/最小X和Y的coords多少是内或观看平面之外的系数。

的问题,我有:

边界框的中心不一定是在视口中(即,它的边界投影后矩形)的中心。 由于视场歪斜投影(参见http://en.wikipedia.org/wiki/File:Perspective-foreshortening.svg)我不能简单地用系数为比例系数,将摄像机,因为它会冲/下冲所需的摄像机位置 Center of the bounding box isn't necessarily in the center of the viewport (that is, it's bounding rectangle after projection). Since the field of view "skew" the projection (see http://en.wikipedia.org/wiki/File:Perspective-foreshortening.svg) I cannot simply use the coefficients as a scale factor to move the camera because it will overshoot/undershoot the desired camera position

我如何找到摄像头的位置,使它充满视口像素完美地(例外是如果纵横比是远远1.0,只需要填写的屏轴之一)?

How do I find the camera position so that it fills the viewport as pixel perfect as possible (exception being if the aspect ratio is far from 1.0, it only needs to fill one of the screen axis)?

我已经尝试了一些其他的事情:

使用边界球和正切找到一个比例系数移动相机。这不很好地工作,因为它没有考虑到透视投影,第二球是坏的包围盒为我所用,因为我有很多板材和长材的几何形状。 在迭代调用该函数以获取一个小错误,在摄像头的位置。这工作有点,但我有时会遇到奇怪的边缘情况下,摄像机的位置超调太多,误差系数增大。此外,这样做的时候我并没有recenter基础上边框位置的模​​型。我无法找到一个坚实,可靠的方法来做到这一点可靠。

请帮助!

推荐答案

有许多可能的摄像头位置+方向所在的边框将适合的视锥内。但是,任何程序将选择一个特定的摄像头位置和方向。

There are many possible camera positions + orientations where the bounding box would fit inside the view frustum. But any procedure would select one specific camera position and orientation.

如果您的会的考虑包围球,一种解决方案可能是

If you would consider bounding spheres, one solution could be to

在第一个变化的方向来看待边界球中心 再移回足够(负的样子方向)包围球,以适应里面的圆台

使用的包围盒的你可以考虑第一定位的早期步骤相机在垂直于最大的中心(或最小的,无论你preFER)多维数据集的脸。

With bounding boxes you could consider an earlier step of first positioning the camera at perpendicular to the center of the largest (or smallest, whatever you prefer) cube face.

我有与DirectX没有经验,但移动和改变摄像机的寻找方向居中的某一点应该很容易。 难的是做决定如何远动查看对象的数学。

I have no experience with DirectX, but moving and changing the looking direction of the camera to center a certain point should be easy. The hard part is to do the math of deciding how far to move to view the object.

如果您知道边界尺寸取值在世界坐标的对象(我们不感兴趣的像素或相机的坐标,这是因为它们依赖于你的距离)从摄像头的方位,就可以计算相机的要求的距离 D 的边界形状,如果你知道x和y场中,视场角透视投影的。

If you know the bounding size s of the object in world coordinates (we are not interested in pixels or camera coordinates, since those are dependent on your distance) from the orientation of the camera, you can compute the required distance d of the camera to the bounding shape if you know the x and y Field-Of-View angle a of the perspective projection.

     frustum      ------            
            ------    *****          -  
       -----          *   *          |
   -===     ) FOV a   *bounding box  | BB size s
camera -----          *   *          |
            ------    *****          -
                  ------

  |-------------------|
        distance d

所以,数学是棕褐色(A / 2)=(​​S / 2)/ D => D =(S / 2)/棕褐色(A / 2) 这将给你的摄像头应该放在从最近的边界表面的距离。

So, the math is tan(a/2) = (s/2) / d => d = (s/2) / tan(a/2) Which will give you the distance the camera should be placed from the closest bounding surface.