2

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Henrik
  • 1,983
  • 3
  • 28
  • 52
  • Ah yep I discovered that the near plane is 1f (and far plane is 100f). So that's why it's not possible. When I changed the near plane to be the same as the distance it seems to work fine and I get the shape expected. Is this a good solution or should I do it in a different way? – Henrik Sep 12 '17 at 06:10

1 Answers1

3

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.

enter image description here

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.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thank you. What I don't grasp is what happens when I set the camera distance. If the near plane is 1f and the camera distance is 10f then does this mean the eye position (camera) is 11f from the near plane (i.e. the image becomes smaler)? I.e. let's say my near plane is 1f and camera distance is 0.5f, isn't the camera still in front of the near plane? – Henrik Sep 12 '17 at 08:56
  • @Henrik No, the camera position and the eye position are equal. The distance to the eye and to the camera are equal. The near plane and the far plane define the range which is *visible*. All out of this range is discarded (this is called the clip space). – Rabbid76 Sep 13 '17 at 17:32
  • Yep I think I understand clip space. But this is what I don't understand: I create a plane at 0,0,0. Near=1 Far=100. When I set camera distance to 10 it's possible to see the object. When I set distance to 0.9 it is not possible to see object. Isn't 0.9 the distance to the near plane? Wouldn't this mean the camera is still in front of the near plane? Or is the camera inside clip space when I set to 13? – Henrik Sep 15 '17 at 09:02
  • @Henrik The near plane does not influence the camera position. But you can **not** see the objects directly in front of the camera (the objects before the near plane). The objects between the camera position and the near plane are clipped, because of mathematical model of the camera frustum. – Rabbid76 Sep 15 '17 at 09:17
  • If I create a plane of 0,0,0 where near=1 far=100 is the plane in between the near and far? Because if I set camera distance to 13 it is possible to see the plane in libgdx. Sorry for the me not understanding :-) (camera 13)-0-(near plane 1)-(object)-(far 100) – Henrik Sep 15 '17 at 11:58
  • 1
    @Henrik If your camera is at any point, that has 13 distance to (0, 0, 0) and you look to the point (0, 0, 0), and the near plan is 1 and the far plan is 100, then a object at the position (0,0,0) is visible. An opject whith the distance of 12,1 from (0,0,0) in the directon to the camera will **not** be visible. – Rabbid76 Sep 15 '17 at 12:06