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 specialized-arrays.float ui ui.gadgets.worlds ;
10 UNIFORM-TUPLE: mvp-uniforms
11 { "mv_matrix" mat4-uniform f }
12 { "p_matrix" mat4-uniform f } ;
14 CONSTANT: -pi/2 $[ pi -2.0 / ]
15 CONSTANT: pi/2 $[ pi 2.0 / ]
17 TUPLE: wasd-world < game-world location yaw pitch p-matrix ;
19 GENERIC: wasd-near-plane ( world -- near-plane )
20 M: wasd-world wasd-near-plane drop 0.25 ;
22 GENERIC: wasd-far-plane ( world -- far-plane )
23 M: wasd-world wasd-far-plane drop 1024.0 ;
25 GENERIC: wasd-movement-speed ( world -- speed )
26 M: wasd-world wasd-movement-speed drop 1/16. ;
28 GENERIC: wasd-mouse-scale ( world -- scale )
29 M: wasd-world wasd-mouse-scale drop 1/600. ;
31 GENERIC: wasd-pitch-range ( world -- min max )
32 M: wasd-world wasd-pitch-range drop -pi/2 pi/2 ;
34 GENERIC: wasd-fly-vertically? ( world -- ? )
35 M: wasd-world wasd-fly-vertically? drop t ;
37 : wasd-mv-matrix ( world -- matrix )
38 [ { 1.0 0.0 0.0 } swap pitch>> rotation-matrix4 ]
39 [ { 0.0 1.0 0.0 } swap yaw>> rotation-matrix4 ]
40 [ location>> vneg translation-matrix4 ] tri m. m. ;
42 : wasd-mv-inv-matrix ( world -- matrix )
43 [ location>> translation-matrix4 ]
44 [ { 0.0 -1.0 0.0 } swap yaw>> rotation-matrix4 ]
45 [ { -1.0 0.0 0.0 } swap pitch>> rotation-matrix4 ] tri m. m. ;
47 : wasd-p-matrix ( world -- matrix )
52 :: generate-p-matrix ( world -- matrix )
53 world wasd-near-plane :> near-plane
54 world wasd-far-plane :> far-plane
56 world dim>> dup first2 min >float v/n fov v*n near-plane v*n
57 near-plane far-plane frustum-matrix4 ;
59 : set-wasd-view ( world location yaw pitch -- world )
60 [ >>location ] [ >>yaw ] [ >>pitch ] tri* ;
62 :: eye-rotate ( yaw pitch v -- v' )
70 cosy 0.0 siny neg 3array
71 siny sinp * cosp cosy sinp * 3array
72 siny cosp * sinp neg cosy cosp * 3array 3array
75 : ?pitch ( world -- pitch )
76 dup wasd-fly-vertically? [ pitch>> ] [ drop 0.0 ] if ;
78 : forward-vector ( world -- v )
79 [ yaw>> ] [ ?pitch ] [ wasd-movement-speed ] tri
80 { 0.0 0.0 -1.0 } n*v eye-rotate ;
81 : rightward-vector ( world -- v )
82 [ yaw>> ] [ ?pitch ] [ wasd-movement-speed ] tri
83 { 1.0 0.0 0.0 } n*v eye-rotate ;
85 : walk-forward ( world -- )
86 dup forward-vector [ v+ ] curry change-location drop ;
87 : walk-backward ( world -- )
88 dup forward-vector [ v- ] curry change-location drop ;
89 : walk-leftward ( world -- )
90 dup rightward-vector [ v- ] curry change-location drop ;
91 : walk-rightward ( world -- )
92 dup rightward-vector [ v+ ] curry change-location drop ;
93 : walk-upward ( world -- )
94 dup wasd-movement-speed { 0.0 1.0 0.0 } n*v [ v+ ] curry change-location drop ;
95 : walk-downward ( world -- )
96 dup wasd-movement-speed { 0.0 1.0 0.0 } n*v [ v- ] curry change-location drop ;
98 : clamp-pitch ( world -- world )
99 dup [ wasd-pitch-range clamp ] curry change-pitch ;
101 : rotate-with-mouse ( world mouse -- )
102 [ [ dup wasd-mouse-scale ] [ dx>> ] bi* * [ + ] curry change-yaw ]
103 [ [ dup wasd-mouse-scale ] [ dy>> ] bi* * [ + ] curry change-pitch clamp-pitch ] bi
106 :: wasd-keyboard-input ( world -- )
107 read-keyboard keys>> :> keys
108 key-w keys nth key-, keys nth or [ world walk-forward ] when
109 key-s keys nth key-o keys nth or [ world walk-backward ] when
110 key-a keys nth [ world walk-leftward ] when
111 key-d keys nth key-e keys nth or [ world walk-rightward ] when
112 key-space keys nth [ world walk-upward ] when
113 key-c keys nth key-j keys nth or [ world walk-downward ] when
114 key-escape keys nth [ world close-window ] when ;
116 : wasd-mouse-input ( world -- )
117 read-mouse rotate-with-mouse ;
121 [ wasd-keyboard-input ] [ wasd-mouse-input ] bi
125 M: wasd-world resize-world
126 [ <viewport-state> set-gpu-state* ]
127 [ dup generate-p-matrix >>p-matrix drop ] bi ;