 M: matrix4 new-sequence 2drop matrix4 (struct) ; inline
 
 <PRIVATE
+
+: rows ( a -- a1 a2 a3 a4 )
+    rows>> first4 ; inline
+
+:: set-rows ( c1 c2 c3 c4 c -- c )
+    c rows>> :> rows
+    c1 rows set-first
+    c2 rows set-second
+    c3 rows set-third
+    c4 rows set-fourth
+    c ; inline
+
 :: 2map-rows ( a b quot -- c )
     matrix4 (struct) :> c
 
-    a rows>> first  :> a1
-    a rows>> second :> a2
-    a rows>> third  :> a3
-    a rows>> fourth :> a4
-    b rows>> first  :> b1
-    b rows>> second :> b2
-    b rows>> third  :> b3
-    b rows>> fourth :> b4
+    a rows :> a4 :> a3 :> a2 :> a1
+    b rows :> b4 :> b3 :> b2 :> b1
 
-    a1 b1 quot call :> c1
-    a2 b2 quot call :> c2
-    a3 b3 quot call :> c3
-    a4 b4 quot call :> c4
+    a1 b1 quot call
+    a2 b2 quot call
+    a3 b3 quot call
+    a4 b4 quot call
 
-    c1 c rows>> set-first
-    c2 c rows>> set-second
-    c3 c rows>> set-third
-    c4 c rows>> set-fourth
-
-    c ; inline
+    c set-rows ; inline
 
 :: map-rows ( a quot -- c )
     matrix4 (struct) :> c
 
-    a rows>> first  :> a1
-    a rows>> second :> a2
-    a rows>> third  :> a3
-    a rows>> fourth :> a4
+    a rows :> a4 :> a3 :> a2 :> a1
 
-    a1 quot call :> c1
-    a2 quot call :> c2
-    a3 quot call :> c3
-    a4 quot call :> c4
+    a1 quot call
+    a2 quot call
+    a3 quot call
+    a4 quot call
 
-    c1 c rows>> set-first
-    c2 c rows>> set-second
-    c3 c rows>> set-third
-    c4 c rows>> set-fourth
-
-    c ; inline
+    c set-rows ; inline
     
 PRIVATE>
 

TYPED:: m4. ( a: matrix4 b: matrix4 -- c: matrix4 )
    matrix4 (struct) :> c

    a rows>> first  :> a1
    a rows>> second :> a2
    a rows>> third  :> a3
    a rows>> fourth :> a4
    b rows>> first  :> b1
    b rows>> second :> b2
    b rows>> third  :> b3
    b rows>> fourth :> b4

    a1 { 0 0 0 0 } vshuffle b1 v* :> c1a
    a2 { 0 0 0 0 } vshuffle b1 v* :> c2a
    a3 { 0 0 0 0 } vshuffle b1 v* :> c3a
    a4 { 0 0 0 0 } vshuffle b1 v* :> c4a

    a1 { 1 1 1 1 } vshuffle b2 v* c1a v+ :> c1b 
    a2 { 1 1 1 1 } vshuffle b2 v* c2a v+ :> c2b
    a3 { 1 1 1 1 } vshuffle b2 v* c3a v+ :> c3b
    a4 { 1 1 1 1 } vshuffle b2 v* c4a v+ :> c4b

    a1 { 2 2 2 2 } vshuffle b3 v* c1b v+ :> c1c 
    a2 { 2 2 2 2 } vshuffle b3 v* c2b v+ :> c2c
    a3 { 2 2 2 2 } vshuffle b3 v* c3b v+ :> c3c
    a4 { 2 2 2 2 } vshuffle b3 v* c4b v+ :> c4c

    a1 { 3 3 3 3 } vshuffle b4 v* c1c v+ :> c1 
    a2 { 3 3 3 3 } vshuffle b4 v* c2c v+ :> c2
    a3 { 3 3 3 3 } vshuffle b4 v* c3c v+ :> c3
    a4 { 3 3 3 3 } vshuffle b4 v* c4c v+ :> c4

    c1 c rows>> set-first
    c2 c rows>> set-second
    c3 c rows>> set-third
    c4 c rows>> set-fourth

    c ;

CONSTANT: identity-matrix4
    S{ matrix4 f

    triangle-a triangle-b v- :> triangle-hi

    diagonal scale-matrix4 :> diagonal-m

    triangle-hi { 3 0 1 3 } vshuffle
    triangle-hi { 3 3 2 3 } vshuffle triangle-lo { 0 3 3 3 } vshuffle v+
    triangle-lo { 1 2 3 3 } vshuffle
    float-4 new

    triangle-m set-rows drop

    diagonal-m triangle-m m4+ ;

TYPED:: frustum-matrix4 ( xy: float-4 near: float far: float -- matrix: matrix4 )
    matrix4 (struct) :> c

    near near near far + 2 near far * * float-4-boa :> num
    { t t f f } xy near far - float-4-with v? :> denom
    num denom v/ :> fov

    fov { 0 0 0 0 } vshuffle { t f f f } vmask
    fov { 1 1 1 1 } vshuffle { f t f f } vmask
    fov { 2 2 2 3 } vshuffle { f f t t } vmask
    float-4{ 0.0 0.0 -1.0 0.0 }

    c set-rows ;

