本文介绍了移动相机以适应 3D 场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种算法来适应视口内的边界框(在我的情况下是 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 坐标在视平面内或外的系数.

我遇到的问题:

  • 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)?

我尝试了其他一些东西:

  • 使用边界球体和切线找到移动相机的比例因子.这效果不佳,因为它没有考虑透视投影,其次球体对于我来说是不好的边界体积,因为我有很多扁平和长几何形状.
  • 迭代调用函数以获得越来越小的相机位置误差.这在一定程度上起作用了,但我有时会遇到奇怪的边缘情况,即相机位置过冲太多并且误差因子增加.此外,在执行此操作时,我没有根据边界矩形的位置重新调整模型.我找不到可靠、可靠的方法来做到这一点.

请帮忙!

推荐答案

有许多可能的相机位置 + 方向,其中边界框适合视锥体.但任何程序都会选择一个特定的相机位置和方向.

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

  • 首先改变方向以查看边界球体中心
  • 然后充分向后移动(负向看方向)使边界球体适合平截头体

使用边界框,您可以考虑先将相机垂直于最大(或最小,无论您喜欢什么)立方体面的中心定位.

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.

如果您知道物体在世界坐标系中的边界尺寸 s(我们对像素或相机坐标不感兴趣,因为它们取决于您的距离)与相机的方向,您如果您知道透视投影的 x 和 y 视场角 a,则可以计算相机到边界形状所需的距离 d.

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

因此,数学tan(a/2) = (s/2)/d =>d = (s/2)/tan(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.

这篇关于移动相机以适应 3D 场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:14