Mark Ingram
Mark Ingram

Reputation: 73663

How to convert mathematical matrix to Direct3D matrix?

When representing a mathematical matrix, rotations are performed as follows:

http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations

Rx(θ) = 1  0      0
        0  cos θ  -sin θ
        0  sin θ  cos θ

Ry(θ) = cos θ  0  sin θ
        0      1  0
        -sin θ 0  cos θ

Rz(θ) = cos θ  -sin θ  0
        sin θ  cos θ   0
        0      0       1

However, I've discovered that Direct3D uses the transpose of these rotation matrices.

In my app I have a generic Matrix class which uses the standard mathematical rotation representations. With a simple rotation about 1 axis, it is easy to convert to a Direct3D matrix, as you can just do the transposition. However, if you rotate about x, y and then z you cannot simply get the transposed matrix.

My question is, how can I convert a mathematical matrix in to a Direct3D matrix?

Here is an example:

Matrix matrix;
matrix.RotateX(1.0f);
matrix.RotateY(1.0f);
matrix.RotateZ(1.0f);

Mathematical matrix =
m_11 0.29192656 float
m_12 -0.45464867 float
m_13 0.84147096 float
m_14 0.00000000 float
m_21 0.83722234 float
m_22 -0.30389664 float
m_23 -0.45464867 float
m_24 0.00000000 float
m_31 0.46242565 float
m_32 0.83722234 float
m_33 0.29192656 float
m_34 0.00000000 float
m_41 0.00000000 float
m_42 0.00000000 float
m_43 0.00000000 float
m_44 1.0000000 float

Direct3D matrix =
_11 0.29192656 float
_12 0.45464867 float
_13 -0.84147096 float
_14 0.00000000 float
_21 -0.072075009 float
_22 0.88774973 float
_23 0.45464867 float
_24 0.00000000 float
_31 0.95372111 float
_32 -0.072075009 float
_33 0.29192656 float
_34 0.00000000 float
_41 0.00000000 float
_42 0.00000000 float
_43 0.00000000 float
_44 1.0000000 float

Edit: Here are some examples of individual rotations.

X-Axis rotation by 1 radian
My matrix class:

1.0000000    0.00000000    0.00000000    0.00000000
0.00000000   0.54030228   -0.84147096    0.00000000
0.00000000   0.84147096    0.54030228    0.00000000
0.00000000   0.00000000    0.00000000    1.0000000

Direct3D:

1.0000000    0.00000000    0.00000000    0.00000000
0.00000000   0.54030228    0.84147096    0.00000000
0.00000000  -0.84147096    0.54030228    0.00000000
0.00000000   0.00000000    0.00000000    1.0000000

As you can see, the Direct3D matrix is exactly the transpose of my matrix class (my class gives the same results as the examples given by Wikipedia at the top of this question).

Upvotes: 2

Views: 1124

Answers (3)

Pete Kirkham
Pete Kirkham

Reputation: 49321

Given that the only terms off of the prime diagonal are of the form ±sin ( θ ) and that sin ( -θ ) = - sin ( θ ) but cos ( -θ ) = cos ( θ ), the relationship between Direct3D and the Wikipedia maths can also be seen as an opposite interpretation of the direction of the angle.

Upvotes: 1

timday
timday

Reputation: 24892

I've always traced this confusion back to the late 80s. As I remember it, there were two particularly influential books on graphics at the time. One of them wrote vectors as row vectors on the left of matrices; the book was quite hardware oriented - conceptually you thought of the vectors as flowing left-to-right through a pipeline of transform matrices. Rendermorphics (which was later picked up by Microsoft to become Direct3D) went down this route. The other book wrote vectors as column vectors on the right of matrices, which is the way OpenGL does it (and I think most mathematicians would naturally gravitate towards this, although I have met exceptions).

However, both approaches are entirely equally valid mathematics! If you're confused by Direct3D, start thinking in terms of writing row vectors on the left of matrices, not in terms of transposing the matrices.

You're not the first person to be confused by this (see also).

Upvotes: 4

CiscoIPPhone
CiscoIPPhone

Reputation: 9477

The DirectX matrix classes are an implementation of a mathematical matrix.

The handedness only exhibits itself when you do operations on it such as rotation. The transpose should give you the result you are looking for as long as the same operations are done on both the DIrectX matrix and your 'mathematical matrix'. My guess is that there's a difference in the implementations of rotation, or the order the rotations are being done differs.

Upvotes: 1

Related Questions