0

How do I set up coordinates for perspective matrix like in orthographic projection for left, right, top and bottom and add in fov and aspect ratio. I have only seen perspective matrix use other arguments in a method without left, right, bottom, top. This is my ortho matrix:

public static Matrix4f Orthographic(float left, float right, float bottom, float top, float near, float far) {
        Matrix4f result = Identity();

        result.elements[0 + 0 * 4] = 2.0f / (right - left);

        result.elements[1 + 1 * 4] = 2.0f / (top - bottom);

        result.elements[2 + 2 * 4] = 2.0f / (near - far);

        result.elements[0 + 3 * 4] = (left + right) / (left - right);
        result.elements[1 + 3 * 4] = (bottom + top) / (bottom - top);
        result.elements[2 + 3 * 4] = (far + near) / (far - near);

        return result;
    }

Now I want to create perspective one with same arguments but with added fov.

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

3

At Perspective Projection the projection matrix describes the mapping from 3D points in the world as they are seen from of a pinhole camera, to 2D points of the viewport.
The eye space coordinates in the camera frustum (a truncated pyramid) are mapped to a cube (the normalized device coordinates).

The Viewing frustum of a perspective projection can be defined by a by 6 distances.

In the following distances left, right, bottom and top, are the distances from the center of the view to the side faces of the frustum, on the near plane. near and far specify the distances to the near and far plane of the frustum.

r = right, l = left, b = bottom, t = top, n = near, f = far

x:    2*n/(r-l)      0              0                0
y:    0              2*n/(t-b)      0                0
z:    (r+l)/(r-l)    (t+b)/(t-b)    -(f+n)/(f-n)    -1
t:    0              0              -2*f*n/(f-n)     0

If the projection is symmetric, where the line of sight is axis of symmetry of the view frustum, then the matrix can be simplified and can be specified by an field of view angle (fov_y), an aspect ratio (w / h) an the 2 distances to the near and far plane

a  = w / h
ta = tan( fov_y / 2 );

2 * n / (r-l) = 1 / (ta * a)
2 * n / (t-b) = 1 / ta
(r+l)/(r-l)   = 0
(t+b)/(t-b)   = 0

So the symmetrically perspective projection is:

x:    1/(ta*a)  0      0              0
y:    0         1/ta   0              0
z:    0         0     -(f+n)/(f-n)   -1
t:    0         0     -2*f*n/(f-n)    0
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • So I don't need to define fov? I can simply use the formula above? –  Oct 29 '19 at 21:13
  • 1
    @GoldSpark You've to define the bottom left point of the near plane and the top right point of the near plane. then you can use the above formula – Rabbid76 Oct 29 '19 at 21:14
  • So I just define r, l, b, t, n, f to for example: 0, 600, 0, 800, -1, 1 respectfully. Thank you :D –  Oct 29 '19 at 21:22
  • 1
    No, at persepctive projection the near and the far plane have to be positive. `0 < near < far`! It has to be a [frustum](https://en.wikipedia.org/wiki/Viewing_frustum). See the image in the answer. – Rabbid76 Oct 29 '19 at 21:24
  • I've tried setting it up like this: projection = Matrix4f.perspective(-100, 100, -100, 100, 0, 1); When I replace perspective with ortho it shows small triangle in the middle. But when I put perspective it shows nothing. I've tried setting Z value to minus: -0.1, -0.2 etc.. and then tried to positive values. Nothing. Here is the perspective code: –  Oct 31 '19 at 14:50
  • @GoldSpark No near has to be grater than 0. Note, the left, right, top and bottom distance is measured on the near plane. Try (-1, 1, -1, 1, 1, 200) and draw the triangle with a z distance of 100 to the eye position. – Rabbid76 Oct 31 '19 at 14:51
  • 1
    Got it haha that is why I deleted previous comment. –  Oct 31 '19 at 15:00