Skip to content

Commit 4e37f0c

Browse files
authored
Merge pull request #79 from treeform/missing
Add some missing converters and quat operations.
2 parents ddfcd73 + c6312ea commit 4e37f0c

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

src/vmath.nim

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,10 +1783,77 @@ proc mat4*[T](q: GVec4[T]): GMat4[T] =
17831783
result[3, 2] = 0
17841784
result[3, 3] = 1.0
17851785

1786+
proc mat4*(m: DMat4): Mat4 {.inline.} =
1787+
## Convert a double precision matrix to a single precision matrix.
1788+
result[0, 0] = float32(m[0, 0])
1789+
result[0, 1] = float32(m[0, 1])
1790+
result[0, 2] = float32(m[0, 2])
1791+
result[0, 3] = float32(m[0, 3])
1792+
result[1, 0] = float32(m[1, 0])
1793+
result[1, 1] = float32(m[1, 1])
1794+
result[1, 2] = float32(m[1, 2])
1795+
result[1, 3] = float32(m[1, 3])
1796+
result[2, 0] = float32(m[2, 0])
1797+
result[2, 1] = float32(m[2, 1])
1798+
result[2, 2] = float32(m[2, 2])
1799+
result[2, 3] = float32(m[2, 3])
1800+
result[3, 0] = float32(m[3, 0])
1801+
result[3, 1] = float32(m[3, 1])
1802+
result[3, 2] = float32(m[3, 2])
1803+
result[3, 3] = float32(m[3, 3])
1804+
1805+
proc mat4*(m: Mat4): Mat4 {.inline.} =
1806+
## Convert a double precision matrix to a single precision matrix.
1807+
return m
1808+
1809+
proc dmat4*(m: Mat4): DMat4 {.inline.} =
1810+
## Convert a single precision matrix to a double precision matrix.
1811+
result[0, 0] = float64(m[0, 0])
1812+
result[0, 1] = float64(m[0, 1])
1813+
result[0, 2] = float64(m[0, 2])
1814+
result[0, 3] = float64(m[0, 3])
1815+
result[1, 0] = float64(m[1, 0])
1816+
result[1, 1] = float64(m[1, 1])
1817+
result[1, 2] = float64(m[1, 2])
1818+
result[1, 3] = float64(m[1, 3])
1819+
result[2, 0] = float64(m[2, 0])
1820+
result[2, 1] = float64(m[2, 1])
1821+
result[2, 2] = float64(m[2, 2])
1822+
result[2, 3] = float64(m[2, 3])
1823+
result[3, 0] = float64(m[3, 0])
1824+
result[3, 1] = float64(m[3, 1])
1825+
result[3, 2] = float64(m[3, 2])
1826+
result[3, 3] = float64(m[3, 3])
1827+
1828+
proc dmat4*(m: DMat4): DMat4 {.inline.} =
1829+
## Convert a double precision matrix to a double precision matrix.
1830+
return m
1831+
17861832
proc rotate*[T](angle: T, axis: GVec3[T]): GMat4[T] =
17871833
## Return a rotation matrix with axis and angle.
17881834
fromAxisAngle(axis, angle).mat4()
17891835

1836+
proc quatRotateX*[T](angle: T): GVec4[T] =
1837+
## Return a quaternion that would rotate around the X axis.
1838+
fromAxisAngle(gvec3[T](1, 0, 0), angle)
1839+
1840+
proc quatRotateY*[T](angle: T): GVec4[T] =
1841+
## Return a quaternion that would rotate around the Y axis.
1842+
fromAxisAngle(gvec3[T](0, 1, 0), angle)
1843+
1844+
proc quatRotateZ*[T](angle: T): GVec4[T] =
1845+
## Return a quaternion that would rotate around the Z axis.
1846+
fromAxisAngle(gvec3[T](0, 0, 1), angle)
1847+
1848+
proc quatMultiply*[T](a: GVec4[T], b: GVec4[T]): GVec4[T] =
1849+
## Return the product of two quaternions.
1850+
gvec4[T](
1851+
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
1852+
a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z,
1853+
a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x,
1854+
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z
1855+
)
1856+
17901857
when defined(release):
17911858
{.pop.}
17921859
{.pop.}

0 commit comments

Comments
 (0)