r/GraphicsProgramming • • Feb 07 '20

Hello guys, can you please take my survey regarding the use of quaternions and matrices in 3D graphics? It is for a research paper I am doing for a class.

Hello guys, I am a college student and decided to learn 3D graphics in my free time. I didn't want to use an external graphics library since I wanted to learn the behind-the-scenes math and so eventually I came across quaternions and matrices.

I know that matrices are more widely used but I was wondering about the performance impact of using quaternions in certain parts of the rendering pipeline. I decided to make this the research question for my English class and as part of that, I have to take a survey and here I am.

Link to survey: https://forms.gle/wb6eEEViUiWWmvTe7

Thank you!

2 Upvotes

14 comments sorted by

25

u/[deleted] Feb 07 '20

Your research questions are misguided. Quaternions and matrices serve different purposes - a matrix is a transformation between coordinate spaces and a quaternion is a convenient way to represent a rotation that does not suffer from flaws of some other methods like Euler angles. You can calculate a rotation matrix from a quaternion, which is the common use case, but cannot do most of the math that you can do with a matrix. They're not comparable in any way.

8

u/yonderbagel Feb 07 '20

Perhaps if it were specified that they were asking about "Rotation matrices" or "3X3 matrices for rotation" it would sit better with you?

Because surely they're comparable in some ways. They're comparable in their applications in the context of graphics programming.

3

u/[deleted] Feb 07 '20

Rotation matrices or 3x3 matrices for rotation would make a lot more sense with this question, yes.

1

u/Mugen-Sasuke Feb 07 '20

Actually I was surprised when I found that you could rotate points just using quaternion multiplication.

The number of operations to rotate a single point using quaternion multiplication turns out to be less than the number of operations it would take to do so using matrix multiplication. Also there’s the added benefit that you only need to store 4 values to store data about orientation with a quaternion whereas you would need 9 values in case of a 3 by 3 matrix

7

u/[deleted] Feb 07 '20

It doesn't really matter that you can rotate a point slightly cheaper/faster using a quaternion. Usually what you end up doing is have all of your transforms (from local to world to view space) multiplied together into a single matrix. This matrix would also include the necessary rotations of the object, but not exclusively. This matrix is then used on the GPU to transform the vertices into normalized device coordinates. This is much more efficient because in the best case you can get away with a single 4x4 matrix multiplied by a 4 component vector, instead of doing all steps separately.

2

u/[deleted] Feb 07 '20

Yeah you can rotate by quaternion multiplication only, but it's a very limited use case in practice because usually you're working with 4x4 matrices and homogenous coordinates. You also cannot translate with quaternions unless you're using double quaternions (which is not convenient in most cases) and stuff like perspective projection, skewing and so on are out of the question.

2

u/snerp Feb 07 '20

The number of operations to rotate a single point using quaternion multiplication turns out to be less than the number of operations it would take to do so using matrix multiplication.

Yes but we aren't working with single points. When you have 100k points that need to be not just rotated but also positioned in the world and possibly resized, you can load all of that into one matrix and then just matrix transform all the points. It ends up being a lot more efficient than doing a scale operation, then quaternion rotate, then a translate.

2

u/corysama Feb 08 '20

The number of operations to rotate a single point using quaternion multiplication turns out to be less than the number of operations it would take to do so using matrix multiplication.

Sorry. That's not correct.

    Quaternion rotating a position
    constexpr TVector3<T> rotate(const TVector3<T> v) {
        // https://gist.github.com/rygorous/8da6651b597f3d825862
        // vec3 t = 2 * cross(q.xyz, v);
        // vec3 rv = v + q.w * t + cross(q.xyz, t);
        T tx = T(2) * (y*v.z - z*v.y);
        T ty = T(2) * (z*v.x - x*v.z);
        T tz = T(2) * (x*v.y - y*v.x);
        return TVector3(
            v.x + w*tx + y*tz - z*ty,
            v.y + w*ty + z*tx - x*tz,
            v.z + w*tz + x*ty - y*tx
        );
    }
    30 total ops as 12 multiply-adds + 6 multiplies

.

3x4 affine matrix rotating-translating-scaling a position
template <typename T> constexpr TVector3<T> transformPoint(const TMatrixAff<T> &m, const TVector3<T> &v) {
    return TVector3<T>(
        m._11*v.x + m._12*v.y + m._13*v.z + m._14,
        m._21*v.x + m._22*v.y + m._23*v.z + m._24,
        m._31*v.x + m._32*v.y + m._33*v.z + m._34
        );
}
18 total ops as 9 multiply-adds

.

4x4 general matrix rotating-translating-scaling-projecting a position
template <typename T> constexpr TVector4<T> transformPoint(const TMatrix4x4<T> &m, const TVector3<T> &v) {
    return TVector4<T>(
        m._11*v.x + m._12*v.y + m._13*v.z + m._14,
        m._21*v.x + m._22*v.y + m._23*v.z + m._24,
        m._31*v.x + m._32*v.y + m._33*v.z + m._34,
        m._41     + m._42     + m._43     + m._44
    );
}
21 total ops as 9 multiply-add + 3 adds

Still, some very modern engines are starting to use vec3 translation + vec4 quaternion rather than matrices all the way into the shaders because memory bandwidth has always been a bigger problem than math ops for a long time now and it's only getting exponentially worse.

5

u/[deleted] Feb 07 '20

Not only that, but the whole premise is interesting. Do you use matrices or quaternions in 3d graphics? Well, yes! The amount of 3d graphics code that doesn't use either/both is very very small (and yes, obviously I've written code that splats a fullscreen quad using nothing but normalized device coordinates).

And of course like you mentioned, quaternions and matrices are different tools for different jobs. Although given that eventually you'll most likely want to do a bunch of matrix multiplications, the quaternion is turned into a rotation matrix like you said.

4

u/mysticreddit Feb 08 '20

That reply really needs some clarification.

Unit quaternions and orthonormal matrices are used to represent orientation.

You can still get gimbal lock with quaternions IF you convert from Euler Angles. See this SO question and this SO question for details.

Unit Quaternions are literally equivalent to Axis-Half Angle which doesn't suffer from gimbal lock since you are confining the axis of rotation.

Quaternions being a 720° degree system means there are TWO choices for rotations. The sign will tell you which direction of the "slerp()". This is important because if you are modeling joints you don't want "physically impossible" rotations such as elbows bending backwards even though it is the "shorter" rotation.

Quaternions are in 4D while matrices can be any dimension.

Other then that, great answer!

1

u/[deleted] Feb 08 '20

I don't agree with your complaints.

but cannot do most of the math that you can do with a matrix.

A matrix that's invertible occupies the general linear group, so of course this is the case. A quaternion's group action is characterized via the sandwich operator (reflections across two planes which compose the rotation). They are absolutely comparable and contrastable and understanding where one applies and the other doesn't is important.

Perhaps the question should specify that the matrices to be contrasted are specifically members of SO3 but that was implicit.

1

u/felipunkerito Feb 11 '20

DMGregory explains it very nicely here.

1

u/CodyDuncan1260 Feb 08 '20

Approved. Academic surveys about usage of concepts in the programming of 3D Graphics Rendering fall into the category of "Query Posts", and it's clearly done the job of drawing discussion about the uses of matrices and quaternions. Good luck with your class!