1 USING: accessors arrays combinators game-input game-loop
2 game-input.scancodes grouping kernel literals locals
3 math math.constants math.functions math.matrices math.order
4 math.vectors opengl opengl.capabilities opengl.gl
5 opengl.shaders opengl.textures opengl.textures.private
6 sequences sequences.product specialized-arrays.float
7 terrain.generation terrain.shaders ui ui.gadgets
8 ui.gadgets.worlds ui.pixel-formats game-worlds method-chains
9 math.affine-transforms noise ui.gestures combinators.short-circuit ;
12 CONSTANT: FOV $[ 2.0 sqrt 1+ ]
13 CONSTANT: NEAR-PLANE $[ 1.0 1024.0 / ]
14 CONSTANT: FAR-PLANE 2.0
15 CONSTANT: PLAYER-START-LOCATION { 0.5 0.51 0.5 }
16 CONSTANT: VELOCITY-MODIFIER-NORMAL { 1.0 1.0 1.0 }
17 CONSTANT: VELOCITY-MODIFIER-FAST { 2.0 1.0 2.0 }
18 CONSTANT: PLAYER-HEIGHT $[ 1.0 256.0 / ]
19 CONSTANT: GRAVITY $[ 1.0 4096.0 / ]
20 CONSTANT: JUMP $[ 1.0 1024.0 / ]
21 CONSTANT: MOUSE-SCALE $[ 1.0 10.0 / ]
22 CONSTANT: MOVEMENT-SPEED $[ 1.0 16384.0 / ]
23 CONSTANT: FRICTION { 0.95 0.99 0.95 }
24 CONSTANT: COMPONENT-SCALE { 0.5 0.01 0.0005 0.0 }
25 CONSTANT: SKY-PERIOD 1200
26 CONSTANT: SKY-SPEED 0.0005
28 CONSTANT: terrain-vertex-size { 512 512 }
29 CONSTANT: terrain-vertex-distance { $[ 1.0 512.0 / ] $[ 1.0 512.0 / ] }
30 CONSTANT: terrain-vertex-row-length $[ 512 1 + 2 * ]
33 location yaw pitch velocity velocity-modifier
36 TUPLE: terrain-world < game-world
38 sky-image sky-texture sky-program
39 terrain terrain-segment terrain-texture terrain-program
43 : <player> ( -- player )
45 PLAYER-START-LOCATION >>location
48 { 0.0 0.0 0.0 } >>velocity
49 VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
51 M: terrain-world tick-length
54 : frustum ( dim -- -x x -y y near far )
56 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
57 NEAR-PLANE FAR-PLANE ;
59 : set-modelview-matrix ( gadget -- )
60 GL_DEPTH_BUFFER_BIT glClear
61 GL_MODELVIEW glMatrixMode
64 [ pitch>> 1.0 0.0 0.0 glRotatef ]
65 [ yaw>> 0.0 1.0 0.0 glRotatef ]
66 [ location>> vneg first3 glTranslatef ] tri ;
68 : vertex-array-vertex ( x z -- vertex )
69 [ terrain-vertex-distance first * ]
70 [ terrain-vertex-distance second * ] bi*
71 [ 0 ] dip float-array{ } 3sequence ;
73 : vertex-array-row ( z -- vertices )
75 terrain-vertex-size first 1 + iota
76 2array [ first2 swap vertex-array-vertex ] product-map
79 : vertex-array ( -- vertices )
80 terrain-vertex-size second iota
81 [ vertex-array-row ] map concat ;
83 : >vertex-buffer ( bytes -- buffer )
84 [ GL_ARRAY_BUFFER ] dip GL_STATIC_DRAW <gl-buffer> ;
86 : draw-vertex-buffer-row ( i -- )
87 [ GL_TRIANGLE_STRIP ] dip
88 terrain-vertex-row-length * terrain-vertex-row-length
91 : draw-vertex-buffer ( buffer -- )
92 [ GL_ARRAY_BUFFER ] dip [
93 3 GL_FLOAT 0 f glVertexPointer
94 terrain-vertex-size second iota [ draw-vertex-buffer-row ] each
97 : degrees ( deg -- rad )
100 :: eye-rotate ( yaw pitch v -- v' )
102 pitch degrees neg :> p
108 cosy 0.0 siny neg 3array
109 siny sinp * cosp cosy sinp * 3array
110 siny cosp * sinp neg cosy cosp * 3array 3array
113 : forward-vector ( player -- v )
115 ${ 0.0 0.0 MOVEMENT-SPEED } vneg eye-rotate ;
116 : rightward-vector ( player -- v )
118 ${ MOVEMENT-SPEED 0.0 0.0 } eye-rotate ;
119 : clamp-pitch ( pitch -- pitch' )
123 : walk-forward ( player -- )
124 dup forward-vector [ v+ ] curry change-velocity drop ;
125 : walk-backward ( player -- )
126 dup forward-vector [ v- ] curry change-velocity drop ;
127 : walk-leftward ( player -- )
128 dup rightward-vector [ v- ] curry change-velocity drop ;
129 : walk-rightward ( player -- )
130 dup rightward-vector [ v+ ] curry change-velocity drop ;
132 [ ${ 0.0 JUMP 0.0 } v+ ] change-velocity drop ;
133 : rotate-leftward ( player x -- )
134 [ - ] curry change-yaw drop ;
135 : rotate-rightward ( player x -- )
136 [ + ] curry change-yaw drop ;
137 : look-horizontally ( player x -- )
138 [ + ] curry change-yaw drop ;
139 : look-vertically ( player x -- )
140 [ + clamp-pitch ] curry change-pitch drop ;
143 : rotate-with-mouse ( player mouse -- )
144 [ dx>> MOUSE-SCALE * look-horizontally ]
145 [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
149 { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
152 :: handle-input ( world -- )
153 world player>> :> player
154 read-keyboard keys>> :> keys
156 key-left-shift keys nth
157 VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player (>>velocity-modifier)
160 [ key-1 keys nth 1 f ? ]
161 [ key-2 keys nth 2 f ? ]
162 [ key-3 keys nth 3 f ? ]
163 [ key-4 keys nth 4 f ? ]
164 [ key-5 keys nth 10000 f ? ]
165 } 0|| player (>>reverse-time)
167 key-w keys nth [ player walk-forward ] when
168 key-s keys nth [ player walk-backward ] when
169 key-a keys nth [ player walk-leftward ] when
170 key-d keys nth [ player walk-rightward ] when
171 key-q keys nth [ player -1 look-horizontally ] when
172 key-e keys nth [ player 1 look-horizontally ] when
173 key-left-arrow keys nth [ player -1 look-horizontally ] when
174 key-right-arrow keys nth [ player 1 look-horizontally ] when
175 key-down-arrow keys nth [ player 1 look-vertically ] when
176 key-up-arrow keys nth [ player -1 look-vertically ] when
177 key-space keys nth [ player jump ] when
178 key-escape keys nth [ world close-window ] when
179 player read-mouse rotate-with-mouse
182 : apply-friction ( velocity -- velocity' )
185 : apply-gravity ( velocity -- velocity' )
186 1 over [ GRAVITY - ] change-nth ;
188 : clamp-coords ( coords dim -- coords' )
189 [ { 0 0 } vmax ] dip { 2 2 } v- vmin ;
191 :: pixel-indices ( coords dim -- indices )
192 coords vfloor [ >integer ] map dim clamp-coords :> floor-coords
193 floor-coords first2 dim first * + :> base-index
194 base-index dim first + :> next-row-index
199 next-row-index 1 + 4array ;
201 :: terrain-height-at ( segment point -- height )
203 dim point v* :> pixel
204 pixel dup vfloor v- :> pixel-mantissa
205 segment bitmap>> 4 <groups> :> pixels
206 pixel dim pixel-indices :> indices
208 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
209 first4 pixel-mantissa bilerp ;
211 : collide ( segment location -- location' )
212 [ [ first ] [ third ] bi 2array terrain-height-at PLAYER-HEIGHT + ]
213 [ [ 1 ] 2dip [ max ] with change-nth ]
216 : scaled-velocity ( player -- velocity )
217 [ velocity>> ] [ velocity-modifier>> ] bi v* ;
219 : save-history ( world player -- )
220 clone swap history>> push ;
222 :: tick-player-reverse ( world player -- )
223 player reverse-time>> :> reverse-time
224 world history>> :> history
226 history length reverse-time 1 - - 1 max history set-length
227 history pop world (>>player)
230 : tick-player-forward ( world player -- )
232 [ apply-friction apply-gravity ] change-velocity
233 dup scaled-velocity [ v+ [ terrain-segment>> ] dip collide ] curry with change-location
236 : tick-player ( world player -- )
243 M: terrain-world tick*
244 [ dup focused?>> [ handle-input ] [ drop ] if ]
245 [ dup player>> tick-player ] bi ;
247 : set-texture-parameters ( texture -- )
248 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
249 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
250 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
251 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
252 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
254 : sky-gradient ( world -- t )
255 game-loop>> tick-number>> SKY-PERIOD mod SKY-PERIOD /f ;
256 : sky-theta ( world -- theta )
257 game-loop>> tick-number>> SKY-SPEED * ;
259 BEFORE: terrain-world begin-world
260 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
261 require-gl-version-or-extensions
262 GL_DEPTH_TEST glEnable
263 GL_TEXTURE_2D glEnable
264 GL_VERTEX_ARRAY glEnableClientState
267 <perlin-noise-table> 0.01 0.01 <scale> { 512 512 } perlin-noise-image
269 make-texture [ set-texture-parameters ] keep >>sky-texture
270 <terrain> [ >>terrain ] keep
271 { 0 0 } terrain-segment [ >>terrain-segment ] keep
272 make-texture [ set-texture-parameters ] keep >>terrain-texture
273 sky-vertex-shader sky-pixel-shader <simple-gl-program>
275 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
277 vertex-array >vertex-buffer >>terrain-vertex-buffer
280 AFTER: terrain-world end-world
282 [ terrain-vertex-buffer>> delete-gl-buffer ]
283 [ terrain-program>> delete-gl-program ]
284 [ terrain-texture>> delete-texture ]
285 [ sky-program>> delete-gl-program ]
286 [ sky-texture>> delete-texture ]
289 M: terrain-world resize-world
290 GL_PROJECTION glMatrixMode
292 dim>> [ [ 0 0 ] dip first2 glViewport ]
293 [ frustum glFrustum ] bi ;
295 M: terrain-world draw-world*
297 [ set-modelview-matrix ]
298 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
299 [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
300 [ GL_DEPTH_TEST glDisable dup sky-program>> [
301 [ nip "sky" glGetUniformLocation 1 glUniform1i ]
302 [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
303 [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
304 { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
306 [ GL_DEPTH_TEST glEnable dup terrain-program>> [
307 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
308 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
309 terrain-vertex-buffer>> draw-vertex-buffer
313 M: terrain-world pref-dim* drop { 640 480 } ;
315 : terrain-window ( -- )
317 f T{ world-attributes
318 { world-class terrain-world }
320 { pixel-format-attributes {
323 T{ depth-bits { value 24 } }