aboutsummaryrefslogtreecommitdiff
path: root/Spear/Math/Matrix4.hs
blob: 41bfadd3a1fee21e07fbb8c31b9c861cda6db5a8 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
module Spear.Math.Matrix4
(
    Matrix4
    -- * Accessors
,   m00, m01, m02, m03
,   m10, m11, m12, m13
,   m20, m21, m22, m23
,   m30, m31, m32, m33
,   col0, col1, col2, col3
,   row0, row1, row2, row3
,   right, up, forward, position
    -- * Construction
,   mat4
,   mat4fromVec
,   transform
,   translation
,   rotation
,   lookAt
,   Spear.Math.Matrix4.id
    -- * Transformations
    -- ** Translation
,   transl
,   translv
    -- ** Rotation
,   rotX
,   rotY
,   rotZ
,   axisAngle
    -- ** Scale
,   Spear.Math.Matrix4.scale
,   scalev
    -- ** Reflection
,   reflectX
,   reflectY
,   reflectZ
    -- ** Projection
,   ortho
,   perspective
,   planeProj
    -- * Operations
,   Spear.Math.Matrix4.zipWith
,   Spear.Math.Matrix4.map
,   transpose
,   inverseTransform
,   inverse
,   mul
,   mulp
,   muld
,   mul'
)
where


import Spear.Math.Vector3 as V3
import Spear.Math.Vector4 as V4

import Foreign.Storable


