From c6312ea0d66a8a9f052b0d11911dd15bb529d2d0 Mon Sep 17 00:00:00 2001 From: treeform Date: Sun, 23 Nov 2025 11:17:33 -0800 Subject: [PATCH] Add some missing converters and quat operations. --- src/vmath.nim | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/src/vmath.nim b/src/vmath.nim index fe8dd23..3902f59 100644 --- a/src/vmath.nim +++ b/src/vmath.nim @@ -1783,10 +1783,77 @@ proc mat4*[T](q: GVec4[T]): GMat4[T] = result[3, 2] = 0 result[3, 3] = 1.0 +proc mat4*(m: DMat4): Mat4 {.inline.} = + ## Convert a double precision matrix to a single precision matrix. + result[0, 0] = float32(m[0, 0]) + result[0, 1] = float32(m[0, 1]) + result[0, 2] = float32(m[0, 2]) + result[0, 3] = float32(m[0, 3]) + result[1, 0] = float32(m[1, 0]) + result[1, 1] = float32(m[1, 1]) + result[1, 2] = float32(m[1, 2]) + result[1, 3] = float32(m[1, 3]) + result[2, 0] = float32(m[2, 0]) + result[2, 1] = float32(m[2, 1]) + result[2, 2] = float32(m[2, 2]) + result[2, 3] = float32(m[2, 3]) + result[3, 0] = float32(m[3, 0]) + result[3, 1] = float32(m[3, 1]) + result[3, 2] = float32(m[3, 2]) + result[3, 3] = float32(m[3, 3]) + +proc mat4*(m: Mat4): Mat4 {.inline.} = + ## Convert a double precision matrix to a single precision matrix. + return m + +proc dmat4*(m: Mat4): DMat4 {.inline.} = + ## Convert a single precision matrix to a double precision matrix. + result[0, 0] = float64(m[0, 0]) + result[0, 1] = float64(m[0, 1]) + result[0, 2] = float64(m[0, 2]) + result[0, 3] = float64(m[0, 3]) + result[1, 0] = float64(m[1, 0]) + result[1, 1] = float64(m[1, 1]) + result[1, 2] = float64(m[1, 2]) + result[1, 3] = float64(m[1, 3]) + result[2, 0] = float64(m[2, 0]) + result[2, 1] = float64(m[2, 1]) + result[2, 2] = float64(m[2, 2]) + result[2, 3] = float64(m[2, 3]) + result[3, 0] = float64(m[3, 0]) + result[3, 1] = float64(m[3, 1]) + result[3, 2] = float64(m[3, 2]) + result[3, 3] = float64(m[3, 3]) + +proc dmat4*(m: DMat4): DMat4 {.inline.} = + ## Convert a double precision matrix to a double precision matrix. + return m + proc rotate*[T](angle: T, axis: GVec3[T]): GMat4[T] = ## Return a rotation matrix with axis and angle. fromAxisAngle(axis, angle).mat4() +proc quatRotateX*[T](angle: T): GVec4[T] = + ## Return a quaternion that would rotate around the X axis. + fromAxisAngle(gvec3[T](1, 0, 0), angle) + +proc quatRotateY*[T](angle: T): GVec4[T] = + ## Return a quaternion that would rotate around the Y axis. + fromAxisAngle(gvec3[T](0, 1, 0), angle) + +proc quatRotateZ*[T](angle: T): GVec4[T] = + ## Return a quaternion that would rotate around the Z axis. + fromAxisAngle(gvec3[T](0, 0, 1), angle) + +proc quatMultiply*[T](a: GVec4[T], b: GVec4[T]): GVec4[T] = + ## Return the product of two quaternions. + gvec4[T]( + a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y, + a.w * b.y + a.y * b.w + a.z * b.x - a.x * b.z, + a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x, + a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z + ) + when defined(release): {.pop.} {.pop.}