Inverting a 4x4 matrix

Is inverting a 4x4 matrix (context: webgpu) easy to implement from scratch, or something to just add an external dependency for ?

Well this is answered in this stackoverflow thread: c++ - Inverting a 4x4 matrix - Stack Overflow

So I’d say it’s not exactly straightforward, but feasible. But in the context of wgpu you’re probably already using a math library, maybe it already provides inversion of 4x4 matrices ?

1 Like

cgmath for example has that: Matrix4 in cgmath - Rust

Do you actually need the inverse or would you be happy with using a decomposition? What are you going to use the inverse for?

1 Like

No, it's a bunch of handrolled {Vec, Mat}{2, 3, 4}

I have a screen space ray that I need to transform into a world space ray. I think (but not 100% sure) I need the matrix inverse for this.

I would highly suggest using an existing math library.

glam is the rust graphics community's goto linalg library right now.

Indeed, you'd need the inverse projection matrix to acquire the view space ray.

1 Like

The linked SO code is not great. Compared to Gaussian elimination, Cramer's rule requires more floating point operations and is less stable.

You might not need the inverse if you're working with projective coordinates, just the inverse up to a scalar multiple.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.