Calculating frustum FOV for a PerspectiveCamera

点点圈 提交于 2019-12-19 10:16:57

问题


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).

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. Game area. Can be zoomed. The size is 1080x1277px (WxH) and located at the top.
  2. The HUD (FYI objects from here can be moved to area (1). Non zoomable. The size is 1080x154 (WxH).

Both have their own cameras.

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

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();

And I create a object:

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);

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.

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).

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?

Thanks.


回答1:


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.

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

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.

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.



来源:https://stackoverflow.com/questions/46164180/calculating-frustum-fov-for-a-perspectivecamera

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!