]> gitweb.factorcode.org Git - factor.git/blob - extra/gpu/util/wasd/wasd.factor
Update some copyright headers to follow the current convention
[factor.git] / extra / gpu / util / wasd / wasd.factor
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
11 IN: gpu.util.wasd
12
13 UNIFORM-TUPLE: mvp-uniforms
14     { "mv_matrix"  mat4-uniform f }
15     { "p_matrix"   mat4-uniform f } ;
16
17 CONSTANT: -pi/2 $[ pi -2.0 / ]
18 CONSTANT:  pi/2 $[ pi  2.0 / ]
19
20 TUPLE: wasd-world < game-world location yaw pitch p-matrix ;
21
22 GENERIC: wasd-near-plane ( world -- near-plane )
23 M: wasd-world wasd-near-plane drop 0.25 ;
24
25 GENERIC: wasd-far-plane ( world -- far-plane )
26 M: wasd-world wasd-far-plane drop 1024.0 ;
27
28 GENERIC: wasd-movement-speed ( world -- speed )
29 M: wasd-world wasd-movement-speed drop 1/16. ;
30
31 GENERIC: wasd-mouse-scale ( world -- scale )
32 M: wasd-world wasd-mouse-scale drop 1/600. ;
33
34 GENERIC: wasd-pitch-range ( world -- min max )
35 M: wasd-world wasd-pitch-range drop -pi/2 pi/2 ;
36
37 GENERIC: wasd-fly-vertically? ( world -- ? )
38 M: wasd-world wasd-fly-vertically? drop t ;
39
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. ;
44
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. ;
49
50 : wasd-p-matrix ( world -- matrix )
51     p-matrix>> ;
52
53 : <mvp-uniforms> ( world -- uniforms )
54     [ wasd-mv-matrix ] [ wasd-p-matrix ] bi mvp-uniforms boa ;
55
56 CONSTANT: fov 0.7
57
58 : wasd-fov-vector ( world -- fov )
59     dim>> dup first2 min >float v/n fov v*n ; inline
60
61 :: generate-p-matrix ( world -- matrix )
62     world wasd-near-plane :> near-plane
63     world wasd-far-plane :> far-plane
64
65     world wasd-fov-vector near-plane v*n
66     near-plane far-plane frustum-matrix4 ;
67
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 ;
73
74 : set-wasd-view ( world location yaw pitch -- world )
75     [ >>location ] [ >>yaw ] [ >>pitch ] tri* ;
76
77 :: eye-rotate ( yaw pitch v -- v' )
78     yaw neg :> y
79     pitch neg :> p
80     y cos :> cosy
81     y sin :> siny
82     p cos :> cosp
83     p sin :> sinp
84
85     cosy         0.0       siny        neg  3array
86     siny sinp *  cosp      cosy sinp *      3array
87     siny cosp *  sinp neg  cosy cosp *      3array 3array
88     v swap v.m ;
89
90 : ?pitch ( world -- pitch )
91     dup wasd-fly-vertically? [ pitch>> ] [ drop 0.0 ] if ;
92
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 ;
99
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
103
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 ;
116
117 : clamp-pitch ( world -- world )
118     dup [ wasd-pitch-range clamp ] curry change-pitch ;
119
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
123     drop ;
124
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 ;
134
135 : wasd-mouse-input ( world -- )
136     read-mouse rotate-with-mouse ;
137
138 M: wasd-world tick-game-world
139     dup focused?>> [
140         [ wasd-keyboard-input ] [ wasd-mouse-input ] bi
141         reset-mouse
142     ] [ drop ] if ;
143
144 M: wasd-world resize-world
145     [ <viewport-state> set-gpu-state* ]
146     [ dup generate-p-matrix >>p-matrix drop ] bi ;