 ! (c)Joe Groff bsd license
-USING: accessors classes.struct kernel locals math math.functions
-math.matrices.simd math.vectors math.vectors.simd sequences
-sequences.private specialized-arrays typed ;
+USING: accessors classes.struct generalizations kernel locals
+math math.functions math.matrices.simd math.vectors
+math.vectors.simd sequences sequences.private specialized-arrays
+typed ;
 QUALIFIED-WITH: alien.c-types c
 SIMD: c:float
 SPECIALIZED-ARRAY: float-4

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

    diagonal scale-matrix4 :> diagonal-m
    triangle-hi { 3 0 1 3 } vshuffle :> tri1
    triangle-hi { 3 3 2 3 } vshuffle
    triangle-lo { 0 3 3 3 } vshuffle v+ :> tri2
    triangle-lo { 1 2 3 3 } vshuffle :> tri3
    tri1 triangle-m rows>> set-first
    tri2 triangle-m rows>> set-second
    tri3 triangle-m rows>> set-third
    float-4 new triangle-m rows>> set-fourth

    diagonal-m triangle-m m4+ ;

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

    float-4{ 0.0 0.0 -1.0 0.0 } :> c4

    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 :> c1
    fov { 1 1 1 1 } vshuffle { f t f f } vmask :> c2
    fov { 2 2 2 3 } vshuffle { f f t t } vmask :> c3

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

    c ;



    float-4{ 0.0 0.0 0.0 1.0 } :> c4
    { t t t f } offset c4 v? :> offset'

    offset' { 3 3 3 0 } vshuffle { t f f t } vmask
    offset' { 3 3 3 1 } vshuffle { f t f t } vmask
    offset' { 3 3 3 2 } vshuffle { f f t t } vmask
    c4

    c set-rows ;

TYPED:: rotation-matrix4 ( axis: float-4 theta: float -- matrix: matrix4 )
    !   x*x + c*(1.0 - x*x)   x*y*(1.0 - c) - s*z   x*z*(1.0 - c) + s*y   0
