计算PerspectiveCamera的视锥FOV [英] Calculating frustum FOV for a PerspectiveCamera

查看:432
本文介绍了计算PerspectiveCamera的视锥FOV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个包含两个区域的屏幕:

I currently have a screen consisting of two areas:

(此示例仅假定值,当然,取决于屏幕,该值可能会有所不同.)

(Values are just assumed for this particular example and may of course vary depending on screen).

该屏幕的总大小为1080x1432px(WxH),由两个区域组成,每个区域均使用glViewPort剪辑.这是因为我希望区域(1)在缩放时不会填满屏幕.

The screen in total is 1080x1432px (WxH) and consists of two areas, each clipped using glViewPort. This because I want area (1) not to fill the screen when zooming.

  1. 游戏区.可以缩放.尺寸为1080x1277px(WxH),位于顶部.
  2. HUD(FYI对象可以从此处移动到区域(1).不可缩放.大小为1080x154(WxH).
  3. )

两个都有自己的相机.

区域(1)的宽度为15f,高度大于15f(只要至少15f无关紧要).

Area (1) width is 15f and the height is more than 15f (does not matter as long as it's at least 15f).

我希望区域(2)的宽度为7f,高度为1f(为方便起见).因此,我想相应地设置相机.我尝试通过计算FOV来做到这一点:

I want area (2) to be 7f in width and 1f in height (for convenience). Thus I want to set the camera accordingly. I've tried to do this by calculating the FOV:

float size = 1f;
float halfHeight = size * 0.5f;
halfHeight *= (float) 154 / (float) 1080;
float fullHeight = 2 * halfHeight;
float halfFovRadians = MathUtils.degreesToRadians * camera.fieldOfView * 0.5f;
float distance = halfHeight / (float) Math.tan(halfFovRadians);

camera.viewportWidth = 1080;
camera.viewportHeight = 154;
camera.position.set(0f, 0, distance);
camera.lookAt(0, 0, 0);
camera.update();

然后我创建一个对象:

ModelBuilder builder = new ModelBuilder();
builder.begin();
builder.node();
MeshPartBuilder meshBuilder = builder.part("lattice", GL20.GL_TRIANGLES,
        VertexAttributes.Usage.Position,
        new Material(ColorAttribute.createDiffuse(Color.GRAY)));

BoxShapeBuilder.build(meshBuilder, 0f, 0f, 0f, 7f, 1f, 0f);
Model model = builder.end();
mHudModel = new ModelInstance(model);

如果我手动尝试将距离设置为1f,我仍然可以查看该框,但是如果低于1.0f,则不会显示该框.计算的距离约为0.76f.

If I manually try to set distance to 1f I can still view the box, but if I go below 1.0f the box won't display. And the distance being calculcated is approx 0.76f.

我正在尝试使用与 https://xoppa相同的概念.github.io/blog/a-simple-card-game/来计算FOV.它适用于区域(1).

I am trying to use the same concept as https://xoppa.github.io/blog/a-simple-card-game/ to calculate the FOV. It works fine for area (1).

我不能这样使用相机吗?我的FOV计算不正确吗?当我的距离低于1f时,为什么我的物体会消失?

Can I not use the camera like this? Do I calculate the FOV incorrectly? Why would my object disappear when I go below 1f in distance?

谢谢.

推荐答案

投影矩阵描述了从场景的3D点到视口的2D点的映射.它从眼睛空间转换到剪辑空间,并且通过除以剪辑坐标的w分量,将剪辑空间中的坐标转换为归一化设备坐标(NDC). NDC的范围是(-1,-1,-1)到(1,1,1).
修剪掉NDC之外的每个几何图形.

The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport. It transforms from eye space to the clip space, and the coordinates in the clip space are transformed to the normalized device coordinates (NDC) by dividing with the w component of the clip coordinates. The NDC are in range (-1,-1,-1) to (1,1,1).
Every geometry which is out of the NDC is clipped.

摄像机视锥的近平面和远平面之间的对象映射到NDC的范围(-1,1).
(另请参见

The objects between the near plane and the far plane of the camera frustum are mapped to the range (-1, 1) of the NDC.
(See further How to render depth linearly in modern OpenGL with gl_FragCoord.z in fragment shader?)

这意味着如果要查看 1.0 更近的对象,则必须将到近平面的距离设置为小于 1.0 em>.

This means if you want to see objects that are nearer then 1.0, then you have to set the distance to the near plane less than 1.0.

请注意,近平面和远平面应尽可能靠近场景,以提高计算精度并避免Z角对抗,但它们必须包含您要从场景中看到的所有内容.

Note, the near plane and the far plane should be as close as possible to the scene, to increase the computational accuracy and to avoid z-fighting, but they must include everything you want to see from the scene.

这篇关于计算PerspectiveCamera的视锥FOV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