{"id":60,"date":"2013-11-08T15:46:23","date_gmt":"2013-11-08T15:46:23","guid":{"rendered":"\/\/3dbym.ru\/2013\/11\/converting-between-matrices-and-quaternions\/"},"modified":"2013-11-08T15:46:23","modified_gmt":"2013-11-08T15:46:23","slug":"converting-between-matrices-and-quaternions","status":"publish","type":"post","link":"https:\/\/3dbym.ru\/2013\/11\/converting-between-matrices-and-quaternions\/","title":{"rendered":"Converting Between Matrices and Quaternions"},"content":{"rendered":"
One of the most important aspects of quaternions is their ability to be converted into rotation matrices. This is useful because graphics APIs such as OpenGL and Direct3D cannot accept quaternions directly rather, they rely on matrices to perform transformations. Because of this, without being able to convert between quaternions, your graphics would become useless because you would not be able to use the infor\u00admation they contain along with OpenGL or other graphics APIs. For example, if you have two sets of rotations for an object, the first set is<\/p>\n
<\/p>\n
the starting rotation and the second is the ending rotation. Without being able to convert these matrices to quaternions, it would be impossible to take advantage of the ease of interpolation between quaternions. Keep in mind that all matrices will be referenced in the manner mij where i is the row and j is the column.<\/p>\n
This section first investigates how to convert a quaternion to a matrix. Because there is much more to cover in the later chapters of the book, there is not room to go through the \u201cwhys\u201d of the conversion. Instead, this section looks at the formula for the conversion. Figure 2.5 shows how to generate a 3×3 rotation matrix from a quaternion. Keep in mind that when writing code to do the conversion, it is helpful to calculate some of the multiplication ahead of time to avoid unnecessary overhead.<\/p>\n
w2<\/a> + x2 \u2014 y2 \u2014 z2 2xy-2wz 2xz+2yn<\/p>\n
\n<\/td>\n<\/tr>\n<\/table>\n <\/p>\n If it is less than or equal to zero, you have to take a different approach that depends on which element of the major diagonal is the largest. If the upper-left corner is the largest, you use this formula:<\/p>\n temp =———- 1<\/p>\n 2\/l + — m22 + m33<\/p>\n 0.25<\/p>\n Qw —<\/p>\n temp<\/p>\n qx = (m2, + m12) x temp qy = (m13 + m31) x temp qz = (m32 — m23) x temp<\/p>\n If the middle element outshines the others, use this formula: 1<\/p>\n temp =<\/p>\n ff?22 \u2014 \/\u041f33<\/p>\n Qw = (\/\u0442\u0442\u0433\u0433 + m12) x temp 0.25<\/p>\n Qx =<\/p>\n temp<\/p>\n Qy = (m32 + m23) x temp qz=(m13-m31)xtemp<\/p>\n And finally, if the lower-right corner takes the cake, use this one: temp = 1<\/p>\n + \u0413\u041f33 \/77 \/ \u0443 \u041b?22<\/p>\n Qw= (m13+ m31)xtemp<\/p>\n Qx = (m32 + m23) x temp<\/p>\n _ 0.25 temp<\/p>\n Qz = (m2) — m12) x temp<\/p>\n <\/p>\n
|