1 ! Copyright (C) 2009 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays combinators.smart game.input
4 game.input.scancodes game.loop game.worlds
5 gpu.render gpu.state kernel literals
6 locals math math.constants math.functions math.matrices
7 math.order math.vectors opengl.gl sequences
8 ui ui.gadgets.worlds specialized-arrays audio.engine ;
9 FROM: alien.c-types => float ;
10 SPECIALIZED-ARRAY: float
13 UNIFORM-TUPLE: mvp-uniforms
14 { "mv_matrix" mat4-uniform f }
15 { "p_matrix" mat4-uniform f } ;
17 CONSTANT: -pi/2 $[ pi -2.0 / ]
18 CONSTANT: pi/2 $[ pi 2.0 / ]
20 TUPLE: wasd-world < game-world location yaw pitch p-matrix ;
22 GENERIC: wasd-near-plane ( world -- near-plane )
23 M: wasd-world wasd-near-plane drop 0.25 ;
25 GENERIC: wasd-far-plane ( world -- far-plane )
26 M: wasd-world wasd-far-plane drop 1024.0 ;
28 GENERIC: wasd-movement-speed ( world -- speed )
29 M: wasd-world wasd-movement-speed drop 1/16. ;
31 GENERIC: wasd-mouse-scale ( world -- scale )
32 M: wasd-world wasd-mouse-scale drop 1/600. ;
34 GENERIC: wasd-pitch-range ( world -- min max )
35 M: wasd-world wasd-pitch-range drop -pi/2 pi/2 ;
37 GENERIC: wasd-fly-vertically? ( world -- ? )
38 M: wasd-world wasd-fly-vertically? drop t ;
40 : wasd-mv-matrix ( world -- matrix )
41 [ { 1.0 0.0 0.0 } swap pitch>> rotation-matrix4 ]
42 [ { 0.0 1.0 0.0 } swap yaw>> rotation-matrix4 ]
43 [ location>> vneg translation-matrix4 ] tri m. m. ;
45 : wasd-mv-inv-matrix ( world -- matrix )
46 [ location>> translation-matrix4 ]
47 [ { 0.0 -1.0 0.0 } swap yaw>> rotation-matrix4 ]
48 [ { -1.0 0.0 0.0 } swap pitch>> rotation-matrix4 ] tri m. m. ;
50 : wasd-p-matrix ( world -- matrix )
53 : <mvp-uniforms> ( world -- uniforms )
54 [ wasd-mv-matrix ] [ wasd-p-matrix ] bi mvp-uniforms boa ;
58 : wasd-fov-vector ( world -- fov )
59 dim>> dup first2 min >float v/n fov v*n ; inline
61 :: generate-p-matrix ( world -- matrix )
62 world wasd-near-plane :> near-plane
63 world wasd-far-plane :> far-plane
65 world wasd-fov-vector near-plane v*n
66 near-plane far-plane frustum-matrix4 ;
68 :: wasd-pixel-ray ( world loc -- direction )
69 loc world dim>> [ /f 0.5 - 2.0 * ] 2map
70 world wasd-fov-vector v*
71 first2 neg -1.0 0.0 4array
72 world wasd-mv-inv-matrix swap m.v ;
74 : set-wasd-view ( world location yaw pitch -- world )
75 [ >>location ] [ >>yaw ] [ >>pitch ] tri* ;
77 :: eye-rotate ( yaw pitch v -- v' )
85 cosy 0.0 siny neg 3array
86 siny sinp * cosp cosy sinp * 3array
87 siny cosp * sinp neg cosy cosp * 3array 3array
90 : ?pitch ( world -- pitch )
91 dup wasd-fly-vertically? [ pitch>> ] [ drop 0.0 ] if ;
93 : forward-vector ( world -- v )
94 [ yaw>> ] [ ?pitch ] [ wasd-movement-speed ] tri
95 { 0.0 0.0 -1.0 } n*v eye-rotate ;
96 : rightward-vector ( world -- v )
97 [ yaw>> ] [ ?pitch ] [ wasd-movement-speed ] tri
98 { 1.0 0.0 0.0 } n*v eye-rotate ;
100 M: wasd-world audio-position location>> ; inline
101 M: wasd-world audio-orientation
102 forward-vector { 0.0 1.0 0.0 } <audio-orientation-state> ; inline
104 : walk-forward ( world -- )
105 dup forward-vector [ v+ ] curry change-location drop ;
106 : walk-backward ( world -- )
107 dup forward-vector [ v- ] curry change-location drop ;
108 : walk-leftward ( world -- )
109 dup rightward-vector [ v- ] curry change-location drop ;
110 : walk-rightward ( world -- )
111 dup rightward-vector [ v+ ] curry change-location drop ;
112 : walk-upward ( world -- )
113 dup wasd-movement-speed { 0.0 1.0 0.0 } n*v [ v+ ] curry change-location drop ;
114 : walk-downward ( world -- )
115 dup wasd-movement-speed { 0.0 1.0 0.0 } n*v [ v- ] curry change-location drop ;
117 : clamp-pitch ( world -- world )
118 dup [ wasd-pitch-range clamp ] curry change-pitch ;
120 : rotate-with-mouse ( world mouse -- )
121 [ [ dup wasd-mouse-scale ] [ dx>> ] bi* * [ + ] curry change-yaw ]
122 [ [ dup wasd-mouse-scale ] [ dy>> ] bi* * [ + ] curry change-pitch clamp-pitch ] bi
125 :: wasd-keyboard-input ( world -- )
126 read-keyboard keys>> :> keys
127 key-w keys nth [ world walk-forward ] when
128 key-s keys nth [ world walk-backward ] when
129 key-a keys nth [ world walk-leftward ] when
130 key-d keys nth [ world walk-rightward ] when
131 key-space keys nth [ world walk-upward ] when
132 key-c keys nth [ world walk-downward ] when
133 key-escape keys nth [ world close-window ] when ;
135 : wasd-mouse-input ( world -- )
136 read-mouse rotate-with-mouse ;
138 M: wasd-world tick-game-world
140 [ wasd-keyboard-input ] [ wasd-mouse-input ] bi
144 M: wasd-world resize-world
145 [ <viewport-state> set-gpu-state* ]
146 [ dup generate-p-matrix >>p-matrix drop ] bi ;