-- | Represents a 4x4 column major matrix.
data Matrix4 = Matrix4
    { m00 :: {-# UNPACK #-} !Float, m10 :: {-# UNPACK #-} !Float, m20 :: {-# UNPACK #-} !Float, m30 :: {-# UNPACK #-} !Float
    , m01 :: {-# UNPACK #-} !Float, m11 :: {-# UNPACK #-} !Float, m21 :: {-# UNPACK #-} !Float, m31 :: {-# UNPACK #-} !Float
    , m02 :: {-# UNPACK #-} !Float, m12 :: {-# UNPACK #-} !Float, m22 :: {-# UNPACK #-} !Float, m32 :: {-# UNPACK #-} !Float
    , m03 :: {-# UNPACK #-} !Float, m13 :: {-# UNPACK #-} !Float, m23 :: {-# UNPACK #-} !Float, m33 :: {-# UNPACK #-} !Float
    }


instance Show Matrix4 where
    
    show (Matrix4 m00 m10 m20 m30 m01 m11 m21 m31 m02 m12 m22 m32 m03 m13 m23 m33) =
        show' m00 ++ ", " ++ show' m10 ++ ", " ++ show' m20 ++ ", " ++ show' m30 ++ "\n" ++
        show' m01 ++ ", " ++ show' m11 ++ ", " ++ show' m21 ++ ", " ++ show' m31 ++ "\n" ++
        show' m02 ++ ", " ++ show' m12 ++ ", " ++ show' m22 ++ ", " ++ show' m32 ++ "\n" ++
        show' m03 ++ ", " ++ show' m13 ++ ", " ++ show' m23 ++ ", " ++ show' m33 ++ "\n"
        where
            show' f = if abs f < 0.0000001 then "0" else show f


instance Num Matrix4 where
    (Matrix4 a00 a01 a02 a03 a04 a05 a06 a07 a08 a09 a10 a11 a12 a13 a14 a15)
        + (Matrix4 b00 b01 b02 b03 b04 b05 b06 b07 b08 b09 b10 b11 b12 b13 b14 b15)
            = Matrix4 (a00 + b00) (a01 + b01) (a02 + b02) (a03 + b03)
                      (a04 + b04) (a05 + b05) (a06 + b06) (a07 + b07)
                      (a08 + b08) (a09 + b09) (a10 + b10) (a11 + b11)
                      (a12 + b12) (a13 + b13) (a14 + b14) (a15 + b15)
    
    (Matrix4 a00 a01 a02 a03 a04 a05 a06 a07 a08 a09 a10 a11 a12 a13 a14 a15)
        - (Matrix4 b00 b01 b02 b03 b04 b05 b06 b07 b08 b09 b10 b11 b12 b13 b14 b15)
            = Matrix4 (a00 - b00) (a01 - b01) (a02 - b02) (a03 - b03)
                      (a04 - b04) (a05 - b05) (a06 - b06) (a07 - b07)
                      (a08 - b08) (a09 - b09) (a10 - b10) (a11 - b11)
                      (a12 - b12) (a13 - b13) (a14 - b14) (a15 - b15)
    
    (Matrix4 a00 a10 a20 a30 a01 a11 a21 a31 a02 a12 a22 a32 a03 a13 a23 a33)
        * (Matrix4 b00 b10 b20 b30 b01 b11 b21 b31 b02 b12 b22 b32 b03 b13 b23 b33)
            = Matrix4 (a00 * b00 + a10 * b01 + a20 * b02 + a30 * b03)
                      (a00 * b10 + a10 * b11 + a20 * b12 + a30 * b13)
                      (a00 * b20 + a10 * b21 + a20 * b22 + a30 * b23)
                      (a00 * b30 + a10 * b31 + a20 * b32 + a30 * b33)
                      
                      (a01 * b00 + a11 * b01 + a21 * b02 + a31 * b03)
                      (a01 * b10 + a11 * b11 + a21 * b12 + a31 * b13)
                      (a01 * b20 + a11 * b21 + a21 * b22 + a31 * b23)
                      (a01 * b30 + a11 * b31 + a21 * b32 + a31 * b33)
                      
                      (a02 * b00 + a12 * b01 + a22 * b02 + a32 * b03)
                      (a02 * b10 + a12 * b11 + a22 * b12 + a32 * b13)
                      (a02 * b20 + a12 * b21 + a22 * b22 + a32 * b23)
                      (a02 * b30 + a12 * b31 + a22 * b32 + a32 * b33)
                      
                      (a03 * b00 + a13 * b01 + a23 * b02 + a33 * b03)
                      (a03 * b10 + a13 * b11 + a23 * b12 + a33 * b13)
                      (a03 * b20 + a13 * b21 + a23 * b22 + a33 * b23)
                      (a03 * b30 + a13 * b31 + a23 * b32 + a33 * b33)
    
    abs = Spear.Math.Matrix4.map abs
    
    signum = Spear.Math.Matrix4.map signum
    
    fromInteger i = mat4 i' i' i' i' i' i' i' i' i' i' i' i' i' i' i' i' where i' = fromInteger i
    
    
instance Storable Matrix4 where
    sizeOf    _ = 64
    alignment _ = 4
    
    peek ptr = do
        a00 <- peekByteOff ptr 0;  a01 <- peekByteOff ptr 4;  a02 <- peekByteOff ptr 8;  a03 <- peekByteOff ptr 12;
        a10 <- peekByteOff ptr 16; a11 <- peekByteOff ptr 20; a12 <- peekByteOff ptr 24; a13 <- peekByteOff ptr 28;
        a20 <- peekByteOff ptr 32; a21 <- peekByteOff ptr 36; a22 <- peekByteOff ptr 40; a23 <- peekByteOff ptr 44;
        a30 <- peekByteOff ptr 48; a31 <- peekByteOff ptr 52; a32 <- peekByteOff ptr 56; a33 <- peekByteOff ptr 60;
        
        return $ Matrix4 a00 a10 a20 a30
                         a01 a11 a21 a31
                         a02 a12 a22 a32
                         a03 a13 a23 a33
    
    poke ptr (Matrix4 a00 a10 a20 a30
                      a01 a11 a21 a31
                      a02 a12 a22 a32
                      a03 a13 a23 a33) = do
        pokeByteOff ptr 0  a00; pokeByteOff ptr 4  a01; pokeByteOff ptr 8  a02; pokeByteOff ptr 12 a03;
        pokeByteOff ptr 16 a10; pokeByteOff ptr 20 a11; pokeByteOff ptr 24 a12; pokeByteOff ptr 28 a13;
        pokeByteOff ptr 32 a20; pokeByteOff ptr 36 a21; pokeByteOff ptr 40 a22; pokeByteOff ptr 44 a23;
        pokeByteOff ptr 48 a30; pokeByteOff ptr 52 a31; pokeByteOff ptr 56 a32; pokeByteOff ptr 60 a33;


col0 (Matrix4 a00 _   _   _   a01 _   _   _   a02 _   _   _   a03 _   _   _  ) = vec4 a00 a01 a02 a03
col1 (Matrix4 _   a10 _   _   _   a11 _   _   _   a12 _   _   _   a13 _   _  ) = vec4 a10 a11 a12 a13
col2 (Matrix4 _   _   a20 _   _   _   a21 _   _   _   a22 _   _   _   a23 _  ) = vec4 a20 a21 a22 a23
col3 (Matrix4 _   _   _   a30 _   _   _   a31 _   _   _   a32 _   _   _   a33) = vec4 a30 a31 a32 a33


row0 (Matrix4 a00 a01 a02 a03 _   _   _   _   _   _   _   _   _   _   _   _  ) = vec4 a00 a01 a02 a03
row1 (Matrix4 _   _   _   _   a10 a11 a12 a13 _   _   _   _   _   _   _   _  ) = vec4 a10 a11 a12 a13
row2 (Matrix4 _   _   _   _   _   _   _   _   a20 a21 a22 a23 _   _   _   _  ) = vec4 a20 a21 a22 a23
row3 (Matrix4 _   _   _   _   _   _   _   _   _   _   _   _   a30 a31 a32 a33) = vec4 a30 a31 a32 a33


right    (Matrix4 a00 _   _   _   a01 _   _   _   a02 _   _   _   _   _   _   _) = vec3 a00 a01 a02
up       (Matrix4 _   a10 _   _   _   a11 _   _   _   a12 _   _   _   _   _   _) = vec3 a10 a11 a12
forward  (Matrix4 _   _   a20 _   _   _   a21 _   _   _   a22 _   _   _   _   _) = vec3 a20 a21 a22
position (Matrix4 _   _   _   a30 _   _   _   a31 _   _   _   a32 _   _   _   _) = vec3 a30 a31 a32


-- | Build a matrix from the specified values.
mat4 = Matrix4


-- | Build a matrix from four vectors in 4D.
mat4fromVec :: Vector4 -> Vector4 -> Vector4 -> Vector4 -> Matrix4
mat4fromVec v0 v1 v2 v3 = Matrix4
    (V4.x v0) (V4.x v1) (V4.x v2) (V4.x v3)
    (V4.y v0) (V4.y v1) (V4.y v2) (V4.y v3)
    (V4.z v0) (V4.z v1) (V4.z v2) (V4.z v3)
    (V4.w v0) (V4.w v1) (V4.w v2) (V4.w v3)


-- | Build a transformation 'Matrix4' from the given vectors.
transform :: Vector3 -- ^ Right vector.
          -> Vector3 -- ^ Up vector.
          -> Vector3 -- ^ Forward vector.
          -> Vector3 -- ^ Position.
          -> Matrix4

transform right up fwd pos = mat4
    (V3.x right) (V3.x up) (V3.x fwd) (V3.x pos)
    (V3.y right) (V3.y up) (V3.y fwd) (V3.y pos)
    (V3.z right) (V3.z up) (V3.z fwd) (V3.z pos)
    0                 0              0               1


-- | Get the translation part of the given transformation matrix.
translation :: Matrix4 -> Matrix4
translation (Matrix4
    a00 a10 a20 a30
    a01 a11 a21 a31
    a02 a12 a22 a32
    a03 a13 a23 a33)
    = mat4
    1   0   0   a30
    0   1   0   a31
    0   0   1   a32
    0   0   0   a33    


-- | Get the rotation part of the given transformation matrix.
rotation :: Matrix4 -> Matrix4
rotation (Matrix4
    a00 a10 a20 a30
    a01 a11 a21 a31
    a02 a12 a22 a32
    a03 a13 a23 a33)
    = mat4
    a00 a10 a20 0
    a01 a11 a21 0
    a02 a12 a22 0
    a03 a13 a23 1    


-- | Build a transformation 'Matrix4' defined by the given position and target.
lookAt :: Vector3 -- ^ Eye position.
       -> Vector3 -- ^ Target point.
       -> Matrix4

lookAt pos target =
        let fwd = V3.normalise $ target - pos
            r    = fwd `cross` V3.unity
            u    = r `cross` fwd
        in
            transform r u (-fwd) pos


-- | Zip two matrices together with the specified function.
zipWith :: (Float -> Float -> Float) -> Matrix4 -> Matrix4 -> Matrix4
zipWith f a b = Matrix4
    (f (m00 a) (m00 b)) (f (m10 a) (m10 b)) (f (m20 a) (m20 b)) (f (m30 a) (m30 b))
    (f (m01 a) (m01 b)) (f (m11 a) (m11 b)) (f (m21 a) (m21 b)) (f (m31 a) (m31 b))
    (f (m02 a) (m02 b)) (f (m12 a) (m12 b)) (f (m22 a) (m22 b)) (f (m32 a) (m32 b))
    (f (m03 a) (m03 b)) (f (m13 a) (m13 b)) (f (m23 a) (m23 b)) (f (m33 a) (m33 b))


-- | Map the specified function to the specified matrix.
map :: (Float -> Float) -> Matrix4 -> Matrix4
map f m = Matrix4
    (f . m00 $ m) (f . m10 $ m) (f . m20 $ m) (f . m30 $ m)
    (f . m01 $ m) (f . m11 $ m) (f . m21 $ m) (f . m31 $ m)
    (f . m02 $ m) (f . m12 $ m) (f . m22 $ m) (f . m32 $ m)
    (f . m03 $ m) (f . m13 $ m) (f . m23 $ m) (f . m33 $ m)


-- | Return the identity matrix.
id :: Matrix4
id = mat4
    1   0   0   0
    0   1   0   0
    0   0   1   0
    0   0   0   1


-- | Create a translation matrix.
transl :: Float -> Float -> Float -> Matrix4
transl x y z = mat4
    1   0   0   x
    0   1   0   y
    0   0   1   z
    0   0   0   1


-- | Create a translation matrix.
translv :: Vector3 -> Matrix4
translv v = mat4
    1    0    0    (V3.x v)
    0    1    0    (V3.y v)
    0    0    1    (V3.z v)
    0    0    0    1


-- | Create a rotation matrix rotating about the X axis.
-- The given angle must be in degrees.
rotX :: Float -> Matrix4
rotX angle = mat4
    1    0    0    0
    0    c    (-s) 0
    0    s    c    0
    0    0    0    1
    where
        s = sin . toRAD $ angle
        c = cos . toRAD $ angle


-- | Create a rotation matrix rotating about the Y axis.
-- The given angle must be in degrees.
rotY :: Float -> Matrix4
rotY angle = mat4
    c    0    s    0
    0    1    0    0
    (-s) 0    c    0
    0    0    0    1
    where
        s = sin . toRAD $ angle
        c = cos . toRAD $ angle


-- | Create a rotation matrix rotating about the Z axis.
-- The given angle must be in degrees.
rotZ :: Float -> Matrix4
rotZ angle = mat4
    c    (-s) 0    0
    s    c    0    0
    0    0    1    0
    0    0    0    1
    where
        s = sin . toRAD $ angle
        c = cos . toRAD $ angle


-- | Create a rotation matrix rotating about the specified axis.
-- The given angle must be in degrees.
axisAngle :: Vector3 -> Float -> Matrix4
axisAngle v angle = mat4
    (c + omc*x^2) (omc*xy-sz) (omc*xz+sy) 0
    (omc*xy+sz)   (c+omc*y^2) (omc*yz-sx) 0
    (omc*xz-sy)   (omc*yz+sx) (c+omc*z^2) 0
     0             0           0          1
    where
        x = V3.x v
        y = V3.y v
        z = V3.z v
        s   = sin . toRAD $ angle
        c   = cos . toRAD $ angle
        xy  = x*y
        xz  = x*z
        yz  = y*z
        sx  = s*x
        sy  = s*y
        sz  = s*z
        omc = 1 - c


-- | Create a scale matrix.
scale :: Float -> Float -> Float -> Matrix4
scale sx sy sz = mat4
    sx  0   0   0
    0   sy  0   0
    0   0   sz  0
    0   0   0   1
    
    
-- | Create a scale matrix.
scalev :: Vector3 -> Matrix4
scalev v = mat4
    sx  0   0   0
    0   sy  0   0
    0   0   sz  0
    0   0   0   1
        where
            sx = V3.x v
            sy = V3.y v
            sz = V3.z v


-- | Create an X reflection matrix.
reflectX :: Matrix4
reflectX = mat4
    (-1)  0   0   0
    0     1   0   0
    0     0   1   0
    0     0   0   1


-- | Create a Y reflection matrix.
reflectY :: Matrix4
reflectY = mat4
    1   0     0   0
    0   (-1)  0   0
    0   0     1   0
    0   0     0   1


-- | Create a Z reflection matrix.
reflectZ :: Matrix4
reflectZ = mat4
    1   0   0     0
    0   1   0     0
    0   0   (-1)  0
    0   0   0     1


-- | Create an orthogonal projection matrix.
ortho :: Float -- ^ Left.
      -> Float -- ^ Right.
      -> Float -- ^ Bottom.
      -> Float -- ^ Top.
      -> Float -- ^ Near clip.
      -> Float -- ^ Far clip.
      -> Matrix4

ortho l r b t n f =
    let tx = (-(r+l)/(r-l))
        ty = (-(t+b)/(t-b))
        tz = (-(f+n)/(f-n))
    in mat4
        (2/(r-l)) 0         0            tx
        0         (2/(t-b)) 0            ty
        0         0         ((-2)/(f-n)) tz
        0         0         0            1


-- | Create a perspective projection matrix.
perspective :: Float -- ^ Fovy - Vertical field of view angle in degrees.
            -> Float -- ^ Aspect ratio.
            -> Float -- ^ Near clip distance.
            -> Float -- ^ Far clip distance
            -> Matrix4
perspective fovy r near far =
    let f = 1 / tan (toRAD fovy / 2)
        a = near - far
    in mat4
        (f/r) 0    0              0
        0     f    0              0
        0     0    ((near+far)/a) (2*near*far/a)
        0     0    (-1)           0


-- | Create a plane projection matrix.
planeProj :: Vector3 -- ^ Plane normal
          -> Float   -- ^ Plane distance from the origin
          -> Vector3 -- ^ Projection direction
          -> Matrix4
planeProj n d l =
    let c = n `V3.dot` l
        nx = V3.x n
        ny = V3.y n
        nz = V3.z n
        lx = V3.x l
        ly = V3.y l
        lz = V3.z l
    in mat4
        (d + c - nx*lx) (-ny*lx)          (-nz*lx)        (-lx*d)
        (-nx*ly)        (d + c - ny*ly)   (-nz*ly)        (-ly*d)
        (-nx*lz)        (-ny*lz)          (d + c - nz*lz) (-lz*d)
        (-nx)           (-ny)             (-nz)           c


-- | Transpose the specified matrix.
transpose :: Matrix4 -> Matrix4
transpose m = mat4
    (m00 m) (m01 m) (m02 m) (m03 m)
    (m10 m) (m11 m) (m12 m) (m13 m)
    (m20 m) (m21 m) (m22 m) (m23 m)
    (m30 m) (m31 m) (m32 m) (m33 m)


-- | Invert the given transformation matrix.
inverseTransform :: Matrix4 -> Matrix4
inverseTransform mat =
    let
        r = right mat
        u = up mat
        f = forward mat
        t = position mat
    in
        mat4
            (V3.x r) (V3.y r) (V3.z r) (-t `V3.dot` r)
            (V3.x u) (V3.y u) (V3.z u) (-t `V3.dot` u)
            (V3.x f) (V3.y f) (V3.z f) (-t `V3.dot` f)
            0        0        0        1


-- | Invert the given matrix.
inverse :: Matrix4 -> Matrix4
inverse mat = 
    let
        a00 = m00 mat
        a01 = m01 mat
        a02 = m02 mat
        a03 = m03 mat
        a04 = m10 mat
        a05 = m11 mat
        a06 = m12 mat
        a07 = m13 mat
        a08 = m20 mat
        a09 = m21 mat
        a10 = m22 mat
        a11 = m23 mat
        a12 = m30 mat
        a13 = m31 mat
        a14 = m32 mat
        a15 = m33 mat
        
        m00' = a05 * a10  * a15
             - a05 * a11  * a14
             - a09 * a06  * a15
             + a09 * a07  * a14
             + a13 * a06  * a11
             - a13 * a07  * a10
        
        m04' = -a04 * a10 * a15
             +  a04 * a11 * a14
             +  a08 * a06 * a15
             -  a08 * a07 * a14
             -  a12 * a06 * a11
             +  a12 * a07 * a10

        m08' = a04 * a09 * a15
             - a04 * a11 * a13
             - a08 * a05 * a15
             + a08 * a07 * a13
             + a12 * a05 * a11
             - a12 * a07 * a09

        m12' = -a04 * a09 * a14
             +  a04 * a10 * a13
             +  a08 * a05 * a14
             -  a08 * a06 * a13
             -  a12 * a05 * a10
             +  a12 * a06 * a09

        m01' = -a01 * a10 * a15
             +  a01 * a11 * a14
             +  a09 * a02 * a15
             -  a09 * a03 * a14
             -  a13 * a02 * a11
             +  a13 * a03 * a10

        m05' = a00 * a10 * a15
             - a00 * a11 * a14
             - a08 * a02 * a15
             + a08 * a03 * a14
             + a12 * a02 * a11
             - a12 * a03 * a10
        
        m09' = -a00 * a09 * a15
             +  a00 * a11 * a13
             +  a08 * a01 * a15
             -  a08 * a03 * a13
             -  a12 * a01 * a11
             +  a12 * a03 * a09

        m13' = a00 * a09 * a14
             - a00 * a10 * a13
             - a08 * a01 * a14
             + a08 * a02 * a13
             + a12 * a01 * a10
             - a12 * a02 * a09

        m02' = a01 * a06 * a15
             - a01 * a07 * a14
             - a05 * a02 * a15
             + a05 * a03 * a14
             + a13 * a02 * a07
             - a13 * a03 * a06

        m06' = -a00 * a06 * a15
             +  a00 * a07 * a14
             +  a04 * a02 * a15
             -  a04 * a03 * a14
             -  a12 * a02 * a07
             +  a12 * a03 * a06

        m10' = a00 * a05 * a15
             - a00 * a07 * a13
             - a04 * a01 * a15
             + a04 * a03 * a13
             + a12 * a01 * a07
             - a12 * a03 * a05

        m14' = -a00 * a05 * a14
             +  a00 * a06 * a13
             +  a04 * a01 * a14
             -  a04 * a02 * a13
             -  a12 * a01 * a06
             +  a12 * a02 * a05

        m03' = -a01 * a06 * a11
             +  a01 * a07 * a10
             +  a05 * a02 * a11
             -  a05 * a03 * a10
             -  a09 * a02 * a07
             +  a09 * a03 * a06

        m07' = a00 * a06 * a11
             - a00 * a07 * a10
             - a04 * a02 * a11
             + a04 * a03 * a10
             + a08 * a02 * a07
             - a08 * a03 * a06

        m11' = -a00 * a05 * a11
             +  a00 * a07 * a09
             +  a04 * a01 * a11
             -  a04 * a03 * a09
             -  a08 * a01 * a07
             +  a08 * a03 * a05

        m15' = a00 * a05 * a10
             - a00 * a06 * a09
             - a04 * a01 * a10
             + a04 * a02 * a09
             + a08 * a01 * a06
             - a08 * a02 * a05
        
        det' = a00 * m00' + a01 * m04' + a02 * m08' + a03 * m12'
    in
        if det' == 0 then Spear.Math.Matrix4.id
        else
            let det = 1 / det'
            in mat4
                (m00' * det) (m04' * det) (m08' * det) (m12' * det)
                (m01' * det) (m05' * det) (m09' * det) (m13' * det)
                (m02' * det) (m06' * det) (m10' * det) (m14' * det)
                (m03' * det) (m07' * det) (m11' * det) (m15' * det)


-- | Transform the given vector in 3D space with the given matrix.
mul :: Float -> Matrix4 -> Vector3 -> Vector3
mul w m v = vec3 x' y' z'
    where
        v' = vec4 (V3.x v) (V3.y v) (V3.z v) w
        x' = row0 m `V4.dot` v'
        y' = row1 m `V4.dot` v'
        z' = row2 m `V4.dot` v'


-- | Transform the given point vector in 3D space with the given matrix.
mulp :: Matrix4 -> Vector3 -> Vector3
mulp = mul 1


-- | Transform the given directional vector in 3D space with the given matrix.
muld :: Matrix4 -> Vector3 -> Vector3
muld = mul 0


-- | Transform the given vector with the given matrix.
--
-- The vector is brought from homogeneous space to 3D space by performing a
-- perspective divide.
mul' :: Float -> Matrix4 -> Vector3 -> Vector3
mul' w m v = vec3 (x'/w') (y'/w') (z'/w')
    where
        v' = vec4 (V3.x v) (V3.y v) (V3.z v) w
        x' = row0 m `V4.dot` v'
        y' = row1 m `V4.dot` v'
        z' = row2 m `V4.dot` v'
        w' = row3 m `V4.dot` v'


toRAD = (*pi) . (/180)