PageRenderTime 66ms CodeModel.GetById 49ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 1ms

/extra/gpu/util/wasd/wasd.factor

http://github.com/abeaumont/factor
Unknown | 145 lines | 113 code | 32 blank | 0 comment | 0 complexity | ea419b9a922343912fc23b4a754ac936 MD5 | raw file
  1! (c)2009 Joe Groff bsd license
  2USING: accessors arrays combinators.smart game.input
  3game.input.scancodes game.loop game.worlds
  4gpu.render gpu.state kernel literals
  5locals math math.constants math.functions math.matrices
  6math.order math.vectors opengl.gl sequences
  7ui ui.gadgets.worlds specialized-arrays audio.engine ;
  8FROM: alien.c-types => float ;
  9SPECIALIZED-ARRAY: float
 10IN: gpu.util.wasd
 11
 12UNIFORM-TUPLE: mvp-uniforms
 13    { "mv_matrix"  mat4-uniform f }
 14    { "p_matrix"   mat4-uniform f } ;
 15
 16CONSTANT: -pi/2 $[ pi -2.0 / ]
 17CONSTANT:  pi/2 $[ pi  2.0 / ]
 18
 19TUPLE: wasd-world < game-world location yaw pitch p-matrix ;
 20
 21GENERIC: wasd-near-plane ( world -- near-plane )
 22M: wasd-world wasd-near-plane drop 0.25 ;
 23
 24GENERIC: wasd-far-plane ( world -- far-plane )
 25M: wasd-world wasd-far-plane drop 1024.0 ;
 26
 27GENERIC: wasd-movement-speed ( world -- speed )
 28M: wasd-world wasd-movement-speed drop 1/16. ;
 29
 30GENERIC: wasd-mouse-scale ( world -- scale )
 31M: wasd-world wasd-mouse-scale drop 1/600. ;
 32
 33GENERIC: wasd-pitch-range ( world -- min max )
 34M: wasd-world wasd-pitch-range drop -pi/2 pi/2 ;
 35
 36GENERIC: wasd-fly-vertically? ( world -- ? )
 37M: wasd-world wasd-fly-vertically? drop t ;
 38
 39: wasd-mv-matrix ( world -- matrix )
 40    [ { 1.0 0.0 0.0 } swap pitch>> rotation-matrix4 ]
 41    [ { 0.0 1.0 0.0 } swap yaw>>   rotation-matrix4 ]
 42    [ location>> vneg translation-matrix4 ] tri m. m. ;
 43
 44: wasd-mv-inv-matrix ( world -- matrix )
 45    [ location>> translation-matrix4 ]
 46    [ {  0.0 -1.0 0.0 } swap yaw>>   rotation-matrix4 ]
 47    [ { -1.0  0.0 0.0 } swap pitch>> rotation-matrix4 ] tri m. m. ;
 48
 49: wasd-p-matrix ( world -- matrix )
 50    p-matrix>> ;
 51
 52: <mvp-uniforms> ( world -- uniforms )
 53    [ wasd-mv-matrix ] [ wasd-p-matrix ] bi mvp-uniforms boa ;
 54
 55CONSTANT: fov 0.7
 56
 57: wasd-fov-vector ( world -- fov )
 58    dim>> dup first2 min >float v/n fov v*n ; inline
 59
 60:: generate-p-matrix ( world -- matrix )
 61    world wasd-near-plane :> near-plane
 62    world wasd-far-plane :> far-plane
 63
 64    world wasd-fov-vector near-plane v*n
 65    near-plane far-plane frustum-matrix4 ;
 66
 67:: wasd-pixel-ray ( world loc -- direction )
 68    loc world dim>> [ /f 0.5 - 2.0 * ] 2map 
 69    world wasd-fov-vector v*
 70    first2 neg -1.0 0.0 4array
 71    world wasd-mv-inv-matrix swap m.v ;
 72
 73: set-wasd-view ( world location yaw pitch -- world )
 74    [ >>location ] [ >>yaw ] [ >>pitch ] tri* ;
 75
 76:: eye-rotate ( yaw pitch v -- v' )
 77    yaw neg :> y
 78    pitch neg :> p
 79    y cos :> cosy
 80    y sin :> siny
 81    p cos :> cosp
 82    p sin :> sinp
 83
 84    cosy         0.0       siny        neg  3array
 85    siny sinp *  cosp      cosy sinp *      3array
 86    siny cosp *  sinp neg  cosy cosp *      3array 3array
 87    v swap v.m ;
 88
 89: ?pitch ( world -- pitch )
 90    dup wasd-fly-vertically? [ pitch>> ] [ drop 0.0 ] if ;
 91
 92: forward-vector ( world -- v )
 93    [ yaw>> ] [ ?pitch ] [ wasd-movement-speed ] tri
 94    { 0.0 0.0 -1.0 } n*v eye-rotate ;
 95: rightward-vector ( world -- v )
 96    [ yaw>> ] [ ?pitch ] [ wasd-movement-speed ] tri
 97    { 1.0 0.0 0.0 } n*v eye-rotate ;
 98
 99M: wasd-world audio-position location>> ; inline
100M: wasd-world audio-orientation forward-vector { 0.0 1.0 0.0 } <audio-orientation> ; inline
101
102: walk-forward ( world -- )
103    dup forward-vector [ v+ ] curry change-location drop ;
104: walk-backward ( world -- )
105    dup forward-vector [ v- ] curry change-location drop ;
106: walk-leftward ( world -- )
107    dup rightward-vector [ v- ] curry change-location drop ;
108: walk-rightward ( world -- )
109    dup rightward-vector [ v+ ] curry change-location drop ;
110: walk-upward ( world -- )
111    dup wasd-movement-speed { 0.0 1.0 0.0 } n*v [ v+ ] curry change-location drop ;
112: walk-downward ( world -- )
113    dup wasd-movement-speed { 0.0 1.0 0.0 } n*v [ v- ] curry change-location drop ;
114
115: clamp-pitch ( world -- world )
116    dup [ wasd-pitch-range clamp ] curry change-pitch ;
117
118: rotate-with-mouse ( world mouse -- )
119    [ [ dup wasd-mouse-scale ] [ dx>> ] bi* * [ + ] curry change-yaw ]
120    [ [ dup wasd-mouse-scale ] [ dy>> ] bi* * [ + ] curry change-pitch clamp-pitch ] bi
121    drop ;
122
123:: wasd-keyboard-input ( world -- )
124    read-keyboard keys>> :> keys
125    key-w keys nth [ world walk-forward   ] when 
126    key-s keys nth [ world walk-backward  ] when 
127    key-a keys nth [ world walk-leftward  ] when 
128    key-d keys nth [ world walk-rightward ] when 
129    key-space keys nth [ world walk-upward ] when 
130    key-c keys nth [ world walk-downward ] when 
131    key-escape keys nth [ world close-window ] when ;
132
133: wasd-mouse-input ( world -- )
134    read-mouse rotate-with-mouse ;
135
136M: wasd-world tick-game-world
137    dup focused?>> [
138        [ wasd-keyboard-input ] [ wasd-mouse-input ] bi
139        reset-mouse
140    ] [ drop ] if ;
141
142M: wasd-world resize-world
143    [ <viewport-state> set-gpu-state* ]
144    [ dup generate-p-matrix >>p-matrix drop ] bi ;
145