]> gitweb.factorcode.org Git - factor.git/blob - extra/gpu/util/wasd/wasd.factor
b0a3d8179a874d81bba9fd25cf06c383b9c22f20
[factor.git] / extra / gpu / util / wasd / wasd.factor
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 ;
8 IN: gpu.util.wasd
9
10 UNIFORM-TUPLE: mvp-uniforms
11     { "mv_matrix"  mat4-uniform f }
12     { "p_matrix"   mat4-uniform f } ;
13
14 CONSTANT: -pi/2 $[ pi -2.0 / ]
15 CONSTANT:  pi/2 $[ pi  2.0 / ]
16
17 TUPLE: wasd-world < game-world location yaw pitch p-matrix ;
18
19 GENERIC: wasd-near-plane ( world -- near-plane )
20 M: wasd-world wasd-near-plane drop 0.25 ;
21
22 GENERIC: wasd-far-plane ( world -- far-plane )
23 M: wasd-world wasd-far-plane drop 1024.0 ;
24
25 GENERIC: wasd-movement-speed ( world -- speed )
26 M: wasd-world wasd-movement-speed drop 1/16. ;
27
28 GENERIC: wasd-mouse-scale ( world -- scale )
29 M: wasd-world wasd-mouse-scale drop 1/600. ;
30
31 GENERIC: wasd-pitch-range ( world -- min max )
32 M: wasd-world wasd-pitch-range drop -pi/2 pi/2 ;
33
34 GENERIC: wasd-fly-vertically? ( world -- ? )
35 M: wasd-world wasd-fly-vertically? drop t ;
36
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. ;
41
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. ;
46
47 : wasd-p-matrix ( world -- matrix )
48     p-matrix>> ;
49
50 CONSTANT: fov 0.7
51
52 :: generate-p-matrix ( world -- matrix )
53     world wasd-near-plane :> near-plane
54     world wasd-far-plane :> far-plane
55
56     world dim>> dup first2 min >float v/n fov v*n near-plane v*n
57     near-plane far-plane frustum-matrix4 ;
58
59 : set-wasd-view ( world location yaw pitch -- world )
60     [ >>location ] [ >>yaw ] [ >>pitch ] tri* ;
61
62 :: eye-rotate ( yaw pitch v -- v' )
63     yaw neg :> y
64     pitch neg :> p
65     y cos :> cosy
66     y sin :> siny
67     p cos :> cosp
68     p sin :> sinp
69
70     cosy         0.0       siny        neg  3array
71     siny sinp *  cosp      cosy sinp *      3array
72     siny cosp *  sinp neg  cosy cosp *      3array 3array
73     v swap v.m ;
74
75 : ?pitch ( world -- pitch )
76     dup wasd-fly-vertically? [ pitch>> ] [ drop 0.0 ] if ;
77
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 ;
84
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 ;
97
98 : clamp-pitch ( world -- world )
99     dup [ wasd-pitch-range clamp ] curry change-pitch ;
100
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
104     drop ;
105
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 ;
115
116 : wasd-mouse-input ( world -- )
117     read-mouse rotate-with-mouse ;
118
119 M: wasd-world tick*
120     dup focused?>> [
121         [ wasd-keyboard-input ] [ wasd-mouse-input ] bi
122         reset-mouse
123     ] [ drop ] if ;
124
125 M: wasd-world resize-world
126     [ <viewport-state> set-gpu-state* ]
127     [ dup generate-p-matrix >>p-matrix drop ] bi ;
128