Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/vmath.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.}