on dbfc352 help sign in
elk/libraries/Math/Matrix4.elm
190 lines 5.5 kB history raw
1module Math.Matrix where
3{-| A high performance linear algebra library using native JS arrays. Geared
4towards 3D graphics and use with `Graphics.WebGL`. All matrices are immutable.
6This library uses the convention that the prefix `make` is creating a new
7array, whereas without the prefix, you are applying some transform to an
8existing matrix.
10@docs mul4x4
12# Operations
14@docs identity, inverseOrthonormal, mul, mulAffine, transpose, makeBasis
16# Projections
18@docs makeFrustrum, makePerspective, makeOrtho, makeOrtho2D, makeLookAt
20# Transforms
22@docs makeRotate, rotate, makeScale3, makeScale, scaleM3, scaleM, makeTranslate3, makeTranslate, translate3, translate
24-}
26import Native.MJS
27import Math.Vector3 (Vec3)
29data Mat4 = Mat4
31{-| Multiply a vector by a 4x4 matrix: m * v
32-}
33mul4x4 : Mat4 -> Vec3 -> Vec3
34mul4x4 = Native.MJS.v3mul4x4
36{-| A matrix with all 0s, except 1s on the diagonal.
37-}
38identity : Mat4
39identity = Native.MJS.m4x4identity
41{-| Computes the inverse of the given matrix m, assuming that the matrix is
42orthonormal.
43-}
44inverseOrthonormal : Mat4 -> Mat4
45inverseOrthonormal = Native.MJS.m4x4inverseOrthonormal
47{-| Creates a matrix for a projection frustum with the given parameters.
49Parameters:
51 * left - the left coordinate of the frustum
52 * right- the right coordinate of the frustum
53 * bottom - the bottom coordinate of the frustum
54 * top - the top coordinate of the frustum
55 * znear - the near z distance of the frustum
56 * zfar - the far z distance of the frustum
57-}
58makeFrustrum : Float -> Float -> Float -> Float -> Float -> Float -> Float -> Mat4
59makeFrustrum = Native.MJS.m4x4makeFrustrum
61{-| Creates a matrix for a perspective projection with the given parameters.
63Parameters:
65 * fovy - field of view in the y axis, in degrees
66 * aspect - aspect ratio
67 * znear - the near z distance of the projection
68 * zfar - the far z distance of the projection
69-}
70makePerspective : Float -> Float -> Float -> Float -> Mat4
71makePerspective = Native.MJS.m4x4makePerspective
73{-|
74Creates a matrix for an orthogonal frustum projection with the given parameters.
76Parameters:
78 * left - the left coordinate of the frustum
79 * right- the right coordinate of the frustum
80 * bottom - the bottom coordinate of the frustum
81 * top - the top coordinate of the frustum
82 * znear - the near z distance of the frustum
83 * zfar - the far z distance of the frustum
84-}
85makeOrtho : Float -> Float -> Float -> Float -> Float -> Float -> Float -> Mat4
86makeOrtho = Native.MJS.m4x4makeOrtho
88{-| Creates a matrix for a 2D orthogonal frustum projection with the given
89parameters. `znear` and `zfar` are assumed to be -1 and 1, respectively.
91Parameters:
93 * left - the left coordinate of the frustum
94 * right- the right coordinate of the frustum
95 * bottom - the bottom coordinate of the frustum
96 * top - the top coordinate of the frustum
97-}
98makeOrtho2D : Float -> Float -> Float -> Float -> Mat4
99makeOrtho2D = Native.MJS.m4x4makeOrtho2D
101{-| Matrix multiplcation: a * b
102-}
103mul : Mat4 -> Mat4 -> Mat4
104mul = Native.MJS.m4x4mul
106{-| Matrix multiplication, assuming a and b are affine: a * b
107-}
108mulAffine : Mat4 -> Mat4 -> Mat4
109mulAffine = Native.MJS.m4x4mulAffine
111{-| Creates a transformation matrix for rotation in radians about the
1123-element vector axis.
113-}
114makeRotate : Float -> Vec3 -> Mat4
115makeRotate = Native.MJS.m4x4makeRotate
117{-| Concatenates a rotation of angle radians about the axis to the given matrix.
118-}
119rotate : Float -> Vec3 -> Mat4 -> Mat4
120rotate = Native.MJS.m4x4rotate
122{-| Creates a transformation matrix for scaling by 3 scalar values, one for
123each of the x, y, and z directions.
124-}
125makeScale3 : Float -> Float -> Float -> Mat4
126makeScale3 = Native.MJS.m4x4makeScale3
128{-| Creates a transformation matrix for scaling each of the x, y, and z axes by
129the amount given in the corresponding element of the 3-element vector.
130-}
131makeScale : Vec3 -> Mat4
132makeScale = Native.MJS.m4x4makeScale
134{-| Concatenates a scaling to the given matrix.
135-}
136scaleM3 : Float -> Float -> Float -> Mat4 -> Mat4
137scaleM3 = Native.MJS.m4x4scale3
139{-| Concatenates a scaling to the given matrix.
140-}
141scaleM : Vec3 -> Mat4 -> Mat4
142scaleM = Native.MJS.m4x4scale
144{-|
145Creates a transformation matrix for translating by 3 scalar values, one for
146each of the x, y, and z directions.
147-}
148makeTranslate3 : Float -> Float -> Float -> Mat4
149makeTranslate3 = Native.MJS.m4x4makeTranslate3
151{-| Creates a transformation matrix for translating each of the x, y, and z
152axes by the amount given in the corresponding element of the 3-element vector.
153-}
154makeTranslate : Vec3 -> Mat4
155makeTranslate = Native.MJS.m4x4makeTranslate
157{-|
158Concatenates a translation to the given matrix.
159-}
160translate3 : Float -> Float -> Float -> Mat4 -> Mat4
161translate3 = Native.MJS.m4x4translate3
163{-|
164Concatenates a translation to the given matrix.
165-}
166translate : Vec3 -> Mat4 -> Mat4
167translate = Native.MJS.m4x4translate
169{-|
170Creates a transformation matrix for a camera.
172Parameters:
174 * eye - The location of the camera
175 * center - The location of the focused object
176 * up - The "up" direction according to the camera
177-}
178makeLookAt : Vec3 -> Vec3 -> Vec3 -> Mat4
179makeLookAt = Native.MJS.m4x4makeLookAt
181{-| "Flip" the matrix across the diagonal by swapping row index and column
182index.
183-}
184transpose : Mat4 -> Mat4
185transpose = Native.MJS.m4x4transpose
187{-| Creates a transform from a basis consisting of 3 linearly independent vectors.
188-}
189makeBasis : Vec3 -> Vec3 -> Vec3 -> Mat4
190makeBasis = Native.MJS.m4x4makeBasis