Skip to content

Commit b1cb7ec

Browse files
committed
Fix quat to mat4 for OpenGL style matrix.
1 parent 4e37f0c commit b1cb7ec

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

src/vmath.nim

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,25 +1764,26 @@ proc mat4*[T](q: GVec4[T]): GMat4[T] =
17641764
zw = q.z * q.w
17651765

17661766
result[0, 0] = 1 - 2 * (yy + zz)
1767-
result[0, 1] = 0 + 2 * (xy - zw)
1768-
result[0, 2] = 0 + 2 * (xz + yw)
1769-
result[0, 3] = 0
1767+
result[1, 0] = 0 + 2 * (xy - zw)
1768+
result[2, 0] = 0 + 2 * (xz + yw)
1769+
result[3, 0] = 0
17701770

1771-
result[1, 0] = 0 + 2 * (xy + zw)
1771+
result[0, 1] = 0 + 2 * (xy + zw)
17721772
result[1, 1] = 1 - 2 * (xx + zz)
1773-
result[1, 2] = 0 + 2 * (yz - xw)
1774-
result[1, 3] = 0
1775-
1776-
result[2, 0] = 0 + 2 * (xz - yw)
1777-
result[2, 1] = 0 + 2 * (yz + xw)
1778-
result[2, 2] = 1 - 2 * (xx + yy)
1779-
result[2, 3] = 0
1780-
1781-
result[3, 0] = 0
1773+
result[2, 1] = 0 + 2 * (yz - xw)
17821774
result[3, 1] = 0
1775+
1776+
result[0, 2] = 0 + 2 * (xz - yw)
1777+
result[1, 2] = 0 + 2 * (yz + xw)
1778+
result[2, 2] = 1 - 2 * (xx + yy)
17831779
result[3, 2] = 0
1780+
1781+
result[0, 3] = 0
1782+
result[1, 3] = 0
1783+
result[2, 3] = 0
17841784
result[3, 3] = 1.0
17851785

1786+
17861787
proc mat4*(m: DMat4): Mat4 {.inline.} =
17871788
## Convert a double precision matrix to a single precision matrix.
17881789
result[0, 0] = float32(m[0, 0])
@@ -1854,6 +1855,18 @@ proc quatMultiply*[T](a: GVec4[T], b: GVec4[T]): GVec4[T] =
18541855
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
18551856
)
18561857

1858+
proc toAngles*[T](m: GVec4[T]): GVec3[T] =
1859+
## Convert a quaternion to Euler angles.
1860+
let
1861+
x = m.x
1862+
y = m.y
1863+
z = m.z
1864+
w = m.w
1865+
1866+
result.x = arctan2(2 * (w * x + y * z), 1 - 2 * (x * x + y * y))
1867+
result.y = arcsin(2 * (w * y - z * x))
1868+
result.z = arctan2(2 * (w * z + x * y), 1 - 2 * (y * y + z * z))
1869+
18571870
when defined(release):
18581871
{.pop.}
18591872
{.pop.}

0 commit comments

Comments
 (0)