linalg icon indicating copy to clipboard operation
linalg copied to clipboard

Convert a mat4 to a mat3 and back

Open mokafolio opened this issue 1 year ago • 0 comments

Hi, I am not sure if I am missing something so please correct me if there already is an easier way to do this. Here's what I want to do:

  • Convert a mat4 transformation matrix to a mat3 rotation matrix (essentially dropping the translation part)
  • Convert it back to a mat4 with the translation part set to {0, 0, 0}

This is a pretty common operation for a bunch of 3D rendering related tasks. I have been doing that manually so far (the types are just linalg typedefs):


auto skyboxViewRot = Mat3f { transform.x.xyz(), transform.y.xyz(), transform.z.xyz() };
auto skyboxView = Mat4f { Vec4f { skyboxViewRot.x, 0.0f },
                                        Vec4f { skyboxViewRot.y, 0.0f },
                                        Vec4f { skyboxViewRot.z, 0.0f },
                                        Vec4f { 0.0f, 0.0f, 0.0f, 1.0f } };

I am pretty sure in glm the mat3 and mat4 constructors allow you to do this as one liners. Is there a similar way to achieve this in linalg? Otherwise I think it might be useful to add utilities for these operations (not sure if this is beyond the scope of the library but these seem to be very common operations to me).

Thanks!

mokafolio avatar Mar 13 '23 18:03 mokafolio