1 ! (c)2009 Joe Groff, Doug Coleman. bsd license
2 USING: accessors arrays combinators game.input game.loop
3 game.input.scancodes grouping kernel literals locals
4 math math.constants math.functions math.order math.trig
5 math.vectors opengl opengl.capabilities opengl.gl
6 opengl.shaders opengl.textures opengl.textures.private
7 sequences sequences.product specialized-arrays
8 terrain.generation terrain.shaders typed ui ui.gadgets
9 ui.gadgets.worlds ui.pixel-formats game.worlds
10 math.matrices.simd noise ui.gestures combinators.short-circuit
11 destructors grid-meshes math.vectors.simd ;
12 QUALIFIED-WITH: alien.c-types c
13 SPECIALIZED-ARRAY: c:float
16 CONSTANT: FOV $[ 2.0 sqrt 1 + ]
17 CONSTANT: NEAR-PLANE 1/1024.
18 CONSTANT: FAR-PLANE 2.0
19 CONSTANT: PLAYER-START-LOCATION float-4{ 0.5 0.51 0.5 1.0 }
20 CONSTANT: VELOCITY-MODIFIER-NORMAL float-4{ 1.0 1.0 1.0 0.0 }
21 CONSTANT: VELOCITY-MODIFIER-FAST float-4{ 2.0 1.0 2.0 0.0 }
22 CONSTANT: BOUNCE float-4{ 1.0 -0.2 1.0 1.0 }
23 CONSTANT: PLAYER-HEIGHT 1/256.
24 CONSTANT: GRAVITY float-4{ 0.0 -1/8192. 0.0 0.0 }
25 CONSTANT: JUMP 1/2048.
26 CONSTANT: MOUSE-SCALE 1/20.
27 CONSTANT: MOVEMENT-SPEED 1/32768.
28 CONSTANT: FRICTION float-4{ 0.97 0.995 0.97 1.0 }
29 CONSTANT: COMPONENT-SCALE float-4{ 0.5 0.01 0.0005 0.0 }
30 CONSTANT: SKY-PERIOD 2400
31 CONSTANT: SKY-SPEED 0.00025
33 CONSTANT: terrain-vertex-size { 512 512 }
40 { velocity-modifier float-4 }
43 TUPLE: terrain-world < game-world
45 sky-image sky-texture sky-program
46 terrain terrain-segment terrain-texture terrain-program
50 : <player> ( -- player )
52 PLAYER-START-LOCATION >>location
55 float-4{ 0.0 0.0 0.0 1.0 } >>velocity
56 VELOCITY-MODIFIER-NORMAL >>velocity-modifier ;
58 : frustum ( dim -- -x x -y y near far )
60 NEAR-PLANE FOV / v*n first2 [ [ neg ] keep ] bi@
61 NEAR-PLANE FAR-PLANE ;
63 : set-modelview-matrix ( gadget -- )
64 GL_DEPTH_BUFFER_BIT glClear
65 GL_MODELVIEW glMatrixMode
68 [ pitch>> 1.0 0.0 0.0 glRotatef ]
69 [ yaw>> 0.0 1.0 0.0 glRotatef ]
70 [ location>> vneg first3 glTranslatef ] tri ;
72 TYPED: eye-rotate ( yaw: float pitch: float v: float-4 -- v': float-4 )
73 [ float-4{ 0.0 -1.0 0.0 0.0 } swap deg>rad rotation-matrix4 ]
74 [ float-4{ -1.0 0.0 0.0 0.0 } swap deg>rad rotation-matrix4 m4. ]
75 [ m4.v ] tri* float-4{ t t t f } vand ;
77 : forward-vector ( player -- v )
79 float-4{ 0.0 0.0 $ MOVEMENT-SPEED 1.0 } vneg eye-rotate ; inline
80 : rightward-vector ( player -- v )
82 float-4{ $ MOVEMENT-SPEED 0.0 0.0 1.0 } eye-rotate ; inline
83 : clamp-pitch ( pitch -- pitch' )
84 -90.0 90.0 clamp ; inline
86 : walk-forward ( player -- )
87 dup forward-vector [ v+ ] curry change-velocity drop ; inline
88 : walk-backward ( player -- )
89 dup forward-vector [ v- ] curry change-velocity drop ; inline
90 : walk-leftward ( player -- )
91 dup rightward-vector [ v- ] curry change-velocity drop ; inline
92 : walk-rightward ( player -- )
93 dup rightward-vector [ v+ ] curry change-velocity drop ; inline
95 [ float-4{ 0.0 $ JUMP 0.0 0.0 } v+ ] change-velocity drop ; inline
96 : rotate-leftward ( player x -- )
97 [ - ] curry change-yaw drop ; inline
98 : rotate-rightward ( player x -- )
99 [ + ] curry change-yaw drop ; inline
100 : look-horizontally ( player x -- )
101 [ + ] curry change-yaw drop ; inline
102 : look-vertically ( player x -- )
103 [ + clamp-pitch ] curry change-pitch drop ; inline
106 : rotate-with-mouse ( player mouse -- )
107 [ dx>> MOUSE-SCALE * look-horizontally ]
108 [ dy>> MOUSE-SCALE * look-vertically ] 2bi ;
112 { T{ key-down { mods { A+ } } { sym "RET" } } [ toggle-fullscreen ] }
115 :: handle-input ( world -- )
116 world player>> :> player
117 read-keyboard keys>> :> keys
119 key-left-shift keys nth
120 VELOCITY-MODIFIER-FAST VELOCITY-MODIFIER-NORMAL ? player velocity-modifier<<
123 [ key-1 keys nth 1 f ? ]
124 [ key-2 keys nth 2 f ? ]
125 [ key-3 keys nth 3 f ? ]
126 [ key-4 keys nth 4 f ? ]
127 [ key-5 keys nth 10000 f ? ]
128 } 0|| player reverse-time<<
130 key-w keys nth [ player walk-forward ] when
131 key-s keys nth [ player walk-backward ] when
132 key-a keys nth [ player walk-leftward ] when
133 key-d keys nth [ player walk-rightward ] when
134 key-q keys nth [ player -1 look-horizontally ] when
135 key-e keys nth [ player 1 look-horizontally ] when
136 key-left-arrow keys nth [ player -1 look-horizontally ] when
137 key-right-arrow keys nth [ player 1 look-horizontally ] when
138 key-down-arrow keys nth [ player 1 look-vertically ] when
139 key-up-arrow keys nth [ player -1 look-vertically ] when
140 key-space keys nth [ player jump ] when
141 key-escape keys nth [ world close-window ] when
142 player read-mouse rotate-with-mouse
145 : apply-friction ( velocity -- velocity' )
148 : apply-gravity ( velocity -- velocity' )
151 : clamp-coords ( coords dim -- coords' )
152 { 0 0 } swap { 2 2 } v- vclamp ;
154 :: pixel-indices ( coords dim -- indices )
155 coords vfloor v>integer dim clamp-coords :> floor-coords
156 floor-coords first2 dim first * + :> base-index
157 base-index dim first + :> next-row-index
162 next-row-index 1 + 4array ;
164 :: terrain-height-at ( segment point -- height )
166 dim point v* :> pixel
167 pixel dup vfloor v- :> pixel-mantissa
168 segment bitmap>> 4 <groups> :> pixels
169 pixel dim pixel-indices :> indices
171 indices [ pixels nth COMPONENT-SCALE v. 255.0 / ] map
172 first4 pixel-mantissa bilerp ;
174 : (collide) ( segment location -- location' )
176 { 0 2 3 3 } vshuffle terrain-height-at PLAYER-HEIGHT +
177 -1/0. swap -1/0. -1/0. float-4-boa
180 TYPED:: collide ( world: terrain-world player: player -- )
181 world terrain-segment>> :> segment
182 player location>> :> location
183 segment location (collide) :> location'
185 location location' = not [
188 [ BOUNCE v* ] change-velocity
192 : scaled-velocity ( player -- velocity )
193 [ velocity>> ] [ velocity-modifier>> ] bi v* ;
195 : save-history ( world player -- )
196 clone swap history>> push ;
198 :: tick-player-reverse ( world player -- )
199 player reverse-time>> :> reverse-time
200 world history>> :> history
202 history length reverse-time 1 - - 1 max history set-length
203 history pop world player<<
206 : tick-player-forward ( world player -- )
208 [ apply-friction apply-gravity ] change-velocity
209 dup scaled-velocity [ v+ ] curry change-location
212 : tick-player ( world player -- )
214 [ tick-player-reverse ]
215 [ tick-player-forward ] if ;
217 M: terrain-world tick-game-world
218 [ dup focused?>> [ handle-input ] [ drop ] if ]
219 [ dup player>> tick-player ] bi ;
221 : set-texture-parameters ( texture -- )
222 GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit
223 GL_TEXTURE_2D GL_TEXTURE_MIN_FILTER GL_LINEAR glTexParameteri
224 GL_TEXTURE_2D GL_TEXTURE_MAG_FILTER GL_LINEAR glTexParameteri
225 GL_TEXTURE_2D GL_TEXTURE_WRAP_S GL_CLAMP_TO_EDGE glTexParameteri
226 GL_TEXTURE_2D GL_TEXTURE_WRAP_T GL_CLAMP_TO_EDGE glTexParameteri ;
228 : sky-gradient ( world -- t )
229 game-loop>> tick#>> SKY-PERIOD mod SKY-PERIOD /f ;
230 : sky-theta ( world -- theta )
231 game-loop>> tick#>> SKY-SPEED * ;
233 M: terrain-world begin-game-world
234 "2.0" { "GL_ARB_vertex_buffer_object" "GL_ARB_shader_objects" }
235 require-gl-version-or-extensions
236 GL_DEPTH_TEST glEnable
237 GL_TEXTURE_2D glEnable
238 GL_VERTEX_ARRAY glEnableClientState
241 <perlin-noise-table> 0.01 float-4-with scale-matrix4 { 512 512 } perlin-noise-image
243 make-texture [ set-texture-parameters ] keep >>sky-texture
244 <terrain> [ >>terrain ] keep
245 float-4{ 0.0 0.0 0.0 1.0 } terrain-segment [ >>terrain-segment ] keep
246 make-texture [ set-texture-parameters ] keep >>terrain-texture
247 sky-vertex-shader sky-pixel-shader <simple-gl-program>
249 terrain-vertex-shader terrain-pixel-shader <simple-gl-program>
251 terrain-vertex-size <grid-mesh> >>terrain-mesh
254 M: terrain-world end-game-world
256 [ terrain-mesh>> dispose ]
257 [ terrain-program>> delete-gl-program ]
258 [ terrain-texture>> delete-texture ]
259 [ sky-program>> delete-gl-program ]
260 [ sky-texture>> delete-texture ]
263 M: terrain-world resize-world
264 GL_PROJECTION glMatrixMode
266 dim>> [ [ { 0 0 } ] dip gl-viewport ]
267 [ frustum glFrustum ] bi ;
269 M: terrain-world draw-world*
271 [ set-modelview-matrix ]
272 [ terrain-texture>> GL_TEXTURE_2D GL_TEXTURE0 bind-texture-unit ]
273 [ sky-texture>> GL_TEXTURE_2D GL_TEXTURE1 bind-texture-unit ]
274 [ GL_DEPTH_TEST glDisable dup sky-program>> [
275 [ nip "sky" glGetUniformLocation 1 glUniform1i ]
276 [ "sky_gradient" glGetUniformLocation swap sky-gradient glUniform1f ]
277 [ "sky_theta" glGetUniformLocation swap sky-theta glUniform1f ] 2tri
278 { -1.0 -1.0 } { 2.0 2.0 } gl-fill-rect
280 [ GL_DEPTH_TEST glEnable dup terrain-program>> [
281 [ "heightmap" glGetUniformLocation 0 glUniform1i ]
282 [ "component_scale" glGetUniformLocation COMPONENT-SCALE first4 glUniform4f ] bi
283 terrain-mesh>> draw-grid-mesh
288 { world-class terrain-world }
290 { pixel-format-attributes {
293 T{ depth-bits { value 24 } }
295 { use-game-input? t }
297 { pref-dim { 1024 768 } }
298 { tick-interval-nanos $[ 60 fps ] }