0
\$\begingroup\$

In my engine, camera is just any other object in the scene. It has a transform -- position, rotation (quaternion), and scale (ignored for camera view matrix). I want to convert this to the camera's view matrix. I tried the following formula (currently, there is no rotation) but for some reason, the position in the X axis is incorrect:

mat4 cameraTransform = translate(identity, transform.position) * to_mat4(transform.rotation); // rotation is quaternion

auto viewMatrix = inverse(cameraTransform);

Camera's transform (from perspective of a generic lookUp camera for testing purposes):

Camera's transform

Camera's view:

Camera's view

The issue here is that, the camera's position is somehow flipped. The camera is positioned to look in the X direction (the red axis line); however, from camera's view matrix, it is looking in the Z direction (blue axis line). The thing is, from the looks of it, the camera's position is actually correct (I counted the grid boxes on the ground). The problem is that, it is rotated. I can't seem to figure out why. There is currently no rotation ((0,0,0) in euler angles).

How should I calculate this? Also, what does the view matrix represent in terms of what valeus are in the matrix? Does it represent where the camera is looking at or where the camera actually is?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

The view matrix represents the transformation you need to apply to a point in the world to get it into camera space — a space where the camera is the origin, the x+ axis points to the right of the camera's view, the y+ axis points to the top, and the z+ axis points forward in the direction the camera is looking (or the z- axis, depending on your coordinate system).

Your strategy of inverting the camera's world matrix looks correct to me. That's how it's usually done.

I think the problem is that you've drawn your camera gizmo as though it looks along its x axis. That is, when unrotated, it looks along the world x+ or x-.That departs from the more common convention your rendering code is using, which assumes the camera's z axis should be it's look direction.

So, rotate the marker graphic you draw for your camera to match the perspective you observe when looking through that camera, and you should have it behaving correctly from there.

\$\endgroup\$
1
  • \$\begingroup\$ facepalm Thank you! Coming from 2D/web dev world, it is very easy to forget that 2D icons are actually drawn on a square box. \$\endgroup\$
    – Gasim
    Commented May 5, 2022 at 15:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .