0
votes

I am working on a Perspective camera. The constructor must be:

PerspectiveCamera::PerspectiveCamera(Vec3f &center, Vec3f &direction, Vec3f &up, float angle)

This is construction different from most others, as it lacks near and far clipping planes. I know what to with center, direction, and up -- the standard look at algorithm.

r

foo+bar

z

We can construct the view matrix and translate matrix accordingly:

V

Thus, the viewing transformation is:

M

For an orthographic camera (which is working correctly for me), the inverse transformation is used to go from screen space to world space. The camera coordinates go from (-1,-1,0) --> (1,1,0) in screen space.

p

For perspective transformation, only the field of view is given. The Wikipedia 3D projection article gives a perspective projection matrix using the field of view angle and assuming camera coordinates go from (-1,-1) --> (1,1):

p

p

In my code, (ex,ey,ez) are the camera coordinates that go from (-1,-1, ez) --> (1,1, ez). Note that the 1 in (3,3) spot of K isn't in the Wikipedia article -- I put it in to make the matrix invertible. So that may be a problem.

But anyways, for perspective projection, I used this transformation:

p

K inverse is multiplied with p to make the canonical view volume to a view frustum, and the result of that is multiplied with M inverse to move into world coordinates.

I get the wrong results. The correct output is:

Correct result

My output looks like this:

My result

Am I using the right algorithm for perspective projection given my constraint (no near and far plane inputs)???

1
Is that a typo in your description that the near and far clip planes are equal to one another (ez)? Using the traditional equation for a perspective projection matrix, m22 and m23 (where indices begin at 0) would be infinite if you did that.Andon M. Coleman
I assume you mean when I said "(-1,-1, ez) --> (1,1, ez)". No, I don't think means the near and far clip planes. According to wikipedia, exyz the viewer's position relative to the display surface.Babbu Maan
The way I thought about is ez is z distance from xy axis assuming you have field of view of alpha and xy coordinates that go from (-1,-1) --> (1,1). But that is just my guess.Babbu Maan
Oh... there is another problem with your matrix though. For perspective, m33 (again, where indices begin at 0) is generally 0. Perspective division would be useless if it were 1. The 4th row should be 0,0,-1,0 so that the clip-space W coordinate is -eye_zAndon M. Coleman
Yes that would cause problems. I added the 1 in m33 so the matrix would be invertible. If the 4th column is 0,0,0,0 then the matrix is singular. My way is definitely hackish based on different sources -- is there a canonical way to make screen space coordinates into world coordinates given a FOV?Babbu Maan

1 Answers

0
votes

Just in case somebody else runs into this issue, the method presented in the question is not proper way to create a viewing frustum. The perspective matrix (K) is for projecting the far plane onto the near plane, and we don't have those planes in this case

To create a frustum, do the inverse transformation on (x,y,ez) [as opposed to (x,y,0) for orthographic projection). Find a new direction by subtracting the transformed point from the center of projection. Shoot the ray.