-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrices.elm
73 lines (62 loc) · 2.04 KB
/
Matrices.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
module Matrices exposing (..)
import Math.Vector4 exposing (vec4,Vec4)
import Math.Matrix4 exposing (..)
-- 4D Rotation Matrices
fromTuples : ((Float,Float,Float,Float),(Float,Float,Float,Float),(Float,Float,Float,Float),(Float,Float,Float,Float)) -> Mat4
fromTuples ((a,b,c,d),(e,f,g,h),(i,j,k,l),(m,n,o,p)) = fromRecord
{ m11 = a, m21 = b, m31 = c, m41 = d
, m12 = e, m22 = f, m32 = g, m42 = h
, m13 = i, m23 = j, m33 = k, m43 = l
, m14 = m, m24 = n, m34 = o, m44 = p
}
moveForward : Float -> Mat4
moveForward theta =
fromTuples
((1,0, 0, 0 )
,(0,1, 0, 0 )
,(0,0,cos theta,-(sin theta))
,(0,0,sin theta, cos theta ))
moveRight : Float -> Mat4
moveRight theta =
fromTuples
(( cos theta,0,0,sin theta)
,( 0,1,0, 0)
,( 0,0,1, 0)
,(-(sin theta),0,0,cos theta))
moveUp : Float -> Mat4
moveUp theta =
fromTuples
((1, 0,0, 0)
,(0, cos theta,0,sin theta)
,(0, 0,1, 0)
,(0,-(sin theta),0,cos theta))
turnRight : Float -> Mat4
turnRight theta =
fromTuples
((cos theta,0,-(sin theta),0)
,( 0,1, 0 ,0)
,(sin theta,0, cos theta ,0)
,( 0,0, 0 ,1))
turnUp : Float -> Mat4
turnUp theta =
fromTuples
((1, 0, 0 ,0)
,(0,cos theta,-(sin theta),0)
,(0,sin theta, cos theta ,0)
,(0, 0, 0 ,1))
spin : Float -> Mat4
spin theta =
fromTuples
((cos theta,-(sin theta),0,0)
,(sin theta, cos theta ,0,0)
,( 0, 0 ,1,0)
,( 0, 0 ,0,1))
transform4 : Mat4 -> Vec4 -> Vec4
transform4 m v =
let {m11,m21,m31,m41,m12,m22,m32,m42,m13,m23,m33,m43,m14,m24,m34,m44} = toRecord m
{x,y,z,w} = Math.Vector4.toRecord v
in vec4
(m11*x+m21*y+m31*z+m41*w)
(m12*x+m22*y+m32*z+m42*w)
(m13*x+m23*y+m33*z+m43*w)
(m14*x+m24*y+m34*z+m44*w)