Questions about composing transformations


#1

Hey, I’m relatively new to rust so excuse the noob questions.

I’m writing a basic rendering engine with gfx-rs and nalgebra, and I’m having issues getting transformations to compose how I expect them to. The user guide says: “Transformations can be composed (by multiplication) even if they do not have the same type”

However, I get errors when I try to multiply a Perspective3 by a Translation3 (or vice versa), but it seems like Perspective3 should be Projective, which should be able to be composed with a Translation. I’ve gotten past that by using to_homogenous, but that feels un-idiomatic because I’m losing the type metadata on the class of transformation I’m using.

Additionally, I’m now using a UnitQuaternion to represent view rotation, and I can’t figure out how to turn that into a rotation matrix that I can compose with a Translation and Perspective.

What’s the correct way to do this stuff?


#2

Hi!

However, I get errors when I try to multiply a Perspective3 by a Translation3 (or vice versa), but it seems like Perspective3 should be Projective, which should be able to be composed with a Translation. I’ve gotten past that by using to_homogenous, but that feels un-idiomatic because I’m losing the type metadata on the class of transformation I’m using.

This is an issue on nalgebra. Multiplications involving Perspective3 are not implemented. You can follow https://github.com/rustsim/nalgebra/issues/392. It will be fixed this saturday.

Additionally, I’m now using a UnitQuaternion to represent view rotation, and I can’t figure out how to turn that into a rotation matrix that I can compose with a Translation and Perspective.

You can use the convert function to convert the UnitQuaternion to the representation you need:

 // Convert the quaternion to a rotation matrix.
let rot: Rotation3<f32> = na::convert(quaternion);
 // Convert the quaternion to a rotation matrix in homogeneous coordinates
let rot: Matrix4<f32> = na::convert(quaternion);

#3

Instead of adding operator overloads to Perspective3, I have added a .as_projective method to convert a ref to a Perspective3 into a ref to a Projective3 so that you can use it to compose with other transformations. This is released on nalgebra v0.16.3.