1 ! (c)2009 Joe Groff bsd license
2 USING: accessors arrays combinators.smart game.input
3 game.input.scancodes game.loop game.worlds
4 gpu.render gpu.state kernel literals
5 locals math math.constants math.functions math.matrices
6 math.order math.vectors opengl.gl sequences
7 ui ui.gadgets.worlds specialized-arrays audio.engine ;
8 FROM: alien.c-types => float ;
9 SPECIALIZED-ARRAY: float
12 UNIFORM-TUPLE: mvp-uniforms
13 { "mv_matrix" mat4-uniform f }
14 { "p_matrix" mat4-uniform f } ;
16 CONSTANT: -pi/2 $[ pi -2.0 / ]
17 CONSTANT: pi/2 $[ pi 2.0 / ]
19 TUPLE: wasd-world < game-world location yaw pitch p-matrix ;
21 GENERIC: wasd-near-plane ( world -- near-plane )
22 M: wasd-world wasd-near-plane drop 0.25 ;
24 GENERIC: wasd-far-plane ( world -- far-plane )
25 M: wasd-world wasd-far-plane drop 1024.0 ;
27 GENERIC: wasd-movement-speed ( world -- speed )
28 M: wasd-world wasd-movement-speed drop 1/16. ;
30 GENERIC: wasd-mouse-scale ( world -- scale )
31 M: wasd-world wasd-mouse-scale drop 1/600. ;
33 GENERIC: wasd-pitch-range ( world -- min max )
34 M: wasd-world wasd-pitch-range drop -pi/2 pi/2 ;
36 GENERIC: wasd-fly-vertically? ( world -- ? )
37 M: wasd-world wasd-fly-vertically? drop t ;
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. ;
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. ;
49 : wasd-p-matrix ( world -- matrix )
52 : <mvp-uniforms> ( world -- uniforms )
53 [ wasd-mv-matrix ] [ wasd-p-matrix ] bi mvp-uniforms boa ;
57 : wasd-fov-vector ( world -- fov )
58 dim>> dup first2 min >float v/n fov v*n ; inline
60 :: generate-p-matrix ( world -- matrix )
61 world wasd-near-plane :> near-plane
62 world wasd-far-plane :> far-plane
64 world wasd-fov-vector near-plane v*n
65 near-plane far-plane frustum-matrix4 ;
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 ;
73 : set-wasd-view ( world location yaw pitch -- world )
74 [ >>location ] [ >>yaw ] [ >>pitch ] tri* ;
76 :: eye-rotate ( yaw pitch v -- v' )
84 cosy 0.0 siny neg 3array
85 siny sinp * cosp cosy sinp * 3array
86 siny cosp * sinp neg cosy cosp * 3array 3array
89 : ?pitch ( world -- pitch )
90 dup wasd-fly-vertically? [ pitch>> ] [ drop 0.0 ] if ;
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 ;
99 M: wasd-world audio-position location>> ; inline
100 M: wasd-world audio-orientation
101 forward-vector { 0.0 1.0 0.0 } <audio-orientation-state> ; inline
103 : walk-forward ( world -- )
104 dup forward-vector [ v+ ] curry change-location drop ;
105 : walk-backward ( world -- )
106 dup forward-vector [ v- ] curry change-location drop ;
107 : walk-leftward ( world -- )
108 dup rightward-vector [ v- ] curry change-location drop ;
109 : walk-rightward ( world -- )
110 dup rightward-vector [ v+ ] curry change-location drop ;
111 : walk-upward ( world -- )
112 dup wasd-movement-speed { 0.0 1.0 0.0 } n*v [ v+ ] curry change-location drop ;
113 : walk-downward ( world -- )
114 dup wasd-movement-speed { 0.0 1.0 0.0 } n*v [ v- ] curry change-location drop ;
116 : clamp-pitch ( world -- world )
117 dup [ wasd-pitch-range clamp ] curry change-pitch ;
119 : rotate-with-mouse ( world mouse -- )
120 [ [ dup wasd-mouse-scale ] [ dx>> ] bi* * [ + ] curry change-yaw ]
121 [ [ dup wasd-mouse-scale ] [ dy>> ] bi* * [ + ] curry change-pitch clamp-pitch ] bi
124 :: wasd-keyboard-input ( world -- )
125 read-keyboard keys>> :> keys
126 key-w keys nth [ world walk-forward ] when
127 key-s keys nth [ world walk-backward ] when
128 key-a keys nth [ world walk-leftward ] when
129 key-d keys nth [ world walk-rightward ] when
130 key-space keys nth [ world walk-upward ] when
131 key-c keys nth [ world walk-downward ] when
132 key-escape keys nth [ world close-window ] when ;
134 : wasd-mouse-input ( world -- )
135 read-mouse rotate-with-mouse ;
137 M: wasd-world tick-game-world
139 [ wasd-keyboard-input ] [ wasd-mouse-input ] bi
143 M: wasd-world resize-world
144 [ <viewport-state> set-gpu-state* ]
145 [ dup generate-p-matrix >>p-matrix drop ] bi ;