[英]OpenGL: Fit scene to screen
I have a 3D scene with a perspective projection. 我有一个带有透视投影的3D场景。
I want to fit the scene to the screen based on a bounding box ( min
and max
). 我想根据边界框(
min
和max
)将场景调整到屏幕。
I have centered my scene like this: 我把我的场景集中在这里:
glm::vec3 center = (min + max) / 2.0f;
rootNode->translate(-center.x, -center.y, -center.z);
Now I need a scale factor to scale my rootNode to fit the screen. 现在我需要一个比例因子来缩放我的rootNode以适应屏幕。 How do I do this?
我该怎么做呢?
(this: 8.070 How can I automatically calculate a view that displays my entire model? (I know the bounding sphere and up vector.) does not help because its based on a orthogonal projection) (这: 8.070如何自动计算显示整个模型的视图?(我知道边界球和向上矢量。)没有帮助,因为它基于正交投影)
The reason this question is harder with a perspective projection than it is with an orthogonal projection is that the min
and max
you need are not constant with a perspective projection. 使用透视投影比使用正交投影更难以解决这个问题的原因是,您需要的
min
和max
与透视投影不一致。
With a perspective projection the distance between either edge of the visible region increases as you move away from the camera. 使用透视投影,当您远离相机时,可见区域的任一边缘之间的距离会增加。
With a perspective projection you typically have a field of view angle, theta
, a camera position, and a "looking at" vector, v
. 通过透视投影,您通常具有视角
theta
,摄像机位置和“看”矢量v
。 At any distance, d
from the camera's position (in the direction of v
) you can imagine a plane whose normal is v
. 在任何距离,
d
从相机的位置(在v
的方向)你可以想象一个正常为v
的飞机。 The region of this plane that your camera can "see" has width: 您的相机可以“看到”的此平面区域具有宽度:
2 * d * tan(theta / 2).
In a simple fixed camera setup you might have your camera at the origin and looking down the z-axis, and then the distance d
for any point will just be the point's z
coordinate. 在简单的固定摄像机设置中,您可能将摄像机放在原点并向下看z轴,然后任何点的距离
d
将只是点的z
坐标。
Note also that you may have different horizontal and vertical field of view angles. 另请注意,您可能具有不同的水平和垂直视角。 If you have set a vertical field of view angle "fovy" and an aspect ratio (viewport width / viewport height) then your horizontal field of view angle is your vertical field of view angle times the aspect ratio.
如果您设置了垂直视角“fovy”和纵横比(视口宽度/视口高度),那么您的水平视角是您的垂直视角角度乘以纵横比。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.